Project

General

Profile

Download (8.83 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.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
            }
117
            tripleId = response.getTripleID();
118
            unitIdsBioCase.add(tripleId);
119

    
120
        }
121

    
122

    
123
        //                if (!unitIdsBioCase.isEmpty()){
124
        //                    configuratorAbcd = Abcd206ImportConfigurator.NewInstance(dataSetUri, null, false);
125
        //                    configuratorAbcd.addMediaAsMediaSpecimen(true);
126
        //                    configuratorAbcd.setIgnoreAuthorship(true);
127
        //                    configuratorAbcd.setMoveNewTaxaToDefaultClassification(false);
128
        //                    if (configuratorAbcd != null){
129
        //                        abcdConfigurators.add(configuratorAbcd);
130
        //                    }
131
        ////                    unitIdsBioCase = new HashSet<String[]>();
132
        //                    bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
133
        //                    configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
134
        //                    configuratorAbcd.setGetSiblings(true);
135
        //                    if (dataImportView.getClassification() != null){
136
        //                        configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
137
        //                    }
138
        //                }
139

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

    
172

    
173
        //  configurator.setQuery(query);
174
        if (!checkedAbcdResults.isEmpty()){
175
            Job bioCaseJob = CdmStore.getImportManager().createIOServiceJob(abcdConfigurators);
176
            CdmStore.getImportManager().run(bioCaseJob);
177
        }
178

    
179
        if (!unitIdsGbif.isEmpty()){
180
            OccurenceQuery query = new OccurenceQuery(unitIdsGbif);
181
            GbifImportConfigurator configurator = GbifImportConfigurator.newInstance(query);
182
            configurator.setSourceReferenceTitle("Import Gbif data");
183

    
184
            Job gbifJob = CdmStore.getImportManager().createIOServiceJob(configurator);
185
            CdmStore.getImportManager().run(gbifJob);
186
        }
187

    
188
    }
189

    
190
    private void persistTerm(DefinedTermBase<?> term, ITermService termService, ConversationHolder conversation){
191
        if(term!=null){
192
            //if the term does not exist in the DB save it
193
            if(termService.find(term.getUuid())==null){
194
                termService.saveOrUpdate(term);
195
            }
196
        }
197
    }
198

    
199
    @Override
200
    public String getText() {
201
        return "Import";
202
    }
203

    
204
    private Comparator<GbifResponse> getComparator(){
205
        if (comparator == null){
206
            comparator = new GbifResponseComparator();
207
        }
208
        return comparator;
209
    }
210

    
211
}
(3-3/7)