Project

General

Profile

Download (9.58 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.view.dataimport.e4;
2

    
3
import java.time.LocalDate;
4
import java.time.format.DateTimeFormatter;
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.eclipse.core.runtime.jobs.Job;
14
import org.eclipse.jface.action.Action;
15
import org.eclipse.swt.widgets.Table;
16
import org.eclipse.swt.widgets.TableItem;
17

    
18
import eu.etaxonomy.cdm.common.URI;
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;
32

    
33
/**
34
 * @author pplitzner
35
 * @since Oct 4, 2017
36
 */
37
public class SaveImportedSpecimenAction extends Action {
38

    
39
    private Comparator<GbifResponse> comparator;
40

    
41
    private DataImportView<?> dataImportView;
42

    
43
    public SaveImportedSpecimenAction(DataImportView<?> 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<>();
52
        Collection<BioCaseResponse> checkedAbcdResults = new HashSet<>();
53
        Object data;
54
        Set<String[]> unitIdsGbif = new HashSet<>();
55
        Set<String[]> unitIdsBioCase = new HashSet<>();
56
        List<GbifResponse> biocaseResponses = new ArrayList<>();
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
        Collections.sort(biocaseResponses, getComparator());
84

    
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){
91

    
92
            if (!response.getDataSetUri().equals(dataSetUri)){
93
                dataSetUri = response.getDataSetUri();
94
                //configuratorAbcd = Abcd206ImportConfigurator.NewInstance(null, null);
95
                configuratorAbcd = PreferencesUtil.getLastUsedAbcdImportConfigurator();
96
                if (configuratorAbcd != null){
97

    
98
                    if (configuratorAbcd.getNomenclaturalCode() == null){
99
                        configuratorAbcd.setNomenclaturalCode(PreferencesUtil.getPreferredNomenclaturalCode());
100
                    }
101
                }else{
102
                    configuratorAbcd = Abcd206ImportConfigurator.NewInstance(null, null);
103

    
104
                    if (configuratorAbcd.getNomenclaturalCode() == null){
105
                        configuratorAbcd.setNomenclaturalCode(PreferencesUtil.getPreferredNomenclaturalCode());
106
                    }
107
                }
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();
112

    
113
                    String sourceRef ="Specimen Import " + localDate.toString();
114
                    configuratorAbcd.setSourceReferenceTitle(sourceRef);
115
                }
116

    
117
                unitIdsBioCase = new HashSet<>();
118
                bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
119
                configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
120

    
121
//                if (dataImportView.getClassification() != null){
122
//                    configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
123
//                }
124
//                if (dataImportView.getReferenceString() != null){
125
//                    configuratorAbcd.setSourceReferenceTitle(dataImportView.getReferenceString());
126
//                }
127
                abcdConfigurators.add(configuratorAbcd);
128

    
129

    
130
            }
131
            tripleId = response.getTripleID();
132
            unitIdsBioCase.add(tripleId);
133

    
134
        }
135

    
136

    
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);
144
        //                    }
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());
151
        //                    }
152
        //                }
153

    
154
        //                for (GbifResponse response: biocaseResponses){
155
        //                	try {
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();
164
        //					}
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);
172
        //                		}
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());
179
        //                		}
180
        //
181
        //                	}
182
        //                	tripleId = response.getTripleID();
183
        //                	unitIdsBioCase.add(tripleId);
184
        //                }
185

    
186

    
187
        //  configurator.setQuery(query);
188
        if (!checkedAbcdResults.isEmpty()){
189
            Job bioCaseJob = CdmStore.getImportManager().createIOServiceJob(abcdConfigurators);
190
            CdmStore.getImportManager().run(bioCaseJob);
191
        }
192

    
193
        if (!unitIdsGbif.isEmpty()){
194
            OccurenceQuery query = new OccurenceQuery(unitIdsGbif);
195
            GbifImportConfigurator configurator = GbifImportConfigurator.newInstance(query);
196
            configurator.setSourceReferenceTitle("Import Gbif data");
197

    
198
            Job gbifJob = CdmStore.getImportManager().createIOServiceJob(configurator);
199
            CdmStore.getImportManager().run(gbifJob);
200
        }
201
    }
202

    
203
    private void persistTerm(DefinedTermBase<?> term, ITermService termService, ConversationHolder conversation){
204
        if(term!=null){
205
            //if the term does not exist in the DB save it
206
            if(termService.find(term.getUuid())==null){
207
                termService.saveOrUpdate(term);
208
            }
209
        }
210
    }
211

    
212
    @Override
213
    public String getText() {
214
        return "Import";
215
    }
216

    
217
    private Comparator<GbifResponse> getComparator(){
218
        if (comparator == null){
219
            comparator = new GbifResponseComparator();
220
        }
221
        return comparator;
222
    }
223
}
(5-5/14)