Project

General

Profile

Download (9.16 KB) Statistics
| Branch: | Tag: | Revision:
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.term.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
                //configuratorAbcd = Abcd206ImportConfigurator.NewInstance(null, null);
97
                configuratorAbcd = PreferencesUtil.getLastUsedAbcdImportConfigurator();
98
                if (configuratorAbcd != null){
99

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

    
106
                    if (configuratorAbcd.getNomenclaturalCode() == null){
107
                        configuratorAbcd.setNomenclaturalCode(PreferencesUtil.getPreferredNomenclaturalCode());
108
                    }
109
                }
110
                configuratorAbcd.setSourceUri(dataSetUri);
111

    
112

    
113
                unitIdsBioCase = new HashSet<String[]>();
114
                bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
115
                configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
116

    
117
                if (dataImportView.getClassification() != null){
118
                    configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
119
                }
120
                if (dataImportView.getReferenceString() != null){
121
                    configuratorAbcd.setSourceReferenceTitle(dataImportView.getReferenceString());
122
                }
123
                abcdConfigurators.add(configuratorAbcd);
124

    
125

    
126
            }
127
            tripleId = response.getTripleID();
128
            unitIdsBioCase.add(tripleId);
129

    
130
        }
131

    
132

    
133
        //                if (!unitIdsBioCase.isEmpty()){
134
        //                    configuratorAbcd = Abcd206ImportConfigurator.NewInstance(dataSetUri, null, false);
135
        //                    configuratorAbcd.addMediaAsMediaSpecimen(true);
136
        //                    configuratorAbcd.setIgnoreAuthorship(true);
137
        //                    configuratorAbcd.setMoveNewTaxaToDefaultClassification(false);
138
        //                    if (configuratorAbcd != null){
139
        //                        abcdConfigurators.add(configuratorAbcd);
140
        //                    }
141
        ////                    unitIdsBioCase = new HashSet<String[]>();
142
        //                    bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
143
        //                    configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
144
        //                    configuratorAbcd.setGetSiblings(true);
145
        //                    if (dataImportView.getClassification() != null){
146
        //                        configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
147
        //                    }
148
        //                }
149

    
150
        //                for (GbifResponse response: biocaseResponses){
151
        //                	try {
152
        //						dataSetResponse = new GbifQueryServiceWrapper().queryOriginalDataSet(response);
153
        //						dataSetUri = dataSetResponse.getEndpoint();
154
        //					} catch (ClientProtocolException e) {
155
        //						// TODO Auto-generated catch block
156
        //						e.printStackTrace();
157
        //					} catch (IOException e) {
158
        //						// TODO Auto-generated catch block
159
        //						e.printStackTrace();
160
        //					}
161
        //                	if (!response.getDataSetUri().equals(dataSetUri)){
162
        //                		configuratorAbcd = Abcd206ImportConfigurator.NewInstance(dataSetUri, null, false);
163
        //                		configuratorAbcd.addMediaAsMediaSpecimen(true);
164
        //                		configuratorAbcd.setIgnoreAuthorship(true);
165
        //                		configuratorAbcd.setMoveNewTaxaToDefaultClassification(false);
166
        //                		if (configuratorAbcd != null){
167
        //                			abcdConfigurators.add(configuratorAbcd);
168
        //                		}
169
        //                		unitIdsBioCase = new HashSet<String[]>();
170
        //                		bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
171
        //                		configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
172
        //                		configuratorAbcd.setGetSiblings(true);
173
        //                		if (dataImportView.getClassification() != null){
174
        //                			configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
175
        //                		}
176
        //
177
        //                	}
178
        //                	tripleId = response.getTripleID();
179
        //                	unitIdsBioCase.add(tripleId);
180
        //                }
181

    
182

    
183
        //  configurator.setQuery(query);
184
        if (!checkedAbcdResults.isEmpty()){
185
            Job bioCaseJob = CdmStore.getImportManager().createIOServiceJob(abcdConfigurators);
186
            CdmStore.getImportManager().run(bioCaseJob);
187
        }
188

    
189
        if (!unitIdsGbif.isEmpty()){
190
            OccurenceQuery query = new OccurenceQuery(unitIdsGbif);
191
            GbifImportConfigurator configurator = GbifImportConfigurator.newInstance(query);
192
            configurator.setSourceReferenceTitle("Import Gbif data");
193

    
194
            Job gbifJob = CdmStore.getImportManager().createIOServiceJob(configurator);
195
            CdmStore.getImportManager().run(gbifJob);
196
        }
197

    
198
    }
199

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

    
209
    @Override
210
    public String getText() {
211
        return "Import";
212
    }
213

    
214
    private Comparator<GbifResponse> getComparator(){
215
        if (comparator == null){
216
            comparator = new GbifResponseComparator();
217
        }
218
        return comparator;
219
    }
220

    
221
}
(3-3/7)