Project

General

Profile

Download (9.83 KB) Statistics
| Branch: | Revision:
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.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.commons.lang3.StringUtils;
22
import org.apache.log4j.Logger;
23
import org.jdom.Content;
24
import org.jdom.Element;
25
import org.jdom.Namespace;
26
import org.jdom.Text;
27

    
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 long serialVersionUID = 3390179691538955000L;
47

    
48
    private static final Logger logger = Logger.getLogger(BfnXmlImportBase.class);
49

    
50
	protected static Namespace nsTcom = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/Common#");
51
	protected static Namespace nsTn = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonName#");
52
	protected static Namespace nsTgeo = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/GeographicRegion#");
53
	protected static Namespace nsTc = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonConcept#");
54
	protected static Namespace nsTpub = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/PublicationCitation#");
55
	protected static Namespace nsTpalm = Namespace.getNamespace("http://wp5.e-taxonomy.eu/import/palmae/common");
56

    
57

    
58
	@Override
59
    protected abstract void doInvoke(BfnXmlImportState state);
60

    
61

    
62
	protected boolean makeStandardMapper(Element parentElement, CdmBase ref, Set<String> omitAttributes,
63
			CdmSingleAttributeXmlMapperBase[] classMappers){
64
		if (omitAttributes == null){
65
			omitAttributes = new HashSet<>();
66
		}
67
		boolean result = true;
68
		for (CdmSingleAttributeXmlMapperBase mapper : classMappers){
69
			Object value = getValue(mapper, parentElement);
70
			//write to destination
71
			if (value != null){
72
				String destinationAttribute = mapper.getDestinationAttribute();
73
				if (! omitAttributes.contains(destinationAttribute)){
74
					result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
75
				}
76
			}
77
		}
78
		return result;
79
	}
80

    
81
	private Object getValue(CdmSingleAttributeXmlMapperBase mapper, Element parentElement){
82
		String sourceAttribute = mapper.getSourceAttribute();
83
		Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
84
		Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
85
		if (child == null){
86
			return null;
87
		}
88
		if (child.getContentSize() > 1){
89
			logger.warn("Element is not String");
90
		}
91
		Object value = child.getTextTrim();
92
		return value;
93
	}
94

    
95
	protected boolean checkAdditionalContents(Element parentElement, CdmSingleAttributeXmlMapperBase[] classMappers,
96
			CdmSingleAttributeXmlMapperBase[] operationalMappers, CdmSingleAttributeXmlMapperBase[] unclearMappers){
97
		List<Content> additionalContentList = new ArrayList<Content>();
98
		@SuppressWarnings("unchecked")
99
        List<Content> contentList = parentElement.getContent();
100
		List<CdmSingleAttributeXmlMapperBase> mapperList = new ArrayList<CdmSingleAttributeXmlMapperBase>();
101

    
102
		mapperList.addAll(Arrays.asList(classMappers));
103
		mapperList.addAll(Arrays.asList(operationalMappers));
104
		mapperList.addAll(Arrays.asList(unclearMappers));
105

    
106
		for(Content content: contentList){
107
			boolean contentExists = false;
108
			if (content instanceof Element){
109
				for (CdmSingleAttributeXmlMapperBase mapper : mapperList){
110
					if (mapper.mapsSource(content, parentElement)){
111
						contentExists = true;
112
						break;
113
					}
114
				}
115

    
116
			}else if (content instanceof Text){
117
				//empty Text
118
				if (((Text)content).getTextNormalize().equals("")){
119
					contentExists = true;
120
				}else{
121
					//
122
				}
123
			}
124

    
125
			if (contentExists == false){
126
				additionalContentList.add(content);
127
			}
128
		}
129
		for (Content additionalContent : additionalContentList){
130
			logger.warn("Additional content: " +  additionalContent);
131
		}
132
		return (additionalContentList.size() == 0);
133
	}
134

    
135
	protected Element getDataSetElement(BfnXmlImportConfigurator bfnConfig){
136
		Element root = bfnConfig.getSourceRoot();
137

    
138
		if (! "DEBExport".equals(root.getName())){//"DataSet"
139
			logger.error("Root element is not 'DEBExport'");
140
			return null;
141
		}
142
		if (bfnConfig.getBfnXmlNamespace() == null){
143
			logger.error("No namespace defined for bfnXML");
144
			return null;
145
		}
146
		if (! bfnConfig.getBfnXmlNamespace().equals(root.getNamespace())){
147
			logger.error("Wrong namespace for element 'DEBExport'");
148
			return null;
149
		}
150
		//TODO prevent multiple elements
151

    
152
		return root;
153
	}
154

    
155
//	static public boolean checkFirstTwoFunctionElements(List<Object> objList){
156
//		if (! (objList.get(0) instanceof BfnXmlImportConfigurator)){
157
//			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()));
158
//			return false;
159
//		}
160
//		if (! (objList.get(1) == null) && ! (objList.get(1) instanceof Element)){
161
//			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()));
162
//			return false;
163
//		}
164
//		return true;
165
//	}
166

    
167

    
168
	protected boolean testAdditionalElements(Element parentElement, List<String> excludeList){
169
		boolean result = true;
170
		@SuppressWarnings("unchecked")
171
        List<Element> list = parentElement.getChildren();
172
		for (Element element : list){
173
			if (! excludeList.contains(element.getName())){
174
				logger.warn("Unknown element (" + element.getName() + ") in parent element (" + parentElement.getName() + ")");
175
				result = false;
176
			}
177
		}
178
		return result;
179
	}
180

    
181

    
182
	protected <T extends IdentifiableEntity> T makeReferenceType(Element element, Class<? extends T> clazz, MapWrapper<? extends T> objectMap, ResultWrapper<Boolean> success){
183
		T result = null;
184
		String linkType = element.getAttributeValue("linkType");
185
		String ref = element.getAttributeValue("ref");
186
		if(ref == null && linkType == null){
187
			result = getInstance(clazz);
188
			if (result != null){
189
				String title = element.getTextNormalize();
190
				result.setTitleCache(title, true);
191
			}
192
		}else if (linkType == null || linkType.equals("local")){
193
			//TODO
194
			result = objectMap.get(ref);
195
			if (result == null){
196
				logger.warn("Object (ref = " + ref + ")could not be found in WrapperMap");
197
			}
198
		}else if(linkType.equals("external")){
199
			logger.warn("External link types not yet implemented");
200
		}else if(linkType.equals("other")){
201
			logger.warn("Other link types not yet implemented");
202
		}else{
203
			logger.warn("Unknown link type or missing ref");
204
		}
205
		if (result == null){
206
			success.setValue(false);
207
		}
208
		return result;
209
	}
210

    
211

    
212
	protected Reference makeAccordingTo(Element elAccordingTo, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
213
		Reference result = null;
214
		if (elAccordingTo != null){
215
			String childName = "AccordingToDetailed";
216
			boolean obligatory = false;
217
			Element elAccordingToDetailed = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
218

    
219
			childName = "Simple";
220
			obligatory = true;
221
			Element elSimple = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
222

    
223
			if (elAccordingToDetailed != null){
224
				result = makeAccordingToDetailed(elAccordingToDetailed, referenceMap, success);
225
			}else{
226
				result = ReferenceFactory.newGeneric();
227
				String title = elSimple.getTextNormalize();
228
				result.setTitleCache(title, true);
229
			}
230
		}
231
		return result;
232
	}
233

    
234

    
235
	private Reference makeAccordingToDetailed(Element elAccordingToDetailed, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
236
		Reference result = null;
237
		if (elAccordingToDetailed != null){
238
		    Namespace tcsNamespace = elAccordingToDetailed.getNamespace();
239
			//AuthorTeam
240
			String childName = "AuthorTeam";
241
			boolean obligatory = false;
242
			Element elAuthorTeam = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
243
			makeAccordingToAuthorTeam(elAuthorTeam, success);
244

    
245
			//PublishedIn
246
			childName = "PublishedIn";
247
			obligatory = false;
248
			Element elPublishedIn = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
249
			result = makeReferenceType(elPublishedIn, Reference.class, referenceMap, success);
250

    
251
			//MicroReference
252
			childName = "MicroReference";
253
			obligatory = false;
254
			Element elMicroReference = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
255
			if (elMicroReference != null){
256
				String microReference = elMicroReference.getTextNormalize();
257
				if (StringUtils.isNotBlank(microReference)){
258
					//TODO
259
					logger.warn("MicroReference not yet implemented for AccordingToDetailed");
260
				}
261
			}
262
		}
263
		return result;
264
	}
265

    
266
	private Team makeAccordingToAuthorTeam(Element elAuthorTeam, ResultWrapper<Boolean> success){
267
		Team result = null;
268
		if (elAuthorTeam != null){
269
			//TODO
270
			logger.warn("AuthorTeam not yet implemented for AccordingToDetailed");
271
		}
272
		return result;
273
	}
274
}
(2-2/9)