- updated SpecimenImportWizard
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / dataimport / DerivedUnitEditorInput.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.dataimport;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.net.URISyntaxException;
15 import java.util.List;
16
17 import org.apache.http.client.ClientProtocolException;
18 import org.apache.log4j.Logger;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.ui.IEditorInput;
26 import org.eclipse.ui.IPersistableElement;
27
28 import eu.etaxonomy.cdm.ext.biocase.BioCaseQuery;
29 import eu.etaxonomy.cdm.ext.biocase.BioCaseQueryServiceWrapper;
30 import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
31 import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
32 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
33 import eu.etaxonomy.taxeditor.dataimport.transientServices.TransientCdmRepository;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35
36 /**
37 * @author pplitzner
38 * @date 20.09.2013
39 *
40 */
41 public class DerivedUnitEditorInput implements IEditorInput {
42
43 private static Logger logger = Logger.getLogger(DerivedUnitEditorInput.class);
44
45 private final BioCaseQuery query;
46
47 private List<SpecimenOrObservationBase> results;
48
49 /**
50 * @param results
51 */
52 public DerivedUnitEditorInput(BioCaseQuery query) {
53 this.query = query;
54 Display.getCurrent().asyncExec(new Runnable() {
55
56 @Override
57 public void run() {
58 String errorMessage = "Could not execute query " + DerivedUnitEditorInput.this.query;
59 try {
60 InputStream resultStream = new BioCaseQueryServiceWrapper().query(DerivedUnitEditorInput.this.query);
61 Abcd206ImportConfigurator configurator = Abcd206ImportConfigurator.NewInstance(resultStream, null, false);
62 TransientCdmRepository repo =
63 new TransientCdmRepository(CdmStore.getCurrentApplicationConfiguration());
64 configurator.setCdmAppController(repo);
65
66 CdmDefaultImport<Abcd206ImportConfigurator> importer = new CdmDefaultImport<Abcd206ImportConfigurator>();
67 importer.invoke(configurator);
68 results = repo.getUnits();
69 // Abcd206Import abcd206Import = new Abcd206Import();
70 // Abcd206ImportState state = new Abcd206ImportState(configurator);
71 // abcd206Import.invoke(state);
72 // state.countTrees();
73 } catch (ClientProtocolException e) {
74 logger.error(errorMessage, e);
75 } catch (IOException e) {
76 logger.error(errorMessage, e);
77 } catch (URISyntaxException e) {
78 logger.error(errorMessage, e);
79 }
80 // return new BioCaseQueryServiceWrapper().dummyData();
81 }
82 });
83 }
84
85 public void query(){
86 Job queryJob = new Job("Query Biocase") {
87
88 @Override
89 protected IStatus run(IProgressMonitor monitor) {
90 String errorMessage = "Could not execute query " + DerivedUnitEditorInput.this.query;
91 try {
92 InputStream resultStream = new BioCaseQueryServiceWrapper().query(DerivedUnitEditorInput.this.query);
93 Abcd206ImportConfigurator configurator = Abcd206ImportConfigurator.NewInstance(resultStream, null, false);
94 TransientCdmRepository repo =
95 new TransientCdmRepository(CdmStore.getCurrentApplicationConfiguration());
96 configurator.setCdmAppController(repo);
97
98 CdmDefaultImport<Abcd206ImportConfigurator> importer = new CdmDefaultImport<Abcd206ImportConfigurator>();
99 importer.invoke(configurator);
100 results = repo.getUnits();
101 // Abcd206Import abcd206Import = new Abcd206Import();
102 // Abcd206ImportState state = new Abcd206ImportState(configurator);
103 // abcd206Import.invoke(state);
104 // state.countTrees();
105 } catch (ClientProtocolException e) {
106 logger.error(errorMessage, e);
107 } catch (IOException e) {
108 logger.error(errorMessage, e);
109 } catch (URISyntaxException e) {
110 logger.error(errorMessage, e);
111 }
112
113 Display.getDefault().asyncExec(new Runnable() {
114
115 @Override
116 public void run() {
117 CdmStore.getContextManager().notifyContextRefresh();
118 }
119 });
120
121 return Status.OK_STATUS;
122 }
123 };
124 }
125
126 /* (non-Javadoc)
127 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
128 */
129 @Override
130 public Object getAdapter(Class adapter) {
131 // TODO Auto-generated method stub
132 return null;
133 }
134
135 /* (non-Javadoc)
136 * @see org.eclipse.ui.IEditorInput#exists()
137 */
138 @Override
139 public boolean exists() {
140 return false;
141 }
142
143 /* (non-Javadoc)
144 * @see org.eclipse.ui.IEditorInput#getImageDescriptor()
145 */
146 @Override
147 public ImageDescriptor getImageDescriptor() {
148 // TODO Auto-generated method stub
149 return null;
150 }
151
152 /* (non-Javadoc)
153 * @see org.eclipse.ui.IEditorInput#getName()
154 */
155 @Override
156 public String getName() {
157 return query.toString();
158 }
159
160 /* (non-Javadoc)
161 * @see org.eclipse.ui.IEditorInput#getPersistable()
162 */
163 @Override
164 public IPersistableElement getPersistable() {
165 return null;
166 }
167
168 /* (non-Javadoc)
169 * @see org.eclipse.ui.IEditorInput#getToolTipText()
170 */
171 @Override
172 public String getToolTipText() {
173 return query.toString();
174 }
175
176 /**
177 * @return the results
178 */
179 public List<SpecimenOrObservationBase> getResults() {
180 return results;
181 }
182
183 /* (non-Javadoc)
184 * @see java.lang.Object#hashCode()
185 */
186 @Override
187 public int hashCode() {
188 final int prime = 31;
189 int result = 1;
190 result = prime * result + ((query == null) ? 0 : query.hashCode());
191 return result;
192 }
193
194 /* (non-Javadoc)
195 * @see java.lang.Object#equals(java.lang.Object)
196 */
197 @Override
198 public boolean equals(Object obj) {
199 if (this == obj) {
200 return true;
201 }
202 if (obj == null) {
203 return false;
204 }
205 if (getClass() != obj.getClass()) {
206 return false;
207 }
208 DerivedUnitEditorInput other = (DerivedUnitEditorInput) obj;
209 if (query == null) {
210 if (other.query != null) {
211 return false;
212 }
213 } else if (!query.equals(other.query)) {
214 return false;
215 }
216 return true;
217 }
218
219 }