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