major refactoring in io-layer (config -> state)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / tcsrdf / TcsRdfImportConfigurator.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 java.io.InputStream;
13 import java.net.MalformedURLException;
14 import java.net.URL;
15
16 import org.apache.log4j.Logger;
17 import org.jdom.Element;
18 import org.jdom.Namespace;
19
20 import eu.etaxonomy.cdm.common.XmlHelp;
21 import eu.etaxonomy.cdm.database.ICdmDataSource;
22 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
23 import eu.etaxonomy.cdm.io.common.ImportConfiguratorBase;
24 import eu.etaxonomy.cdm.io.common.ImportStateBase;
25 import eu.etaxonomy.cdm.model.reference.Database;
26 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
27
28 /**
29 * @author a.mueller
30 * @created 29.05.2008
31 * @version 1.0
32 */
33 public class TcsRdfImportConfigurator extends ImportConfiguratorBase<TcsRdfImportState> implements IImportConfigurator {
34 private static final Logger logger = Logger.getLogger(TcsRdfImportConfigurator.class);
35
36 //rdfNamespace
37 Namespace rdfNamespace;
38 //TaxonConcept namespace
39 Namespace tcNamespace;
40 //TaxonName namespace
41 Namespace tnNamespace;
42 //TDWG common namespace
43 Namespace commonNamespace;
44 //TDWG geoNamespace
45 Namespace geoNamespace;
46 //publicationNamespace
47 Namespace publicationNamespace;
48
49 //TODO
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");
56
57 protected void makeIoClassList(){
58 ioClassList = new Class[]{
59 TcsRdfReferenceImport.class
60 , TcsRdfTaxonNameImport.class
61 , TcsRdfTaxonNameRelationsImport.class
62 , TcsRdfTaxonImport.class
63 , TcsRdfTaxonRelationsImport.class
64 };
65 };
66
67 public static TcsRdfImportConfigurator NewInstance(String url,
68 ICdmDataSource destination){
69 return new TcsRdfImportConfigurator(url, destination);
70 }
71
72 //TODO for spring use only
73 private TcsRdfImportConfigurator(){
74
75 }
76
77
78 /**
79 * @param berlinModelSource
80 * @param sourceReference
81 * @param destination
82 */
83 private TcsRdfImportConfigurator(String url, ICdmDataSource destination) {
84 super();
85 setSource(url);
86 setDestination(destination);
87 }
88
89
90 /* (non-Javadoc)
91 * @see eu.etaxonomy.cdm.io.common.ImportConfiguratorBase#getSource()
92 */
93 public String getSource() {
94 return (String)super.getSource();
95 }
96
97 /**
98 * @param file
99 */
100 public void setSource(String file) {
101 super.setSource(file);
102 }
103
104 /**
105 * @return
106 */
107 public Element getSourceRoot(){
108 String source = getSource();
109 try {
110 URL url;
111 url = new URL(source);
112 Object o = url.getContent();
113 InputStream is = (InputStream)o;
114 Element root = XmlHelp.getRoot(is);
115 makeNamespaces(root);
116 return root;
117 } catch (MalformedURLException e) {
118 e.printStackTrace();
119 }catch (Exception e) {
120 // TODO Auto-generated catch block
121 e.printStackTrace();
122 }
123 return null;
124 }
125
126 private boolean makeNamespaces(Element root){
127 //String strTnNamespace = "http://rs.tdwg.org/ontology/voc/TaxonName#";
128 //Namespace taxonNameNamespace = Namespace.getNamespace("tn", strTnNamespace);
129
130 String prefix;
131 rdfNamespace = root.getNamespace();
132 prefix = "tc";
133 tcNamespace = root.getNamespace(prefix);
134 prefix = "tn";
135 tnNamespace = root.getNamespace(prefix);
136 prefix = "tcom";
137 commonNamespace = root.getNamespace(prefix);
138 prefix = "tgeo";
139 geoNamespace = root.getNamespace(prefix);
140 prefix = "tpub";
141 publicationNamespace = root.getNamespace(prefix);
142 if (rdfNamespace == null || tcNamespace == null || tnNamespace == null ||
143 commonNamespace == null || geoNamespace == null || publicationNamespace == null){
144 logger.warn("At least one Namespace is NULL");
145 }
146 return true;
147 }
148
149
150 /* (non-Javadoc)
151 * @see eu.etaxonomy.cdm.io.common.ImportConfiguratorBase#getSourceReference()
152 */
153 @Override
154 public ReferenceBase getSourceReference() {
155 //TODO
156 if (this.sourceReference == null){
157 logger.warn("getSource Reference not yet fully implemented");
158 sourceReference = Database.NewInstance();
159 sourceReference.setTitleCache("XXX");
160 }
161 return sourceReference;
162 }
163
164
165 /* (non-Javadoc)
166 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getSourceNameString()
167 */
168 public String getSourceNameString() {
169 if (this.getSource() == null){
170 return null;
171 }else{
172 return this.getSource();
173 }
174 }
175
176 public Namespace getRdfNamespace() {
177 return rdfNamespace;
178 }
179
180 public void setRdfNamespace(Namespace rdfNamespace) {
181 this.rdfNamespace = rdfNamespace;
182 }
183
184 public Namespace getTcNamespace() {
185 return tcNamespace;
186 }
187
188 public void setTcNamespace(Namespace tcNamespace) {
189 this.tcNamespace = tcNamespace;
190 }
191
192 public Namespace getTnNamespace() {
193 return tnNamespace;
194 }
195
196 public void setTnNamespace(Namespace tnNamespace) {
197 this.tnNamespace = tnNamespace;
198 }
199
200 public Namespace getCommonNamespace() {
201 return commonNamespace;
202 }
203
204 public void setCommonNamespace(Namespace commonNamespace) {
205 this.commonNamespace = commonNamespace;
206 }
207
208 public Namespace getGeoNamespace() {
209 return geoNamespace;
210 }
211
212 public void setGeoNamespace(Namespace geoNamespace) {
213 this.geoNamespace = geoNamespace;
214 }
215
216 public Namespace getPublicationNamespace() {
217 return publicationNamespace;
218 }
219
220 public void setPublicationNamespace(Namespace publicationNamespace) {
221 this.publicationNamespace = publicationNamespace;
222 }
223
224 /* (non-Javadoc)
225 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getNewState()
226 */
227 public TcsRdfImportState getNewState() {
228 return new TcsRdfImportState(this);
229 }
230
231
232
233
234 }