60d1302cbbc6bdda9f35f4abeb465885c209c0e4
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / dataimport / BioCaseEditorInput.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.editor.view.dataimport;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.Collection;
17
18 import org.apache.http.client.ClientProtocolException;
19 import org.eclipse.swt.widgets.Display;
20
21 import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
22 import eu.etaxonomy.cdm.ext.occurrence.bioCase.BioCaseQueryServiceWrapper;
23 import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
24 import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
25 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
26 import eu.etaxonomy.taxeditor.editor.view.dataimport.transientServices.TransientCdmRepository;
27 import eu.etaxonomy.taxeditor.store.CdmStore;
28
29 /**
30 * @author pplitzner
31 * @date 25.02.2014
32 *
33 */
34 public class BioCaseEditorInput extends DataImportEditorInput<SpecimenOrObservationBase<?>> {
35
36 private final URI endPoint;
37
38 /**
39 * Constructs a new BioCaseEditorInput.
40 * @param query The {@link OccurenceQuery} object holding all necessary parameters.
41 * @param endPoint If the endPoint {@link URI} is <code>null</code> then the default endPoint is used
42 * (<i>http://ww3.bgbm.org/biocase/pywrapper.cgi?dsa=Herbar</i>)
43 */
44 public BioCaseEditorInput(OccurenceQuery query, URI endPoint) {
45 super();
46 this.query = query;
47 this.endPoint = endPoint;
48 }
49
50 @Override
51 public void query(){
52 String errorMessage = "Could not execute query " + query;
53
54 Collection<SpecimenOrObservationBase<?>> results = new ArrayList<SpecimenOrObservationBase<?>>();
55 try {
56 //FIXME move ABCD import to cdmlib -> this will also get rid of the transient services
57 InputStream resultStream;
58 resultStream = new BioCaseQueryServiceWrapper().query(query, endPoint);
59 Abcd206ImportConfigurator configurator = Abcd206ImportConfigurator.NewInstance(resultStream, null, false);
60 TransientCdmRepository repo =
61 new TransientCdmRepository(CdmStore.getCurrentApplicationConfiguration());
62 configurator.setCdmAppController(repo);
63
64 CdmDefaultImport<Abcd206ImportConfigurator> importer = new CdmDefaultImport<Abcd206ImportConfigurator>();
65 importer.invoke(configurator);
66 results = repo.getUnits();
67 setResults(results);
68 } catch (ClientProtocolException e) {
69 logger.error(errorMessage, e);
70 } catch (IOException e) {
71 logger.error(errorMessage, e);
72 }
73
74 setResults(results);
75
76 Display.getDefault().asyncExec(new Runnable() {
77
78 @Override
79 public void run() {
80 CdmStore.getContextManager().notifyContextRefresh();
81 }
82 });
83 }
84
85 /* (non-Javadoc)
86 * @see org.eclipse.ui.IEditorInput#getName()
87 */
88 @Override
89 public String getName() {
90 return "[BioCASE] " + query.toString();
91 }
92
93 /* (non-Javadoc)
94 * @see org.eclipse.ui.IEditorInput#getToolTipText()
95 */
96 @Override
97 public String getToolTipText() {
98 return "[BioCASE] " + query.toString();
99 }
100
101 /* (non-Javadoc)
102 * @see java.lang.Object#hashCode()
103 */
104 @Override
105 public int hashCode() {
106 final int prime = 31;
107 int result = 1;
108 result = prime * result + ((query == null) ? 0 : query.hashCode());
109 return result;
110 }
111
112 /* (non-Javadoc)
113 * @see java.lang.Object#equals(java.lang.Object)
114 */
115 @Override
116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (obj == null) {
121 return false;
122 }
123 if (getClass() != obj.getClass()) {
124 return false;
125 }
126 BioCaseEditorInput other = (BioCaseEditorInput) obj;
127 if (query == null) {
128 if (other.query != null) {
129 return false;
130 }
131 } else if (!query.equals(other.query)) {
132 return false;
133 }
134 return true;
135 }
136 }