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

Last change on this file since 5549 was 5549, checked in by olhsha@mpi.nl, 10 years ago

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

  • Property svn:mime-type set to text/plain
File size: 5.0 KB
Line 
1package clarin.cmdi.componentregistry.impl;
2
3import clarin.cmdi.componentregistry.ComponentRegistryException;
4import java.text.ParseException;
5import java.util.ArrayList;
6import java.util.Comparator;
7import java.util.Date;
8import java.util.List;
9
10import org.apache.commons.lang.time.DateFormatUtils;
11import org.apache.commons.lang.time.DateUtils;
12import org.springframework.beans.BeanUtils;
13
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     * Compares two descriptions by the their value as returned by      {@link BaseDescription#getName()
58     * }
59     */
60    public static final Comparator<? super BaseDescription> COMPARE_ON_NAME = new Comparator<BaseDescription>() {
61        @Override
62        public int compare(BaseDescription o1, BaseDescription o2) {
63            int result = 0;
64            if (o1.getName() != null && o2.getName() != null) {
65                result = o1.getName().compareToIgnoreCase(o2.getName());
66            }
67            if (o1.getId() != null && result == 0) {
68                result = o1.getId().compareTo(o2.getId());
69            }
70            return result;
71        }
72    };
73    /**
74     * Compares two descriptions by the their value as returned by
75     * {@link BaseDescription#getRegistrationDate() () * }
76     */
77    public static final Comparator<? super BaseDescription> COMPARE_ON_DATE = new Comparator<BaseDescription>() {
78        /**
79         * @returns 1 if o11 is older than o2, returns -1 if o1 is younger than
80         * o2
81         */
82        @Override
83        public int compare(BaseDescription o1, BaseDescription o2) {
84            return o1.getRegistrationDate().compareTo(o2.getRegistrationDate());
85        }
86    };
87
88    public static ProfileDescription toProfile(BaseDescription baseDescription) throws ComponentRegistryException {
89        if (baseDescription == null) {
90            return null;
91        }
92        if (baseDescription.getId().startsWith(ProfileDescription.PROFILE_PREFIX)) {
93            ProfileDescription copy = new ProfileDescription();
94            BeanUtils.copyProperties(baseDescription, copy);
95            return copy;
96        } else {
97            throw new ComponentRegistryException("The item is not a profile.");
98        }
99    }
100
101    public static ComponentDescription toComponent(BaseDescription baseDescription) throws ComponentRegistryException {
102        if (baseDescription == null) {
103            return null;
104        }
105        if (baseDescription.getId().startsWith(ComponentDescription.COMPONENT_PREFIX)) {
106            ComponentDescription copy = new ComponentDescription();
107            BeanUtils.copyProperties(baseDescription, copy);
108            return copy;
109        } else {
110            throw new ComponentRegistryException("The item is not a component");
111        }
112    }
113
114    public static List<ProfileDescription> toProfiles(
115            List<BaseDescription> baseDescription) {
116        if (baseDescription == null) {
117            return null;
118        }
119        List<ProfileDescription> list = new ArrayList<ProfileDescription>();
120        for (BaseDescription c : baseDescription) {
121            try {
122                list.add(toProfile(c));
123            } catch (ComponentRegistryException e) {
124            }
125        }
126        return list;
127    }
128
129    public static List<ComponentDescription> toComponents(
130            List<BaseDescription> baseDescription) {
131        if (baseDescription == null) {
132            return null;
133        }
134        List<ComponentDescription> list = new ArrayList<ComponentDescription>();
135        for (BaseDescription c : baseDescription) {
136            try {
137                list.add(toComponent(c));
138            } catch (ComponentRegistryException e) {
139            }
140        }
141        return list;
142    }
143}
Note: See TracBrowser for help on using the repository browser.