cleanup
[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.HashSet;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.eclipse.jface.layout.GridLayoutFactory;
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.metadata.PreferencePredicate;
49 import eu.etaxonomy.cdm.model.taxon.Classification;
50 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
51 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByNameComparator;
52 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoByRankAndNameComparator;
53 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDtoNaturalComparator;
54 import eu.etaxonomy.taxeditor.preference.NavigatorOrderEnum;
55 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
56 import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
57 import eu.etaxonomy.taxeditor.store.CdmStore;
58
59 /**
60 * ExportToFileDestinationWizardPage class.
61 *
62 * @author n.hoffmann
63 * @created 15.06.2009
64 */
65 public class ExportToFileDestinationWizardPage extends WizardPage implements SelectionListener {
66
67 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
68 public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
69
70 /** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
71 public static final String JAXB_EXPORT = "JAXB_EXPORT";
72
73 /** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
74 public static final String TCS_EXPORT = "TCS_EXPORT";
75
76 /** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
77 public static final String SDD_EXPORT = "SDD_EXPORT";
78
79 public static final String DWCA_EXPORT = "DWCA_EXPORT";
80 public static final String CDM_LIGHT_EXPORT = "CDM_LIGHT(CSV)";
81 public static final String CSV_EXPORT = "CSV_EXPORT";
82 public static final String CSV_NAME_EXPORT = "CSV_NAME_EXPORT";
83 private static final String CSV_PRINT_EXPORT ="CSV_PRINT_EXPORT";
84 private static final String EXPORT_FOLDER ="EXPORT_FOLDER";
85 public static final String XML = "xml";
86
87 public static final String CSV = "csv";
88
89 public static final String ZIP = "zip";
90
91 private boolean csvExport = false;
92
93 private boolean csvNameExport = false;
94
95 private boolean csvPrintExport = false;
96
97 private boolean cdmLightExport = false;
98 private boolean dwcaExport = false;
99
100 private DirectoryDialog folderDialog;
101 private Text text_exportFileName;
102
103 private Text text_folder;
104
105 private final String type;
106
107 private final String extension;
108
109 private Combo classificationSelectionCombo;
110
111 private List<Classification> classifications;
112
113 private Classification selectedClassification;
114
115 private Label classificationLabel;
116
117 private ExportConfiguratorBase configurator;
118 private Button checkUseSelectedtaxonNode;
119
120 private Button checkUseSelectedClassification;
121 private Button checkUseAllClassification;
122
123 private Button checkExportUnpublished;
124 private Button checkExportFactualData;
125 Combo orderSelectionCombo;
126
127
128 protected ExportToFileDestinationWizardPage(String pageName, String type,
129 String title, String description, String extension, ExportConfiguratorBase configurator) {
130 super(pageName);
131 this.configurator = configurator;
132 this.type = type;
133 switch(type) {
134 case CSV_EXPORT :
135 csvExport = true;
136 break;
137 case CSV_NAME_EXPORT:
138 csvNameExport = true;
139 break;
140 case CDM_LIGHT_EXPORT:
141 cdmLightExport = true;
142 break;
143 case CSV_PRINT_EXPORT:
144 csvPrintExport = true;
145 break;
146 case DWCA_EXPORT:
147 dwcaExport = true;
148 break;
149 }
150 this.extension = extension;
151 this.setTitle(title);
152 this.setDescription(description);
153 }
154
155 public static ExportToFileDestinationWizardPage Jaxb(JaxbExportConfigurator configurator) {
156 return new ExportToFileDestinationWizardPage(
157 JAXB_EXPORT,
158 "jaxb",
159 "JAXB Export",
160 "Exports the content of the currently selected database into the cdm jaxb format.",
161 XML, configurator);
162 }
163
164 public static ExportToFileDestinationWizardPage Tcs(TcsXmlExportConfigurator config) {
165 return new ExportToFileDestinationWizardPage(
166 TCS_EXPORT,
167 "tcs",
168 "Tcs Export",
169 "Export the content of the currently selected database into TCS format.",
170 XML, config);
171 }
172
173 public static ExportToFileDestinationWizardPage Sdd(SDDExportConfigurator config) {
174 return new ExportToFileDestinationWizardPage(
175 SDD_EXPORT,
176 "sdd",
177 "Sdd Export",
178 "Export the content of the currently selected database into SDD format.",
179 XML, config);
180 }
181
182 public static ExportToFileDestinationWizardPage Dwca(DwcaTaxExportConfigurator config) {
183 return new ExportToFileDestinationWizardPage(
184 DWCA_EXPORT,
185 DWCA_EXPORT,
186 "DwC-Archive Export",
187 "Export the content of the currently selected database into Darwin Core Archive format.",
188 ZIP, config);
189 }
190
191 public static ExportToFileDestinationWizardPage OutputModel(CdmLightExportConfigurator config) {
192
193 return new ExportToFileDestinationWizardPage(
194 CDM_LIGHT_EXPORT,
195 CDM_LIGHT_EXPORT,
196 "CDM Light Export (csv)",
197 "Export the content of the currently selected database into the CDM light (csv) format.",
198 CSV,config);
199 }
200
201 public static ExportToFileDestinationWizardPage Csv(CsvDemoExportConfigurator config) {
202
203 return new ExportToFileDestinationWizardPage(
204 CSV_EXPORT,
205 CSV_EXPORT,
206 "CSV Export",
207 "Export the content of the currently selected database into Comma Separated Value format.",
208 CSV, config);
209 }
210
211 public static ExportToFileDestinationWizardPage CsvNames(CsvNameExportConfigurator config) {
212
213 return new ExportToFileDestinationWizardPage(
214 CSV_NAME_EXPORT,
215 CSV_NAME_EXPORT,
216 "CSV Name Export",
217 "Export the names of the currently selected database into Semicolon Separated Value format.",
218 CSV, config);
219 }
220
221 public static ExportToFileDestinationWizardPage CsvPrint(CsvNameExportConfigurator config) {
222
223 return new ExportToFileDestinationWizardPage(
224 CSV_PRINT_EXPORT,
225 CSV_NAME_EXPORT,
226 "CSV Print Export",
227 "Export the content of the currently selected database into Semicolon Separated Value format.",
228 CSV, config);
229 }
230
231 /** {@inheritDoc} */
232 @Override
233 public void createControl(Composite parent) {
234
235 setPageComplete(false);
236
237 Composite composite = new Composite(parent, SWT.NONE);
238 GridLayout gridLayout = new GridLayout();
239
240 gridLayout.numColumns = 2;
241 TaxonNode node = null;
242
243 composite.setLayout(gridLayout);
244 if (cdmLightExport || dwcaExport){
245 checkExportUnpublished = new Button(composite, SWT.CHECK);
246 checkExportUnpublished.setText("Export unpublished taxa");
247 checkExportFactualData = new Button(composite, SWT.CHECK);
248 checkExportFactualData.setText("Export factual data");
249 checkExportFactualData.setSelection(true);
250 @SuppressWarnings("unused")
251 Label nope = new Label(composite, SWT.NONE);
252
253 Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
254 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
255 gridData.horizontalSpan = 2;
256 separator.setLayoutData(gridData);
257 }
258 if(classifications == null){
259 classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
260 Collections.sort(classifications, (o1,o2) -> {
261 if (o1.equals(o2)){
262 return 0;
263 }
264 int result = o1.getTitleCache().compareTo(o2.getTitleCache());
265 if (result == 0){
266 return o1.getUuid().compareTo(o2.getUuid());
267 }
268 return result;
269 });
270 if (!configurator.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
271 selectedClassification = CdmStore.getService(IClassificationService.class).load(configurator.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
272 }else{
273
274 selectedClassification = classifications.iterator().next();
275 }
276 }
277
278
279 if(csvExport || csvPrintExport || cdmLightExport|| csvNameExport || dwcaExport){
280
281 // Label comboBoxLabel = new Label(composite, SWT.NONE);
282 // comboBoxLabel.setText("Classification");
283 Composite selectNodeOrClassification = new Composite(composite, SWT.NONE);
284 GridLayout grid = new GridLayout();
285 grid.numColumns = 1;
286 selectNodeOrClassification.setLayout(grid);
287 if ((cdmLightExport || dwcaExport)&& !configurator.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
288 checkUseSelectedtaxonNode= new Button(selectNodeOrClassification, SWT.RADIO);
289 String taxonStr = "";
290 int count = configurator.getTaxonNodeFilter().getSubtreeFilter().size();
291 for (LogicFilter<TaxonNode> filter: configurator.getTaxonNodeFilter().getSubtreeFilter()){
292 node = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(filter.getUuid());
293 count--;
294 if (node.hasTaxon()){
295 taxonStr += node.getTaxon().getName().getTitleCache();
296 if (count>0){
297 taxonStr += ", ";
298 }
299 }
300 }
301
302 checkUseSelectedtaxonNode.setText("Export selected subtree(s) ("+ taxonStr+")");
303 checkUseSelectedtaxonNode.addListener(SWT.Selection, new Listener() {
304 @Override
305 public void handleEvent(Event e) {
306 Button b = (Button) e.widget;
307 GridData data = (GridData) classificationSelectionCombo.getLayoutData();
308 data.exclude = b.getSelection();
309 classificationSelectionCombo.setEnabled(!data.exclude);
310
311 }
312 });
313 checkUseSelectedtaxonNode.setSelection(true);
314 }
315 if(cdmLightExport || dwcaExport){
316 if (dwcaExport){
317 checkUseAllClassification= new Button(selectNodeOrClassification, SWT.RADIO);
318 checkUseAllClassification.setText("Export all classifications");
319 checkUseAllClassification.addListener(SWT.Selection, new Listener() {
320 @Override
321 public void handleEvent(Event e) {
322 Button b = (Button) e.widget;
323 GridData data = (GridData) classificationSelectionCombo.getLayoutData();
324 data.exclude = b.getSelection();
325 classificationSelectionCombo.setEnabled(!data.exclude);
326
327 }
328 });
329 }
330 checkUseSelectedClassification= new Button(selectNodeOrClassification, SWT.RADIO);
331 checkUseSelectedClassification.setText("Export selected classification");
332 checkUseSelectedClassification.addListener(SWT.Selection, new Listener() {
333 @Override
334 public void handleEvent(Event e) {
335 Button b = (Button) e.widget;
336 GridData data = (GridData) classificationSelectionCombo.getLayoutData();
337 data.exclude = b.getSelection();
338 classificationSelectionCombo.setEnabled(data.exclude);
339
340 }
341 });
342 }
343
344 if (node!= null){
345 selectedClassification = node.getClassification();
346 }
347 createClassificationSelectionCombo(selectNodeOrClassification);
348 if (cdmLightExport){
349 Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
350 GridData gridDataSep = new GridData();
351 gridDataSep = new GridData(GridData.BEGINNING, GridData.CENTER, true, true);
352 gridDataSep.horizontalSpan = 2;
353 separator.setLayoutData(gridDataSep);
354
355 Composite comp = new Composite(composite, SWT.NONE);
356
357 GridData gridData = new GridData();
358 gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
359 gridData.horizontalSpan = 2;
360 comp.setLayoutData(gridData);
361
362 Label label = new Label(comp, SWT.NONE);
363 label.setText("Sorting of taxa");
364
365 orderSelectionCombo = new Combo(comp, SWT.BORDER| SWT.READ_ONLY);
366
367 GridLayoutFactory.fillDefaults().applyTo(comp);
368 for(NavigatorOrderEnum display: NavigatorOrderEnum.values()){
369 orderSelectionCombo.add(display.getLabel());
370 }
371
372 int index = 0;
373 if (PreferencesUtil.getStringValue(PreferencePredicate.TaxonNodeOrder.getKey()) != null){
374 NavigatorOrderEnum orderInNavigator = NavigatorOrderEnum.valueOf(PreferencesUtil.getStringValue(PreferencePredicate.TaxonNodeOrder.getKey()));
375
376 for (String itemLabel : orderSelectionCombo.getItems()){
377 if (itemLabel.equalsIgnoreCase(orderInNavigator.getLabel())){
378 orderSelectionCombo.select(index);
379 setComparatorToConfig();
380 break;
381 }
382 index++;
383 }
384 }
385
386 orderSelectionCombo.addSelectionListener(new SelectionListener() {
387
388 @Override
389 public void widgetSelected(SelectionEvent e) {
390 setComparatorToConfig();
391 }
392
393 @Override
394 public void widgetDefaultSelected(SelectionEvent e) {
395 // TODO Auto-generated method stub
396 }
397 });
398 }
399
400 if (checkUseSelectedtaxonNode != null){
401 if (checkUseSelectedtaxonNode.getSelection()){
402 classificationSelectionCombo.setEnabled(false);
403 }
404 }else{
405 classificationSelectionCombo.setEnabled(true);
406 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
407 true, false, 2, 1));
408 }
409 Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
410 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
411 gridData.horizontalSpan = 2;
412 separator.setLayoutData(gridData);
413
414 }
415
416 Label folderLabel = new Label(composite, SWT.NONE);
417 folderLabel.setText("Select Folder for exported files");
418 folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
419 true, false, 2, 1));
420 folderDialog = new DirectoryDialog(parent.getShell());
421 String exportFolder = PreferencesUtil.getStringValue(EXPORT_FOLDER, true);
422 folderDialog.setFilterPath(exportFolder);
423
424 text_folder = new Text(composite, SWT.BORDER);
425 text_folder.setEditable(false);
426 text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
427 false));
428 if (exportFolder != null){
429 text_folder.setText(exportFolder);
430 setPageComplete(true);
431 }
432
433 Button button = new Button(composite, SWT.PUSH);
434 button.setText("Browse...");
435
436 button.addSelectionListener(new SelectionAdapter() {
437 @Override
438 public void widgetSelected(SelectionEvent e) {
439 super.widgetSelected(e);
440
441 String path = folderDialog.open();
442 if (path != null) { // a folder was selected
443 text_folder.setText(path);
444 PreferencesUtil.setStringValue(EXPORT_FOLDER, path);
445 setPageComplete(true);
446 }
447 }
448 });
449 if (!cdmLightExport){
450
451 Label fileLabel = new Label(composite, SWT.NONE);
452 fileLabel.setText("File");
453 text_exportFileName = new Text(composite, SWT.BORDER);
454 text_exportFileName.setText(generateFilename());
455 text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
456 true, false));
457 }
458
459 // make the composite the wizard pages control
460 setControl(composite);
461 }
462
463 protected void setComparatorToConfig() {
464 if (orderSelectionCombo.getText().equals(NavigatorOrderEnum.NaturalOrder.getLabel())){
465 ((CdmLightExportConfigurator)configurator).setTaxonNodeComparator(new TaxonNodeDtoNaturalComparator());
466 } else if (orderSelectionCombo.getText().equals(NavigatorOrderEnum.AlphabeticalOrder.getLabel())){
467 ((CdmLightExportConfigurator)configurator).setTaxonNodeComparator(new TaxonNodeDtoByNameComparator());
468 }else {
469 ((CdmLightExportConfigurator)configurator).setTaxonNodeComparator(new TaxonNodeDtoByRankAndNameComparator());
470 }
471 }
472
473 protected String generateFilename() {
474 StringBuffer buffer = new StringBuffer();
475
476 Calendar cal = Calendar.getInstance();
477 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
478 buffer.append(sdf.format(cal.getTime()));
479
480 buffer.append("-");
481 buffer.append(type +"_");
482
483 String source = ((ICdmRemoteSource)CdmStore.getActiveCdmSource()).toString();
484 source = source.substring(source.indexOf("/")+1);
485 buffer.append(source);
486
487 buffer.append(".");
488 buffer.append(extension);
489
490 return buffer.toString();
491 }
492
493 public String getExportFileName() {
494 return text_exportFileName.getText();
495 }
496
497 public String getFolderText() {
498 return text_folder.getText();
499 }
500
501 public Text getFolderComposite() {
502 return text_folder;
503 }
504
505 public boolean isExportUnpublishedData(){
506 return checkExportUnpublished.getSelection();
507 }
508
509 public boolean isExportFactualData(){
510 return checkExportFactualData.getSelection();
511 }
512
513 private void createClassificationSelectionCombo(Composite parent){
514
515 // Composite classificationSelection = new Composite(parent, SWT.NULL);
516 // classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
517
518 // GridLayout layout = new GridLayout();
519 // classificationSelection.setLayout(layout);
520 GridData gridData = new GridData();
521 gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
522 gridData.horizontalIndent = 5;
523 // classificationSelection.setLayoutData(gridData);
524
525 classificationSelectionCombo = new Combo(parent, SWT.BORDER| SWT.READ_ONLY);
526 classificationSelectionCombo.setLayoutData(gridData);
527 for(Classification tree : classifications){
528 classificationSelectionCombo.add(tree.getTitleCache(), classifications.indexOf(tree));
529 }
530
531 classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
532
533 // TODO remember last selection
534 classificationSelectionCombo.addSelectionListener(this);
535
536 //return classificationSelection;
537 }
538
539 public Combo getCombo(){
540 return classificationSelectionCombo;
541 }
542
543 @Override
544 public void widgetSelected(SelectionEvent e) {
545 selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
546 }
547
548 public Classification getSelectedClassification(){
549 return selectedClassification;
550 }
551
552 @Override
553 public void widgetDefaultSelected(SelectionEvent e) {
554 //not needed here
555 }
556
557 public UUID getSelectedClassificationUUID() {
558 return selectedClassification.getUuid();
559 }
560
561 public boolean getCheckUseSelectedTaxonNode() {
562 if (checkUseSelectedtaxonNode== null){
563 return false;
564 }
565 return checkUseSelectedtaxonNode.getSelection();
566 }
567 public boolean getCheckUseAllClassifications() {
568 if (checkUseAllClassification== null){
569 return false;
570 }
571 return checkUseAllClassification.getSelection();
572 }
573
574 public Set<UUID> getAllClassificationUuids(){
575 Set<UUID> allClassificationUuids = new HashSet<>();
576 for (Classification classification: this.classifications){
577 allClassificationUuids.add(classification.getUuid());
578 }
579 return allClassificationUuids;
580 }
581
582 @Override
583 public boolean canFlipToNextPage() {
584 return getFolderText() != null;
585 }
586 }