b8bf8d8955964ae770508a1d8e1c4151e8ca8ab5
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / tcsrdf / TcsRdfImportBase.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.tcsrdf;
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.io.common.CdmImportBase;
29 import eu.etaxonomy.cdm.io.common.CdmIoBase;
30 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
31 import eu.etaxonomy.cdm.io.common.IXmlMapper;
32 import eu.etaxonomy.cdm.io.common.ImportHelper;
33 import eu.etaxonomy.cdm.io.common.ImportStateBase;
34 import eu.etaxonomy.cdm.io.common.MapWrapper;
35 import eu.etaxonomy.cdm.model.common.CdmBase;
36
37 /**
38 * @author a.mueller
39 * @created 04.08.2008
40 * @version 1.0
41 */
42 public abstract class TcsRdfImportBase extends CdmImportBase<TcsRdfImportConfigurator, TcsRdfImportState> {
43 private static final Logger logger = Logger.getLogger(TcsRdfImportBase.class);
44
45 protected static Namespace nsTcom = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/Common#");
46 protected static Namespace nsTn = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonName#");
47 protected static Namespace nsTgeo = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/GeographicRegion#");
48 protected static Namespace nsTc = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonConcept#");
49 protected static Namespace nsTpub = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/PublicationCitation#");
50 protected static Namespace nsTpalm = Namespace.getNamespace("http://wp5.e-taxonomy.eu/import/palmae/common");
51
52
53 protected abstract boolean doInvoke(TcsRdfImportState state);
54
55 // /* (non-Javadoc)
56 // * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
57 // */
58 // @Override
59 // protected boolean doInvoke(IImportConfigurator config,
60 // Map<String, MapWrapper<? extends CdmBase>> stores){
61 // TcsRdfImportState state = ((TcsRdfImportConfigurator)config).getState();
62 // state.setConfig((TcsRdfImportConfigurator)config);
63 // return doInvoke(state);
64 // }
65
66 protected boolean makeStandardMapper(Element parentElement, CdmBase ref, Set<String> omitAttributes, CdmSingleAttributeXmlMapperBase[] classMappers){
67 if (omitAttributes == null){
68 omitAttributes = new HashSet<String>();
69 }
70 boolean result = true;
71 for (CdmSingleAttributeXmlMapperBase mapper : classMappers){
72 Object value = getValue(mapper, parentElement);
73 //write to destination
74 if (value != null){
75 String destinationAttribute = mapper.getDestinationAttribute();
76 if (! omitAttributes.contains(destinationAttribute)){
77 result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
78 }
79 }
80 }
81 return true;
82 }
83
84 private Object getValue(CdmSingleAttributeXmlMapperBase mapper, Element parentElement){
85 String sourceAttribute = mapper.getSourceAttribute();
86 Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
87 Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
88 if (child == null){
89 return null;
90 }
91 if (child.getContentSize() > 1){
92 logger.warn("Element is not String");
93 }
94 Object value = child.getTextTrim();
95 return value;
96 }
97
98 protected boolean checkAdditionalContents(Element parentElement, IXmlMapper[] classMappers, CdmSingleAttributeXmlMapperBase[] operationalMappers, CdmSingleAttributeXmlMapperBase[] unclearMappers){
99 List<Content> additionalContentList = new ArrayList<Content>();
100 List<Content> contentList = parentElement.getContent();
101 List<IXmlMapper> mapperList = new ArrayList<IXmlMapper>();
102
103 mapperList.addAll(Arrays.asList(classMappers));
104 mapperList.addAll(Arrays.asList(operationalMappers));
105 mapperList.addAll(Arrays.asList(unclearMappers));
106
107 for(Content content: contentList){
108 boolean contentExists = false;
109 if (content instanceof Element){
110 Element elementContent = (Element)content;
111 for (IXmlMapper mapper : mapperList){
112 if (mapper.mapsSource(content, parentElement)){
113 contentExists = true;
114 break;
115 }
116 }
117
118 }else if (content instanceof Text){
119 //empty Text
120 if (((Text)content).getTextNormalize().equals("")){
121 contentExists = true;
122 }else{
123 //
124 }
125 }
126
127 if (contentExists == false){
128 additionalContentList.add(content);
129 }
130 }
131 for (Content additionalContent : additionalContentList){
132 logger.warn("Additional content: " + additionalContent);
133 }
134 return (additionalContentList.size() == 0);
135 }
136
137 }