1
2
3
4
5
6
7
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.ReferenceBase;
42 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
43 import eu.etaxonomy.cdm.model.reference.ReferenceType;
44
45
46
47
48
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 ReferenceFactory refFactory = ReferenceFactory.newInstance();
60
61 protected abstract boolean doInvoke(EndnoteImportState state);
62
63
64
65
66
67
68
69
70
71
72
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
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
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 getXmlElement(EndnoteImportConfigurator tcsConfig){
147 Element root = tcsConfig.getSourceRoot();
148
149 if (! "xml".equals(root.getName())){
150 logger.error("Root element is not 'xml'");
151 return null;
152 }
153 if (tcsConfig.getEndnoteNamespace() == null){
154 logger.error("No namespace defined for tcs");
155 return null;
156 }
157 if (! tcsConfig.getEndnoteNamespace().equals(root.getNamespace())){
158 logger.error("Wrong namespace for element 'xml'");
159 return null;
160 }
161
162
163 return root;
164 }
165
166
167
168
169
170
171
172
173
174
175
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, ReferenceType refType, MapWrapper<? extends T> objectMap, ResultWrapper<Boolean> success){
193 T result = null;
194
195 String linkType = element.getAttributeValue("linkType");
196 String ref = element.getAttributeValue("ref");
197 if(ref == null && linkType == null){
198 result = (T) refFactory.newReference(refType);
199
200 if (result != null){
201 String title = element.getTextNormalize();
202 result.setTitleCache(title, true);
203 }
204 }else if (linkType == null || linkType.equals("local")){
205
206 result = objectMap.get(ref);
207 if (result == null){
208 logger.warn("Object (ref = " + ref + ")could not be found in WrapperMap");
209 }
210 }else if(linkType.equals("external")){
211 logger.warn("External link types not yet implemented");
212 }else if(linkType.equals("other")){
213 logger.warn("Other link types not yet implemented");
214 }else{
215 logger.warn("Unknown link type or missing ref");
216 }
217 if (result == null){
218 success.setValue(false);
219 }
220 return result;
221 }
222
223
224 protected ReferenceBase makeAccordingTo(Element elAccordingTo, MapWrapper<ReferenceBase> referenceMap, ResultWrapper<Boolean> success){
225 ReferenceBase result = null;
226 if (elAccordingTo != null){
227 String childName = "AccordingToDetailed";
228 boolean obligatory = false;
229 Element elAccordingToDetailed = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
230
231 childName = "Simple";
232 obligatory = true;
233 Element elSimple = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
234
235 if (elAccordingToDetailed != null){
236 result = makeAccordingToDetailed(elAccordingToDetailed, referenceMap, success);
237 }else{
238 result = refFactory.newGeneric();
239 String title = elSimple.getTextNormalize();
240 result.setTitleCache(title, true);
241 }
242 }
243 return result;
244 }
245
246
247 private ReferenceBase makeAccordingToDetailed(Element elAccordingToDetailed, MapWrapper<ReferenceBase> referenceMap, ResultWrapper<Boolean> success){
248 ReferenceBase result = null;
249 Namespace tcsNamespace = elAccordingToDetailed.getNamespace();
250 if (elAccordingToDetailed != null){
251
252 String childName = "AuthorTeam";
253 boolean obligatory = false;
254 Element elAuthorTeam = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
255 makeAccordingToAuthorTeam(elAuthorTeam, success);
256
257
258 childName = "PublishedIn";
259 obligatory = false;
260 Element elPublishedIn = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
261 result = makeReferenceType(elPublishedIn, ReferenceType.Generic, referenceMap, success);
262
263
264 childName = "MicroReference";
265 obligatory = false;
266 Element elMicroReference = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
267 String microReference = elMicroReference.getTextNormalize();
268 if (CdmUtils.Nz(microReference).equals("")){
269
270 logger.warn("MicroReference not yet implemented for AccordingToDetailed");
271 }
272 }
273 return result;
274 }
275
276 private Team makeAccordingToAuthorTeam(Element elAuthorTeam, ResultWrapper<Boolean> succes){
277 Team result = null;
278 if (elAuthorTeam != null){
279
280 logger.warn("AuthorTeam not yet implemented for AccordingToDetailed");
281 }
282 return result;
283 }
284
285
286
287 }