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.io.StringWriter;
|
15
|
import java.net.URISyntaxException;
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.Collection;
|
18
|
|
19
|
import net.sf.json.JSONArray;
|
20
|
import net.sf.json.JSONObject;
|
21
|
|
22
|
import org.apache.commons.io.IOUtils;
|
23
|
import org.apache.http.client.ClientProtocolException;
|
24
|
import org.eclipse.swt.widgets.Display;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
|
27
|
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
|
28
|
import eu.etaxonomy.cdm.ext.occurrence.bioCase.BioCaseQueryServiceWrapper;
|
29
|
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifQueryServiceWrapper;
|
30
|
import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
|
31
|
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
|
32
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
33
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
34
|
import eu.etaxonomy.taxeditor.editor.view.dataimport.transientServices.TransientCdmRepository;
|
35
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
36
|
|
37
|
/**
|
38
|
* @author pplitzner
|
39
|
* @date 25.02.2014
|
40
|
*
|
41
|
*/
|
42
|
public class SpecimenImportEditorInput extends DataImportEditorInput<SpecimenOrObservationBase<?>> {
|
43
|
|
44
|
private final OccurenceQuery query;
|
45
|
private final QueryType queryType;
|
46
|
|
47
|
/**
|
48
|
* @param results
|
49
|
*/
|
50
|
public SpecimenImportEditorInput(OccurenceQuery query, QueryType queryType) {
|
51
|
super();
|
52
|
this.query = query;
|
53
|
this.queryType = queryType;
|
54
|
}
|
55
|
|
56
|
public void query(){
|
57
|
String errorMessage = "Could not execute query " + query;
|
58
|
|
59
|
Collection<SpecimenOrObservationBase<?>> results = new ArrayList<SpecimenOrObservationBase<?>>();
|
60
|
try {
|
61
|
InputStream resultStream;
|
62
|
switch (queryType) {
|
63
|
case GBIF:
|
64
|
resultStream = new GbifQueryServiceWrapper().query(query);
|
65
|
StringWriter stringWriter = new StringWriter();
|
66
|
IOUtils.copy(resultStream, stringWriter);
|
67
|
String jsonString = stringWriter.toString();
|
68
|
JSONArray resultsArray = JSONObject.fromObject(jsonString).getJSONArray("results");
|
69
|
for(Object o:resultsArray){
|
70
|
//parse every record
|
71
|
if(o instanceof JSONObject){
|
72
|
DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
|
73
|
JSONObject record = (JSONObject)o;
|
74
|
|
75
|
if(record.has("locality")){
|
76
|
String locality = record.getString("locality");
|
77
|
derivedUnitFacade.setLocality(locality);
|
78
|
}
|
79
|
results.add(derivedUnitFacade.innerDerivedUnit());
|
80
|
}
|
81
|
|
82
|
}
|
83
|
break;
|
84
|
case BIOCASE:
|
85
|
resultStream = new BioCaseQueryServiceWrapper().query(SpecimenImportEditorInput.this.query);
|
86
|
Abcd206ImportConfigurator configurator = Abcd206ImportConfigurator.NewInstance(resultStream, null, false);
|
87
|
TransientCdmRepository repo =
|
88
|
new TransientCdmRepository(CdmStore.getCurrentApplicationConfiguration());
|
89
|
configurator.setCdmAppController(repo);
|
90
|
|
91
|
CdmDefaultImport<Abcd206ImportConfigurator> importer = new CdmDefaultImport<Abcd206ImportConfigurator>();
|
92
|
importer.invoke(configurator);
|
93
|
results = repo.getUnits();
|
94
|
setResults(results);
|
95
|
break;
|
96
|
}
|
97
|
} catch (ClientProtocolException e) {
|
98
|
logger.error(errorMessage, e);
|
99
|
} catch (IOException e) {
|
100
|
logger.error(errorMessage, e);
|
101
|
} catch (URISyntaxException e) {
|
102
|
logger.error(errorMessage, e);
|
103
|
}
|
104
|
|
105
|
setResults(results);
|
106
|
|
107
|
Display.getDefault().asyncExec(new Runnable() {
|
108
|
|
109
|
@Override
|
110
|
public void run() {
|
111
|
CdmStore.getContextManager().notifyContextRefresh();
|
112
|
}
|
113
|
});
|
114
|
}
|
115
|
|
116
|
/* (non-Javadoc)
|
117
|
* @see org.eclipse.ui.IEditorInput#getName()
|
118
|
*/
|
119
|
@Override
|
120
|
public String getName() {
|
121
|
return queryType + " " + query.toString();
|
122
|
}
|
123
|
|
124
|
/* (non-Javadoc)
|
125
|
* @see org.eclipse.ui.IEditorInput#getToolTipText()
|
126
|
*/
|
127
|
@Override
|
128
|
public String getToolTipText() {
|
129
|
return query.toString();
|
130
|
}
|
131
|
|
132
|
/* (non-Javadoc)
|
133
|
* @see java.lang.Object#hashCode()
|
134
|
*/
|
135
|
@Override
|
136
|
public int hashCode() {
|
137
|
final int prime = 31;
|
138
|
int result = 1;
|
139
|
result = prime * result + ((query == null) ? 0 : query.hashCode());
|
140
|
return result;
|
141
|
}
|
142
|
|
143
|
/* (non-Javadoc)
|
144
|
* @see java.lang.Object#equals(java.lang.Object)
|
145
|
*/
|
146
|
@Override
|
147
|
public boolean equals(Object obj) {
|
148
|
if (this == obj) {
|
149
|
return true;
|
150
|
}
|
151
|
if (obj == null) {
|
152
|
return false;
|
153
|
}
|
154
|
if (getClass() != obj.getClass()) {
|
155
|
return false;
|
156
|
}
|
157
|
SpecimenImportEditorInput other = (SpecimenImportEditorInput) obj;
|
158
|
if (query == null) {
|
159
|
if (other.query != null) {
|
160
|
return false;
|
161
|
}
|
162
|
} else if (!query.equals(other.query)) {
|
163
|
return false;
|
164
|
}
|
165
|
return true;
|
166
|
}
|
167
|
}
|