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