1 | package eu.clarin.cmdi.virtualcollectionregistry.service.impl; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.virtualcollectionregistry.model.Creator; |
---|
4 | import eu.clarin.cmdi.virtualcollectionregistry.model.GeneratedBy; |
---|
5 | import eu.clarin.cmdi.virtualcollectionregistry.model.Resource; |
---|
6 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection; |
---|
7 | import static eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection.State.DEAD; |
---|
8 | import static eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection.State.DELETED; |
---|
9 | import static eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection.State.PUBLIC; |
---|
10 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.CMD; |
---|
11 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.CMD.Components.VirtualCollection.Creator.Email; |
---|
12 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.CMD.Components.VirtualCollection.Creator.Organisation; |
---|
13 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.CMD.Components.VirtualCollection.Description; |
---|
14 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.CMD.Components.VirtualCollection.GeneratedBy.QueryProfile; |
---|
15 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.CMD.Components.VirtualCollection.GeneratedBy.QueryValue; |
---|
16 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.CMD.Components.VirtualCollection.Name; |
---|
17 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.CMD.Components.VirtualCollection.ReproducabilityNotice; |
---|
18 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.CMD.Resources.ResourceProxyList.ResourceProxy; |
---|
19 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.CMD.Resources.ResourceProxyList.ResourceProxy.ResourceType; |
---|
20 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.ComplextypePurpose1; |
---|
21 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.ComplextypeReproducability1; |
---|
22 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.ComplextypeStatus1; |
---|
23 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.ResourcetypeSimple; |
---|
24 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.SimpletypePurpose1; |
---|
25 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.SimpletypeReproducability1; |
---|
26 | import eu.clarin.cmdi.virtualcollectionregistry.model.cmdi.SimpletypeStatus1; |
---|
27 | import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionCMDICreator; |
---|
28 | import java.util.Date; |
---|
29 | import java.util.List; |
---|
30 | import java.util.UUID; |
---|
31 | import javax.xml.datatype.DatatypeConfigurationException; |
---|
32 | import javax.xml.datatype.DatatypeFactory; |
---|
33 | import javax.xml.datatype.XMLGregorianCalendar; |
---|
34 | import org.apache.commons.lang.StringUtils; |
---|
35 | import org.apache.commons.lang3.time.FastDateFormat; |
---|
36 | import org.slf4j.Logger; |
---|
37 | import org.slf4j.LoggerFactory; |
---|
38 | import org.springframework.beans.factory.annotation.Value; |
---|
39 | import org.springframework.stereotype.Service; |
---|
40 | |
---|
41 | /** |
---|
42 | * A service implementation that creates a CMDI object hierarchy for a |
---|
43 | * {@link VirtualCollection} object based on the classes generated from the |
---|
44 | * Virtual Collection profile schema by JAXB (as configured in the pom.xml of |
---|
45 | * this project) |
---|
46 | * |
---|
47 | * @author twagoo |
---|
48 | */ |
---|
49 | @Service |
---|
50 | public class VirtualCollectionCMDICreatorImpl implements VirtualCollectionCMDICreator { |
---|
51 | |
---|
52 | private final static Logger logger = LoggerFactory.getLogger(VirtualCollectionCMDICreatorImpl.class); |
---|
53 | private final DatatypeFactory dataTypeFactory; |
---|
54 | |
---|
55 | // Collection display name value read from context.xml with a fallback value inserted by Spring |
---|
56 | @Value("${eu.clarin.cmdi.virtualcollectionregistry.collectiondisplayname:CLARIN Virtual Collection Registry}") |
---|
57 | private String collectionDisplayName; |
---|
58 | |
---|
59 | public VirtualCollectionCMDICreatorImpl() throws DatatypeConfigurationException { |
---|
60 | dataTypeFactory = DatatypeFactory.newInstance(); |
---|
61 | } |
---|
62 | |
---|
63 | ///////// CMDI CONSTANTS //////////////////// |
---|
64 | // These should match up with the schema |
---|
65 | // the classes in eu.clarin.cmdi. |
---|
66 | // virtualcollectionregistry.model.cmdi are |
---|
67 | // generated from by JAXB |
---|
68 | ///////////////////////////////////////////// |
---|
69 | public static final String VIRTUAL_COLLECTION_PROFILE_ID |
---|
70 | = "clarin.eu:cr1:p_1404130561238"; |
---|
71 | public static final String VIRTUAL_COLLECTION_PROFILE_SCHEMA_LOCATION |
---|
72 | = "http://www.clarin.eu/cmd/ http://catalog.clarin.eu/ds/ComponentRegistry/rest/registry/profiles/" |
---|
73 | + VIRTUAL_COLLECTION_PROFILE_ID |
---|
74 | + "/xsd"; |
---|
75 | private static final String CMD_VERSION = "1.1"; |
---|
76 | private static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd"); |
---|
77 | ///////// END OF CMDI CONSTANTS ///////// |
---|
78 | |
---|
79 | @Override |
---|
80 | public String getSchemaLocation() { |
---|
81 | return VIRTUAL_COLLECTION_PROFILE_SCHEMA_LOCATION; |
---|
82 | } |
---|
83 | |
---|
84 | @Override |
---|
85 | public CMD createMetadataStructure(VirtualCollection vc) { |
---|
86 | logger.debug("Creating CMD root"); |
---|
87 | final CMD cmdRoot = new CMD(); |
---|
88 | cmdRoot.setCMDVersion(CMD_VERSION); |
---|
89 | logger.trace("Creating header"); |
---|
90 | cmdRoot.setHeader(createHeader(vc)); |
---|
91 | logger.trace("Creating resources"); |
---|
92 | final CMD.Resources resources = createResources(vc); |
---|
93 | cmdRoot.setResources(resources); |
---|
94 | logger.trace("Creating components"); |
---|
95 | cmdRoot.setComponents(createComponents(vc, resources)); |
---|
96 | return cmdRoot; |
---|
97 | } |
---|
98 | |
---|
99 | private CMD.Header createHeader(VirtualCollection vc) { |
---|
100 | final CMD.Header header = new CMD.Header(); |
---|
101 | header.setMdCreationDate(dataTypeFactory.newXMLGregorianCalendar(DATE_FORMAT.format(new Date()))); |
---|
102 | header.setMdProfile(VIRTUAL_COLLECTION_PROFILE_ID); |
---|
103 | header.getMdCreator().add(vc.getOwner().getName()); |
---|
104 | header.setMdSelfLink(vc.getPersistentIdentifier().getURI()); |
---|
105 | if (collectionDisplayName != null) { |
---|
106 | header.setMdCollectionDisplayName(collectionDisplayName); |
---|
107 | } |
---|
108 | return header; |
---|
109 | } |
---|
110 | |
---|
111 | private CMD.Resources createResources(VirtualCollection vc) { |
---|
112 | final CMD.Resources resources = new CMD.Resources(); |
---|
113 | |
---|
114 | final CMD.Resources.ResourceProxyList resourceProxyList = new CMD.Resources.ResourceProxyList(); |
---|
115 | resources.setResourceProxyList(resourceProxyList); |
---|
116 | final List<ResourceProxy> proxyList = resourceProxyList.getResourceProxy(); |
---|
117 | |
---|
118 | for (Resource resource : vc.getResources()) { |
---|
119 | final ResourceProxy resourceProxy = new ResourceProxy(); |
---|
120 | if (resource.getId() == null) { |
---|
121 | resourceProxy.setId("r" + UUID.randomUUID().toString()); |
---|
122 | } else { |
---|
123 | resourceProxy.setId("r" + Long.toString(resource.getId())); |
---|
124 | } |
---|
125 | resourceProxy.setResourceRef(resource.getRef()); |
---|
126 | |
---|
127 | final ResourceType type = new ResourceType(); |
---|
128 | if (resource.getType() == Resource.Type.METADATA) { |
---|
129 | type.setValue(ResourcetypeSimple.METADATA); |
---|
130 | } else { |
---|
131 | type.setValue(ResourcetypeSimple.RESOURCE); |
---|
132 | } |
---|
133 | resourceProxy.setResourceType(type); |
---|
134 | |
---|
135 | proxyList.add(resourceProxy); |
---|
136 | } |
---|
137 | |
---|
138 | // add empty instance of mandatory journal file proxy list |
---|
139 | resources.setJournalFileProxyList(new CMD.Resources.JournalFileProxyList()); |
---|
140 | // add empty instance of mandatory resource relation list |
---|
141 | resources.setResourceRelationList(new CMD.Resources.ResourceRelationList()); |
---|
142 | return resources; |
---|
143 | } |
---|
144 | |
---|
145 | private CMD.Components createComponents(VirtualCollection vc, CMD.Resources resources) { |
---|
146 | final CMD.Components.VirtualCollection virtualCollection = new CMD.Components.VirtualCollection(); |
---|
147 | |
---|
148 | final Name name = new Name(); |
---|
149 | name.setValue(vc.getName()); |
---|
150 | virtualCollection.setName(name); |
---|
151 | |
---|
152 | if (vc.getDescription() != null) { |
---|
153 | final Description description = new Description(); |
---|
154 | description.setValue(vc.getDescription()); |
---|
155 | virtualCollection.setDescription(description); |
---|
156 | } |
---|
157 | |
---|
158 | virtualCollection.setCreationDate(getCreationDate(vc)); |
---|
159 | virtualCollection.setStatus(getStatus(vc)); |
---|
160 | virtualCollection.setPurpose(getPurpose(vc)); |
---|
161 | virtualCollection.getCreator().add(getCreator(vc)); |
---|
162 | virtualCollection.setReproducability(getReproducability(vc)); |
---|
163 | |
---|
164 | if (vc.getReproducibilityNotice() != null) { |
---|
165 | final ReproducabilityNotice reproducabilityNotice = new ReproducabilityNotice(); |
---|
166 | reproducabilityNotice.setValue(vc.getReproducibilityNotice()); |
---|
167 | virtualCollection.setReproducabilityNotice(reproducabilityNotice); |
---|
168 | } |
---|
169 | |
---|
170 | for (String keyword : vc.getKeywords()) { |
---|
171 | final CMD.Components.VirtualCollection.Keyword keywordElement = new CMD.Components.VirtualCollection.Keyword(); |
---|
172 | keywordElement.setValue(keyword); |
---|
173 | virtualCollection.getKeyword().add(keywordElement); |
---|
174 | } |
---|
175 | |
---|
176 | for (Resource resource : vc.getResources()) { |
---|
177 | addResource(virtualCollection, resource, resources); |
---|
178 | } |
---|
179 | |
---|
180 | if (vc.getType() == VirtualCollection.Type.INTENSIONAL && vc.getGeneratedBy() != null) { |
---|
181 | virtualCollection.setGeneratedBy(getGeneratedBy(vc.getGeneratedBy())); |
---|
182 | } |
---|
183 | |
---|
184 | final CMD.Components components = new CMD.Components(); |
---|
185 | components.setVirtualCollection(virtualCollection); |
---|
186 | return components; |
---|
187 | } |
---|
188 | |
---|
189 | private CMD.Components.VirtualCollection.GeneratedBy getGeneratedBy(GeneratedBy generatedBy) { |
---|
190 | final CMD.Components.VirtualCollection.GeneratedBy component = new CMD.Components.VirtualCollection.GeneratedBy(); |
---|
191 | |
---|
192 | final String uri = generatedBy.getURI(); |
---|
193 | if (uri != null) { |
---|
194 | component.setURI(uri); |
---|
195 | } |
---|
196 | |
---|
197 | final String description = generatedBy.getDescription(); |
---|
198 | if (description != null) { |
---|
199 | final CMD.Components.VirtualCollection.GeneratedBy.Description descriptionElement = new CMD.Components.VirtualCollection.GeneratedBy.Description(); |
---|
200 | descriptionElement.setValue(description); |
---|
201 | component.setDescription(descriptionElement); |
---|
202 | } |
---|
203 | |
---|
204 | if (generatedBy.getQuery() != null) { |
---|
205 | final String profile = generatedBy.getQuery().getProfile(); |
---|
206 | if (profile != null) { |
---|
207 | final QueryProfile profileElement = new QueryProfile(); |
---|
208 | profileElement.setValue(profile); |
---|
209 | component.setQueryProfile(profileElement); |
---|
210 | } |
---|
211 | |
---|
212 | final String value = generatedBy.getQuery().getValue(); |
---|
213 | if (value != null) { |
---|
214 | final QueryValue valueElement = new QueryValue(); |
---|
215 | valueElement.setValue(value); |
---|
216 | component.setQueryValue(valueElement); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | return component; |
---|
221 | } |
---|
222 | |
---|
223 | private XMLGregorianCalendar getCreationDate(VirtualCollection vc) { |
---|
224 | final Date creationDate = vc.getCreationDate(); |
---|
225 | return dataTypeFactory.newXMLGregorianCalendar(DATE_FORMAT.format(creationDate)); |
---|
226 | } |
---|
227 | |
---|
228 | private ComplextypeStatus1 getStatus(VirtualCollection vc) { |
---|
229 | // status is a mandatory field |
---|
230 | final ComplextypeStatus1 status = new ComplextypeStatus1(); |
---|
231 | switch (vc.getState()) { |
---|
232 | case PUBLIC: |
---|
233 | status.setValue(SimpletypeStatus1.PUBLISHED); |
---|
234 | break; |
---|
235 | case DELETED: |
---|
236 | status.setValue(SimpletypeStatus1.DEPRECATED); |
---|
237 | break; |
---|
238 | case DEAD: |
---|
239 | status.setValue(SimpletypeStatus1.DEPRECATED); |
---|
240 | break; |
---|
241 | default: |
---|
242 | status.setValue(SimpletypeStatus1.DRAFT); |
---|
243 | } |
---|
244 | return status; |
---|
245 | } |
---|
246 | |
---|
247 | private ComplextypeReproducability1 getReproducability(VirtualCollection vc) { |
---|
248 | if (vc.getReproducibility() == null) { |
---|
249 | return null; |
---|
250 | } else { |
---|
251 | final ComplextypeReproducability1 reproducability = new ComplextypeReproducability1(); |
---|
252 | switch (vc.getReproducibility()) { |
---|
253 | //TODO: better mapping |
---|
254 | case FLUCTUATING: |
---|
255 | reproducability.setValue(SimpletypeReproducability1.FLUCTUATING); |
---|
256 | break; |
---|
257 | case INTENDED: |
---|
258 | reproducability.setValue(SimpletypeReproducability1.INTENDED); |
---|
259 | break; |
---|
260 | case UNTENDED: |
---|
261 | reproducability.setValue(SimpletypeReproducability1.UNTENDED); |
---|
262 | break; |
---|
263 | } |
---|
264 | return reproducability; |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | private ComplextypePurpose1 getPurpose(VirtualCollection vc) { |
---|
269 | if (vc.getPurpose() == null) { |
---|
270 | return null; |
---|
271 | } else { |
---|
272 | final ComplextypePurpose1 purpose = new ComplextypePurpose1(); |
---|
273 | switch (vc.getPurpose()) { |
---|
274 | case FUTURE_USE: |
---|
275 | purpose.setValue(SimpletypePurpose1.FUTURE_USE); |
---|
276 | break; |
---|
277 | case REFERENCE: |
---|
278 | purpose.setValue(SimpletypePurpose1.REFERENCE); |
---|
279 | break; |
---|
280 | case RESEARCH: |
---|
281 | purpose.setValue(SimpletypePurpose1.RESEARCH); |
---|
282 | break; |
---|
283 | case SAMPLE: |
---|
284 | purpose.setValue(SimpletypePurpose1.SAMPLE); |
---|
285 | break; |
---|
286 | } |
---|
287 | return purpose; |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | private CMD.Components.VirtualCollection.Creator getCreator(VirtualCollection vc) { |
---|
292 | if (vc.getCreators().isEmpty()) { |
---|
293 | return null; |
---|
294 | } else { |
---|
295 | final CMD.Components.VirtualCollection.Creator creator = new CMD.Components.VirtualCollection.Creator(); |
---|
296 | final Creator vcCreator = vc.getCreators().get(0); |
---|
297 | |
---|
298 | // name/creator is a mandatory field |
---|
299 | final CMD.Components.VirtualCollection.Creator.Name name = new CMD.Components.VirtualCollection.Creator.Name(); |
---|
300 | name.setValue(vcCreator.getPerson()); |
---|
301 | creator.setName(name); |
---|
302 | |
---|
303 | if (vcCreator.getEMail() != null) { |
---|
304 | final Email email = new Email(); |
---|
305 | email.setValue(vcCreator.getEMail()); |
---|
306 | creator.setEmail(email); |
---|
307 | } |
---|
308 | |
---|
309 | if (vcCreator.getOrganisation() != null) { |
---|
310 | final Organisation organisation = new Organisation(); |
---|
311 | organisation.setValue(vcCreator.getOrganisation()); |
---|
312 | creator.setOrganisation(organisation); |
---|
313 | } |
---|
314 | return creator; |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | private void addResource(final CMD.Components.VirtualCollection virtualCollection, Resource resource, CMD.Resources resources) { |
---|
319 | if (!StringUtils.isEmpty(resource.getLabel()) || !StringUtils.isEmpty(resource.getDescription())) { |
---|
320 | final CMD.Components.VirtualCollection.Resource resourceComponent = new CMD.Components.VirtualCollection.Resource(); |
---|
321 | if (!StringUtils.isEmpty(resource.getLabel())) { |
---|
322 | // add label |
---|
323 | final CMD.Components.VirtualCollection.Resource.Label label = new CMD.Components.VirtualCollection.Resource.Label(); |
---|
324 | label.setValue(resource.getLabel()); |
---|
325 | resourceComponent.setLabel(label); |
---|
326 | } |
---|
327 | if (!StringUtils.isEmpty(resource.getDescription())) { |
---|
328 | // add description |
---|
329 | final CMD.Components.VirtualCollection.Resource.Description description = new CMD.Components.VirtualCollection.Resource.Description(); |
---|
330 | description.setValue(resource.getDescription()); |
---|
331 | resourceComponent.setDescription(description); |
---|
332 | } |
---|
333 | // look up the resource proxy (by URI) and set a reference |
---|
334 | for (ResourceProxy rp : resources.getResourceProxyList().getResourceProxy()) { |
---|
335 | if (rp.getResourceRef().equals(resource.getRef())) { |
---|
336 | resourceComponent.getRef().add(rp); |
---|
337 | break; |
---|
338 | } |
---|
339 | } |
---|
340 | virtualCollection.getResource().add(resourceComponent); |
---|
341 | } |
---|
342 | } |
---|
343 | } |
---|