1 | package clarin.cmdi.componentregistry.impl; |
---|
2 | |
---|
3 | import clarin.cmdi.componentregistry.ComponentRegistryException; |
---|
4 | import java.text.ParseException; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.Comparator; |
---|
7 | import java.util.Date; |
---|
8 | import java.util.List; |
---|
9 | |
---|
10 | import org.apache.commons.lang.time.DateFormatUtils; |
---|
11 | import org.apache.commons.lang.time.DateUtils; |
---|
12 | import org.springframework.beans.BeanUtils; |
---|
13 | |
---|
14 | import clarin.cmdi.componentregistry.model.BaseDescription; |
---|
15 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
16 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
17 | |
---|
18 | /** |
---|
19 | * Utilities for working with {@link BaseDescription}s |
---|
20 | * |
---|
21 | * @author george.georgovassilis@mpi.nl |
---|
22 | * |
---|
23 | */ |
---|
24 | public 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 | } |
---|