source: ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/ComponentUtils.java @ 4098

Last change on this file since 4098 was 4098, checked in by George.Georgovassilis@mpi.nl, 10 years ago

#360, #431, #432: JPA and unified component entities

  • Property svn:mime-type set to text/plain
File size: 3.8 KB
Line 
1package clarin.cmdi.componentregistry.impl;
2
3import java.text.ParseException;
4import java.util.ArrayList;
5import java.util.Comparator;
6import java.util.Date;
7import java.util.List;
8
9import org.apache.commons.lang.time.DateFormatUtils;
10import org.apache.commons.lang.time.DateUtils;
11import org.springframework.beans.BeanUtils;
12
13import clarin.cmdi.componentregistry.DatesHelper;
14import clarin.cmdi.componentregistry.model.BaseDescription;
15import clarin.cmdi.componentregistry.model.ComponentDescription;
16import clarin.cmdi.componentregistry.model.ProfileDescription;
17
18/**
19 * Utilities for working with {@link BaseDescription}s
20 *
21 * @author george.georgovassilis@mpi.nl
22 *
23 */
24public class ComponentUtils {
25
26    public static boolean isProfileId(String componentId) {
27        return ("" + componentId).startsWith(ProfileDescription.PROFILE_PREFIX);
28    }
29
30    public static boolean isComponentId(String componentId) {
31        return ("" + componentId)
32                .startsWith(ComponentDescription.COMPONENT_PREFIX);
33    }
34
35    public static void copyPropertiesFrom(BaseDescription from, BaseDescription to) {
36        BeanUtils.copyProperties(from, to);
37    }
38
39    public static Date getDate(String registrationDate) throws ParseException {
40        return DateUtils.parseDate(registrationDate,
41                new String[] { DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT
42                        .getPattern() });
43    }
44
45    public static String createPublicHref(String href) {
46        String result = href;
47        if (href != null) {
48            int index = href.indexOf("?");
49            if (index != -1) { // strip off query params the rest should be the
50                               // public href.
51                result = href.substring(0, index);
52            }
53        }
54        return result;
55    }
56
57    /**
58     * Compares two descriptions by the their value as returned by
59     * {@link BaseDescription#getName()
60     * }
61     */
62    public static final Comparator<? super BaseDescription> COMPARE_ON_NAME = new Comparator<BaseDescription>() {
63        @Override
64        public int compare(BaseDescription o1, BaseDescription o2) {
65            int result = 0;
66            if (o1.getName() != null && o2.getName() != null) {
67                result = o1.getName().compareToIgnoreCase(o2.getName());
68            }
69            if (o1.getId() != null && result == 0) {
70                result = o1.getId().compareTo(o2.getId());
71            }
72            return result;
73        }
74    };
75    /**
76     * Compares two descriptions by the their value as returned by
77     * {@link BaseDescription#getRegistrationDate() () * }
78     */
79    public static final Comparator<? super BaseDescription> COMPARE_ON_DATE = new Comparator<BaseDescription>() {
80        /**
81         * @returns 1 if o11 is older than o2, returns -1 if o1 is younger than
82         *          o2
83         */
84        @Override
85        public int compare(BaseDescription o1, BaseDescription o2) {
86            return o1.getRegistrationDate().compareTo(o2.getRegistrationDate());
87        }
88    };
89
90    public static ProfileDescription toProfile(BaseDescription baseDescription) {
91        if (baseDescription == null)
92            return null;
93        ProfileDescription copy = new ProfileDescription();
94        BeanUtils.copyProperties(baseDescription, copy);
95        return copy;
96    }
97
98    public static ComponentDescription toComponent(BaseDescription baseDescription) {
99        if (baseDescription == null)
100            return null;
101        ComponentDescription copy = new ComponentDescription();
102        BeanUtils.copyProperties(baseDescription, copy);
103        return copy;
104    }
105
106    public static List<ProfileDescription> toProfiles(
107            List<BaseDescription> baseDescription) {
108        if (baseDescription == null)
109            return null;
110        List<ProfileDescription> list = new ArrayList<ProfileDescription>();
111        for (BaseDescription c : baseDescription)
112            list.add(toProfile(c));
113        return list;
114    }
115
116    public static List<ComponentDescription> toComponents(
117            List<BaseDescription> baseDescription) {
118        if (baseDescription == null)
119            return null;
120        List<ComponentDescription> list = new ArrayList<ComponentDescription>();
121        for (BaseDescription c : baseDescription)
122            list.add(toComponent(c));
123        return list;
124    }
125
126}
Note: See TracBrowser for help on using the repository browser.