2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.io
.wizard
;
11 import java
.text
.SimpleDateFormat
;
12 import java
.util
.Calendar
;
13 import java
.util
.Collections
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.eclipse
.jface
.layout
.GridLayoutFactory
;
20 import org
.eclipse
.jface
.wizard
.WizardPage
;
21 import org
.eclipse
.swt
.SWT
;
22 import org
.eclipse
.swt
.events
.SelectionAdapter
;
23 import org
.eclipse
.swt
.events
.SelectionEvent
;
24 import org
.eclipse
.swt
.events
.SelectionListener
;
25 import org
.eclipse
.swt
.layout
.GridData
;
26 import org
.eclipse
.swt
.layout
.GridLayout
;
27 import org
.eclipse
.swt
.widgets
.Button
;
28 import org
.eclipse
.swt
.widgets
.Combo
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.DirectoryDialog
;
31 import org
.eclipse
.swt
.widgets
.Event
;
32 import org
.eclipse
.swt
.widgets
.Label
;
33 import org
.eclipse
.swt
.widgets
.Listener
;
34 import org
.eclipse
.swt
.widgets
.Text
;
36 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
37 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
38 import eu
.etaxonomy
.cdm
.filter
.LogicFilter
;
39 import eu
.etaxonomy
.cdm
.io
.cdmLight
.CdmLightExportConfigurator
;
40 import eu
.etaxonomy
.cdm
.io
.coldp
.ColDpExportConfigurator
;
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
.io
.wfo
.out
.WfoExportConfigurator
;
49 import eu
.etaxonomy
.cdm
.model
.metadata
.PreferencePredicate
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
52 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDtoByNameComparator
;
53 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDtoByRankAndNameComparator
;
54 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDtoNaturalComparator
;
55 import eu
.etaxonomy
.taxeditor
.preference
.NavigatorOrderEnum
;
56 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
57 import eu
.etaxonomy
.taxeditor
.remoting
.source
.ICdmRemoteSource
;
58 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
61 * ExportToFileDestinationWizardPage class.
66 public class ExportToFileDestinationWizardPage
extends WizardPage
implements SelectionListener
{
68 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
69 public static final String DATE_FORMAT_NOW
= "yyyyMMddHHmm";
71 /** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
72 public static final String JAXB_EXPORT
= "JAXB_EXPORT";
74 /** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
75 public static final String TCS_EXPORT
= "TCS_EXPORT";
77 /** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
78 public static final String SDD_EXPORT
= "SDD_EXPORT";
80 public static final String DWCA_EXPORT
= "DWCA_EXPORT";
81 public static final String WFO_EXPORT
= "WFO_EXPORT";
82 public static final String CDM_LIGHT_EXPORT
= "CDM_LIGHT(CSV)";
83 public static final String COL_DP_EXPORT
= "COL DP";
84 public static final String CSV_EXPORT
= "CSV_EXPORT";
85 public static final String CSV_NAME_EXPORT
= "CSV_NAME_EXPORT";
86 private static final String CSV_PRINT_EXPORT
= "CSV_PRINT_EXPORT";
87 private static final String EXPORT_FOLDER
= "EXPORT_FOLDER";
88 public static final String XML
= "xml";
90 public static final String CSV
= "csv";
92 public static final String ZIP
= "zip";
94 private boolean csvExport
= false;
96 private boolean csvNameExport
= false;
98 private boolean csvPrintExport
= false;
100 private boolean cdmLightExport
= false;
102 private boolean colDPExport
= false;
103 private boolean dwcaExport
= false;
104 private boolean wfoExport
= false;
106 private DirectoryDialog folderDialog
;
107 private Text text_exportFileName
;
109 private Text text_folder
;
111 private final String type
;
113 private final String extension
;
115 private Combo classificationSelectionCombo
;
117 private List
<Classification
> classifications
;
119 private Classification selectedClassification
;
121 private ExportConfiguratorBase
<?
, ?
, ?
> configurator
;
122 private Button checkUseSelectedTaxonNode
;
124 private Button checkUseSelectedClassification
;
125 private Button checkUseAllClassification
;
127 private Button checkExportUnpublished
;
128 private Button checkExportFactualData
;
129 private Button checkExportUnpublishedFactualData
;
131 private Combo orderSelectionCombo
;
133 protected ExportToFileDestinationWizardPage(String pageName
, String type
, String title
, String description
,
134 String extension
, ExportConfiguratorBase
<?
, ?
, ?
> configurator
) {
136 this.configurator
= configurator
;
142 case CSV_NAME_EXPORT
:
143 csvNameExport
= true;
145 case CDM_LIGHT_EXPORT
:
146 cdmLightExport
= true;
151 case CSV_PRINT_EXPORT
:
152 csvPrintExport
= true;
161 throw new RuntimeException("Export type not yet handeled: " + type
);
163 this.extension
= extension
;
164 this.setTitle(title
);
165 this.setDescription(description
);
168 public static ExportToFileDestinationWizardPage
Jaxb(JaxbExportConfigurator configurator
) {
169 return new ExportToFileDestinationWizardPage(JAXB_EXPORT
, "jaxb", "JAXB Export",
170 "Exports the content of the currently selected database into the cdm jaxb format.", XML
, configurator
);
173 public static ExportToFileDestinationWizardPage
Tcs(TcsXmlExportConfigurator config
) {
174 return new ExportToFileDestinationWizardPage(TCS_EXPORT
, "tcs", "Tcs Export",
175 "Export the content of the currently selected database into TCS format.", XML
, config
);
178 public static ExportToFileDestinationWizardPage
Sdd(SDDExportConfigurator config
) {
179 return new ExportToFileDestinationWizardPage(SDD_EXPORT
, "sdd", "Sdd Export",
180 "Export the content of the currently selected database into SDD format.", XML
, config
);
183 public static ExportToFileDestinationWizardPage
Dwca(DwcaTaxExportConfigurator config
) {
184 return new ExportToFileDestinationWizardPage(DWCA_EXPORT
, DWCA_EXPORT
, "DwC-Archive Export",
185 "Export the content of the currently selected database into Darwin Core Archive format.", ZIP
, config
);
187 public static ExportToFileDestinationWizardPage
WFO(WfoExportConfigurator config
) {
188 return new ExportToFileDestinationWizardPage(WFO_EXPORT
, WFO_EXPORT
, "WFO DwC-Archive Classification Export",
189 "Export the content of the currently selected database into WFO "
190 + "Darwin Core Archive format.", ZIP
, config
);
193 public static ExportToFileDestinationWizardPage
CdmLight(CdmLightExportConfigurator config
) {
195 return new ExportToFileDestinationWizardPage(CDM_LIGHT_EXPORT
, CDM_LIGHT_EXPORT
, "CDM Light Export (csv)",
196 "Export the content of the currently selected database into the CDM light (csv) format.", CSV
, config
);
199 public static ExportToFileDestinationWizardPage
ColDp(ColDpExportConfigurator config
) {
201 return new ExportToFileDestinationWizardPage(COL_DP_EXPORT
, COL_DP_EXPORT
, "Col DP Export (zip)",
202 "Export the content of the currently selected database into the Catalogue of Life Data Package (Col DP) format.", ZIP
, config
);
205 public static ExportToFileDestinationWizardPage
Csv(CsvDemoExportConfigurator config
) {
207 return new ExportToFileDestinationWizardPage(CSV_EXPORT
, CSV_EXPORT
, "CSV Export",
208 "Export the content of the currently selected database into Comma Separated Value format.", CSV
,
212 public static ExportToFileDestinationWizardPage
CsvNames(CsvNameExportConfigurator config
) {
214 return new ExportToFileDestinationWizardPage(CSV_NAME_EXPORT
, CSV_NAME_EXPORT
, "CSV Name Export",
215 "Export the names of the currently selected database into Semicolon Separated Value format.", CSV
,
219 public static ExportToFileDestinationWizardPage
CsvPrint(CsvNameExportConfigurator config
) {
221 return new ExportToFileDestinationWizardPage(CSV_PRINT_EXPORT
, CSV_NAME_EXPORT
, "CSV Print Export",
222 "Export the content of the currently selected database into Semicolon Separated Value format.", CSV
,
228 public void createControl(Composite parent
) {
230 setPageComplete(false);
232 Composite composite
= new Composite(parent
, SWT
.NONE
);
233 GridLayout gridLayout
= new GridLayout();
235 gridLayout
.numColumns
= 2;
236 TaxonNode node
= null;
238 composite
.setLayout(gridLayout
);
239 if (cdmLightExport
|| dwcaExport
|| colDPExport
|| wfoExport
) {
240 checkExportUnpublished
= new Button(composite
, SWT
.CHECK
);
241 checkExportUnpublished
.setText("Export unpublished taxa");
242 Label emptyLabel
= new Label(composite
, SWT
.NONE
);
243 checkExportFactualData
= new Button(composite
, SWT
.CHECK
);
244 checkExportFactualData
.setText("Export factual data");
245 checkExportFactualData
.setSelection(true);
246 checkExportFactualData
.addListener(SWT
.Selection
, e
-> {
247 Button b
= (Button
) e
.widget
;
248 GridData data
= (GridData
) checkExportUnpublishedFactualData
.getLayoutData();
249 data
.exclude
= b
.getSelection();
250 checkExportUnpublishedFactualData
.setEnabled(data
.exclude
);
252 if (cdmLightExport
|| colDPExport
) {
253 emptyLabel
= new Label(composite
, SWT
.NONE
);
255 checkExportUnpublishedFactualData
= new Button(composite
, SWT
.CHECK
);
256 checkExportUnpublishedFactualData
.setText("Export unpublished factual data");
257 checkExportUnpublishedFactualData
.setSelection(false);
258 GridData gridData
= new GridData();
259 gridData
.horizontalIndent
= 20;
260 checkExportUnpublishedFactualData
.setLayoutData(gridData
);
263 @SuppressWarnings("unused")
264 Label nope
= new Label(composite
, SWT
.NONE
);
266 Label separator
= new Label(composite
, SWT
.HORIZONTAL
| SWT
.SEPARATOR
);
267 GridData gridDataSep
= new GridData(GridData
.FILL_HORIZONTAL
);
268 gridDataSep
.horizontalSpan
= 2;
269 separator
.setLayoutData(gridDataSep
);
271 if (classifications
== null) {
272 classifications
= CdmStore
.getService(IClassificationService
.class).list(null, null, null, null, null);
273 Collections
.sort(classifications
, (o1
, o2
) -> {
277 int result
= o1
.getTitleCache().compareTo(o2
.getTitleCache());
279 return o1
.getUuid().compareTo(o2
.getUuid());
283 if (!configurator
.getTaxonNodeFilter().getClassificationFilter().isEmpty()) {
284 selectedClassification
= CdmStore
.getService(IClassificationService
.class)
285 .load(configurator
.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
288 selectedClassification
= classifications
.iterator().next();
292 if (csvExport
|| csvPrintExport
|| cdmLightExport
|| csvNameExport
|| dwcaExport
|| colDPExport
|| wfoExport
) {
294 // Label comboBoxLabel = new Label(composite, SWT.NONE);
295 // comboBoxLabel.setText("Classification");
296 Composite selectNodeOrClassification
= new Composite(composite
, SWT
.NONE
);
297 GridLayout grid
= new GridLayout();
299 selectNodeOrClassification
.setLayout(grid
);
300 if ((cdmLightExport
|| dwcaExport
|| colDPExport
) && !configurator
.getTaxonNodeFilter().getSubtreeFilter().isEmpty()) {
301 checkUseSelectedTaxonNode
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
302 String taxonStr
= "";
303 int count
= configurator
.getTaxonNodeFilter().getSubtreeFilter().size();
304 for (LogicFilter
<TaxonNode
> filter
: configurator
.getTaxonNodeFilter().getSubtreeFilter()) {
305 node
= CdmApplicationState
.getCurrentAppConfig().getTaxonNodeService().load(filter
.getUuid());
307 if (node
.hasTaxon()) {
308 taxonStr
+= node
.getTaxon().getName().getTitleCache();
315 checkUseSelectedTaxonNode
.setText("Export selected subtree(s) (" + taxonStr
+ ")");
316 checkUseSelectedTaxonNode
.addListener(SWT
.Selection
, e
-> {
317 Button b
= (Button
) e
.widget
;
318 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
319 data
.exclude
= b
.getSelection();
320 classificationSelectionCombo
.setEnabled(!data
.exclude
);
322 checkUseSelectedTaxonNode
.setSelection(true);
324 if (cdmLightExport
|| dwcaExport
|| colDPExport
|| wfoExport
) {
326 checkUseAllClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
327 checkUseAllClassification
.setText("Export all classifications");
328 checkUseAllClassification
.addListener(SWT
.Selection
, new Listener() {
330 public void handleEvent(Event e
) {
331 Button b
= (Button
) e
.widget
;
332 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
333 data
.exclude
= b
.getSelection();
334 classificationSelectionCombo
.setEnabled(!data
.exclude
);
339 checkUseSelectedClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
340 checkUseSelectedClassification
.setText("Export selected classification");
341 checkUseSelectedClassification
.addListener(SWT
.Selection
, e
-> {
342 Button b
= (Button
) e
.widget
;
343 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
344 data
.exclude
= b
.getSelection();
345 classificationSelectionCombo
.setEnabled(data
.exclude
);
350 selectedClassification
= node
.getClassification();
352 createClassificationSelectionCombo(selectNodeOrClassification
);
353 if (cdmLightExport
) {
354 Label separator
= new Label(composite
, SWT
.HORIZONTAL
| SWT
.SEPARATOR
);
355 GridData gridDataSep
= new GridData();
356 gridDataSep
= new GridData(GridData
.BEGINNING
, GridData
.CENTER
, true, true);
357 gridDataSep
.horizontalSpan
= 2;
358 separator
.setLayoutData(gridDataSep
);
360 Composite comp
= new Composite(composite
, SWT
.NONE
);
362 GridData gridData
= new GridData();
363 gridData
= new GridData(GridData
.BEGINNING
, GridData
.CENTER
, true, false);
364 gridData
.horizontalSpan
= 2;
365 comp
.setLayoutData(gridData
);
367 Label label
= new Label(comp
, SWT
.NONE
);
368 label
.setText("Sorting of taxa");
370 orderSelectionCombo
= new Combo(comp
, SWT
.BORDER
| SWT
.READ_ONLY
);
372 GridLayoutFactory
.fillDefaults().applyTo(comp
);
373 for (NavigatorOrderEnum display
: NavigatorOrderEnum
.values()) {
374 orderSelectionCombo
.add(display
.getLabel());
378 if (PreferencesUtil
.getStringValue(PreferencePredicate
.TaxonNodeOrder
.getKey()) != null) {
379 NavigatorOrderEnum orderInNavigator
= NavigatorOrderEnum
380 .valueOf(PreferencesUtil
.getStringValue(PreferencePredicate
.TaxonNodeOrder
.getKey()));
382 for (String itemLabel
: orderSelectionCombo
.getItems()) {
383 if (itemLabel
.equalsIgnoreCase(orderInNavigator
.getLabel())) {
384 orderSelectionCombo
.select(index
);
385 setComparatorToConfig();
392 orderSelectionCombo
.addSelectionListener(new SelectionListener() {
395 public void widgetSelected(SelectionEvent e
) {
396 setComparatorToConfig();
400 public void widgetDefaultSelected(SelectionEvent e
) {
401 // TODO Auto-generated method stub
406 if (checkUseSelectedTaxonNode
!= null) {
407 if (checkUseSelectedTaxonNode
.getSelection()) {
408 classificationSelectionCombo
.setEnabled(false);
411 classificationSelectionCombo
.setEnabled(true);
412 classificationSelectionCombo
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1));
414 Label separator
= new Label(composite
, SWT
.HORIZONTAL
| SWT
.SEPARATOR
);
415 GridData gridData
= new GridData(GridData
.FILL_HORIZONTAL
);
416 gridData
.horizontalSpan
= 2;
417 separator
.setLayoutData(gridData
);
421 Label folderLabel
= new Label(composite
, SWT
.NONE
);
422 folderLabel
.setText("Select Folder for exported files");
423 folderLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false, 2, 1));
424 folderDialog
= new DirectoryDialog(parent
.getShell());
425 String exportFolder
= PreferencesUtil
.getStringValue(EXPORT_FOLDER
, true);
426 folderDialog
.setFilterPath(exportFolder
);
428 text_folder
= new Text(composite
, SWT
.BORDER
);
429 text_folder
.setEditable(false);
430 text_folder
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
431 if (exportFolder
!= null) {
432 text_folder
.setText(exportFolder
);
433 setPageComplete(true);
436 Button button
= new Button(composite
, SWT
.PUSH
);
437 button
.setText("Browse...");
439 button
.addSelectionListener(new SelectionAdapter() {
441 public void widgetSelected(SelectionEvent e
) {
442 super.widgetSelected(e
);
444 String path
= folderDialog
.open();
445 if (path
!= null) { // a folder was selected
446 text_folder
.setText(path
);
447 PreferencesUtil
.setStringValue(EXPORT_FOLDER
, path
);
448 setPageComplete(true);
452 if (!cdmLightExport
) {
454 Label fileLabel
= new Label(composite
, SWT
.NONE
);
455 fileLabel
.setText("File");
456 text_exportFileName
= new Text(composite
, SWT
.BORDER
);
457 text_exportFileName
.setText(generateFilename());
458 text_exportFileName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
461 // make the composite the wizard pages control
462 setControl(composite
);
465 protected void setComparatorToConfig() {
466 if (orderSelectionCombo
.getText().equals(NavigatorOrderEnum
.NaturalOrder
.getLabel())) {
467 ((CdmLightExportConfigurator
) configurator
).setTaxonNodeComparator(new TaxonNodeDtoNaturalComparator());
468 } else if (orderSelectionCombo
.getText().equals(NavigatorOrderEnum
.AlphabeticalOrder
.getLabel())) {
469 ((CdmLightExportConfigurator
) configurator
).setTaxonNodeComparator(new TaxonNodeDtoByNameComparator());
471 ((CdmLightExportConfigurator
) configurator
)
472 .setTaxonNodeComparator(new TaxonNodeDtoByRankAndNameComparator());
476 protected String
generateFilename() {
477 StringBuffer buffer
= new StringBuffer();
479 Calendar cal
= Calendar
.getInstance();
480 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT_NOW
);
481 buffer
.append(sdf
.format(cal
.getTime()));
484 buffer
.append(type
+ "_");
486 String source
= ((ICdmRemoteSource
) CdmStore
.getActiveCdmSource()).toString();
487 source
= source
.substring(source
.indexOf("/") + 1);
488 buffer
.append(source
);
491 buffer
.append(extension
);
493 return buffer
.toString();
496 public String
getExportFileName() {
497 return text_exportFileName
.getText();
500 public String
getFolderText() {
501 return text_folder
.getText();
504 public Text
getFolderComposite() {
508 public boolean isExportUnpublishedData() {
509 return checkExportUnpublished
.getSelection();
512 public boolean isExportFactualData() {
513 return checkExportFactualData
.getSelection();
516 public boolean isExportUnpublishedFactualData() {
517 return checkExportUnpublishedFactualData
.getSelection();
520 private void createClassificationSelectionCombo(Composite parent
) {
522 // Composite classificationSelection = new Composite(parent, SWT.NULL);
523 // classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP,
524 // true, false, 1, 1));
526 // GridLayout layout = new GridLayout();
527 // classificationSelection.setLayout(layout);
528 GridData gridData
= new GridData();
529 gridData
= new GridData(GridData
.BEGINNING
, GridData
.CENTER
, true, false);
530 gridData
.horizontalIndent
= 5;
531 // classificationSelection.setLayoutData(gridData);
533 classificationSelectionCombo
= new Combo(parent
, SWT
.BORDER
| SWT
.READ_ONLY
);
534 classificationSelectionCombo
.setLayoutData(gridData
);
535 for (Classification tree
: classifications
) {
536 classificationSelectionCombo
.add(tree
.getTitleCache(), classifications
.indexOf(tree
));
539 classificationSelectionCombo
.select(classifications
.indexOf(selectedClassification
));
541 // TODO remember last selection
542 classificationSelectionCombo
.addSelectionListener(this);
544 // return classificationSelection;
547 public Combo
getCombo() {
548 return classificationSelectionCombo
;
552 public void widgetSelected(SelectionEvent e
) {
553 selectedClassification
= classifications
.get(classificationSelectionCombo
.getSelectionIndex());
556 public Classification
getSelectedClassification() {
557 return selectedClassification
;
561 public void widgetDefaultSelected(SelectionEvent e
) {
565 public UUID
getSelectedClassificationUUID() {
566 return selectedClassification
.getUuid();
569 public boolean getCheckUseSelectedTaxonNode() {
570 if (checkUseSelectedTaxonNode
== null) {
573 return checkUseSelectedTaxonNode
.getSelection();
576 public boolean getCheckUseAllClassifications() {
577 if (checkUseAllClassification
== null) {
580 return checkUseAllClassification
.getSelection();
583 public Set
<UUID
> getAllClassificationUuids() {
584 Set
<UUID
> allClassificationUuids
= new HashSet
<>();
585 for (Classification classification
: this.classifications
) {
586 allClassificationUuids
.add(classification
.getUuid());
588 return allClassificationUuids
;
592 public boolean canFlipToNextPage() {
593 return getFolderText() != null;