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
.Comparator
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
19 import java
.util
.UUID
;
21 import org
.eclipse
.jface
.layout
.GridLayoutFactory
;
22 import org
.eclipse
.jface
.wizard
.WizardPage
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.events
.SelectionAdapter
;
25 import org
.eclipse
.swt
.events
.SelectionEvent
;
26 import org
.eclipse
.swt
.events
.SelectionListener
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Button
;
30 import org
.eclipse
.swt
.widgets
.Combo
;
31 import org
.eclipse
.swt
.widgets
.Composite
;
32 import org
.eclipse
.swt
.widgets
.DirectoryDialog
;
33 import org
.eclipse
.swt
.widgets
.Event
;
34 import org
.eclipse
.swt
.widgets
.Label
;
35 import org
.eclipse
.swt
.widgets
.Listener
;
36 import org
.eclipse
.swt
.widgets
.Text
;
38 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
39 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
40 import eu
.etaxonomy
.cdm
.filter
.LogicFilter
;
41 import eu
.etaxonomy
.cdm
.io
.cdmLight
.CdmLightExportConfigurator
;
42 import eu
.etaxonomy
.cdm
.io
.common
.ExportConfiguratorBase
;
43 import eu
.etaxonomy
.cdm
.io
.csv
.caryophyllales
.out
.CsvNameExportConfigurator
;
44 import eu
.etaxonomy
.cdm
.io
.csv
.redlist
.demo
.CsvDemoExportConfigurator
;
45 import eu
.etaxonomy
.cdm
.io
.dwca
.out
.DwcaTaxExportConfigurator
;
46 import eu
.etaxonomy
.cdm
.io
.jaxb
.JaxbExportConfigurator
;
47 import eu
.etaxonomy
.cdm
.io
.sdd
.out
.SDDExportConfigurator
;
48 import eu
.etaxonomy
.cdm
.io
.tcsxml
.out
.TcsXmlExportConfigurator
;
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
.datasource
.CdmDataSourceRepository
;
56 import eu
.etaxonomy
.taxeditor
.preference
.NavigatorOrderEnum
;
57 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
58 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmRemoteSourceException
;
59 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
63 * ExportToFileDestinationWizardPage class.
70 public class ExportToFileDestinationWizardPage
extends WizardPage
implements SelectionListener
{
72 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
73 public static final String DATE_FORMAT_NOW
= "yyyyMMddHHmm";
75 /** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
76 public static final String JAXB_EXPORT
= "JAXB_EXPORT";
78 /** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
79 public static final String TCS_EXPORT
= "TCS_EXPORT";
81 /** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
82 public static final String SDD_EXPORT
= "SDD_EXPORT";
84 public static final String DWCA_EXPORT
= "DWCA_EXPORT";
85 public static final String OUTPUT_MODEL_EXPORT
= "CDM_LIGHT(CSV)";
86 public static final String CSV_EXPORT
= "CSV_EXPORT";
87 public static final String CSV_NAME_EXPORT
= "CSV_NAME_EXPORT";
88 private static final String CSV_PRINT_EXPORT
="CSV_PRINT_EXPORT";
89 private static final String EXPORT_FOLDER
="EXPORT_FOLDER";
90 public static final String XML
= "xml";
92 public static final String CSV
= "csv";
94 public static final String ZIP
= "zip";
96 private boolean csvExport
= false;
98 private boolean csvNameExport
= false;
100 private boolean csvPrintExport
= false;
102 private boolean outputModelExport
= false;
103 private boolean dwcaExport
= false;
105 private DirectoryDialog folderDialog
;
106 private Text text_exportFileName
;
108 private Text text_folder
;
110 private final String type
;
112 private final String extension
;
114 private Combo classificationSelectionCombo
;
116 private List
<Classification
> classifications
;
118 private Classification selectedClassification
;
120 private Label classificationLabel
;
122 private ExportConfiguratorBase configurator
;
123 private Button checkUseSelectedtaxonNode
;
125 private Button checkUseSelectedClassification
;
126 private Button checkUseAllClassification
;
128 private Button checkExportUnpublished
;
129 Combo orderSelectionCombo
;
132 protected ExportToFileDestinationWizardPage(String pageName
, String type
,
133 String title
, String description
, String extension
, ExportConfiguratorBase configurator
) {
135 this.configurator
= configurator
;
141 case CSV_NAME_EXPORT
:
142 csvNameExport
= true;
144 case OUTPUT_MODEL_EXPORT
:
145 outputModelExport
= true;
147 case CSV_PRINT_EXPORT
:
148 csvPrintExport
= true;
155 this.extension
= extension
;
156 this.setTitle(title
);
157 this.setDescription(description
);
160 public static ExportToFileDestinationWizardPage
Jaxb(JaxbExportConfigurator configurator
) {
161 return new ExportToFileDestinationWizardPage(
165 "Exports the content of the currently selected database into the cdm jaxb format.",
169 public static ExportToFileDestinationWizardPage
Tcs(TcsXmlExportConfigurator config
) {
170 return new ExportToFileDestinationWizardPage(
174 "Export the content of the currently selected database into TCS format.",
178 public static ExportToFileDestinationWizardPage
Sdd(SDDExportConfigurator config
) {
179 return new ExportToFileDestinationWizardPage(
183 "Export the content of the currently selected database into SDD format.",
187 public static ExportToFileDestinationWizardPage
Dwca(DwcaTaxExportConfigurator config
) {
188 return new ExportToFileDestinationWizardPage(
191 "DwC-Archive Export",
192 "Export the content of the currently selected database into Darwin Core Archive format.",
196 public static ExportToFileDestinationWizardPage
OutputModel(CdmLightExportConfigurator config
) {
198 return new ExportToFileDestinationWizardPage(
201 "CDM Light Export (csv)",
202 "Export the content of the currently selected database into the CDM light (csv) format.",
206 public static ExportToFileDestinationWizardPage
Csv(CsvDemoExportConfigurator config
) {
208 return new ExportToFileDestinationWizardPage(
212 "Export the content of the currently selected database into Comma Separated Value format.",
216 public static ExportToFileDestinationWizardPage
CsvNames(CsvNameExportConfigurator config
) {
218 return new ExportToFileDestinationWizardPage(
222 "Export the names of the currently selected database into Semicolon Separated Value format.",
226 public static ExportToFileDestinationWizardPage
CsvPrint(CsvNameExportConfigurator config
) {
228 return new ExportToFileDestinationWizardPage(
232 "Export the content of the currently selected database into Semicolon Separated Value format.",
238 public void createControl(Composite parent
) {
240 setPageComplete(false);
242 Composite composite
= new Composite(parent
, SWT
.NONE
);
243 GridLayout gridLayout
= new GridLayout();
245 gridLayout
.numColumns
= 2;
246 TaxonNode node
= null;
248 composite
.setLayout(gridLayout
);
249 if (outputModelExport
|| dwcaExport
){
250 checkExportUnpublished
= new Button(composite
, SWT
.CHECK
);
251 checkExportUnpublished
.setText("Export unpublished taxa");
252 Label label
= new Label(composite
, SWT
.NONE
);
254 Label separator
= new Label(composite
, SWT
.HORIZONTAL
| SWT
.SEPARATOR
);
255 GridData gridData
= new GridData(GridData
.FILL_HORIZONTAL
);
256 gridData
.horizontalSpan
= 2;
257 separator
.setLayoutData(gridData
);
259 if(classifications
== null){
260 classifications
= CdmStore
.getService(IClassificationService
.class).list(null, null, null, null, null);
261 Collections
.sort(classifications
, new Comparator
<Classification
>() {
264 public int compare(Classification o1
, Classification o2
) {
268 int result
= o1
.getTitleCache().compareTo(o2
.getTitleCache());
270 return o1
.getUuid().compareTo(o2
.getUuid());
275 if (!configurator
.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
276 selectedClassification
= CdmStore
.getService(IClassificationService
.class).load(configurator
.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
279 selectedClassification
= classifications
.iterator().next();
284 if(csvExport
|| csvPrintExport
|| outputModelExport
|| csvNameExport
|| dwcaExport
){
286 // Label comboBoxLabel = new Label(composite, SWT.NONE);
287 // comboBoxLabel.setText("Classification");
288 Composite selectNodeOrClassification
= new Composite(composite
, SWT
.NONE
);
289 GridLayout grid
= new GridLayout();
291 selectNodeOrClassification
.setLayout(grid
);
292 if ((outputModelExport
|| dwcaExport
)&& !configurator
.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
293 checkUseSelectedtaxonNode
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
294 String taxonStr
= "";
295 int count
= configurator
.getTaxonNodeFilter().getSubtreeFilter().size();
296 for (LogicFilter
<TaxonNode
> filter
: configurator
.getTaxonNodeFilter().getSubtreeFilter()){
297 node
= CdmApplicationState
.getCurrentAppConfig().getTaxonNodeService().load(filter
.getUuid());
299 if (node
.hasTaxon()){
300 taxonStr
+= node
.getTaxon().getName().getTitleCache();
307 checkUseSelectedtaxonNode
.setText("Export selected subtree(s) ("+ taxonStr
+")");
308 checkUseSelectedtaxonNode
.addListener(SWT
.Selection
, new Listener() {
310 public void handleEvent(Event e
) {
311 Button b
= (Button
) e
.widget
;
312 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
313 data
.exclude
= b
.getSelection();
314 classificationSelectionCombo
.setEnabled(!data
.exclude
);
318 checkUseSelectedtaxonNode
.setSelection(true);
320 if(outputModelExport
|| dwcaExport
){
322 checkUseAllClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
323 checkUseAllClassification
.setText("Export all classifications");
324 checkUseAllClassification
.addListener(SWT
.Selection
, new Listener() {
326 public void handleEvent(Event e
) {
327 Button b
= (Button
) e
.widget
;
328 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
329 data
.exclude
= b
.getSelection();
330 classificationSelectionCombo
.setEnabled(!data
.exclude
);
335 checkUseSelectedClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
336 checkUseSelectedClassification
.setText("Export selected classification");
337 checkUseSelectedClassification
.addListener(SWT
.Selection
, new Listener() {
339 public void handleEvent(Event e
) {
340 Button b
= (Button
) e
.widget
;
341 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
342 data
.exclude
= b
.getSelection();
343 classificationSelectionCombo
.setEnabled(data
.exclude
);
350 selectedClassification
= node
.getClassification();
352 createClassificationSelectionCombo(selectNodeOrClassification
);
353 if (outputModelExport
){
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
.valueOf(PreferencesUtil
.getStringValue(PreferencePredicate
.TaxonNodeOrder
.getKey()));
381 for (String itemLabel
: orderSelectionCombo
.getItems()){
382 if (itemLabel
.equalsIgnoreCase(orderInNavigator
.getLabel())){
383 orderSelectionCombo
.select(index
);
384 setComparatorToConfig();
391 orderSelectionCombo
.addSelectionListener(new SelectionListener() {
394 public void widgetSelected(SelectionEvent e
) {
395 setComparatorToConfig();
399 public void widgetDefaultSelected(SelectionEvent e
) {
400 // TODO Auto-generated method stub
408 if (checkUseSelectedtaxonNode
!= null){
410 if (checkUseSelectedtaxonNode
.getSelection()){
411 classificationSelectionCombo
.setEnabled(false);
414 classificationSelectionCombo
.setEnabled(true);
415 classificationSelectionCombo
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
418 Label separator
= new Label(composite
, SWT
.HORIZONTAL
| SWT
.SEPARATOR
);
419 GridData gridData
= new GridData(GridData
.FILL_HORIZONTAL
);
420 gridData
.horizontalSpan
= 2;
421 separator
.setLayoutData(gridData
);
425 Label folderLabel
= new Label(composite
, SWT
.NONE
);
426 folderLabel
.setText("Select Folder for exported files");
427 folderLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
429 folderDialog
= new DirectoryDialog(parent
.getShell());
430 folderDialog
.setFilterPath(PreferencesUtil
.getStringValue(EXPORT_FOLDER
));
432 text_folder
= new Text(composite
, SWT
.BORDER
);
433 text_folder
.setEditable(false);
434 text_folder
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true,
436 if (PreferencesUtil
.getStringValue(EXPORT_FOLDER
) != null){
437 text_folder
.setText(PreferencesUtil
.getStringValue(EXPORT_FOLDER
));
438 setPageComplete(true);
441 Button button
= new Button(composite
, SWT
.PUSH
);
442 button
.setText("Browse...");
444 button
.addSelectionListener(new SelectionAdapter() {
446 public void widgetSelected(SelectionEvent e
) {
447 super.widgetSelected(e
);
449 String path
= folderDialog
.open();
450 if (path
!= null) { // a folder was selected
451 text_folder
.setText(path
);
452 PreferencesUtil
.setStringValue(EXPORT_FOLDER
, path
);
453 setPageComplete(true);
457 if (!outputModelExport
){
459 Label fileLabel
= new Label(composite
, SWT
.NONE
);
460 fileLabel
.setText("File");
461 text_exportFileName
= new Text(composite
, SWT
.BORDER
);
462 text_exportFileName
.setText(generateFilename());
463 text_exportFileName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
468 // make the composite the wizard pages control
469 setControl(composite
);
472 protected void setComparatorToConfig() {
473 if (orderSelectionCombo
.getText().equals(NavigatorOrderEnum
.NaturalOrder
.getLabel())){
474 ((CdmLightExportConfigurator
)configurator
).setComparator(new TaxonNodeDtoNaturalComparator());
475 } else if (orderSelectionCombo
.getText().equals(NavigatorOrderEnum
.AlphabeticalOrder
.getLabel())){
476 ((CdmLightExportConfigurator
)configurator
).setComparator(new TaxonNodeDtoByNameComparator());
478 ((CdmLightExportConfigurator
)configurator
).setComparator(new TaxonNodeDtoByRankAndNameComparator());
482 protected String
generateFilename() {
483 StringBuffer buffer
= new StringBuffer();
485 Calendar cal
= Calendar
.getInstance();
486 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT_NOW
);
487 buffer
.append(sdf
.format(cal
.getTime()));
491 buffer
.append(type
+ "_export-");
493 buffer
.append(CdmDataSourceRepository
.getCurrentCdmSource());
494 } catch (CdmRemoteSourceException e
) {
495 buffer
.append("Unknown");
499 buffer
.append(extension
);
501 return buffer
.toString();
504 public String
getExportFileName() {
505 return text_exportFileName
.getText();
508 public String
getFolderText() {
509 return text_folder
.getText();
512 public Text
getFolderComposite() {
516 public boolean isExportUnpublishedData(){
517 return checkExportUnpublished
.getSelection();
520 private void createClassificationSelectionCombo(Composite parent
){
522 // Composite classificationSelection = new Composite(parent, SWT.NULL);
523 // classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
525 // GridLayout layout = new GridLayout();
526 // classificationSelection.setLayout(layout);
527 GridData gridData
= new GridData();
528 gridData
= new GridData(GridData
.BEGINNING
, GridData
.CENTER
, true, false);
529 gridData
.horizontalIndent
= 5;
530 // classificationSelection.setLayoutData(gridData);
532 classificationSelectionCombo
= new Combo(parent
, SWT
.BORDER
| SWT
.READ_ONLY
);
533 classificationSelectionCombo
.setLayoutData(gridData
);
534 for(Classification tree
: classifications
){
535 classificationSelectionCombo
.add(tree
.getName().getText(), classifications
.indexOf(tree
));
539 classificationSelectionCombo
.select(classifications
.indexOf(selectedClassification
));
541 // TODO remember last selection
542 classificationSelectionCombo
.addSelectionListener(this);
546 //return classificationSelection;
549 public Combo
getCombo(){
550 return classificationSelectionCombo
;
554 public void widgetSelected(SelectionEvent e
) {
555 selectedClassification
= classifications
.get(classificationSelectionCombo
.getSelectionIndex());
560 public void widgetDefaultSelected(SelectionEvent e
) {
565 public UUID
getSelectedClassificationUUID() {
567 return selectedClassification
.getUuid();
570 public boolean getCheckUseSelectedTaxonNode() {
571 if (checkUseSelectedtaxonNode
== null){
574 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;