Project

General

Profile

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

    
3
import java.io.IOException;
4
import java.net.URI;
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.apache.http.client.ClientProtocolException;
14
import org.apache.log4j.Logger;
15
import org.eclipse.core.runtime.jobs.Job;
16
import org.eclipse.jface.action.Action;
17
import org.eclipse.swt.widgets.Table;
18
import org.eclipse.swt.widgets.TableItem;
19
import org.eclipse.ui.IWorkbenchPart;
20
import org.eclipse.ui.IWorkbenchWindow;
21
import org.eclipse.ui.PlatformUI;
22

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

    
35
public class SaveImportedSpecimenAction extends Action {
36

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

    
39
    private Comparator<GbifResponse> comparator;
40

    
41
    /* (non-Javadoc)
42
     * @see org.eclipse.jface.action.Action#run()
43
     */
44
    @Override
45
    public void run() {
46
        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
47
        IWorkbenchPart activePart = window.getActivePage().getActivePart();
48
        if(activePart instanceof DataImportView<?>){
49
            DataImportView<?> dataImportView = (DataImportView<?>)activePart;
50
            Table table = dataImportView.getTable();
51
            Collection<GbifResponse> checkedResults = new HashSet<GbifResponse>();
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
                }
65
            }
66
            String[] tripleId ;
67
                for (GbifResponse response:checkedResults){
68
                	if (response != null){
69
                		tripleId = response.getTripleID();
70

    
71
                		if (response.getDataSetProtocol().equals(GbifDataSetProtocol.BIOCASE)){
72
                			biocaseResponses.add(response);
73

    
74
                		}else{
75
                			unitIdsGbif.add(tripleId);
76
                		}
77

    
78

    
79
                	}
80
                }
81
                Collections.sort(biocaseResponses, getComparator());
82
                URI dataSetUri = null;
83
                Abcd206ImportConfigurator configuratorAbcd;
84

    
85
                List<Abcd206ImportConfigurator> abcdConfigurators = new ArrayList<Abcd206ImportConfigurator>();
86
                DataSetResponse dataSetResponse;
87
                OccurenceQuery bioCaseOccurrenceQuery = null;
88
                for (GbifResponse response: biocaseResponses){
89
                	try {
90
						dataSetResponse = new GbifQueryServiceWrapper().queryOriginalDataSet(response);
91
						dataSetUri = dataSetResponse.getEndpoint();
92
					} catch (ClientProtocolException e) {
93
						// TODO Auto-generated catch block
94
						e.printStackTrace();
95
					} catch (IOException e) {
96
						// TODO Auto-generated catch block
97
						e.printStackTrace();
98
					}
99
                	if (!response.getDataSetUri().equals(dataSetUri)){
100
                		configuratorAbcd = Abcd206ImportConfigurator.NewInstance(dataSetUri, null, false);
101
                		configuratorAbcd.addMediaAsMediaSpecimen(true);
102
                		configuratorAbcd.setIgnoreAuthorship(true);
103
                		configuratorAbcd.setMoveNewTaxaToDefaultClassification(false);
104
                		if (configuratorAbcd != null){
105
                			abcdConfigurators.add(configuratorAbcd);
106
                		}
107
                		unitIdsBioCase = new HashSet<String[]>();
108
                		bioCaseOccurrenceQuery = new OccurenceQuery(unitIdsBioCase);
109
                		configuratorAbcd.setOccurenceQuery(bioCaseOccurrenceQuery);
110
                		if (dataImportView.getClassification() != null){
111
                			configuratorAbcd.setClassificationUuid(dataImportView.getClassification().getUuid());
112
                		}
113

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

    
119
                OccurenceQuery query = new OccurenceQuery(unitIdsGbif);
120
                GbifImportConfigurator configurator = GbifImportConfigurator.newInstance(query);
121
                configurator.setSourceReferenceTitle("Import Gbif data");
122

    
123

    
124

    
125
                //  configurator.setQuery(query);
126

    
127
               Job bioCaseJob = CdmStore.getImportManager().createIOServiceJob(abcdConfigurators);
128
               CdmStore.getImportManager().run(bioCaseJob);
129

    
130
               Job gbifJob = CdmStore.getImportManager().createIOServiceJob(configurator);
131
               CdmStore.getImportManager().run(gbifJob);
132

    
133

    
134

    
135
            }
136
        }
137

    
138

    
139
    private void persistTerm(DefinedTermBase<?> term, ITermService termService, ConversationHolder conversation){
140
        if(term!=null){
141
            //if the term does not exist in the DB save it
142
            if(termService.find(term.getUuid())==null){
143
                termService.saveOrUpdate(term);
144
            }
145
        }
146
    }
147

    
148
    /* (non-Javadoc)
149
     * @see org.eclipse.jface.action.Action#getText()
150
     */
151
    @Override
152
    public String getText() {
153
        return "Import";
154
    }
155

    
156
    private Comparator<GbifResponse> getComparator(){
157
    	if (comparator == null){
158
    		comparator = new GbifResponseComparator();
159
    	}
160
    	return comparator;
161
    }
162

    
163

    
164
}
(11-11/16)