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.
10 package eu
.etaxonomy
.taxeditor
.io
.wizard
;
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
;
18 import java
.util
.UUID
;
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
;
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
;
60 * ExportToFileDestinationWizardPage class.
65 public class ExportToFileDestinationWizardPage
extends WizardPage
implements SelectionListener
{
67 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
68 public static final String DATE_FORMAT_NOW
= "yyyyMMddHHmm";
70 /** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
71 public static final String JAXB_EXPORT
= "JAXB_EXPORT";
73 /** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
74 public static final String TCS_EXPORT
= "TCS_EXPORT";
76 /** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
77 public static final String SDD_EXPORT
= "SDD_EXPORT";
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";
87 public static final String CSV
= "csv";
89 public static final String ZIP
= "zip";
91 private boolean csvExport
= false;
93 private boolean csvNameExport
= false;
95 private boolean csvPrintExport
= false;
97 private boolean cdmLightExport
= false;
98 private boolean dwcaExport
= false;
100 private DirectoryDialog folderDialog
;
101 private Text text_exportFileName
;
103 private Text text_folder
;
105 private final String type
;
107 private final String extension
;
109 private Combo classificationSelectionCombo
;
111 private List
<Classification
> classifications
;
113 private Classification selectedClassification
;
115 private Label classificationLabel
;
117 private ExportConfiguratorBase configurator
;
118 private Button checkUseSelectedtaxonNode
;
120 private Button checkUseSelectedClassification
;
121 private Button checkUseAllClassification
;
123 private Button checkExportUnpublished
;
124 private Button checkExportFactualData
;
125 Combo orderSelectionCombo
;
128 protected ExportToFileDestinationWizardPage(String pageName
, String type
,
129 String title
, String description
, String extension
, ExportConfiguratorBase configurator
) {
131 this.configurator
= configurator
;
137 case CSV_NAME_EXPORT
:
138 csvNameExport
= true;
140 case CDM_LIGHT_EXPORT
:
141 cdmLightExport
= true;
143 case CSV_PRINT_EXPORT
:
144 csvPrintExport
= true;
150 this.extension
= extension
;
151 this.setTitle(title
);
152 this.setDescription(description
);
155 public static ExportToFileDestinationWizardPage
Jaxb(JaxbExportConfigurator configurator
) {
156 return new ExportToFileDestinationWizardPage(
160 "Exports the content of the currently selected database into the cdm jaxb format.",
164 public static ExportToFileDestinationWizardPage
Tcs(TcsXmlExportConfigurator config
) {
165 return new ExportToFileDestinationWizardPage(
169 "Export the content of the currently selected database into TCS format.",
173 public static ExportToFileDestinationWizardPage
Sdd(SDDExportConfigurator config
) {
174 return new ExportToFileDestinationWizardPage(
178 "Export the content of the currently selected database into SDD format.",
182 public static ExportToFileDestinationWizardPage
Dwca(DwcaTaxExportConfigurator config
) {
183 return new ExportToFileDestinationWizardPage(
186 "DwC-Archive Export",
187 "Export the content of the currently selected database into Darwin Core Archive format.",
191 public static ExportToFileDestinationWizardPage
OutputModel(CdmLightExportConfigurator config
) {
193 return new ExportToFileDestinationWizardPage(
196 "CDM Light Export (csv)",
197 "Export the content of the currently selected database into the CDM light (csv) format.",
201 public static ExportToFileDestinationWizardPage
Csv(CsvDemoExportConfigurator config
) {
203 return new ExportToFileDestinationWizardPage(
207 "Export the content of the currently selected database into Comma Separated Value format.",
211 public static ExportToFileDestinationWizardPage
CsvNames(CsvNameExportConfigurator config
) {
213 return new ExportToFileDestinationWizardPage(
217 "Export the names of the currently selected database into Semicolon Separated Value format.",
221 public static ExportToFileDestinationWizardPage
CsvPrint(CsvNameExportConfigurator config
) {
223 return new ExportToFileDestinationWizardPage(
227 "Export the content of the currently selected database into Semicolon Separated Value format.",
233 public void createControl(Composite parent
) {
235 setPageComplete(false);
237 Composite composite
= new Composite(parent
, SWT
.NONE
);
238 GridLayout gridLayout
= new GridLayout();
240 gridLayout
.numColumns
= 2;
241 TaxonNode node
= null;
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
);
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
);
258 if(classifications
== null){
259 classifications
= CdmStore
.getService(IClassificationService
.class).list(null, null, null, null, null);
260 Collections
.sort(classifications
, (o1
,o2
) -> {
264 int result
= o1
.getTitleCache().compareTo(o2
.getTitleCache());
266 return o1
.getUuid().compareTo(o2
.getUuid());
270 if (!configurator
.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
271 selectedClassification
= CdmStore
.getService(IClassificationService
.class).load(configurator
.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
274 selectedClassification
= classifications
.iterator().next();
279 if(csvExport
|| csvPrintExport
|| cdmLightExport
|| csvNameExport
|| dwcaExport
){
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();
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());
294 if (node
.hasTaxon()){
295 taxonStr
+= node
.getTaxon().getName().getTitleCache();
302 checkUseSelectedtaxonNode
.setText("Export selected subtree(s) ("+ taxonStr
+")");
303 checkUseSelectedtaxonNode
.addListener(SWT
.Selection
, new Listener() {
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
);
313 checkUseSelectedtaxonNode
.setSelection(true);
315 if(cdmLightExport
|| dwcaExport
){
317 checkUseAllClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
318 checkUseAllClassification
.setText("Export all classifications");
319 checkUseAllClassification
.addListener(SWT
.Selection
, new Listener() {
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
);
330 checkUseSelectedClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
331 checkUseSelectedClassification
.setText("Export selected classification");
332 checkUseSelectedClassification
.addListener(SWT
.Selection
, new Listener() {
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
);
345 selectedClassification
= node
.getClassification();
347 createClassificationSelectionCombo(selectNodeOrClassification
);
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
);
355 Composite comp
= new Composite(composite
, SWT
.NONE
);
357 GridData gridData
= new GridData();
358 gridData
= new GridData(GridData
.BEGINNING
, GridData
.CENTER
, true, false);
359 gridData
.horizontalSpan
= 2;
360 comp
.setLayoutData(gridData
);
362 Label label
= new Label(comp
, SWT
.NONE
);
363 label
.setText("Sorting of taxa");
365 orderSelectionCombo
= new Combo(comp
, SWT
.BORDER
| SWT
.READ_ONLY
);
367 GridLayoutFactory
.fillDefaults().applyTo(comp
);
368 for(NavigatorOrderEnum display
: NavigatorOrderEnum
.values()){
369 orderSelectionCombo
.add(display
.getLabel());
373 if (PreferencesUtil
.getStringValue(PreferencePredicate
.TaxonNodeOrder
.getKey()) != null){
374 NavigatorOrderEnum orderInNavigator
= NavigatorOrderEnum
.valueOf(PreferencesUtil
.getStringValue(PreferencePredicate
.TaxonNodeOrder
.getKey()));
376 for (String itemLabel
: orderSelectionCombo
.getItems()){
377 if (itemLabel
.equalsIgnoreCase(orderInNavigator
.getLabel())){
378 orderSelectionCombo
.select(index
);
379 setComparatorToConfig();
386 orderSelectionCombo
.addSelectionListener(new SelectionListener() {
389 public void widgetSelected(SelectionEvent e
) {
390 setComparatorToConfig();
394 public void widgetDefaultSelected(SelectionEvent e
) {
395 // TODO Auto-generated method stub
400 if (checkUseSelectedtaxonNode
!= null){
401 if (checkUseSelectedtaxonNode
.getSelection()){
402 classificationSelectionCombo
.setEnabled(false);
405 classificationSelectionCombo
.setEnabled(true);
406 classificationSelectionCombo
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
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
);
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
,
420 folderDialog
= new DirectoryDialog(parent
.getShell());
421 String exportFolder
= PreferencesUtil
.getStringValue(EXPORT_FOLDER
, true);
422 folderDialog
.setFilterPath(exportFolder
);
424 text_folder
= new Text(composite
, SWT
.BORDER
);
425 text_folder
.setEditable(false);
426 text_folder
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true,
428 if (exportFolder
!= null){
429 text_folder
.setText(exportFolder
);
430 setPageComplete(true);
433 Button button
= new Button(composite
, SWT
.PUSH
);
434 button
.setText("Browse...");
436 button
.addSelectionListener(new SelectionAdapter() {
438 public void widgetSelected(SelectionEvent e
) {
439 super.widgetSelected(e
);
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);
449 if (!cdmLightExport
){
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
,
459 // make the composite the wizard pages control
460 setControl(composite
);
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());
469 ((CdmLightExportConfigurator
)configurator
).setTaxonNodeComparator(new TaxonNodeDtoByRankAndNameComparator());
473 protected String
generateFilename() {
474 StringBuffer buffer
= new StringBuffer();
476 Calendar cal
= Calendar
.getInstance();
477 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT_NOW
);
478 buffer
.append(sdf
.format(cal
.getTime()));
481 buffer
.append(type
+"_");
483 String source
= ((ICdmRemoteSource
)CdmStore
.getActiveCdmSource()).toString();
484 source
= source
.substring(source
.indexOf("/")+1);
485 buffer
.append(source
);
488 buffer
.append(extension
);
490 return buffer
.toString();
493 public String
getExportFileName() {
494 return text_exportFileName
.getText();
497 public String
getFolderText() {
498 return text_folder
.getText();
501 public Text
getFolderComposite() {
505 public boolean isExportUnpublishedData(){
506 return checkExportUnpublished
.getSelection();
509 public boolean isExportFactualData(){
510 return checkExportFactualData
.getSelection();
513 private void createClassificationSelectionCombo(Composite parent
){
515 // Composite classificationSelection = new Composite(parent, SWT.NULL);
516 // classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
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);
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
));
531 classificationSelectionCombo
.select(classifications
.indexOf(selectedClassification
));
533 // TODO remember last selection
534 classificationSelectionCombo
.addSelectionListener(this);
536 //return classificationSelection;
539 public Combo
getCombo(){
540 return classificationSelectionCombo
;
544 public void widgetSelected(SelectionEvent e
) {
545 selectedClassification
= classifications
.get(classificationSelectionCombo
.getSelectionIndex());
548 public Classification
getSelectedClassification(){
549 return selectedClassification
;
553 public void widgetDefaultSelected(SelectionEvent e
) {
557 public UUID
getSelectedClassificationUUID() {
558 return selectedClassification
.getUuid();
561 public boolean getCheckUseSelectedTaxonNode() {
562 if (checkUseSelectedtaxonNode
== null){
565 return checkUseSelectedtaxonNode
.getSelection();
567 public boolean getCheckUseAllClassifications() {
568 if (checkUseAllClassification
== null){
571 return checkUseAllClassification
.getSelection();
574 public Set
<UUID
> getAllClassificationUuids(){
575 Set
<UUID
> allClassificationUuids
= new HashSet
<>();
576 for (Classification classification
: this.classifications
){
577 allClassificationUuids
.add(classification
.getUuid());
579 return allClassificationUuids
;
583 public boolean canFlipToNextPage() {
584 return getFolderText() != null;