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
.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 OUTPUT_MODEL_EXPORT
= "CDM_LIGHT(CSV)";
82 public static final String CSV_EXPORT
= "CSV_EXPORT";
83 public static final String CSV_NAME_EXPORT
= "CSV_NAME_EXPORT";
84 private static final String CSV_PRINT_EXPORT
="CSV_PRINT_EXPORT";
85 private static final String EXPORT_FOLDER
="EXPORT_FOLDER";
86 public static final String XML
= "xml";
88 public static final String CSV
= "csv";
90 public static final String ZIP
= "zip";
92 private boolean csvExport
= false;
94 private boolean csvNameExport
= false;
96 private boolean csvPrintExport
= false;
98 private boolean outputModelExport
= false;
99 private boolean dwcaExport
= false;
101 private DirectoryDialog folderDialog
;
102 private Text text_exportFileName
;
104 private Text text_folder
;
106 private final String type
;
108 private final String extension
;
110 private Combo classificationSelectionCombo
;
112 private List
<Classification
> classifications
;
114 private Classification selectedClassification
;
116 private Label classificationLabel
;
118 private ExportConfiguratorBase configurator
;
119 private Button checkUseSelectedtaxonNode
;
121 private Button checkUseSelectedClassification
;
122 private Button checkUseAllClassification
;
124 private Button checkExportUnpublished
;
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 OUTPUT_MODEL_EXPORT
:
141 outputModelExport
= 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 (outputModelExport
|| dwcaExport
){
245 checkExportUnpublished
= new Button(composite
, SWT
.CHECK
);
246 checkExportUnpublished
.setText("Export unpublished taxa");
247 @SuppressWarnings("unused")
248 Label nope
= new Label(composite
, SWT
.NONE
);
250 Label separator
= new Label(composite
, SWT
.HORIZONTAL
| SWT
.SEPARATOR
);
251 GridData gridData
= new GridData(GridData
.FILL_HORIZONTAL
);
252 gridData
.horizontalSpan
= 2;
253 separator
.setLayoutData(gridData
);
255 if(classifications
== null){
256 classifications
= CdmStore
.getService(IClassificationService
.class).list(null, null, null, null, null);
257 Collections
.sort(classifications
, new Comparator
<Classification
>() {
260 public int compare(Classification o1
, Classification o2
) {
264 int result
= o1
.getTitleCache().compareTo(o2
.getTitleCache());
266 return o1
.getUuid().compareTo(o2
.getUuid());
271 if (!configurator
.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
272 selectedClassification
= CdmStore
.getService(IClassificationService
.class).load(configurator
.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
275 selectedClassification
= classifications
.iterator().next();
280 if(csvExport
|| csvPrintExport
|| outputModelExport
|| csvNameExport
|| dwcaExport
){
282 // Label comboBoxLabel = new Label(composite, SWT.NONE);
283 // comboBoxLabel.setText("Classification");
284 Composite selectNodeOrClassification
= new Composite(composite
, SWT
.NONE
);
285 GridLayout grid
= new GridLayout();
287 selectNodeOrClassification
.setLayout(grid
);
288 if ((outputModelExport
|| dwcaExport
)&& !configurator
.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
289 checkUseSelectedtaxonNode
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
290 String taxonStr
= "";
291 int count
= configurator
.getTaxonNodeFilter().getSubtreeFilter().size();
292 for (LogicFilter
<TaxonNode
> filter
: configurator
.getTaxonNodeFilter().getSubtreeFilter()){
293 node
= CdmApplicationState
.getCurrentAppConfig().getTaxonNodeService().load(filter
.getUuid());
295 if (node
.hasTaxon()){
296 taxonStr
+= node
.getTaxon().getName().getTitleCache();
303 checkUseSelectedtaxonNode
.setText("Export selected subtree(s) ("+ taxonStr
+")");
304 checkUseSelectedtaxonNode
.addListener(SWT
.Selection
, new Listener() {
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
.setEnabled(!data
.exclude
);
314 checkUseSelectedtaxonNode
.setSelection(true);
316 if(outputModelExport
|| dwcaExport
){
318 checkUseAllClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
319 checkUseAllClassification
.setText("Export all classifications");
320 checkUseAllClassification
.addListener(SWT
.Selection
, new Listener() {
322 public void handleEvent(Event e
) {
323 Button b
= (Button
) e
.widget
;
324 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
325 data
.exclude
= b
.getSelection();
326 classificationSelectionCombo
.setEnabled(!data
.exclude
);
331 checkUseSelectedClassification
= new Button(selectNodeOrClassification
, SWT
.RADIO
);
332 checkUseSelectedClassification
.setText("Export selected classification");
333 checkUseSelectedClassification
.addListener(SWT
.Selection
, new Listener() {
335 public void handleEvent(Event e
) {
336 Button b
= (Button
) e
.widget
;
337 GridData data
= (GridData
) classificationSelectionCombo
.getLayoutData();
338 data
.exclude
= b
.getSelection();
339 classificationSelectionCombo
.setEnabled(data
.exclude
);
346 selectedClassification
= node
.getClassification();
348 createClassificationSelectionCombo(selectNodeOrClassification
);
349 if (outputModelExport
){
350 Label separator
= new Label(composite
, SWT
.HORIZONTAL
| SWT
.SEPARATOR
);
351 GridData gridDataSep
= new GridData();
352 gridDataSep
= new GridData(GridData
.BEGINNING
, GridData
.CENTER
, true, true);
353 gridDataSep
.horizontalSpan
= 2;
354 separator
.setLayoutData(gridDataSep
);
356 Composite comp
= new Composite(composite
, SWT
.NONE
);
358 GridData gridData
= new GridData();
359 gridData
= new GridData(GridData
.BEGINNING
, GridData
.CENTER
, true, false);
360 gridData
.horizontalSpan
= 2;
361 comp
.setLayoutData(gridData
);
363 Label label
= new Label(comp
, SWT
.NONE
);
364 label
.setText("Sorting of taxa");
366 orderSelectionCombo
= new Combo(comp
, SWT
.BORDER
| SWT
.READ_ONLY
);
368 GridLayoutFactory
.fillDefaults().applyTo(comp
);
369 for(NavigatorOrderEnum display
: NavigatorOrderEnum
.values()){
370 orderSelectionCombo
.add(display
.getLabel());
374 if (PreferencesUtil
.getStringValue(PreferencePredicate
.TaxonNodeOrder
.getKey()) != null){
375 NavigatorOrderEnum orderInNavigator
= NavigatorOrderEnum
.valueOf(PreferencesUtil
.getStringValue(PreferencePredicate
.TaxonNodeOrder
.getKey()));
377 for (String itemLabel
: orderSelectionCombo
.getItems()){
378 if (itemLabel
.equalsIgnoreCase(orderInNavigator
.getLabel())){
379 orderSelectionCombo
.select(index
);
380 setComparatorToConfig();
387 orderSelectionCombo
.addSelectionListener(new SelectionListener() {
390 public void widgetSelected(SelectionEvent e
) {
391 setComparatorToConfig();
395 public void widgetDefaultSelected(SelectionEvent e
) {
396 // TODO Auto-generated method stub
401 if (checkUseSelectedtaxonNode
!= null){
402 if (checkUseSelectedtaxonNode
.getSelection()){
403 classificationSelectionCombo
.setEnabled(false);
406 classificationSelectionCombo
.setEnabled(true);
407 classificationSelectionCombo
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
410 Label separator
= new Label(composite
, SWT
.HORIZONTAL
| SWT
.SEPARATOR
);
411 GridData gridData
= new GridData(GridData
.FILL_HORIZONTAL
);
412 gridData
.horizontalSpan
= 2;
413 separator
.setLayoutData(gridData
);
417 Label folderLabel
= new Label(composite
, SWT
.NONE
);
418 folderLabel
.setText("Select Folder for exported files");
419 folderLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
421 folderDialog
= new DirectoryDialog(parent
.getShell());
422 String exportFolder
= PreferencesUtil
.getStringValue(EXPORT_FOLDER
, true);
423 folderDialog
.setFilterPath(exportFolder
);
425 text_folder
= new Text(composite
, SWT
.BORDER
);
426 text_folder
.setEditable(false);
427 text_folder
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true,
429 if (exportFolder
!= null){
430 text_folder
.setText(exportFolder
);
431 setPageComplete(true);
434 Button button
= new Button(composite
, SWT
.PUSH
);
435 button
.setText("Browse...");
437 button
.addSelectionListener(new SelectionAdapter() {
439 public void widgetSelected(SelectionEvent e
) {
440 super.widgetSelected(e
);
442 String path
= folderDialog
.open();
443 if (path
!= null) { // a folder was selected
444 text_folder
.setText(path
);
445 PreferencesUtil
.setStringValue(EXPORT_FOLDER
, path
);
446 setPageComplete(true);
450 if (!outputModelExport
){
452 Label fileLabel
= new Label(composite
, SWT
.NONE
);
453 fileLabel
.setText("File");
454 text_exportFileName
= new Text(composite
, SWT
.BORDER
);
455 text_exportFileName
.setText(generateFilename());
456 text_exportFileName
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
,
460 // make the composite the wizard pages control
461 setControl(composite
);
464 protected void setComparatorToConfig() {
465 if (orderSelectionCombo
.getText().equals(NavigatorOrderEnum
.NaturalOrder
.getLabel())){
466 ((CdmLightExportConfigurator
)configurator
).setComparator(new TaxonNodeDtoNaturalComparator());
467 } else if (orderSelectionCombo
.getText().equals(NavigatorOrderEnum
.AlphabeticalOrder
.getLabel())){
468 ((CdmLightExportConfigurator
)configurator
).setComparator(new TaxonNodeDtoByNameComparator());
470 ((CdmLightExportConfigurator
)configurator
).setComparator(new TaxonNodeDtoByRankAndNameComparator());
474 protected String
generateFilename() {
475 StringBuffer buffer
= new StringBuffer();
477 Calendar cal
= Calendar
.getInstance();
478 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT_NOW
);
479 buffer
.append(sdf
.format(cal
.getTime()));
482 buffer
.append(type
+"_");
484 String source
= ((ICdmRemoteSource
)CdmStore
.getActiveCdmSource()).toString();
485 source
= source
.substring(source
.indexOf("/")+1);
486 buffer
.append(source
);
489 buffer
.append(extension
);
491 return buffer
.toString();
494 public String
getExportFileName() {
495 return text_exportFileName
.getText();
498 public String
getFolderText() {
499 return text_folder
.getText();
502 public Text
getFolderComposite() {
506 public boolean isExportUnpublishedData(){
507 return checkExportUnpublished
.getSelection();
510 private void createClassificationSelectionCombo(Composite parent
){
512 // Composite classificationSelection = new Composite(parent, SWT.NULL);
513 // classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
515 // GridLayout layout = new GridLayout();
516 // classificationSelection.setLayout(layout);
517 GridData gridData
= new GridData();
518 gridData
= new GridData(GridData
.BEGINNING
, GridData
.CENTER
, true, false);
519 gridData
.horizontalIndent
= 5;
520 // classificationSelection.setLayoutData(gridData);
522 classificationSelectionCombo
= new Combo(parent
, SWT
.BORDER
| SWT
.READ_ONLY
);
523 classificationSelectionCombo
.setLayoutData(gridData
);
524 for(Classification tree
: classifications
){
525 classificationSelectionCombo
.add(tree
.getName().getText(), classifications
.indexOf(tree
));
528 classificationSelectionCombo
.select(classifications
.indexOf(selectedClassification
));
530 // TODO remember last selection
531 classificationSelectionCombo
.addSelectionListener(this);
533 //return classificationSelection;
536 public Combo
getCombo(){
537 return classificationSelectionCombo
;
541 public void widgetSelected(SelectionEvent e
) {
542 selectedClassification
= classifications
.get(classificationSelectionCombo
.getSelectionIndex());
545 public Classification
getSelectedClassification(){
546 return selectedClassification
;
550 public void widgetDefaultSelected(SelectionEvent e
) {
554 public UUID
getSelectedClassificationUUID() {
555 return selectedClassification
.getUuid();
558 public boolean getCheckUseSelectedTaxonNode() {
559 if (checkUseSelectedtaxonNode
== null){
562 return checkUseSelectedtaxonNode
.getSelection();
564 public boolean getCheckUseAllClassifications() {
565 if (checkUseAllClassification
== null){
568 return checkUseAllClassification
.getSelection();
571 public Set
<UUID
> getAllClassificationUuids(){
572 Set
<UUID
> allClassificationUuids
= new HashSet
<>();
573 for (Classification classification
: this.classifications
){
574 allClassificationUuids
.add(classification
.getUuid());
576 return allClassificationUuids
;
580 public boolean canFlipToNextPage() {
581 return getFolderText() != null;