View Javadoc

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