2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.tcsxml
;
12 import static eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
.OBLIGATORY
;
13 import static eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
.OVERWRITE
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
17 import java
.util
.HashSet
;
18 import java
.util
.List
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.jdom
.Content
;
24 import org
.jdom
.Element
;
25 import org
.jdom
.Namespace
;
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
;
47 public abstract class TcsXmlImportBase
extends CdmIoBase
<IImportConfigurator
> {
48 private static final Logger logger
= Logger
.getLogger(TcsXmlImportBase
.class);
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");
58 protected abstract boolean doInvoke(TcsXmlImportState state
);
62 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
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
);
73 protected boolean makeStandardMapper(Element parentElement
, CdmBase ref
, Set
<String
> omitAttributes
, CdmSingleAttributeXmlMapperBase
[] classMappers
){
74 if (omitAttributes
== null){
75 omitAttributes
= new HashSet
<String
>();
77 boolean result
= true;
78 for (CdmSingleAttributeXmlMapperBase mapper
: classMappers
){
79 Object value
= getValue(mapper
, parentElement
);
80 //write to destination
82 String destinationAttribute
= mapper
.getDestinationAttribute();
83 if (! omitAttributes
.contains(destinationAttribute
)){
84 result
&= ImportHelper
.addValue(value
, ref
, destinationAttribute
, mapper
.getTypeClass(), OVERWRITE
, OBLIGATORY
);
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
);
98 if (child
.getContentSize() > 1){
99 logger
.warn("Element is not String");
101 Object value
= child
.getTextTrim();
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
>();
110 mapperList
.addAll(Arrays
.asList(classMappers
));
111 mapperList
.addAll(Arrays
.asList(operationalMappers
));
112 mapperList
.addAll(Arrays
.asList(unclearMappers
));
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;
124 }else if (content
instanceof Text
){
126 if (((Text
)content
).getTextNormalize().equals("")){
127 contentExists
= true;
133 if (contentExists
== false){
134 additionalContentList
.add(content
);
137 for (Content additionalContent
: additionalContentList
){
138 logger
.warn("Additional content: " + additionalContent
);
140 return (additionalContentList
.size() == 0);
143 protected Element
getDataSetElement(TcsXmlImportConfigurator tcsConfig
){
144 Element root
= tcsConfig
.getSourceRoot();
146 if (! "DataSet".equals(root
.getName())){
147 logger
.error("Root element is not 'DataSet'");
150 if (tcsConfig
.getTcsXmlNamespace() == null){
151 logger
.error("No namespace defined for tcs");
154 if (! tcsConfig
.getTcsXmlNamespace().equals(root
.getNamespace())){
155 logger
.error("Wrong namespace for element 'DataSet'");
158 //TODO prevent multiple elements
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()));
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()));
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() + ")");
189 protected <T
extends IdentifiableEntity
> T
makeReferenceType(Element element
, Class
<?
extends T
> clazz
, MapWrapper
<?
extends T
> objectMap
, ResultWrapper
<Boolean
> success
){
191 String linkType
= element
.getAttributeValue("linkType");
192 String ref
= element
.getAttributeValue("ref");
193 if(ref
== null && linkType
== null){
194 result
= getInstance(clazz
);
196 String title
= element
.getTextNormalize();
197 result
.setTitleCache(title
);
199 }else if (linkType
== null || linkType
.equals("local")){
201 result
= objectMap
.get(ref
);
203 logger
.warn("Object (ref = " + ref
+ ")could not be found in WrapperMap");
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");
210 logger
.warn("Unknown link type or missing ref");
213 success
.setValue(false);
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
);
226 childName
= "Simple";
228 Element elSimple
= XmlHelp
.getSingleChildElement(success
, elAccordingTo
, childName
, elAccordingTo
.getNamespace(), obligatory
);
230 if (elAccordingToDetailed
!= null){
231 result
= makeAccordingToDetailed(elAccordingToDetailed
, referenceMap
, success
);
233 result
= Generic
.NewInstance();
234 String title
= elSimple
.getTextNormalize();
235 result
.setTitleCache(title
);
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){
247 String childName
= "AuthorTeam";
248 boolean obligatory
= false;
249 Element elAuthorTeam
= XmlHelp
.getSingleChildElement(success
, elAccordingToDetailed
, childName
, tcsNamespace
, obligatory
);
250 makeAccordingToAuthorTeam(elAuthorTeam
, success
);
253 childName
= "PublishedIn";
255 Element elPublishedIn
= XmlHelp
.getSingleChildElement(success
, elAccordingToDetailed
, childName
, tcsNamespace
, obligatory
);
256 result
= makeReferenceType(elPublishedIn
, Generic
.class, referenceMap
, success
);
259 childName
= "MicroReference";
261 Element elMicroReference
= XmlHelp
.getSingleChildElement(success
, elAccordingToDetailed
, childName
, tcsNamespace
, obligatory
);
262 String microReference
= elMicroReference
.getTextNormalize();
263 if (CdmUtils
.Nz(microReference
).equals("")){
265 logger
.warn("MicroReference not yet implemented for AccordingToDetailed");
271 private Team
makeAccordingToAuthorTeam(Element elAuthorTeam
, ResultWrapper
<Boolean
> succes
){
273 if (elAuthorTeam
!= null){
275 logger
.warn("AuthorTeam not yet implemented for AccordingToDetailed");