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.tcsxml.in;
|
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.tcsrdf.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.mueller
|
42
|
* @created 04.08.2008
|
43
|
* @version 1.0
|
44
|
*/
|
45
|
public abstract class TcsXmlImportBase extends CdmImportBase<TcsXmlImportConfigurator, TcsXmlImportState> {
|
46
|
private static final Logger logger = Logger.getLogger(TcsXmlImportBase.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(TcsXmlImportState state);
|
57
|
ReferenceFactory refFactory = ReferenceFactory.newInstance();
|
58
|
|
59
|
// /* (non-Javadoc)
|
60
|
// * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, 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
|
// TcsXmlImportState state = ((TcsXmlImportConfigurator)config).getState();
|
66
|
// state.setConfig((TcsXmlImportConfigurator)config);
|
67
|
// return doInvoke(state);
|
68
|
// }
|
69
|
|
70
|
|
71
|
protected boolean makeStandardMapper(Element parentElement, CdmBase ref, Set<String> omitAttributes, CdmSingleAttributeXmlMapperBase[] classMappers){
|
72
|
if (omitAttributes == null){
|
73
|
omitAttributes = new HashSet<String>();
|
74
|
}
|
75
|
boolean result = true;
|
76
|
for (CdmSingleAttributeXmlMapperBase mapper : classMappers){
|
77
|
Object value = getValue(mapper, parentElement);
|
78
|
//write to destination
|
79
|
if (value != null){
|
80
|
String destinationAttribute = mapper.getDestinationAttribute();
|
81
|
if (! omitAttributes.contains(destinationAttribute)){
|
82
|
result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
|
83
|
}
|
84
|
}
|
85
|
}
|
86
|
return true;
|
87
|
}
|
88
|
|
89
|
private Object getValue(CdmSingleAttributeXmlMapperBase mapper, Element parentElement){
|
90
|
String sourceAttribute = mapper.getSourceAttribute();
|
91
|
Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
|
92
|
Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
|
93
|
if (child == null){
|
94
|
return null;
|
95
|
}
|
96
|
if (child.getContentSize() > 1){
|
97
|
logger.warn("Element is not String");
|
98
|
}
|
99
|
Object value = child.getTextTrim();
|
100
|
return value;
|
101
|
}
|
102
|
|
103
|
protected boolean checkAdditionalContents(Element parentElement, CdmSingleAttributeXmlMapperBase[] classMappers, CdmSingleAttributeXmlMapperBase[] operationalMappers, CdmSingleAttributeXmlMapperBase[] unclearMappers){
|
104
|
List<Content> additionalContentList = new ArrayList<Content>();
|
105
|
List<Content> contentList = parentElement.getContent();
|
106
|
List<CdmSingleAttributeXmlMapperBase> mapperList = new ArrayList<CdmSingleAttributeXmlMapperBase>();
|
107
|
|
108
|
mapperList.addAll(Arrays.asList(classMappers));
|
109
|
mapperList.addAll(Arrays.asList(operationalMappers));
|
110
|
mapperList.addAll(Arrays.asList(unclearMappers));
|
111
|
|
112
|
for(Content content: contentList){
|
113
|
boolean contentExists = false;
|
114
|
if (content instanceof Element){
|
115
|
for (CdmSingleAttributeXmlMapperBase mapper : mapperList){
|
116
|
if (mapper.mapsSource(content, parentElement)){
|
117
|
contentExists = true;
|
118
|
break;
|
119
|
}
|
120
|
}
|
121
|
|
122
|
}else if (content instanceof Text){
|
123
|
//empty Text
|
124
|
if (((Text)content).getTextNormalize().equals("")){
|
125
|
contentExists = true;
|
126
|
}else{
|
127
|
//
|
128
|
}
|
129
|
}
|
130
|
|
131
|
if (contentExists == false){
|
132
|
additionalContentList.add(content);
|
133
|
}
|
134
|
}
|
135
|
for (Content additionalContent : additionalContentList){
|
136
|
logger.warn("Additional content: " + additionalContent);
|
137
|
}
|
138
|
return (additionalContentList.size() == 0);
|
139
|
}
|
140
|
|
141
|
protected Element getDataSetElement(TcsXmlImportConfigurator tcsConfig){
|
142
|
Element root = tcsConfig.getSourceRoot();
|
143
|
|
144
|
if (! "DataSet".equals(root.getName())){
|
145
|
logger.error("Root element is not 'DataSet'");
|
146
|
return null;
|
147
|
}
|
148
|
if (tcsConfig.getTcsXmlNamespace() == null){
|
149
|
logger.error("No namespace defined for tcs");
|
150
|
return null;
|
151
|
}
|
152
|
if (! tcsConfig.getTcsXmlNamespace().equals(root.getNamespace())){
|
153
|
logger.error("Wrong namespace for element 'DataSet'");
|
154
|
return null;
|
155
|
}
|
156
|
//TODO prevent multiple elements
|
157
|
|
158
|
return root;
|
159
|
}
|
160
|
|
161
|
// static public boolean checkFirstTwoFunctionElements(List<Object> objList){
|
162
|
// if (! (objList.get(0) instanceof TcsXmlImportConfigurator)){
|
163
|
// logger.error("first method object has wrong type. Must be " + TcsXmlImportConfigurator.class.getSimpleName() + " but is " + (objList.get(0) == null ? "null": objList.get(0).getClass().getSimpleName()));
|
164
|
// return false;
|
165
|
// }
|
166
|
// if (! (objList.get(1) == null) && ! (objList.get(1) instanceof Element)){
|
167
|
// 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()));
|
168
|
// return false;
|
169
|
// }
|
170
|
// return true;
|
171
|
// }
|
172
|
|
173
|
|
174
|
protected boolean testAdditionalElements(Element parentElement, List<String> excludeList){
|
175
|
boolean result = true;
|
176
|
List<Element> list = parentElement.getChildren();
|
177
|
for (Element element : list){
|
178
|
if (! excludeList.contains(element.getName())){
|
179
|
logger.warn("Unknown element (" + element.getName() + ") in parent element (" + parentElement.getName() + ")");
|
180
|
result = false;
|
181
|
}
|
182
|
}
|
183
|
return result;
|
184
|
}
|
185
|
|
186
|
|
187
|
protected <T extends IdentifiableEntity> T makeReferenceType(Element element, Class<? extends T> clazz, MapWrapper<? extends T> objectMap, ResultWrapper<Boolean> success){
|
188
|
T result = null;
|
189
|
String linkType = element.getAttributeValue("linkType");
|
190
|
String ref = element.getAttributeValue("ref");
|
191
|
if(ref == null && linkType == null){
|
192
|
result = getInstance(clazz);
|
193
|
if (result != null){
|
194
|
String title = element.getTextNormalize();
|
195
|
result.setTitleCache(title, true);
|
196
|
}
|
197
|
}else if (linkType == null || linkType.equals("local")){
|
198
|
//TODO
|
199
|
result = objectMap.get(ref);
|
200
|
if (result == null){
|
201
|
logger.warn("Object (ref = " + ref + ")could not be found in WrapperMap");
|
202
|
}
|
203
|
}else if(linkType.equals("external")){
|
204
|
logger.warn("External link types not yet implemented");
|
205
|
}else if(linkType.equals("other")){
|
206
|
logger.warn("Other link types not yet implemented");
|
207
|
}else{
|
208
|
logger.warn("Unknown link type or missing ref");
|
209
|
}
|
210
|
if (result == null){
|
211
|
success.setValue(false);
|
212
|
}
|
213
|
return result;
|
214
|
}
|
215
|
|
216
|
|
217
|
protected Reference makeAccordingTo(Element elAccordingTo, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
|
218
|
Reference result = null;
|
219
|
if (elAccordingTo != null){
|
220
|
String childName = "AccordingToDetailed";
|
221
|
boolean obligatory = false;
|
222
|
Element elAccordingToDetailed = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
|
223
|
|
224
|
childName = "Simple";
|
225
|
obligatory = true;
|
226
|
Element elSimple = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
|
227
|
|
228
|
if (elAccordingToDetailed != null){
|
229
|
result = makeAccordingToDetailed(elAccordingToDetailed, referenceMap, success);
|
230
|
}else{
|
231
|
result = refFactory.newGeneric();
|
232
|
String title = elSimple.getTextNormalize();
|
233
|
result.setTitleCache(title, true);
|
234
|
}
|
235
|
}
|
236
|
return result;
|
237
|
}
|
238
|
|
239
|
|
240
|
private Reference makeAccordingToDetailed(Element elAccordingToDetailed, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
|
241
|
Reference result = null;
|
242
|
Namespace tcsNamespace = elAccordingToDetailed.getNamespace();
|
243
|
if (elAccordingToDetailed != null){
|
244
|
//AuthorTeam
|
245
|
String childName = "AuthorTeam";
|
246
|
boolean obligatory = false;
|
247
|
Element elAuthorTeam = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
248
|
makeAccordingToAuthorTeam(elAuthorTeam, success);
|
249
|
|
250
|
//PublishedIn
|
251
|
childName = "PublishedIn";
|
252
|
obligatory = false;
|
253
|
Element elPublishedIn = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
254
|
result = makeReferenceType(elPublishedIn, Reference.class, referenceMap, success);
|
255
|
|
256
|
//MicroReference
|
257
|
childName = "MicroReference";
|
258
|
obligatory = false;
|
259
|
Element elMicroReference = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
260
|
if (elMicroReference != null){
|
261
|
String microReference = elMicroReference.getTextNormalize();
|
262
|
if (CdmUtils.Nz(microReference).equals("")){
|
263
|
//TODO
|
264
|
logger.warn("MicroReference not yet implemented for AccordingToDetailed");
|
265
|
}
|
266
|
}
|
267
|
}
|
268
|
return result;
|
269
|
}
|
270
|
|
271
|
private Team makeAccordingToAuthorTeam(Element elAuthorTeam, ResultWrapper<Boolean> succes){
|
272
|
Team result = null;
|
273
|
if (elAuthorTeam != null){
|
274
|
//TODO
|
275
|
logger.warn("AuthorTeam not yet implemented for AccordingToDetailed");
|
276
|
}
|
277
|
return result;
|
278
|
}
|
279
|
|
280
|
|
281
|
|
282
|
}
|