Project

General

Profile

Download (9.13 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.view.dataimport;
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.apache.log4j.Logger;
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
import org.eclipse.ui.IWorkbenchPart;
18
import org.eclipse.ui.IWorkbenchWindow;
19
import org.eclipse.ui.PlatformUI;
20

    
21
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
22
import eu.etaxonomy.cdm.api.service.ITermService;
23
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
24
import eu.etaxonomy.cdm.ext.occurrence.bioCase.BioCaseResponse;
25
import eu.etaxonomy.cdm.ext.occurrence.gbif.DataSetResponse;
26
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifDataSetProtocol;
27
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifResponse;
28
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
29
import eu.etaxonomy.cdm.io.specimen.gbif.in.GbifImportConfigurator;
30
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
31
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
32
import eu.etaxonomy.taxeditor.store.CdmStore;
33

    
34
public class SaveImportedSpecimenAction extends Action {
35

    
36
    private final Logger logger = Logger.getLogger(SaveImportedSpecimenAction.class);
37

    
38
    private Comparator<GbifResponse> comparator;
39

    
40
    /* (non-Javadoc)
41
     * @see org.eclipse.jface.action.Action#run()
42
     */
43
    @Override
44
    public void run() {
45
        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
46
        IWorkbenchPart activePart = window.getActivePage().getActivePart();
47
        if(activePart instanceof DataImportView<?>){
48
            DataImportView<?> dataImportView = (DataImportView<?>)activePart;
49
            Table table = dataImportView.getTable();
50
            Collection<GbifResponse> checkedResults = new HashSet<GbifResponse>();
51
            Collection<BioCaseResponse> checkedAbcdResults = new HashSet<BioCaseResponse>();
52
            Object data;
53
            Set<String[]> unitIdsGbif = new HashSet<String[]>();
54
            Set<String[]> unitIdsBioCase = new HashSet<String[]>();
55
            List<GbifResponse> biocaseResponses = new ArrayList<GbifResponse>();
56

    
57
            for(TableItem item:table.getItems()){
58
                if(item.getChecked()){
59
                    //Save Specimen
60
                	 data = item.getData();
61
                	 if(data instanceof GbifResponse){
62
                		 checkedResults.add((GbifResponse)item.getData());
63
                	 }
64
                	 if(data instanceof BioCaseResponse){
65
                	     checkedAbcdResults.add((BioCaseResponse)data);
66
                     }
67
                }
68
            }
69
            String[] tripleId ;
70
                for (GbifResponse response:checkedResults){
71
                	if (response != null){
72
                		tripleId = response.getTripleID();
73

    
74
                		if (response.getDataSetProtocol().equals(GbifDataSetProtocol.BIOCASE)){
75
                			biocaseResponses.add(response);
76

    
77
                		}else{
78
                			unitIdsGbif.add(tripleId);
79
                		}
80

    
81

    
82
                	}
83
                }
84
                Collections.sort(biocaseResponses, getComparator());
85

    
86
                Abcd206ImportConfigurator configuratorAbcd;
87
                List<Abcd206ImportConfigurator> abcdConfigurators = new ArrayList<Abcd206ImportConfigurator>();
88
                DataSetResponse dataSetResponse;
89
                OccurenceQuery bioCaseOccurrenceQuery = null;
90
                URI dataSetUri = null;
91
                for (BioCaseResponse response:checkedAbcdResults){
92

    
93
                    if (!response.getDataSetUri().equals(dataSetUri)){
94
                        dataSetUri = response.getDataSetUri();
95

    
96
                        configuratorAbcd = PreferencesUtil.getAbcdImportConfigurationPreference(true);
97
                        configuratorAbcd.setSourceUri(dataSetUri);
98
//                        configuratorAbcd.addMediaAsMediaSpecimen(true);
99
//                        configuratorAbcd.setIgnoreAuthorship(true);
100
//                        configuratorAbcd.setMoveNewTaxaToDefaultClassification(false);
101

    
102
                        unitIdsBioCase = new HashSet<String[]>();
103
                        bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
104
                        configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
105
                       // configuratorAbcd.setGetSiblings(true);
106
                        if (dataImportView.getClassification() != null){
107
                            configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
108
                        }
109
                        if (dataImportView.getReferenceString() != null){
110
                            configuratorAbcd.setSourceReferenceTitle(dataImportView.getReferenceString());
111
                        }
112
                        abcdConfigurators.add(configuratorAbcd);
113

    
114
                    }
115
                    tripleId = response.getTripleID();
116
                    unitIdsBioCase.add(tripleId);
117

    
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

    
173

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

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

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

    
189

    
190

    
191
            }
192
        }
193

    
194

    
195
    private void persistTerm(DefinedTermBase<?> term, ITermService termService, ConversationHolder conversation){
196
        if(term!=null){
197
            //if the term does not exist in the DB save it
198
            if(termService.find(term.getUuid())==null){
199
                termService.saveOrUpdate(term);
200
            }
201
        }
202
    }
203

    
204
    /* (non-Javadoc)
205
     * @see org.eclipse.jface.action.Action#getText()
206
     */
207
    @Override
208
    public String getText() {
209
        return "Import";
210
    }
211

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

    
219

    
220
}
(8-8/13)