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