move berlinModelimport to .in folder
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / tcsxml / TcsXmlImportBase.java
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;
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.CdmIoBase;
32 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
33 import eu.etaxonomy.cdm.io.common.ImportHelper;
34 import eu.etaxonomy.cdm.io.common.MapWrapper;
35 import eu.etaxonomy.cdm.io.tcsrdf.CdmSingleAttributeXmlMapperBase;
36 import eu.etaxonomy.cdm.model.agent.Team;
37 import eu.etaxonomy.cdm.model.common.CdmBase;
38 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
39 import eu.etaxonomy.cdm.model.reference.Generic;
40 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
41
42 /**
43 * @author a.mueller
44 * @created 04.08.2008
45 * @version 1.0
46 */
47 public abstract class TcsXmlImportBase extends CdmIoBase<IImportConfigurator> {
48 private static final Logger logger = Logger.getLogger(TcsXmlImportBase.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 protected abstract boolean doInvoke(TcsXmlImportState state);
59
60
61 /* (non-Javadoc)
62 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
63 */
64 @Override
65 protected boolean doInvoke(IImportConfigurator config,
66 Map<String, MapWrapper<? extends CdmBase>> stores){
67 TcsXmlImportState state = ((TcsXmlImportConfigurator)config).getState();
68 state.setConfig((TcsXmlImportConfigurator)config);
69 return doInvoke(state);
70 }
71
72
73 protected boolean makeStandardMapper(Element parentElement, CdmBase ref, Set<String> omitAttributes, CdmSingleAttributeXmlMapperBase[] classMappers){
74 if (omitAttributes == null){
75 omitAttributes = new HashSet<String>();
76 }
77 boolean result = true;
78 for (CdmSingleAttributeXmlMapperBase mapper : classMappers){
79 Object value = getValue(mapper, parentElement);
80 //write to destination
81 if (value != null){
82 String destinationAttribute = mapper.getDestinationAttribute();
83 if (! omitAttributes.contains(destinationAttribute)){
84 result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
85 }
86 }
87 }
88 return true;
89 }
90
91 private Object getValue(CdmSingleAttributeXmlMapperBase mapper, Element parentElement){
92 String sourceAttribute = mapper.getSourceAttribute();
93 Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
94 Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
95 if (child == null){
96 return null;
97 }
98 if (child.getContentSize() > 1){
99 logger.warn("Element is not String");
100 }
101 Object value = child.getTextTrim();
102 return value;
103 }
104
105 protected boolean checkAdditionalContents(Element parentElement, CdmSingleAttributeXmlMapperBase[] classMappers, 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(TcsXmlImportConfigurator tcsConfig){
144 Element root = tcsConfig.getSourceRoot();
145
146 if (! "DataSet".equals(root.getName())){
147 logger.error("Root element is not 'DataSet'");
148 return null;
149 }
150 if (tcsConfig.getTcsXmlNamespace() == null){
151 logger.error("No namespace defined for tcs");
152 return null;
153 }
154 if (! tcsConfig.getTcsXmlNamespace().equals(root.getNamespace())){
155 logger.error("Wrong namespace for element 'DataSet'");
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 TcsXmlImportConfigurator)){
165 // 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()));
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);
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 ReferenceBase makeAccordingTo(Element elAccordingTo, MapWrapper<ReferenceBase> referenceMap, ResultWrapper<Boolean> success){
220 ReferenceBase 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 = Generic.NewInstance();
234 String title = elSimple.getTextNormalize();
235 result.setTitleCache(title);
236 }
237 }
238 return result;
239 }
240
241
242 private ReferenceBase makeAccordingToDetailed(Element elAccordingToDetailed, MapWrapper<ReferenceBase> referenceMap, ResultWrapper<Boolean> success){
243 ReferenceBase 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, Generic.class, referenceMap, success);
257
258 //MicroReference
259 childName = "MicroReference";
260 obligatory = false;
261 Element elMicroReference = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
262 String microReference = elMicroReference.getTextNormalize();
263 if (CdmUtils.Nz(microReference).equals("")){
264 //TODO
265 logger.warn("MicroReference not yet implemented for AccordingToDetailed");
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 }