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
.taxonx2013
;
12 import java
.awt
.Dimension
;
14 import java
.io
.FileWriter
;
15 import java
.io
.IOException
;
16 import java
.io
.InputStream
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
24 import javax
.swing
.JOptionPane
;
25 import javax
.swing
.JScrollPane
;
26 import javax
.swing
.JTextArea
;
27 import javax
.xml
.parsers
.DocumentBuilder
;
28 import javax
.xml
.parsers
.DocumentBuilderFactory
;
30 import org
.apache
.commons
.lang
.StringUtils
;
31 import org
.apache
.log4j
.Logger
;
32 import org
.springframework
.stereotype
.Component
;
33 import org
.springframework
.transaction
.TransactionStatus
;
34 import org
.w3c
.dom
.Document
;
36 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
37 import eu
.etaxonomy
.cdm
.io
.specimen
.SpecimenImportBase
;
38 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
39 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
40 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
41 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
42 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
43 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
44 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
45 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
48 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
51 * @author p.kelbert 2013
54 public class TaxonXImport
extends SpecimenImportBase
<TaxonXImportConfigurator
, TaxonXImportState
> implements ICdmIO
<TaxonXImportState
> {
55 private static final Logger logger
= Logger
.getLogger(TaxonXImport
.class);
56 private static String prefix
= "";
58 public static String LOG_FOLDER
= "C:\\Users\\pesiimport\\.cdmLibrary\\log\\taxonX\\";
60 private Classification classification
= null;
61 private Reference ref
= null;
63 private TaxonXImportState taxonXstate
;
64 private TaxonXDataHolder dataHolder
;
65 private DerivedUnit derivedUnitBase
;
67 private TransactionStatus tx
;
69 private TaxonXXMLFieldGetter taxonXFieldGetter
;
71 public TaxonXImport() {
75 private Map
<String
,Feature
> featuresMap
= new HashMap
<String
, Feature
>();
78 protected boolean doCheck(TaxonXImportState state
) {
79 logger
.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
80 this.taxonXstate
= state
;
86 * asks for user interaction for decision
87 * @param classificationName : the name of the classification we are looking for
88 * @set the global classification object
90 private void setClassification() {
91 logger
.info("SET CLASSIFICATION "+classification
);
92 String classificationName
= taxonXstate
.getConfig().getClassificationName();
93 String defaultClassificatioName
= taxonXstate
.getConfig().getImportClassificationName();
95 List
<Classification
> classifList
= getClassificationService().list(Classification
.class, null, null, null, null);
96 Map
<String
,Classification
> classificationMap
= new HashMap
<String
,Classification
>();
97 ArrayList
<String
> nodeList
= new ArrayList
<String
>();
100 for (Classification cla
:classifList
){
102 // citation = cla.getCitation().toString();
106 title
=StringUtils
.isBlank(cla
.getTitleCache()) ?
"no name" : cla
.getTitleCache();
107 if (citation
.length()>title
.length()){
108 nodeList
.add(citation
);
109 classificationMap
.put(citation
, cla
);
113 classificationMap
.put(title
, cla
);
116 boolean defaultClassificationExists
= nodeList
.indexOf(defaultClassificatioName
)>-1;
117 if (! defaultClassificationExists
) {
118 nodeList
.add(defaultClassificatioName
);
120 if (nodeList
.indexOf(classificationName
)<0) {
121 nodeList
.add(classificationName
);
123 final String other
= "Other classification - add a new one";
127 if (defaultClassificatioName
!= null && defaultClassificationExists
&& taxonXstate
.getConfig().isAlwaysUseDefaultClassification()) {
128 s
=defaultClassificatioName
;
130 JTextArea textArea
= new JTextArea("Which classification do you want to use ? \nThe current reference is " + classificationName
);
131 JScrollPane scrollPane
= new JScrollPane(textArea
);
132 textArea
.setLineWrap(true);
133 textArea
.setWrapStyleWord(true);
134 scrollPane
.setPreferredSize( new Dimension( 600, 100 ) );
136 s
= (String
)JOptionPane
.showInputDialog(
140 JOptionPane
.PLAIN_MESSAGE
,
143 defaultClassificatioName
);
145 ref
=getReferenceService().find(ref
.getUuid());
146 if (!classificationMap
.containsKey(s
)){
147 //System.out.println("Classif inconnue ?? "+s+", "+classifDic);
148 Reference classificationReference
= null;
150 if (s
.equalsIgnoreCase(other
)){
151 classificationName
= askForValue("classification name ?", classificationName
);
152 classificationReference
= ReferenceFactory
.newDatabase();
153 classificationReference
.setTitle(classificationName
);
154 }else if (s
.equalsIgnoreCase(defaultClassificatioName
)){
155 classificationName
= defaultClassificatioName
;
156 classificationReference
= ReferenceFactory
.newDatabase();
157 classificationReference
.setTitle(classificationName
);
160 classification
= Classification
.NewInstance(classificationName
, classificationReference
, Language
.DEFAULT());
161 getClassificationService().saveOrUpdate(classification
);
162 refreshTransaction();
165 classification
= classificationMap
.get(s
);
167 if (classification
== null) {
168 String name
= taxonXstate
.getConfig().getClassificationName();
169 classification
= Classification
.NewInstance(name
, ref
, Language
.DEFAULT());
170 if (taxonXstate
.getConfig().getClassificationUuid() != null) {
171 classification
.setUuid(taxonXstate
.getConfig().getClassificationUuid());
173 getClassificationService().saveOrUpdate(classification
);
174 refreshTransaction();
179 * asks users for decision
180 * @param string : the parameter name we are looking at
181 * @param defaultValue : the default value
184 private String
askForValue(String string
, String defaultValue
) {
185 JTextArea textArea
= new JTextArea(string
);
186 JScrollPane scrollPane
= new JScrollPane(textArea
);
187 textArea
.setLineWrap(true);
188 textArea
.setWrapStyleWord(true);
189 scrollPane
.setPreferredSize( new Dimension( 600, 100 ) );
191 String s
= (String
)JOptionPane
.showInputDialog(
194 "What should be the "+string
,
195 JOptionPane
.PLAIN_MESSAGE
,
205 public void doInvoke(TaxonXImportState state
) {
207 tx
= startTransaction();
209 logger
.info("INVOKE TaxonXImport ");
210 URI sourceName
= this.taxonXstate
.getConfig().getSource();
212 // this.taxonXstate.getConfig().getClassificationName();
213 // this.taxonXstate.getConfig().getClassificationUuid();
215 ref
= taxonXstate
.getConfig().getSourceReference();
219 Reference secundum
= taxonXstate
.getConfig().getSecundum();
220 List
<Reference
> references
= this.getReferenceService().list(Reference
.class, null, null, null, null);
221 boolean refFound
= false;
222 for (Reference re
:references
){
223 if (re
.getCitation().equalsIgnoreCase(secundum
.getCitation())){
229 secundum
= CdmBase
.deproxy(secundum
, Reference
.class);
230 taxonXstate
.getConfig().setSecundum(secundum
);
232 this.getReferenceService().saveOrUpdate(secundum
);
233 taxonXstate
.getConfig().setSecundum(secundum
);
237 Reference urlRef
= taxonXstate
.getConfig().getOriginalSourceURL();
238 for (Reference re
:references
){
239 if (re
.getCitation().equalsIgnoreCase(urlRef
.getCitation())){
243 this.getReferenceService().saveOrUpdate(urlRef
);
244 urlRef
=getReferenceService().find(urlRef
.getUuid());
246 if(!taxonXstate
.getConfig().hasAskedForHigherRank()){
247 Rank maxRank
= askForHigherRank(taxonXstate
.getConfig().getNomenclaturalCode());
248 taxonXstate
.getConfig().setMaxRank(maxRank
);
249 taxonXstate
.getConfig().setHasAskedForHigherRank(true);
253 String message
= "go taxonx!";
254 logger
.info(message
);
255 updateProgress(this.taxonXstate
, message
);
256 dataHolder
= new TaxonXDataHolder();
258 DocumentBuilderFactory factory
= DocumentBuilderFactory
.newInstance();
259 DocumentBuilder builder
;
263 builder
= factory
.newDocumentBuilder();
264 url
= sourceName
.toURL();
265 Object o
= url
.getContent();
266 InputStream is
= (InputStream
) o
;
267 Document document
= builder
.parse(is
);
269 taxonXFieldGetter
= new TaxonXXMLFieldGetter(dataHolder
, prefix
, document
, this, taxonXstate
, classification
, featuresMap
);
270 /*parse the Mods from the TaxonX file
271 *create the appropriate Reference object
273 ref
= taxonXFieldGetter
.parseMods();
274 // logger.info("CLASSNAME :" +taxonXstate.getConfig().getClassificationName());
276 taxonXFieldGetter
.updateClassification(classification
);
277 // logger.info("classif :"+classification);
278 taxonXFieldGetter
.parseTreatment(ref
,sourceName
);
279 featuresMap
= taxonXFieldGetter
.getFeaturesUsed();
285 File file
= new File(LOG_FOLDER
+ "urlTaxonX.txt");
288 writer
= new FileWriter(file
,true);
289 writer
.write(sourceName
+"\n");
292 } catch (IOException e1
) {
293 logger
.error(e1
.getMessage());
296 logger
.warn(sourceName
);
299 //TODO:check how deduplicate work (and if it works..)
300 // if(taxonXstate.getConfig().getLastImport()) {
303 commitTransaction(tx
);
306 public void deduplicate(){
307 //System.out.println("DEDUPLICATE REFERENCE");
308 getReferenceService().deduplicate(Reference
.class, null,null);
309 //System.out.println("DEDUPLICATE TAXONBASE");
310 getTaxonService().deduplicate(TaxonBase
.class, null, null);
311 //System.out.println("DEDUP END");
315 private void refreshTransaction(){
316 commitTransaction(tx
);
317 tx
= startTransaction();
318 ref
= getReferenceService().find(ref
.getUuid());
319 classification
= getClassificationService().find(classification
.getUuid());
321 derivedUnitBase
= (DerivedUnit
) getOccurrenceService().find(derivedUnitBase
.getUuid());
323 //logger.warn("derivedunit up to date or not created yet");
330 private Rank
askForHigherRank(NomenclaturalCode nomenclaturalCode
) {
331 JTextArea textArea
= new JTextArea("Everything below that rank should be imported:");
332 JScrollPane scrollPane
= new JScrollPane(textArea
);
333 textArea
.setLineWrap(true);
334 textArea
.setWrapStyleWord(true);
335 scrollPane
.setPreferredSize( new Dimension( 600, 50 ) );
337 List
<Rank
> rankList
= new ArrayList
<Rank
>();
338 rankList
= getTermService().list(Rank
.class, null, null, null, null);
340 List
<String
> rankListStr
= new ArrayList
<String
>();
341 for (Rank r
:rankList
) {
342 rankListStr
.add(r
.toString());
344 String s
= (String
)JOptionPane
.showInputDialog(
348 JOptionPane
.PLAIN_MESSAGE
,
350 rankListStr
.toArray(),
351 "Suprageneric Taxon");
355 cR
= Rank
.getRankByEnglishName(s
,nomenclaturalCode
,true);
356 } catch (UnknownCdmTypeException e
) {
357 logger
.warn("Unknown rank ?!"+s
);
365 protected boolean isIgnore(TaxonXImportState state
) {