Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | 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.reference.endnote.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.Map;
20
import java.util.Set;
21

    
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.CdmUtils;
29
import eu.etaxonomy.cdm.common.ResultWrapper;
30
import eu.etaxonomy.cdm.common.XmlHelp;
31
import eu.etaxonomy.cdm.io.common.CdmImportBase;
32
import eu.etaxonomy.cdm.io.common.CdmIoBase;
33
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
34
import eu.etaxonomy.cdm.io.common.ImportHelper;
35
import eu.etaxonomy.cdm.io.common.MapWrapper;
36
import eu.etaxonomy.cdm.io.tcsrdf.CdmSingleAttributeXmlMapperBase;
37
import eu.etaxonomy.cdm.model.agent.Team;
38
import eu.etaxonomy.cdm.model.common.CdmBase;
39
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
40
import eu.etaxonomy.cdm.model.reference.IGeneric;
41
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
43
import eu.etaxonomy.cdm.model.reference.ReferenceType;
44

    
45
/**
46
 * @author a.mueller
47
 * @created 04.08.2008
48
 * @version 1.0
49
 */
50
public abstract class EndNoteImportBase  extends CdmImportBase<EndnoteImportConfigurator, EndnoteImportState> {
51
	private static final Logger logger = Logger.getLogger(EndNoteImportBase.class);
52

    
53
	protected static Namespace nsTcom = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/Common#");
54
	protected static Namespace nsTn = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonName#");
55
	protected static Namespace nsTgeo = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/GeographicRegion#");
56
	protected static Namespace nsTc = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonConcept#");
57
	protected static Namespace nsTpub = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/PublicationCitation#");
58
	protected static Namespace nsTpalm = Namespace.getNamespace("http://wp5.e-taxonomy.eu/import/palmae/common");
59
	
60
	protected abstract void doInvoke(EndnoteImportState state);
61

    
62
	
63
//	/* (non-Javadoc)
64
//	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
65
//	 */
66
//	@Override
67
//	protected boolean doInvoke(IImportConfigurator config, 
68
//			Map<String, MapWrapper<? extends CdmBase>> stores){ 
69
//		TcsXmlImportState state = ((TcsXmlImportConfigurator)config).getState();
70
//		state.setConfig((TcsXmlImportConfigurator)config);
71
//		return doInvoke(state);
72
//	}
73
	
74
	
75
	protected boolean makeStandardMapper(Element parentElement, CdmBase ref, Set<String> omitAttributes, CdmSingleAttributeXmlMapperBase[] classMappers){
76
		if (omitAttributes == null){
77
			omitAttributes = new HashSet<String>();
78
		}
79
		boolean result = true;	
80
		for (CdmSingleAttributeXmlMapperBase mapper : classMappers){
81
			Object value = getValue(mapper, parentElement);
82
			//write to destination
83
			if (value != null){
84
				String destinationAttribute = mapper.getDestinationAttribute();
85
				if (! omitAttributes.contains(destinationAttribute)){
86
					result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
87
				}
88
			}
89
		}
90
		return true;
91
	}
92
	
93
	private Object getValue(CdmSingleAttributeXmlMapperBase mapper, Element parentElement){
94
		String sourceAttribute = mapper.getSourceAttribute();
95
		Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
96
		Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
97
		if (child == null){
98
			return null;
99
		}
100
		if (child.getContentSize() > 1){
101
			logger.warn("Element is not String");
102
		}
103
		Object value = child.getTextTrim();
104
		return value;
105
	}
106
	
107
	protected boolean checkAdditionalContents(Element parentElement, CdmSingleAttributeXmlMapperBase[] classMappers, CdmSingleAttributeXmlMapperBase[] operationalMappers, CdmSingleAttributeXmlMapperBase[] unclearMappers){
108
		List<Content> additionalContentList = new ArrayList<Content>();
109
		List<Content> contentList = parentElement.getContent();
110
		List<CdmSingleAttributeXmlMapperBase> mapperList = new ArrayList<CdmSingleAttributeXmlMapperBase>();
111
		
112
		mapperList.addAll(Arrays.asList(classMappers));
113
		mapperList.addAll(Arrays.asList(operationalMappers));
114
		mapperList.addAll(Arrays.asList(unclearMappers));
115
		
116
		for(Content content: contentList){
117
			boolean contentExists = false;
118
			if (content instanceof Element){
119
				for (CdmSingleAttributeXmlMapperBase mapper : mapperList){
120
					if (mapper.mapsSource(content, parentElement)){
121
						contentExists = true;
122
						break;
123
					}
124
				}
125
				
126
			}else if (content instanceof Text){
127
				//empty Text
128
				if (((Text)content).getTextNormalize().equals("")){
129
					contentExists = true;
130
				}else{
131
					//
132
				}
133
			}
134
			
135
			if (contentExists == false){
136
				additionalContentList.add(content);
137
			}
138
		}
139
		for (Content additionalContent : additionalContentList){
140
			logger.warn("Additional content: " +  additionalContent);
141
		}
142
		return (additionalContentList.size() == 0);
143
	}
144
	
145
	protected Element getXmlElement(EndnoteImportConfigurator tcsConfig){
146
		Element root = tcsConfig.getSourceRoot();
147
		
148
		if (! "xml".equals(root.getName())){
149
			logger.error("Root element is not 'xml'");
150
			return null;
151
		}
152
		if (tcsConfig.getEndnoteNamespace() == null){
153
			logger.error("No namespace defined for tcs");
154
			return null;
155
		}
156
		if (! tcsConfig.getEndnoteNamespace().equals(root.getNamespace())){
157
			logger.error("Wrong namespace for element 'xml'");
158
			return null;
159
		}
160
		//TODO prevent multiple elements
161
		
162
		return root;
163
	}
164
	
165
//	static public boolean checkFirstTwoFunctionElements(List<Object> objList){
166
//		if (! (objList.get(0) instanceof TcsXmlImportConfigurator)){
167
//			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()));
168
//			return false;
169
//		}
170
//		if (! (objList.get(1) == null) && ! (objList.get(1) instanceof Element)){
171
//			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()));
172
//			return false;
173
//		}
174
//		return true;
175
//	}
176
	
177
	
178
	protected boolean testAdditionalElements(Element parentElement, List<String> excludeList){
179
		boolean result = true;
180
		List<Element> list = parentElement.getChildren();
181
		for (Element element : list){
182
			if (! excludeList.contains(element.getName())){
183
				logger.warn("Unknown element (" + element.getName() + ") in parent element (" + parentElement.getName() + ")");
184
				result = false;
185
			}
186
		}
187
		return result;
188
	}
189
	
190
	
191
	protected <T extends IdentifiableEntity> T makeReferenceType(Element element, ReferenceType refType, MapWrapper<? extends T> objectMap, ResultWrapper<Boolean> success){
192
		T result = null;
193
		
194
		String linkType = element.getAttributeValue("linkType");
195
		String ref = element.getAttributeValue("ref");
196
		if(ref == null && linkType == null){
197
			result = (T) ReferenceFactory.newReference(refType);
198
				
199
			if (result != null){
200
				String title = element.getTextNormalize();
201
				result.setTitleCache(title, true);
202
			}
203
		}else if (linkType == null || linkType.equals("local")){
204
			//TODO
205
			result = objectMap.get(ref);
206
			if (result == null){
207
				logger.warn("Object (ref = " + ref + ")could not be found in WrapperMap");
208
			}
209
		}else if(linkType.equals("external")){
210
			logger.warn("External link types not yet implemented");
211
		}else if(linkType.equals("other")){
212
			logger.warn("Other link types not yet implemented");
213
		}else{
214
			logger.warn("Unknown link type or missing ref");
215
		}
216
		if (result == null){
217
			success.setValue(false);
218
		}
219
		return result;
220
	}
221
	
222
	
223
	protected Reference makeAccordingTo(Element elAccordingTo, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
224
		Reference result = null;
225
		if (elAccordingTo != null){
226
			String childName = "AccordingToDetailed";
227
			boolean obligatory = false;
228
			Element elAccordingToDetailed = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
229

    
230
			childName = "Simple";
231
			obligatory = true;
232
			Element elSimple = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
233
			
234
			if (elAccordingToDetailed != null){
235
				result = makeAccordingToDetailed(elAccordingToDetailed, referenceMap, success);
236
			}else{
237
				result = ReferenceFactory.newGeneric();
238
				String title = elSimple.getTextNormalize();
239
				result.setTitleCache(title, true);
240
			}
241
		}
242
		return result;
243
	}
244
	
245
	
246
	private Reference makeAccordingToDetailed(Element elAccordingToDetailed, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
247
		Reference result = null;
248
		Namespace tcsNamespace = elAccordingToDetailed.getNamespace();
249
		if (elAccordingToDetailed != null){
250
			//AuthorTeam
251
			String childName = "AuthorTeam";
252
			boolean obligatory = false;
253
			Element elAuthorTeam = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
254
			makeAccordingToAuthorTeam(elAuthorTeam, success);
255
			
256
			//PublishedIn
257
			childName = "PublishedIn";
258
			obligatory = false;
259
			Element elPublishedIn = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
260
			result = makeReferenceType(elPublishedIn, ReferenceType.Generic, referenceMap, success);
261
			
262
			//MicroReference
263
			childName = "MicroReference";
264
			obligatory = false;
265
			Element elMicroReference = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
266
			String microReference = elMicroReference.getTextNormalize();
267
			if (CdmUtils.Nz(microReference).equals("")){
268
				//TODO
269
				logger.warn("MicroReference not yet implemented for AccordingToDetailed");	
270
			}
271
		}
272
		return result;
273
	}
274

    
275
	private Team makeAccordingToAuthorTeam(Element elAuthorTeam, ResultWrapper<Boolean> succes){
276
		Team result = null;
277
		if (elAuthorTeam != null){
278
			//TODO
279
			logger.warn("AuthorTeam not yet implemented for AccordingToDetailed");
280
		}
281
		return result;
282
	}
283

    
284

    
285

    
286
}
(1-1/5)