d5a5061568acd015a44585e98fac367e4c752e5d
[cdmlib-apps.git] / cdmlib-eflora / src / main / java / eu / etaxonomy / cdm / io / eflora / EfloraImportBase.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.eflora;
11
12 import java.util.List;
13
14 import org.apache.log4j.Logger;
15 import org.jdom.Element;
16 import org.jdom.Namespace;
17
18 import eu.etaxonomy.cdm.common.CdmUtils;
19 import eu.etaxonomy.cdm.common.ResultWrapper;
20 import eu.etaxonomy.cdm.common.XmlHelp;
21 import eu.etaxonomy.cdm.io.common.CdmImportBase;
22 import eu.etaxonomy.cdm.io.common.MapWrapper;
23 import eu.etaxonomy.cdm.model.agent.Team;
24 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
25 import eu.etaxonomy.cdm.model.reference.Reference;
26 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
27
28 /**
29 * @author a.mueller
30 * @created 04.08.2008
31 * @version 1.0
32 */
33 public abstract class EfloraImportBase extends CdmImportBase<EfloraImportConfigurator, EfloraImportState> {
34 private static final Logger logger = Logger.getLogger(EfloraImportBase.class);
35
36
37 protected abstract void doInvoke(EfloraImportState state);
38
39
40
41 protected Element getBodyElement(EfloraImportConfigurator config){
42 Element root = config.getSourceRoot();
43
44 if (! "body".equalsIgnoreCase(root.getName())){
45 logger.error("Root element is not 'body'");
46 return null;
47 }
48 //TODO prevent multiple elements
49
50 return root;
51 }
52
53
54 protected boolean testAdditionalElements(Element parentElement, List<String> excludeList){
55 boolean result = true;
56 List<Element> list = parentElement.getChildren();
57 for (Element element : list){
58 if (! excludeList.contains(element.getName())){
59 logger.warn("Unknown element (" + element.getName() + ") in parent element (" + parentElement.getName() + ")");
60 result = false;
61 }
62 }
63 return result;
64 }
65
66
67 protected <T extends IdentifiableEntity> T makeReferenceType(Element element, Class<? extends T> clazz, MapWrapper<? extends T> objectMap, ResultWrapper<Boolean> success){
68 T result = null;
69 String linkType = element.getAttributeValue("linkType");
70 String ref = element.getAttributeValue("ref");
71 if(ref == null && linkType == null){
72 result = getInstance(clazz);
73 if (result != null){
74 String title = element.getTextNormalize();
75 result.setTitleCache(title, true);
76 }
77 }else if (linkType == null || linkType.equals("local")){
78 //TODO
79 result = objectMap.get(ref);
80 if (result == null){
81 logger.warn("Object (ref = " + ref + ")could not be found in WrapperMap");
82 }
83 }else if(linkType.equals("external")){
84 logger.warn("External link types not yet implemented");
85 }else if(linkType.equals("other")){
86 logger.warn("Other link types not yet implemented");
87 }else{
88 logger.warn("Unknown link type or missing ref");
89 }
90 if (result == null){
91 success.setValue(false);
92 }
93 return result;
94 }
95
96
97 protected Reference makeAccordingTo(Element elAccordingTo, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
98 Reference result = null;
99 if (elAccordingTo != null){
100 String childName = "AccordingToDetailed";
101 boolean obligatory = false;
102 Element elAccordingToDetailed = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
103
104 childName = "Simple";
105 obligatory = true;
106 Element elSimple = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
107
108 if (elAccordingToDetailed != null){
109 result = makeAccordingToDetailed(elAccordingToDetailed, referenceMap, success);
110 }else{
111 result = ReferenceFactory.newGeneric();
112 String title = elSimple.getTextNormalize();
113 result.setTitleCache(title, true);
114 }
115 }
116 return result;
117 }
118
119
120 private Reference makeAccordingToDetailed(Element elAccordingToDetailed, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
121 Reference result = null;
122 Namespace tcsNamespace = elAccordingToDetailed.getNamespace();
123 if (elAccordingToDetailed != null){
124 //AuthorTeam
125 String childName = "AuthorTeam";
126 boolean obligatory = false;
127 Element elAuthorTeam = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
128 makeAccordingToAuthorTeam(elAuthorTeam, success);
129
130 //PublishedIn
131 childName = "PublishedIn";
132 obligatory = false;
133 Element elPublishedIn = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
134 result = makeReferenceType(elPublishedIn, Reference.class, referenceMap, success);
135
136 //MicroReference
137 childName = "MicroReference";
138 obligatory = false;
139 Element elMicroReference = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
140 String microReference = elMicroReference.getTextNormalize();
141 if (CdmUtils.Nz(microReference).equals("")){
142 //TODO
143 logger.warn("MicroReference not yet implemented for AccordingToDetailed");
144 }
145 }
146 return result;
147 }
148
149 private Team makeAccordingToAuthorTeam(Element elAuthorTeam, ResultWrapper<Boolean> succes){
150 Team result = null;
151 if (elAuthorTeam != null){
152 //TODO
153 logger.warn("AuthorTeam not yet implemented for AccordingToDetailed");
154 }
155 return result;
156 }
157
158
159
160 }