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