1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.redlist.bfnXml;
|
11
|
|
12
|
import static eu.etaxonomy.cdm.io.common.ImportHelper.OBLIGATORY;
|
13
|
import static eu.etaxonomy.cdm.io.common.ImportHelper.OVERWRITE;
|
14
|
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.Arrays;
|
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.Content;
|
23
|
import org.jdom.Element;
|
24
|
import org.jdom.Namespace;
|
25
|
import org.jdom.Text;
|
26
|
|
27
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
28
|
import eu.etaxonomy.cdm.common.ResultWrapper;
|
29
|
import eu.etaxonomy.cdm.common.XmlHelp;
|
30
|
import eu.etaxonomy.cdm.io.common.CdmImportBase;
|
31
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
32
|
import eu.etaxonomy.cdm.io.common.MapWrapper;
|
33
|
import eu.etaxonomy.cdm.io.tcsxml.CdmSingleAttributeXmlMapperBase;
|
34
|
import eu.etaxonomy.cdm.model.agent.Team;
|
35
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
36
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
37
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
38
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
39
|
|
40
|
/**
|
41
|
* @author a.oppermann
|
42
|
* @date 03.07.2013
|
43
|
*
|
44
|
*/
|
45
|
public abstract class BfnXmlImportBase extends CdmImportBase<BfnXmlImportConfigurator, BfnXmlImportState> {
|
46
|
private static final Logger logger = Logger.getLogger(BfnXmlImportBase.class);
|
47
|
|
48
|
protected static Namespace nsTcom = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/Common#");
|
49
|
protected static Namespace nsTn = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonName#");
|
50
|
protected static Namespace nsTgeo = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/GeographicRegion#");
|
51
|
protected static Namespace nsTc = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonConcept#");
|
52
|
protected static Namespace nsTpub = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/PublicationCitation#");
|
53
|
protected static Namespace nsTpalm = Namespace.getNamespace("http://wp5.e-taxonomy.eu/import/palmae/common");
|
54
|
|
55
|
|
56
|
protected abstract void doInvoke(BfnXmlImportState state);
|
57
|
|
58
|
// /* (non-Javadoc)
|
59
|
// * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator,
|
60
|
// * eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
|
61
|
// */
|
62
|
// @Override
|
63
|
// protected boolean doInvoke(IImportConfigurator config,
|
64
|
// Map<String, MapWrapper<? extends CdmBase>> stores){
|
65
|
// BfnXmlImportState state = ((BfnXmlImportConfigurator)config).getState();
|
66
|
// state.setConfig((BfnXmlImportConfigurator)config);
|
67
|
// return doInvoke(state);
|
68
|
// }
|
69
|
|
70
|
|
71
|
protected boolean makeStandardMapper(Element parentElement, CdmBase ref, Set<String> omitAttributes,
|
72
|
CdmSingleAttributeXmlMapperBase[] classMappers){
|
73
|
if (omitAttributes == null){
|
74
|
omitAttributes = new HashSet<String>();
|
75
|
}
|
76
|
boolean result = true;
|
77
|
for (CdmSingleAttributeXmlMapperBase mapper : classMappers){
|
78
|
Object value = getValue(mapper, parentElement);
|
79
|
//write to destination
|
80
|
if (value != null){
|
81
|
String destinationAttribute = mapper.getDestinationAttribute();
|
82
|
if (! omitAttributes.contains(destinationAttribute)){
|
83
|
result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
|
84
|
}
|
85
|
}
|
86
|
}
|
87
|
return true;
|
88
|
}
|
89
|
|
90
|
private Object getValue(CdmSingleAttributeXmlMapperBase mapper, Element parentElement){
|
91
|
String sourceAttribute = mapper.getSourceAttribute();
|
92
|
Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
|
93
|
Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
|
94
|
if (child == null){
|
95
|
return null;
|
96
|
}
|
97
|
if (child.getContentSize() > 1){
|
98
|
logger.warn("Element is not String");
|
99
|
}
|
100
|
Object value = child.getTextTrim();
|
101
|
return value;
|
102
|
}
|
103
|
|
104
|
protected boolean checkAdditionalContents(Element parentElement, CdmSingleAttributeXmlMapperBase[] classMappers,
|
105
|
CdmSingleAttributeXmlMapperBase[] operationalMappers, CdmSingleAttributeXmlMapperBase[] unclearMappers){
|
106
|
List<Content> additionalContentList = new ArrayList<Content>();
|
107
|
List<Content> contentList = parentElement.getContent();
|
108
|
List<CdmSingleAttributeXmlMapperBase> mapperList = new ArrayList<CdmSingleAttributeXmlMapperBase>();
|
109
|
|
110
|
mapperList.addAll(Arrays.asList(classMappers));
|
111
|
mapperList.addAll(Arrays.asList(operationalMappers));
|
112
|
mapperList.addAll(Arrays.asList(unclearMappers));
|
113
|
|
114
|
for(Content content: contentList){
|
115
|
boolean contentExists = false;
|
116
|
if (content instanceof Element){
|
117
|
for (CdmSingleAttributeXmlMapperBase mapper : mapperList){
|
118
|
if (mapper.mapsSource(content, parentElement)){
|
119
|
contentExists = true;
|
120
|
break;
|
121
|
}
|
122
|
}
|
123
|
|
124
|
}else if (content instanceof Text){
|
125
|
//empty Text
|
126
|
if (((Text)content).getTextNormalize().equals("")){
|
127
|
contentExists = true;
|
128
|
}else{
|
129
|
//
|
130
|
}
|
131
|
}
|
132
|
|
133
|
if (contentExists == false){
|
134
|
additionalContentList.add(content);
|
135
|
}
|
136
|
}
|
137
|
for (Content additionalContent : additionalContentList){
|
138
|
logger.warn("Additional content: " + additionalContent);
|
139
|
}
|
140
|
return (additionalContentList.size() == 0);
|
141
|
}
|
142
|
|
143
|
protected Element getDataSetElement(BfnXmlImportConfigurator bfnConfig){
|
144
|
Element root = bfnConfig.getSourceRoot();
|
145
|
|
146
|
if (! "DEBExport".equals(root.getName())){//"DataSet"
|
147
|
logger.error("Root element is not 'DEBExport'");
|
148
|
return null;
|
149
|
}
|
150
|
if (bfnConfig.getBfnXmlNamespace() == null){
|
151
|
logger.error("No namespace defined for bfnXML");
|
152
|
return null;
|
153
|
}
|
154
|
if (! bfnConfig.getBfnXmlNamespace().equals(root.getNamespace())){
|
155
|
logger.error("Wrong namespace for element 'DEBExport'");
|
156
|
return null;
|
157
|
}
|
158
|
//TODO prevent multiple elements
|
159
|
|
160
|
return root;
|
161
|
}
|
162
|
|
163
|
// static public boolean checkFirstTwoFunctionElements(List<Object> objList){
|
164
|
// if (! (objList.get(0) instanceof BfnXmlImportConfigurator)){
|
165
|
// logger.error("first method object has wrong type. Must be " + BfnXmlImportConfigurator.class.getSimpleName() + " but is " + (objList.get(0) == null ? "null": objList.get(0).getClass().getSimpleName()));
|
166
|
// return false;
|
167
|
// }
|
168
|
// if (! (objList.get(1) == null) && ! (objList.get(1) instanceof Element)){
|
169
|
// logger.error("first method object has wrong type. Must be " + Element.class.getSimpleName() + " but is " + (objList.get(1) == null ? "null": objList.get(1).getClass().getSimpleName()));
|
170
|
// return false;
|
171
|
// }
|
172
|
// return true;
|
173
|
// }
|
174
|
|
175
|
|
176
|
protected boolean testAdditionalElements(Element parentElement, List<String> excludeList){
|
177
|
boolean result = true;
|
178
|
List<Element> list = parentElement.getChildren();
|
179
|
for (Element element : list){
|
180
|
if (! excludeList.contains(element.getName())){
|
181
|
logger.warn("Unknown element (" + element.getName() + ") in parent element (" + parentElement.getName() + ")");
|
182
|
result = false;
|
183
|
}
|
184
|
}
|
185
|
return result;
|
186
|
}
|
187
|
|
188
|
|
189
|
protected <T extends IdentifiableEntity> T makeReferenceType(Element element, Class<? extends T> clazz, MapWrapper<? extends T> objectMap, ResultWrapper<Boolean> success){
|
190
|
T result = null;
|
191
|
String linkType = element.getAttributeValue("linkType");
|
192
|
String ref = element.getAttributeValue("ref");
|
193
|
if(ref == null && linkType == null){
|
194
|
result = getInstance(clazz);
|
195
|
if (result != null){
|
196
|
String title = element.getTextNormalize();
|
197
|
result.setTitleCache(title, true);
|
198
|
}
|
199
|
}else if (linkType == null || linkType.equals("local")){
|
200
|
//TODO
|
201
|
result = objectMap.get(ref);
|
202
|
if (result == null){
|
203
|
logger.warn("Object (ref = " + ref + ")could not be found in WrapperMap");
|
204
|
}
|
205
|
}else if(linkType.equals("external")){
|
206
|
logger.warn("External link types not yet implemented");
|
207
|
}else if(linkType.equals("other")){
|
208
|
logger.warn("Other link types not yet implemented");
|
209
|
}else{
|
210
|
logger.warn("Unknown link type or missing ref");
|
211
|
}
|
212
|
if (result == null){
|
213
|
success.setValue(false);
|
214
|
}
|
215
|
return result;
|
216
|
}
|
217
|
|
218
|
|
219
|
protected Reference makeAccordingTo(Element elAccordingTo, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
|
220
|
Reference result = null;
|
221
|
if (elAccordingTo != null){
|
222
|
String childName = "AccordingToDetailed";
|
223
|
boolean obligatory = false;
|
224
|
Element elAccordingToDetailed = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
|
225
|
|
226
|
childName = "Simple";
|
227
|
obligatory = true;
|
228
|
Element elSimple = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
|
229
|
|
230
|
if (elAccordingToDetailed != null){
|
231
|
result = makeAccordingToDetailed(elAccordingToDetailed, referenceMap, success);
|
232
|
}else{
|
233
|
result = ReferenceFactory.newGeneric();
|
234
|
String title = elSimple.getTextNormalize();
|
235
|
result.setTitleCache(title, true);
|
236
|
}
|
237
|
}
|
238
|
return result;
|
239
|
}
|
240
|
|
241
|
|
242
|
private Reference makeAccordingToDetailed(Element elAccordingToDetailed, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
|
243
|
Reference result = null;
|
244
|
Namespace tcsNamespace = elAccordingToDetailed.getNamespace();
|
245
|
if (elAccordingToDetailed != null){
|
246
|
//AuthorTeam
|
247
|
String childName = "AuthorTeam";
|
248
|
boolean obligatory = false;
|
249
|
Element elAuthorTeam = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
250
|
makeAccordingToAuthorTeam(elAuthorTeam, success);
|
251
|
|
252
|
//PublishedIn
|
253
|
childName = "PublishedIn";
|
254
|
obligatory = false;
|
255
|
Element elPublishedIn = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
256
|
result = makeReferenceType(elPublishedIn, Reference.class, referenceMap, success);
|
257
|
|
258
|
//MicroReference
|
259
|
childName = "MicroReference";
|
260
|
obligatory = false;
|
261
|
Element elMicroReference = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
262
|
if (elMicroReference != null){
|
263
|
String microReference = elMicroReference.getTextNormalize();
|
264
|
if (CdmUtils.Nz(microReference).equals("")){
|
265
|
//TODO
|
266
|
logger.warn("MicroReference not yet implemented for AccordingToDetailed");
|
267
|
}
|
268
|
}
|
269
|
}
|
270
|
return result;
|
271
|
}
|
272
|
|
273
|
private Team makeAccordingToAuthorTeam(Element elAuthorTeam, ResultWrapper<Boolean> succes){
|
274
|
Team result = null;
|
275
|
if (elAuthorTeam != null){
|
276
|
//TODO
|
277
|
logger.warn("AuthorTeam not yet implemented for AccordingToDetailed");
|
278
|
}
|
279
|
return result;
|
280
|
}
|
281
|
|
282
|
|
283
|
|
284
|
}
|