Merge branch 'develop' into wset
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / wizard / ExportToFileDestinationWizardPage.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The content of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.io.wizard;
11
12 import java.text.SimpleDateFormat;
13 import java.util.Calendar;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.UUID;
20
21 import org.eclipse.jface.wizard.WizardPage;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Combo;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.DirectoryDialog;
32 import org.eclipse.swt.widgets.Event;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Listener;
35 import org.eclipse.swt.widgets.Text;
36
37 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
38 import eu.etaxonomy.cdm.api.service.IClassificationService;
39 import eu.etaxonomy.cdm.filter.LogicFilter;
40 import eu.etaxonomy.cdm.io.cdmLight.CdmLightExportConfigurator;
41 import eu.etaxonomy.cdm.io.common.ExportConfiguratorBase;
42 import eu.etaxonomy.cdm.io.csv.caryophyllales.out.CsvNameExportConfigurator;
43 import eu.etaxonomy.cdm.io.csv.redlist.demo.CsvDemoExportConfigurator;
44 import eu.etaxonomy.cdm.io.dwca.out.DwcaTaxExportConfigurator;
45 import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
46 import eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator;
47 import eu.etaxonomy.cdm.io.tcsxml.out.TcsXmlExportConfigurator;
48 import eu.etaxonomy.cdm.model.taxon.Classification;
49 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
50 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
51 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
52 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
53 import eu.etaxonomy.taxeditor.store.CdmStore;
54
55 /**
56 * <p>
57 * ExportToFileDestinationWizardPage class.
58 * </p>
59 *
60 * @author n.hoffmann
61 * @created 15.06.2009
62 * @version 1.0
63 */
64 public class ExportToFileDestinationWizardPage extends WizardPage implements SelectionListener {
65
66 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
67 public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
68
69 /** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
70 public static final String JAXB_EXPORT = "JAXB_EXPORT";
71
72 /** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
73 public static final String TCS_EXPORT = "TCS_EXPORT";
74
75 /** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
76 public static final String SDD_EXPORT = "SDD_EXPORT";
77
78 public static final String DWCA_EXPORT = "DWCA_EXPORT";
79 public static final String OUTPUT_MODEL_EXPORT = "CDM_LIGHT(CSV)";
80 public static final String CSV_EXPORT = "CSV_EXPORT";
81 public static final String CSV_NAME_EXPORT = "CSV_NAME_EXPORT";
82 private static final String CSV_PRINT_EXPORT ="CSV_PRINT_EXPORT";
83 private static final String EXPORT_FOLDER ="EXPORT_FOLDER";
84 public static final String XML = "xml";
85
86 public static final String CSV = "csv";
87
88 public static final String ZIP = "zip";
89
90 private boolean csvExport = false;
91
92 private boolean csvNameExport = false;
93
94 private boolean csvPrintExport = false;
95
96 private boolean outputModelExport = false;
97 private boolean dwcaExport = false;
98
99 private DirectoryDialog folderDialog;
100 private Text text_exportFileName;
101
102 private Text text_folder;
103
104 private final String type;
105
106 private final String extension;
107
108 private Combo classificationSelectionCombo;
109
110 private List<Classification> classifications;
111
112 private Classification selectedClassification;
113
114 private Label classificationLabel;
115
116 private ExportConfiguratorBase configurator;
117 private Button checkUseSelectedtaxonNode;
118
119 private Button checkUseSelectedClassification;
120 private Button checkUseAllClassification;
121
122 protected ExportToFileDestinationWizardPage(String pageName, String type,
123 String title, String description, String extension, ExportConfiguratorBase configurator) {
124 super(pageName);
125 this.configurator = configurator;
126 this.type = type;
127 switch(type) {
128 case CSV_EXPORT :
129 csvExport = true;
130 break;
131 case CSV_NAME_EXPORT:
132 csvNameExport = true;
133 break;
134 case OUTPUT_MODEL_EXPORT:
135 outputModelExport = true;
136 break;
137 case CSV_PRINT_EXPORT:
138 csvPrintExport = true;
139 break;
140 case DWCA_EXPORT:
141 dwcaExport = true;
142 break;
143
144 }
145 this.extension = extension;
146 this.setTitle(title);
147 this.setDescription(description);
148 }
149
150 public static ExportToFileDestinationWizardPage Jaxb(JaxbExportConfigurator configurator) {
151 return new ExportToFileDestinationWizardPage(
152 JAXB_EXPORT,
153 "jaxb",
154 "JAXB Export",
155 "Exports the content of the currently selected database into the cdm jaxb format.",
156 XML, configurator);
157 }
158
159 public static ExportToFileDestinationWizardPage Tcs(TcsXmlExportConfigurator config) {
160 return new ExportToFileDestinationWizardPage(
161 TCS_EXPORT,
162 "tcs",
163 "Tcs Export",
164 "Export the content of the currently selected database into TCS format.",
165 XML, config);
166 }
167
168 public static ExportToFileDestinationWizardPage Sdd(SDDExportConfigurator config) {
169 return new ExportToFileDestinationWizardPage(
170 SDD_EXPORT,
171 "sdd",
172 "Sdd Export",
173 "Export the content of the currently selected database into SDD format.",
174 XML, config);
175 }
176
177 public static ExportToFileDestinationWizardPage Dwca(DwcaTaxExportConfigurator config) {
178 return new ExportToFileDestinationWizardPage(
179 DWCA_EXPORT,
180 DWCA_EXPORT,
181 "DwC-Archive Export",
182 "Export the content of the currently selected database into Darwin Core Archive format.",
183 ZIP, config);
184 }
185
186 public static ExportToFileDestinationWizardPage OutputModel(CdmLightExportConfigurator config) {
187
188 return new ExportToFileDestinationWizardPage(
189 OUTPUT_MODEL_EXPORT,
190 OUTPUT_MODEL_EXPORT,
191 "CDM Light Export (csv)",
192 "Export the content of the currently selected database into the CDM light (csv) format.",
193 CSV,config);
194 }
195
196 public static ExportToFileDestinationWizardPage Csv(CsvDemoExportConfigurator config) {
197
198 return new ExportToFileDestinationWizardPage(
199 CSV_EXPORT,
200 CSV_EXPORT,
201 "CSV Export",
202 "Export the content of the currently selected database into Comma Separated Value format.",
203 CSV, config);
204 }
205
206 public static ExportToFileDestinationWizardPage CsvNames(CsvNameExportConfigurator config) {
207
208 return new ExportToFileDestinationWizardPage(
209 CSV_NAME_EXPORT,
210 CSV_NAME_EXPORT,
211 "CSV Name Export",
212 "Export the names of the currently selected database into Semicolon Separated Value format.",
213 CSV, config);
214 }
215
216 public static ExportToFileDestinationWizardPage CsvPrint(CsvNameExportConfigurator config) {
217
218 return new ExportToFileDestinationWizardPage(
219 CSV_PRINT_EXPORT,
220 CSV_NAME_EXPORT,
221 "CSV Print Export",
222 "Export the content of the currently selected database into Semicolon Separated Value format.",
223 CSV, config);
224 }
225
226 /** {@inheritDoc} */
227 @Override
228 public void createControl(Composite parent) {
229
230 setPageComplete(false);
231
232 Composite composite = new Composite(parent, SWT.NONE);
233 GridLayout gridLayout = new GridLayout();
234
235 gridLayout.numColumns = 2;
236 TaxonNode node = null;
237
238 composite.setLayout(gridLayout);
239 if(classifications == null){
240 classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
241 Collections.sort(classifications, new Comparator<Classification>() {
242
243 @Override
244 public int compare(Classification o1, Classification o2) {
245 if (o1.equals(o2)){
246 return 0;
247 }
248 int result = o1.getTitleCache().compareTo(o2.getTitleCache());
249 if (result == 0){
250 return o1.getUuid().compareTo(o2.getUuid());
251 }
252 return result;
253 }
254 });
255 if (!configurator.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
256 selectedClassification = CdmStore.getService(IClassificationService.class).load(configurator.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
257 }else{
258
259 selectedClassification = classifications.iterator().next();
260 }
261 }
262
263
264 if(csvExport || csvPrintExport || outputModelExport|| csvNameExport || dwcaExport){
265
266 // Label comboBoxLabel = new Label(composite, SWT.NONE);
267 // comboBoxLabel.setText("Classification");
268 Composite selectNodeOrClassification = new Composite(composite, SWT.NONE);
269 GridLayout grid = new GridLayout();
270 grid.numColumns = 1;
271 selectNodeOrClassification.setLayout(grid);
272 if ((outputModelExport || dwcaExport)&& !configurator.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
273 checkUseSelectedtaxonNode= new Button(selectNodeOrClassification, SWT.RADIO);
274 String taxonStr = "";
275 int count = configurator.getTaxonNodeFilter().getSubtreeFilter().size();
276 for (LogicFilter<TaxonNode> filter: configurator.getTaxonNodeFilter().getSubtreeFilter()){
277 node = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(filter.getUuid());
278 count--;
279 if (node.hasTaxon()){
280 taxonStr += node.getTaxon().getName().getTitleCache();
281 if (count>0){
282 taxonStr += ", ";
283 }
284 }
285 }
286
287 checkUseSelectedtaxonNode.setText("Export selected subtree(s) ("+ taxonStr+")");
288 checkUseSelectedtaxonNode.addListener(SWT.Selection, new Listener() {
289 @Override
290 public void handleEvent(Event e) {
291 Button b = (Button) e.widget;
292 GridData data = (GridData) classificationSelectionCombo.getLayoutData();
293 data.exclude = b.getSelection();
294 classificationSelectionCombo.setVisible(!data.exclude);
295
296 }
297 });
298 checkUseSelectedtaxonNode.setSelection(true);
299 }
300 if(outputModelExport || dwcaExport){
301 if (dwcaExport){
302 checkUseAllClassification= new Button(selectNodeOrClassification, SWT.RADIO);
303 checkUseAllClassification.setText("Export all classifications");
304 checkUseAllClassification.addListener(SWT.Selection, new Listener() {
305 @Override
306 public void handleEvent(Event e) {
307 Button b = (Button) e.widget;
308 GridData data = (GridData) classificationSelectionCombo.getLayoutData();
309 data.exclude = b.getSelection();
310 classificationSelectionCombo.setVisible(!data.exclude);
311
312 }
313 });
314 }
315 checkUseSelectedClassification= new Button(selectNodeOrClassification, SWT.RADIO);
316 checkUseSelectedClassification.setText("Export selected classification");
317 checkUseSelectedClassification.addListener(SWT.Selection, new Listener() {
318 @Override
319 public void handleEvent(Event e) {
320 Button b = (Button) e.widget;
321 GridData data = (GridData) classificationSelectionCombo.getLayoutData();
322 data.exclude = b.getSelection();
323 classificationSelectionCombo.setVisible(data.exclude);
324
325 }
326 });
327 }
328
329 if (node!= null){
330 selectedClassification = node.getClassification();
331 }
332 createClassificationSelectionCombo(selectNodeOrClassification);
333
334
335
336 if (checkUseSelectedtaxonNode != null){
337
338 if (checkUseSelectedtaxonNode.getSelection()){
339 classificationSelectionCombo.setVisible(false);
340
341 }
342 }else{
343 classificationSelectionCombo.setVisible(true);
344 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
345 true, false, 2, 1));
346 }
347
348 }
349
350 Label folderLabel = new Label(composite, SWT.NONE);
351 folderLabel.setText("Select Folder for exported files");
352 folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
353 true, false, 2, 1));
354 folderDialog = new DirectoryDialog(parent.getShell());
355 folderDialog.setFilterPath(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
356
357 text_folder = new Text(composite, SWT.BORDER);
358 text_folder.setEditable(false);
359 text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
360 false));
361 if (PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER) != null){
362 text_folder.setText(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
363 setPageComplete(true);
364 }
365
366 Button button = new Button(composite, SWT.PUSH);
367 button.setText("Browse...");
368
369 button.addSelectionListener(new SelectionAdapter() {
370 @Override
371 public void widgetSelected(SelectionEvent e) {
372 super.widgetSelected(e);
373
374 String path = folderDialog.open();
375 if (path != null) { // a folder was selected
376 text_folder.setText(path);
377 PreferencesUtil.getPreferenceStore().setValue(EXPORT_FOLDER, path);
378 setPageComplete(true);
379 }
380 }
381 });
382 if (!outputModelExport){
383
384 Label fileLabel = new Label(composite, SWT.NONE);
385 fileLabel.setText("File");
386 text_exportFileName = new Text(composite, SWT.BORDER);
387 text_exportFileName.setText(generateFilename());
388 text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
389 true, false));
390 }
391 // make the composite the wizard pages control
392 setControl(composite);
393 }
394
395 protected String generateFilename() {
396 StringBuffer buffer = new StringBuffer();
397
398 Calendar cal = Calendar.getInstance();
399 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
400 buffer.append(sdf.format(cal.getTime()));
401
402 buffer.append("-");
403
404 buffer.append(type + "_export-");
405 try {
406 buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
407 } catch (CdmRemoteSourceException e) {
408 buffer.append("Unknown");
409 }
410
411 buffer.append(".");
412 buffer.append(extension);
413
414 return buffer.toString();
415 }
416
417 public String getExportFileName() {
418 return text_exportFileName.getText();
419 }
420
421 public String getFolderText() {
422 return text_folder.getText();
423 }
424
425 public Text getFolderComposite() {
426 return text_folder;
427 }
428
429 private void createClassificationSelectionCombo(Composite parent){
430
431 // Composite classificationSelection = new Composite(parent, SWT.NULL);
432 // classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
433
434 // GridLayout layout = new GridLayout();
435 // classificationSelection.setLayout(layout);
436 GridData gridData = new GridData();
437 gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
438 gridData.horizontalIndent = 5;
439 // classificationSelection.setLayoutData(gridData);
440
441 classificationSelectionCombo = new Combo(parent, SWT.BORDER| SWT.READ_ONLY);
442 classificationSelectionCombo.setLayoutData(gridData);
443 for(Classification tree : classifications){
444 classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
445
446 }
447
448 classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
449
450 // TODO remember last selection
451 classificationSelectionCombo.addSelectionListener(this);
452
453
454
455 //return classificationSelection;
456 }
457
458 public Combo getCombo(){
459 return classificationSelectionCombo;
460 }
461
462 @Override
463 public void widgetSelected(SelectionEvent e) {
464 selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
465
466 }
467
468 @Override
469 public void widgetDefaultSelected(SelectionEvent e) {
470 //not needed here
471
472 }
473
474 public UUID getSelectedClassificationUUID() {
475
476 return selectedClassification.getUuid();
477 }
478
479 public boolean getCheckUseSelectedTaxonNode() {
480 if (checkUseSelectedtaxonNode== null){
481 return false;
482 }
483 return checkUseSelectedtaxonNode.getSelection();
484 }
485 public boolean getCheckUseAllClassifications() {
486 if (checkUseAllClassification== null){
487 return false;
488 }
489 return checkUseAllClassification.getSelection();
490 }
491
492 public Set<UUID> getAllClassificationUuids(){
493 Set<UUID> allClassificationUuids = new HashSet();
494 for (Classification classification: this.classifications){
495 allClassificationUuids.add(classification.getUuid());
496 }
497 return allClassificationUuids;
498 }
499
500 @Override
501 public boolean canFlipToNextPage() {
502 return getFolderText() != null;
503 }
504
505 }