Remove generics from Reference in cdmlib (except for cdmlib-model) #5830
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / taxonx2013 / TaxonXImport.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.taxonx2013;
11
12 import java.awt.Dimension;
13 import java.io.File;
14 import java.io.FileWriter;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.URI;
18 import java.net.URL;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
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;
29
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;
35
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;
49
50 /**
51 * @author p.kelbert 2013
52 */
53 @Component
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 = "";
57
58 public static String LOG_FOLDER = "C:\\Users\\pesiimport\\.cdmLibrary\\log\\taxonX\\";
59
60 private Classification classification = null;
61 private Reference ref = null;
62
63 private TaxonXImportState taxonXstate;
64 private TaxonXDataHolder dataHolder;
65 private DerivedUnit derivedUnitBase;
66
67 private TransactionStatus tx;
68
69 private TaxonXXMLFieldGetter taxonXFieldGetter;
70
71 public TaxonXImport() {
72 super();
73 }
74
75 private Map<String,Feature> featuresMap = new HashMap<String, Feature>();
76
77 @Override
78 protected boolean doCheck(TaxonXImportState state) {
79 logger.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
80 this.taxonXstate = state;
81 return true;
82 }
83
84 /**
85 * getClassification
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
89 */
90 private void setClassification() {
91 logger.info("SET CLASSIFICATION "+classification);
92 String classificationName = taxonXstate.getConfig().getClassificationName();
93 String defaultClassificatioName = taxonXstate.getConfig().getImportClassificationName();
94
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>();
98 String citation ="";
99 String title ="";
100 for (Classification cla:classifList){
101 try{
102 // citation = cla.getCitation().toString();
103 }catch(Exception e){
104 citation="";
105 }
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);
110 }
111 else{
112 nodeList.add(title);
113 classificationMap.put(title, cla);
114 }
115 }
116 boolean defaultClassificationExists = nodeList.indexOf(defaultClassificatioName)>-1;
117 if (! defaultClassificationExists ) {
118 nodeList.add(defaultClassificatioName);
119 }
120 if (nodeList.indexOf(classificationName)<0) {
121 nodeList.add(classificationName);
122 }
123 final String other = "Other classification - add a new one";
124 nodeList.add(other);
125
126 String s;
127 if (defaultClassificatioName != null && defaultClassificationExists && taxonXstate.getConfig().isAlwaysUseDefaultClassification()) {
128 s=defaultClassificatioName;
129 } else{
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 ) );
135
136 s = (String)JOptionPane.showInputDialog(
137 null,
138 scrollPane,
139 "Classification",
140 JOptionPane.PLAIN_MESSAGE,
141 null,
142 nodeList.toArray(),
143 defaultClassificatioName);
144 }
145 ref=getReferenceService().find(ref.getUuid());
146 if (!classificationMap.containsKey(s)){
147 //System.out.println("Classif inconnue ?? "+s+", "+classifDic);
148 Reference classificationReference = null;
149
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);
158 }
159 //new classification
160 classification = Classification.NewInstance(classificationName, classificationReference, Language.DEFAULT());
161 getClassificationService().saveOrUpdate(classification);
162 refreshTransaction();
163 }
164 else{
165 classification = classificationMap.get(s);
166 }
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());
172 }
173 getClassificationService().saveOrUpdate(classification);
174 refreshTransaction();
175 }
176 }
177
178 /**
179 * asks users for decision
180 * @param string : the parameter name we are looking at
181 * @param defaultValue : the default value
182 * @return
183 */
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 ) );
190
191 String s = (String)JOptionPane.showInputDialog(
192 null,
193 scrollPane,
194 "What should be the "+string,
195 JOptionPane.PLAIN_MESSAGE,
196 null,
197 null,
198 defaultValue);
199
200 return s;
201
202 }
203
204 @Override
205 public void doInvoke(TaxonXImportState state) {
206 taxonXstate = state;
207 tx = startTransaction();
208
209 logger.info("INVOKE TaxonXImport ");
210 URI sourceName = this.taxonXstate.getConfig().getSource();
211
212 // this.taxonXstate.getConfig().getClassificationName();
213 // this.taxonXstate.getConfig().getClassificationUuid();
214
215 ref = taxonXstate.getConfig().getSourceReference();
216
217
218
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())){
224 refFound = true;
225 secundum = re;
226 }
227 }
228 if (refFound) {
229 secundum = CdmBase.deproxy(secundum, Reference.class);
230 taxonXstate.getConfig().setSecundum(secundum);
231 } else {
232 this.getReferenceService().saveOrUpdate(secundum);
233 taxonXstate.getConfig().setSecundum(secundum);
234 }
235
236
237 Reference urlRef = taxonXstate.getConfig().getOriginalSourceURL();
238 for (Reference re:references){
239 if (re.getCitation().equalsIgnoreCase(urlRef.getCitation())){
240 urlRef=re;
241 }
242 }
243 this.getReferenceService().saveOrUpdate(urlRef);
244 urlRef=getReferenceService().find(urlRef.getUuid());
245
246 if(!taxonXstate.getConfig().hasAskedForHigherRank()){
247 Rank maxRank = askForHigherRank(taxonXstate.getConfig().getNomenclaturalCode());
248 taxonXstate.getConfig().setMaxRank(maxRank);
249 taxonXstate.getConfig().setHasAskedForHigherRank(true);
250 }
251
252
253 String message = "go taxonx!";
254 logger.info(message);
255 updateProgress(this.taxonXstate, message);
256 dataHolder = new TaxonXDataHolder();
257
258 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
259 DocumentBuilder builder;
260 URL url;
261
262 try {
263 builder = factory.newDocumentBuilder();
264 url = sourceName.toURL();
265 Object o = url.getContent();
266 InputStream is = (InputStream) o;
267 Document document = builder.parse(is);
268
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
272 */
273 ref = taxonXFieldGetter.parseMods();
274 // logger.info("CLASSNAME :" +taxonXstate.getConfig().getClassificationName());
275 setClassification();
276 taxonXFieldGetter.updateClassification(classification);
277 // logger.info("classif :"+classification);
278 taxonXFieldGetter.parseTreatment(ref,sourceName);
279 featuresMap = taxonXFieldGetter.getFeaturesUsed();
280
281 }catch(Exception e){
282 e.printStackTrace();
283 logger.warn(e);
284
285 File file = new File(LOG_FOLDER + "urlTaxonX.txt");
286 FileWriter writer;
287 try {
288 writer = new FileWriter(file ,true);
289 writer.write(sourceName+"\n");
290 writer.flush();
291 writer.close();
292 } catch (IOException e1) {
293 logger.error(e1.getMessage());
294 }
295
296 logger.warn(sourceName);
297 }
298
299 //TODO:check how deduplicate work (and if it works..)
300 // if(taxonXstate.getConfig().getLastImport()) {
301 // deduplicate();
302 // }
303 commitTransaction(tx);
304 }
305
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");
312 }
313
314
315 private void refreshTransaction(){
316 commitTransaction(tx);
317 tx = startTransaction();
318 ref = getReferenceService().find(ref.getUuid());
319 classification = getClassificationService().find(classification.getUuid());
320 try{
321 derivedUnitBase = (DerivedUnit) getOccurrenceService().find(derivedUnitBase.getUuid());
322 }catch(Exception e){
323 //logger.warn("derivedunit up to date or not created yet");
324 }
325 }
326
327 /**
328 * @return
329 */
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 ) );
336
337 List<Rank> rankList = new ArrayList<Rank>();
338 rankList = getTermService().list(Rank.class, null, null, null, null);
339
340 List<String> rankListStr = new ArrayList<String>();
341 for (Rank r:rankList) {
342 rankListStr.add(r.toString());
343 }
344 String s = (String)JOptionPane.showInputDialog(
345 null,
346 scrollPane,
347 null,
348 JOptionPane.PLAIN_MESSAGE,
349 null,
350 rankListStr.toArray(),
351 "Suprageneric Taxon");
352
353 Rank cR = null;
354 try {
355 cR = Rank.getRankByEnglishName(s,nomenclaturalCode,true);
356 } catch (UnknownCdmTypeException e) {
357 logger.warn("Unknown rank ?!"+s);
358 logger.warn(e);
359 }
360 return cR;
361 }
362
363
364 @Override
365 protected boolean isIgnore(TaxonXImportState state) {
366 return false;
367 }
368
369
370 }