1
|
package eu.etaxonomy.taxeditor.view.dataimport;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.net.URI;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.Collection;
|
7
|
import java.util.Collections;
|
8
|
import java.util.Comparator;
|
9
|
import java.util.HashSet;
|
10
|
import java.util.List;
|
11
|
import java.util.Set;
|
12
|
|
13
|
import org.apache.http.client.ClientProtocolException;
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.eclipse.core.runtime.jobs.Job;
|
16
|
import org.eclipse.jface.action.Action;
|
17
|
import org.eclipse.swt.widgets.Table;
|
18
|
import org.eclipse.swt.widgets.TableItem;
|
19
|
import org.eclipse.ui.IWorkbenchPart;
|
20
|
import org.eclipse.ui.IWorkbenchWindow;
|
21
|
import org.eclipse.ui.PlatformUI;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
24
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
25
|
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
|
26
|
import eu.etaxonomy.cdm.ext.occurrence.bioCase.BioCaseResponse;
|
27
|
import eu.etaxonomy.cdm.ext.occurrence.gbif.DataSetResponse;
|
28
|
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifDataSetProtocol;
|
29
|
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifQueryServiceWrapper;
|
30
|
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifResponse;
|
31
|
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
|
32
|
import eu.etaxonomy.cdm.io.specimen.gbif.in.GbifImportConfigurator;
|
33
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
34
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
35
|
|
36
|
public class SaveImportedSpecimenAction extends Action {
|
37
|
|
38
|
private final Logger logger = Logger.getLogger(SaveImportedSpecimenAction.class);
|
39
|
|
40
|
private Comparator<GbifResponse> comparator;
|
41
|
|
42
|
/* (non-Javadoc)
|
43
|
* @see org.eclipse.jface.action.Action#run()
|
44
|
*/
|
45
|
@Override
|
46
|
public void run() {
|
47
|
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
|
48
|
IWorkbenchPart activePart = window.getActivePage().getActivePart();
|
49
|
if(activePart instanceof DataImportView<?>){
|
50
|
DataImportView<?> dataImportView = (DataImportView<?>)activePart;
|
51
|
Table table = dataImportView.getTable();
|
52
|
Collection<GbifResponse> checkedResults = new HashSet<GbifResponse>();
|
53
|
Collection<BioCaseResponse> checkedAbcdResults = new HashSet<BioCaseResponse>();
|
54
|
Object data;
|
55
|
Set<String[]> unitIdsGbif = new HashSet<String[]>();
|
56
|
Set<String[]> unitIdsBioCase = new HashSet<String[]>();
|
57
|
List<GbifResponse> biocaseResponses = new ArrayList<GbifResponse>();
|
58
|
|
59
|
for(TableItem item:table.getItems()){
|
60
|
if(item.getChecked()){
|
61
|
//Save Specimen
|
62
|
data = item.getData();
|
63
|
if(data instanceof GbifResponse){
|
64
|
checkedResults.add((GbifResponse)item.getData());
|
65
|
}
|
66
|
if(data instanceof BioCaseResponse){
|
67
|
checkedAbcdResults.add((BioCaseResponse)data);
|
68
|
}
|
69
|
}
|
70
|
}
|
71
|
String[] tripleId ;
|
72
|
for (GbifResponse response:checkedResults){
|
73
|
if (response != null){
|
74
|
tripleId = response.getTripleID();
|
75
|
|
76
|
if (response.getDataSetProtocol().equals(GbifDataSetProtocol.BIOCASE)){
|
77
|
biocaseResponses.add(response);
|
78
|
|
79
|
}else{
|
80
|
unitIdsGbif.add(tripleId);
|
81
|
}
|
82
|
|
83
|
|
84
|
}
|
85
|
}
|
86
|
Collections.sort(biocaseResponses, getComparator());
|
87
|
|
88
|
Abcd206ImportConfigurator configuratorAbcd;
|
89
|
List<Abcd206ImportConfigurator> abcdConfigurators = new ArrayList<Abcd206ImportConfigurator>();
|
90
|
DataSetResponse dataSetResponse;
|
91
|
OccurenceQuery bioCaseOccurrenceQuery = null;
|
92
|
URI dataSetUri = null;
|
93
|
for (BioCaseResponse response:checkedAbcdResults){
|
94
|
|
95
|
// if (!response.getDataSetUri().equals(dataSetUri)){
|
96
|
dataSetUri = response.getDataSetUri();
|
97
|
configuratorAbcd = Abcd206ImportConfigurator.NewInstance(dataSetUri, null, false);
|
98
|
configuratorAbcd.addMediaAsMediaSpecimen(true);
|
99
|
configuratorAbcd.setIgnoreAuthorship(true);
|
100
|
configuratorAbcd.setMoveNewTaxaToDefaultClassification(false);
|
101
|
|
102
|
unitIdsBioCase = new HashSet<String[]>();
|
103
|
bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
|
104
|
configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
|
105
|
configuratorAbcd.setGetSiblings(true);
|
106
|
if (dataImportView.getClassification() != null){
|
107
|
configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
|
108
|
}
|
109
|
|
110
|
//}
|
111
|
tripleId = response.getTripleID();
|
112
|
unitIdsBioCase.add(tripleId);
|
113
|
|
114
|
}
|
115
|
|
116
|
|
117
|
if (!unitIdsBioCase.isEmpty()){
|
118
|
configuratorAbcd = Abcd206ImportConfigurator.NewInstance(dataSetUri, null, false);
|
119
|
configuratorAbcd.addMediaAsMediaSpecimen(true);
|
120
|
configuratorAbcd.setIgnoreAuthorship(true);
|
121
|
configuratorAbcd.setMoveNewTaxaToDefaultClassification(false);
|
122
|
if (configuratorAbcd != null){
|
123
|
abcdConfigurators.add(configuratorAbcd);
|
124
|
}
|
125
|
// unitIdsBioCase = new HashSet<String[]>();
|
126
|
bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
|
127
|
configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
|
128
|
configuratorAbcd.setGetSiblings(true);
|
129
|
if (dataImportView.getClassification() != null){
|
130
|
configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
|
131
|
}
|
132
|
}
|
133
|
|
134
|
for (GbifResponse response: biocaseResponses){
|
135
|
try {
|
136
|
dataSetResponse = new GbifQueryServiceWrapper().queryOriginalDataSet(response);
|
137
|
dataSetUri = dataSetResponse.getEndpoint();
|
138
|
} catch (ClientProtocolException e) {
|
139
|
// TODO Auto-generated catch block
|
140
|
e.printStackTrace();
|
141
|
} catch (IOException e) {
|
142
|
// TODO Auto-generated catch block
|
143
|
e.printStackTrace();
|
144
|
}
|
145
|
if (!response.getDataSetUri().equals(dataSetUri)){
|
146
|
configuratorAbcd = Abcd206ImportConfigurator.NewInstance(dataSetUri, null, false);
|
147
|
configuratorAbcd.addMediaAsMediaSpecimen(true);
|
148
|
configuratorAbcd.setIgnoreAuthorship(true);
|
149
|
configuratorAbcd.setMoveNewTaxaToDefaultClassification(false);
|
150
|
if (configuratorAbcd != null){
|
151
|
abcdConfigurators.add(configuratorAbcd);
|
152
|
}
|
153
|
unitIdsBioCase = new HashSet<String[]>();
|
154
|
bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
|
155
|
configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
|
156
|
configuratorAbcd.setGetSiblings(true);
|
157
|
if (dataImportView.getClassification() != null){
|
158
|
configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
|
159
|
}
|
160
|
|
161
|
}
|
162
|
tripleId = response.getTripleID();
|
163
|
unitIdsBioCase.add(tripleId);
|
164
|
}
|
165
|
|
166
|
OccurenceQuery query = new OccurenceQuery(unitIdsGbif);
|
167
|
GbifImportConfigurator configurator = GbifImportConfigurator.newInstance(query);
|
168
|
configurator.setSourceReferenceTitle("Import Gbif data");
|
169
|
|
170
|
|
171
|
|
172
|
// configurator.setQuery(query);
|
173
|
|
174
|
Job bioCaseJob = CdmStore.getImportManager().createIOServiceJob(abcdConfigurators);
|
175
|
CdmStore.getImportManager().run(bioCaseJob);
|
176
|
|
177
|
Job gbifJob = CdmStore.getImportManager().createIOServiceJob(configurator);
|
178
|
CdmStore.getImportManager().run(gbifJob);
|
179
|
|
180
|
|
181
|
|
182
|
}
|
183
|
}
|
184
|
|
185
|
|
186
|
private void persistTerm(DefinedTermBase<?> term, ITermService termService, ConversationHolder conversation){
|
187
|
if(term!=null){
|
188
|
//if the term does not exist in the DB save it
|
189
|
if(termService.find(term.getUuid())==null){
|
190
|
termService.saveOrUpdate(term);
|
191
|
}
|
192
|
}
|
193
|
}
|
194
|
|
195
|
/* (non-Javadoc)
|
196
|
* @see org.eclipse.jface.action.Action#getText()
|
197
|
*/
|
198
|
@Override
|
199
|
public String getText() {
|
200
|
return "Import";
|
201
|
}
|
202
|
|
203
|
private Comparator<GbifResponse> getComparator(){
|
204
|
if (comparator == null){
|
205
|
comparator = new GbifResponseComparator();
|
206
|
}
|
207
|
return comparator;
|
208
|
}
|
209
|
|
210
|
|
211
|
}
|