1 package eu
.etaxonomy
.taxeditor
.view
.dataimport
.e4
;
4 import java
.time
.LocalDate
;
5 import java
.time
.format
.DateTimeFormatter
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Collection
;
8 import java
.util
.Collections
;
9 import java
.util
.Comparator
;
10 import java
.util
.HashSet
;
11 import java
.util
.List
;
14 import org
.eclipse
.core
.runtime
.jobs
.Job
;
15 import org
.eclipse
.jface
.action
.Action
;
16 import org
.eclipse
.swt
.widgets
.Table
;
17 import org
.eclipse
.swt
.widgets
.TableItem
;
19 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
20 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
21 import eu
.etaxonomy
.cdm
.ext
.occurrence
.OccurenceQuery
;
22 import eu
.etaxonomy
.cdm
.ext
.occurrence
.bioCase
.BioCaseResponse
;
23 import eu
.etaxonomy
.cdm
.ext
.occurrence
.gbif
.DataSetResponse
;
24 import eu
.etaxonomy
.cdm
.ext
.occurrence
.gbif
.GbifDataSetProtocol
;
25 import eu
.etaxonomy
.cdm
.ext
.occurrence
.gbif
.GbifResponse
;
26 import eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
.Abcd206ImportConfigurator
;
27 import eu
.etaxonomy
.cdm
.io
.specimen
.gbif
.in
.GbifImportConfigurator
;
28 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
29 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
30 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
31 import eu
.etaxonomy
.taxeditor
.view
.dataimport
.GbifResponseComparator
;
37 public class SaveImportedSpecimenActionE4
extends Action
{
39 private Comparator
<GbifResponse
> comparator
;
41 private DataImportViewE4
<?
> dataImportView
;
43 public SaveImportedSpecimenActionE4(DataImportViewE4
<?
> dataImportView
) {
45 this.dataImportView
= dataImportView
;
50 Table table
= dataImportView
.getTable();
51 Collection
<GbifResponse
> checkedResults
= new HashSet
<>();
52 Collection
<BioCaseResponse
> checkedAbcdResults
= new HashSet
<>();
54 Set
<String
[]> unitIdsGbif
= new HashSet
<>();
55 Set
<String
[]> unitIdsBioCase
= new HashSet
<>();
56 List
<GbifResponse
> biocaseResponses
= new ArrayList
<>();
58 for(TableItem item
:table
.getItems()){
59 if(item
.getChecked()){
61 data
= item
.getData();
62 if(data
instanceof GbifResponse
){
63 checkedResults
.add((GbifResponse
)item
.getData());
65 if(data
instanceof BioCaseResponse
){
66 checkedAbcdResults
.add((BioCaseResponse
)data
);
71 for (GbifResponse response
:checkedResults
){
72 if (response
!= null){
73 tripleId
= response
.getTripleID();
75 if (response
.getDataSetProtocol()!=null && response
.getDataSetProtocol().equals(GbifDataSetProtocol
.BIOCASE
)){
76 biocaseResponses
.add(response
);
79 unitIdsGbif
.add(tripleId
);
83 Collections
.sort(biocaseResponses
, getComparator());
85 Abcd206ImportConfigurator configuratorAbcd
;
86 List
<Abcd206ImportConfigurator
> abcdConfigurators
= new ArrayList
<>();
87 DataSetResponse dataSetResponse
;
88 OccurenceQuery bioCaseOccurrenceQuery
= null;
89 URI dataSetUri
= null;
90 for (BioCaseResponse response
:checkedAbcdResults
){
92 if (!response
.getDataSetUri().equals(dataSetUri
)){
93 dataSetUri
= response
.getDataSetUri();
94 //configuratorAbcd = Abcd206ImportConfigurator.NewInstance(null, null);
95 configuratorAbcd
= PreferencesUtil
.getLastUsedAbcdImportConfigurator();
96 if (configuratorAbcd
!= null){
98 if (configuratorAbcd
.getNomenclaturalCode() == null){
99 configuratorAbcd
.setNomenclaturalCode(PreferencesUtil
.getPreferredNomenclaturalCode());
102 configuratorAbcd
= Abcd206ImportConfigurator
.NewInstance(null, null);
104 if (configuratorAbcd
.getNomenclaturalCode() == null){
105 configuratorAbcd
.setNomenclaturalCode(PreferencesUtil
.getPreferredNomenclaturalCode());
108 configuratorAbcd
.setSourceUri(dataSetUri
);
109 if (configuratorAbcd
.getSourceReference() == null && configuratorAbcd
.getSourceRefUuid() == null){
110 DateTimeFormatter dtf
= DateTimeFormatter
.ofPattern("yyyy/MM/dd");
111 LocalDate localDate
= LocalDate
.now();
113 String sourceRef
="Specimen Import " + localDate
.toString();
114 configuratorAbcd
.setSourceReferenceTitle(sourceRef
);
117 unitIdsBioCase
= new HashSet
<>();
118 bioCaseOccurrenceQuery
= new OccurenceQuery(unitIdsBioCase
);
119 configuratorAbcd
.setOccurenceQuery(bioCaseOccurrenceQuery
);
121 // if (dataImportView.getClassification() != null){
122 // configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
124 // if (dataImportView.getReferenceString() != null){
125 // configuratorAbcd.setSourceReferenceTitle(dataImportView.getReferenceString());
127 abcdConfigurators
.add(configuratorAbcd
);
131 tripleId
= response
.getTripleID();
132 unitIdsBioCase
.add(tripleId
);
137 // if (!unitIdsBioCase.isEmpty()){
138 // configuratorAbcd = Abcd206ImportConfigurator.NewInstance(dataSetUri, null, false);
139 // configuratorAbcd.addMediaAsMediaSpecimen(true);
140 // configuratorAbcd.setIgnoreAuthorship(true);
141 // configuratorAbcd.setMoveNewTaxaToDefaultClassification(false);
142 // if (configuratorAbcd != null){
143 // abcdConfigurators.add(configuratorAbcd);
145 //// unitIdsBioCase = new HashSet<String[]>();
146 // bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
147 // configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
148 // configuratorAbcd.setGetSiblings(true);
149 // if (dataImportView.getClassification() != null){
150 // configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
154 // for (GbifResponse response: biocaseResponses){
156 // dataSetResponse = new GbifQueryServiceWrapper().queryOriginalDataSet(response);
157 // dataSetUri = dataSetResponse.getEndpoint();
158 // } catch (ClientProtocolException e) {
159 // // TODO Auto-generated catch block
160 // e.printStackTrace();
161 // } catch (IOException e) {
162 // // TODO Auto-generated catch block
163 // e.printStackTrace();
165 // if (!response.getDataSetUri().equals(dataSetUri)){
166 // configuratorAbcd = Abcd206ImportConfigurator.NewInstance(dataSetUri, null, false);
167 // configuratorAbcd.addMediaAsMediaSpecimen(true);
168 // configuratorAbcd.setIgnoreAuthorship(true);
169 // configuratorAbcd.setMoveNewTaxaToDefaultClassification(false);
170 // if (configuratorAbcd != null){
171 // abcdConfigurators.add(configuratorAbcd);
173 // unitIdsBioCase = new HashSet<String[]>();
174 // bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
175 // configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
176 // configuratorAbcd.setGetSiblings(true);
177 // if (dataImportView.getClassification() != null){
178 // configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
182 // tripleId = response.getTripleID();
183 // unitIdsBioCase.add(tripleId);
187 // configurator.setQuery(query);
188 if (!checkedAbcdResults
.isEmpty()){
189 Job bioCaseJob
= CdmStore
.getImportManager().createIOServiceJob(abcdConfigurators
);
190 CdmStore
.getImportManager().run(bioCaseJob
);
193 if (!unitIdsGbif
.isEmpty()){
194 OccurenceQuery query
= new OccurenceQuery(unitIdsGbif
);
195 GbifImportConfigurator configurator
= GbifImportConfigurator
.newInstance(query
);
196 configurator
.setSourceReferenceTitle("Import Gbif data");
198 Job gbifJob
= CdmStore
.getImportManager().createIOServiceJob(configurator
);
199 CdmStore
.getImportManager().run(gbifJob
);
203 private void persistTerm(DefinedTermBase
<?
> term
, ITermService termService
, ConversationHolder conversation
){
205 //if the term does not exist in the DB save it
206 if(termService
.find(term
.getUuid())==null){
207 termService
.saveOrUpdate(term
);
213 public String
getText() {
217 private Comparator
<GbifResponse
> getComparator(){
218 if (comparator
== null){
219 comparator
= new GbifResponseComparator();