1
2
3
4
5
6
7
8
9
10 package eu.etaxonomy.cdm.io.tcsrdf;
11
12 import static eu.etaxonomy.cdm.io.common.ImportHelper.NO_OVERWRITE;
13 import static eu.etaxonomy.cdm.io.common.ImportHelper.OBLIGATORY;
14 import static eu.etaxonomy.cdm.io.common.ImportHelper.OVERWRITE;
15
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.apache.log4j.Logger;
22 import org.jdom.Element;
23 import org.jdom.Namespace;
24 import org.springframework.stereotype.Component;
25
26 import eu.etaxonomy.cdm.common.CdmUtils;
27 import eu.etaxonomy.cdm.common.XmlHelp;
28 import eu.etaxonomy.cdm.io.berlinModel.CdmOneToManyMapper;
29 import eu.etaxonomy.cdm.io.common.ICdmIO;
30 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
31 import eu.etaxonomy.cdm.io.common.mapping.IXmlMapper;
32 import eu.etaxonomy.cdm.io.common.ImportHelper;
33 import eu.etaxonomy.cdm.io.common.MapWrapper;
34 import eu.etaxonomy.cdm.io.common.mapping.CdmSingleAttributeMapperBase;
35 import eu.etaxonomy.cdm.model.agent.Team;
36 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
37 import eu.etaxonomy.cdm.model.common.Marker;
38 import eu.etaxonomy.cdm.model.common.MarkerType;
39 import eu.etaxonomy.cdm.model.common.TimePeriod;
40
41
42
43
44
45 import eu.etaxonomy.cdm.model.reference.IArticle;
46 import eu.etaxonomy.cdm.model.reference.IBook;
47 import eu.etaxonomy.cdm.model.reference.IBookSection;
48 import eu.etaxonomy.cdm.model.reference.IJournal;
49 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
50 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
51 import eu.etaxonomy.cdm.model.reference.ReferenceType;
52 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
53
54
55
56
57
58
59 @Component
60 public class TcsRdfReferenceImport extends TcsRdfImportBase implements ICdmIO<TcsRdfImportState> {
61 private static final Logger logger = Logger.getLogger(TcsRdfReferenceImport.class);
62
63 private static int modCount = 1000;
64
65 public TcsRdfReferenceImport(){
66 super();
67 }
68
69 @Override
70 public boolean doCheck(TcsRdfImportState state){
71 boolean result = true;
72 result &= checkArticlesWithoutJournal(state.getConfig());
73
74
75 return result;
76 }
77
78 private static boolean checkArticlesWithoutJournal(IImportConfigurator bmiConfig){
79 try {
80 boolean result = true;
81
82
83
84
85 return result;
86 } catch (Exception e) {
87 e.printStackTrace();
88 return false;
89 }
90 }
91
92
93 protected static IXmlMapper[] standardMappers = new IXmlMapper[]{
94
95 new CdmTextElementMapper("volume", "volume"),
96 new CdmTextElementMapper("placePublished", "placePublished"),
97 new CdmTextElementMapper("publisher", "publisher"),
98
99 new CdmTextElementMapper("pages", "pages"),
100
101
102
103 };
104
105 protected static CdmSingleAttributeXmlMapperBase[] operationalMappers = new CdmSingleAttributeXmlMapperBase[]{
106 new CdmUnclearMapper("year")
107 , new CdmUnclearMapper("title")
108 , new CdmUnclearMapper("shortTitle")
109 , new CdmUnclearMapper("publicationType")
110 , new CdmUnclearMapper("parentPublication")
111 , new CdmUnclearMapper("authorship")
112
113 };
114
115
116
117
118
119 protected static CdmSingleAttributeXmlMapperBase[] unclearMappers = new CdmSingleAttributeXmlMapperBase[]{
120
121 };
122
123
124
125 private boolean makeStandardMapper(Element parentElement, ReferenceBase ref, Set<String> omitAttributes){
126 if (omitAttributes == null){
127 omitAttributes = new HashSet<String>();
128 }
129 boolean result = true;
130 for (IXmlMapper mapper : standardMappers){
131 if (mapper instanceof CdmSingleAttributeMapperBase){
132 makeSingleAttributeMapper((CdmSingleAttributeXmlMapperBase)mapper, parentElement, ref, omitAttributes);
133 }else if (mapper instanceof CdmOneToManyMapper){
134 makeMultipleAttributeMapper((CdmOneToManyMapper)mapper, parentElement, ref, omitAttributes);
135 }else{
136 logger.error("Unrecognized mapper type");
137 return false;
138 }
139
140
141 }
142 return true;
143 }
144
145 private boolean makeSingleAttributeMapper(CdmSingleAttributeXmlMapperBase mapper, Element parentElement, ReferenceBase ref, Set<String> omitAttributes){
146 boolean result = true;
147 Object value = getValue(mapper, parentElement);
148
149 if (value != null){
150 String destinationAttribute = mapper.getDestinationAttribute();
151 if (! omitAttributes.contains(destinationAttribute)){
152 result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
153 }
154 }
155 return result;
156 }
157
158 private boolean makeMultipleAttributeMapper(CdmOneToManyMapper<?,?,CdmTextElementMapper> mapper, Element parentElement, ReferenceBase ref, Set<String> omitAttributes){
159 if (omitAttributes == null){
160 omitAttributes = new HashSet<String>();
161 }
162 boolean result = true;
163 String destinationAttribute = mapper.getSingleAttributeName();
164 List<Object> sourceValues = new ArrayList<Object>();
165 List<Class> classes = new ArrayList<Class>();
166 for (CdmTextElementMapper singleMapper : mapper.getSingleMappers()){
167 String sourceAttribute = singleMapper.getSourceAttribute();
168 Object value = getValue(singleMapper, parentElement);
169
170 sourceValues.add(value);
171 Class clazz = singleMapper.getTypeClass();
172 classes.add(clazz);
173 }
174
175 result &= ImportHelper.addMultipleValues(sourceValues, ref, destinationAttribute, classes, NO_OVERWRITE, OBLIGATORY);
176 return result;
177 }
178
179 private Object getValue(CdmSingleAttributeXmlMapperBase mapper, Element parentElement){
180 String sourceAttribute = mapper.getSourceAttribute().toLowerCase();
181 Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
182 Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
183 if (child == null){
184 return null;
185 }
186 if (child.getContentSize() > 1){
187 logger.warn("Element is not String");
188 }
189 Object value = child.getTextTrim();
190 return value;
191 }
192
193 protected boolean doInvoke(TcsRdfImportState state){
194 MapWrapper<ReferenceBase> referenceMap = (MapWrapper<ReferenceBase>)state.getStore(ICdmIO.REFERENCE_STORE);
195 MapWrapper<ReferenceBase> nomRefMap = (MapWrapper<ReferenceBase>)state.getStore(ICdmIO.NOMREF_STORE);
196
197 TcsRdfImportConfigurator config = state.getConfig();
198 Element root = config.getSourceRoot();
199 logger.info("start makeReferences ...");
200
201 String tcsElementName;
202 Namespace tcsNamespace;
203 boolean success = true;
204
205 Namespace rdfNamespace = config.getRdfNamespace();
206 Namespace publicationNamespace = config.getPublicationNamespace();
207 ReferenceFactory refFactory = ReferenceFactory.newInstance();
208
209 String idNamespace = "PublicationCitation";
210 tcsElementName = "PublicationCitation";
211 tcsNamespace = publicationNamespace;
212 List<Element> elPublicationCitations = root.getChildren(tcsElementName, tcsNamespace);
213
214 int nomRefCount = 0;
215 int biblioRefsCount = 0;
216
217 int i = 0;
218
219 for (Element elPublicationCitation : elPublicationCitations){
220
221 if ((++i % modCount) == 0){ logger.info("references handled: " + (i-1));}
222
223
224 String strAbout = elPublicationCitation.getAttributeValue("about", rdfNamespace);
225
226 tcsElementName = "publicationType";
227 tcsNamespace = publicationNamespace;
228 String strPubType = XmlHelp.getChildAttributeValue(elPublicationCitation, tcsElementName, tcsNamespace, "resource", rdfNamespace);
229
230 try {
231 ReferenceBase<?> ref = TcsRdfTransformer.pubTypeStr2PubType(strPubType);
232 if (ref==null){
233 ref = refFactory.newGeneric();
234 }
235
236 Set<String> omitAttributes = null;
237 makeStandardMapper(elPublicationCitation, ref, omitAttributes);
238
239
240 tcsElementName = "authorship";
241 tcsNamespace = publicationNamespace;
242 String strAuthorship = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
243
244 TeamOrPersonBase authorTeam = Team.NewInstance();
245 authorTeam.setTitleCache(strAuthorship, true);
246 ref.setAuthorTeam(authorTeam);
247
248 tcsElementName = "year";
249 tcsNamespace = publicationNamespace;
250 String strYear = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
251 TimePeriod datePublished = ImportHelper.getDatePublished(strYear);
252 ref.setDatePublished(datePublished);
253
254
255
256 tcsElementName = "parentPublication";
257 tcsNamespace = publicationNamespace;
258 String strParent = XmlHelp.getChildAttributeValue(elPublicationCitation, tcsElementName, tcsNamespace, "resource", rdfNamespace);
259 ReferenceBase parent = referenceMap.get(strParent);
260 if (parent != null){
261 if ((ref.getType().equals(ReferenceType.Article)) && (parent.getType().equals(ReferenceType.Journal))){
262 ((IArticle)ref).setInJournal((IJournal)parent);
263 }else if ((ref.getType().equals(ReferenceType.BookSection)) && (parent.getType().equals(ReferenceType.Book))){
264 ((IBookSection)ref).setInBook((IBook)parent);
265 }else{
266 logger.warn("parent type (parent: " + parent.getType() +", child("+strAbout+"): " + ref.getType() + ")not yet implemented");
267
268 }
269 }
270
271
272
273
274 tcsElementName = "shortTitle";
275 tcsNamespace = publicationNamespace;
276 boolean nomRefExists = false;
277 String strShortTitle = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
278 if (! CdmUtils.Nz(strShortTitle).trim().equals("")){
279 ref.setTitle(strShortTitle);
280 ImportHelper.setOriginalSource(ref, config.getSourceReference(), strAbout, idNamespace);
281 nomRefMap.put(strAbout, ref);
282 nomRefCount++;
283 nomRefExists = true;
284 }
285
286 tcsElementName = "title";
287 tcsNamespace = publicationNamespace;
288 String strTitle = elPublicationCitation.getChildText(tcsElementName, tcsNamespace);
289 tcsNamespace = publicationNamespace;
290 if (! CdmUtils.Nz(strTitle).trim().equals("") || nomRefExists == false){
291
292 ReferenceBase<?> biblioRef = (ReferenceBase<?>)ref.clone();
293 biblioRef.setTitle(strTitle);
294 ImportHelper.setOriginalSource(biblioRef, config.getSourceReference(), strAbout, idNamespace);
295 referenceMap.put(strAbout, biblioRef);
296 biblioRefsCount++;
297 }
298
299
300 checkAdditionalContents(elPublicationCitation, standardMappers, operationalMappers, unclearMappers);
301
302 if (state.getConfig().isPublishReferences()){
303 ref.addMarker(Marker.NewInstance(MarkerType.IN_BIBLIOGRAPHY(), false));
304 }
305
306
307
308 } catch (UnknownCdmTypeException e) {
309
310 logger.warn("Name with id " + strAbout + " has unknown type " + strPubType + " and could not be saved.");
311 success = false;
312 }
313 }
314
315
316 ReferenceBase sec = referenceMap.get(config.getSourceSecId());
317 if (sec == null){
318 sec = nomRefMap.get(config.getSourceSecId());
319 }
320 if (sec != null){
321 sec.setUuid(config.getSecUuid());
322 logger.info("concept reference uuid changed to: " + config.getSecUuid());
323 }
324
325
326 logger.info("Save nomenclatural references (" + nomRefCount + ")");
327 getReferenceService().save(nomRefMap.objects());
328 logger.info("Save bibliographical references (" + biblioRefsCount +")");
329 getReferenceService().save(referenceMap.objects());
330
331
332 logger.info("end makeReferences ...");
333 return success;
334
335 }
336
337
338
339
340 protected boolean isIgnore(TcsRdfImportState state){
341 return (state.getConfig().getDoReferences() == IImportConfigurator.DO_REFERENCES.NONE);
342 }
343
344 }