4c2735fc52620dc6847b40af8def4416b37f497e
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / dataimport / DataImportEditorContextMenu.java
1 package eu.etaxonomy.taxeditor.editor.view.dataimport;
2
3 import java.io.IOException;
4
5 import org.apache.http.client.ClientProtocolException;
6 import org.apache.log4j.Logger;
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.Status;
10 import org.eclipse.core.runtime.jobs.Job;
11 import org.eclipse.jface.action.ContributionItem;
12 import org.eclipse.jface.action.IContributionItem;
13 import org.eclipse.jface.viewers.ISelection;
14 import org.eclipse.jface.viewers.StructuredSelection;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.widgets.Event;
17 import org.eclipse.swt.widgets.Listener;
18 import org.eclipse.swt.widgets.Menu;
19 import org.eclipse.swt.widgets.MenuItem;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.IWorkbenchWindow;
22 import org.eclipse.ui.PartInitException;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.actions.CompoundContributionItem;
25
26 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
27 import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
28 import eu.etaxonomy.cdm.ext.occurrence.gbif.DataSetResponse;
29 import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifDataSetProtocol;
30 import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifQueryServiceWrapper;
31 import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifResponse;
32 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
33 import eu.etaxonomy.taxeditor.editor.EditorUtil;
34 import eu.etaxonomy.taxeditor.model.AbstractUtility;
35 import eu.etaxonomy.taxeditor.store.CdmStore;
36
37 public class DataImportEditorContextMenu extends CompoundContributionItem {
38
39 private final Logger logger = Logger.getLogger(DataImportEditorContextMenu.class);
40
41 /* (non-Javadoc)
42 * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
43 */
44 @Override
45 protected IContributionItem[] getContributionItems() {
46 IContributionItem[] contributionItems = new IContributionItem[] {
47 new ContributionItem() {
48 @Override
49 public void fill(Menu menu, int index) {
50 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
51 final IEditorPart activeEditor = AbstractUtility.getActiveEditor();
52 final ISelection selection = window.getActivePage().getSelection();
53 if(selection instanceof StructuredSelection){
54 final StructuredSelection structuredSelection = (StructuredSelection)selection;
55
56 //Save Specimen
57 MenuItem saveSpecimenItem = new MenuItem(menu, SWT.NONE);
58 saveSpecimenItem.setText("Save Specimen");
59 saveSpecimenItem.addListener(SWT.Selection, new Listener() {
60
61 @Override
62 public void handleEvent(Event event) {
63 if(structuredSelection.iterator().hasNext()){
64 Object next = structuredSelection.iterator().next();
65 if(next instanceof GbifResponse){
66 //unwrap DerivedUnit from GbifResponse
67 next = ((GbifResponse) next).getDerivedUnitFacade().innerDerivedUnit();
68 }
69 if(next instanceof SpecimenOrObservationBase){
70 if(activeEditor instanceof DataImportEditor){
71 DataImportEditor<?> dataImportEditor = (DataImportEditor<?>)activeEditor;
72 CdmStore.getService(IOccurrenceService.class).saveOrUpdate((SpecimenOrObservationBase<?>) next);
73 dataImportEditor.getConversationHolder().commit(true);
74 }
75 }
76 }
77 }
78 });
79
80 //Query original data provider
81 if(structuredSelection.size()==1
82 && structuredSelection.iterator().next() instanceof GbifResponse
83 && ((GbifResponse)structuredSelection.iterator().next()).getDataSetProtocol()!=null
84 && ((GbifResponse)structuredSelection.iterator().next()).getDataSetProtocol()==GbifDataSetProtocol.BIOCASE){
85 final GbifResponse gbifResponse = (GbifResponse)structuredSelection.iterator().next();
86 MenuItem queryOriginalItem = new MenuItem(menu, SWT.NONE);
87 queryOriginalItem.setText("Query original data provider");
88 queryOriginalItem.addListener(SWT.Selection, new Listener() {
89
90 @Override
91 public void handleEvent(Event event) {
92 try{
93 DataSetResponse response = new GbifQueryServiceWrapper().queryOriginalDataSet(gbifResponse);
94 final BioCaseEditorInput input = new BioCaseEditorInput(new OccurenceQuery(response.getUnitId()), response.getEndpoint());
95 EditorUtil.open(input);
96 Job queryJob = new Job("Querying for original record...") {
97
98 @Override
99 protected IStatus run(IProgressMonitor monitor) {
100 input.query();
101 return Status.OK_STATUS;
102 }
103 };
104 queryJob.schedule();
105 } catch (ClientProtocolException e) {
106 logger.error("Query for single record failed.", e);
107 } catch (IOException e) {
108 logger.error("Query for single record failed.", e);
109 } catch (PartInitException e) {
110 logger.error("Could not open editor.", e);
111 }
112 }
113 });
114 }
115 }
116 }
117 }};
118 return contributionItems;
119 }
120
121 }