Merge branch 'release/5.18.0'
[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.Comparator;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.UUID;
20
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;
37
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;
59
60 /**
61 * ExportToFileDestinationWizardPage class.
62 *
63 * @author n.hoffmann
64 * @created 15.06.2009
65 */
66 public class ExportToFileDestinationWizardPage extends WizardPage implements SelectionListener {
67
68 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
69 public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
70
71 /** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
72 public static final String JAXB_EXPORT = "JAXB_EXPORT";
73
74 /** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
75 public static final String TCS_EXPORT = "TCS_EXPORT";
76
77 /** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
78 public static final String SDD_EXPORT = "SDD_EXPORT";
79
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";
87
88 public static final String CSV = "csv";
89
90 public static final String ZIP = "zip";
91
92 private boolean csvExport = false;
93
94 private boolean csvNameExport = false;
95
96 private boolean csvPrintExport = false;
97
98 private boolean outputModelExport = false;
99 private boolean dwcaExport = false;
100
101 private DirectoryDialog folderDialog;
102 private Text text_exportFileName;
103
104 private Text text_folder;
105
106 private final String type;
107
108 private final String extension;
109
110 private Combo classificationSelectionCombo;
111
112 private List<Classification> classifications;
113
114 private Classification selectedClassification;
115
116 private Label classificationLabel;
117
118 private ExportConfiguratorBase configurator;
119 private Button checkUseSelectedtaxonNode;
120
121 private Button checkUseSelectedClassification;
122 private Button checkUseAllClassification;
123
124 private Button checkExportUnpublished;
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 OUTPUT_MODEL_EXPORT:
141 outputModelExport = 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 OUTPUT_MODEL_EXPORT,
195 OUTPUT_MODEL_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 (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);
249
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);
254 }
255 if(classifications == null){
256 classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
257 Collections.sort(classifications, new Comparator<Classification>() {
258
259 @Override
260 public int compare(Classification o1, Classification 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 });
271 if (!configurator.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
272 selectedClassification = CdmStore.getService(IClassificationService.class).load(configurator.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
273 }else{
274
275 selectedClassification = classifications.iterator().next();
276 }
277 }
278
279
280 if(csvExport || csvPrintExport || outputModelExport|| csvNameExport || dwcaExport){
281
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();
286 grid.numColumns = 1;
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());
294 count--;
295 if (node.hasTaxon()){
296 taxonStr += node.getTaxon().getName().getTitleCache();
297 if (count>0){
298 taxonStr += ", ";
299 }
300 }
301 }
302
303 checkUseSelectedtaxonNode.setText("Export selected subtree(s) ("+ taxonStr+")");
304 checkUseSelectedtaxonNode.addListener(SWT.Selection, new Listener() {
305 @Override
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);
311
312 }
313 });
314 checkUseSelectedtaxonNode.setSelection(true);
315 }
316 if(outputModelExport || dwcaExport){
317 if (dwcaExport){
318 checkUseAllClassification= new Button(selectNodeOrClassification, SWT.RADIO);
319 checkUseAllClassification.setText("Export all classifications");
320 checkUseAllClassification.addListener(SWT.Selection, new Listener() {
321 @Override
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);
327
328 }
329 });
330 }
331 checkUseSelectedClassification= new Button(selectNodeOrClassification, SWT.RADIO);
332 checkUseSelectedClassification.setText("Export selected classification");
333 checkUseSelectedClassification.addListener(SWT.Selection, new Listener() {
334 @Override
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);
340
341 }
342 });
343 }
344
345 if (node!= null){
346 selectedClassification = node.getClassification();
347 }
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);
355
356 Composite comp = new Composite(composite, SWT.NONE);
357
358 GridData gridData = new GridData();
359 gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
360 gridData.horizontalSpan = 2;
361 comp.setLayoutData(gridData);
362
363 Label label = new Label(comp, SWT.NONE);
364 label.setText("Sorting of taxa");
365
366 orderSelectionCombo = new Combo(comp, SWT.BORDER| SWT.READ_ONLY);
367
368 GridLayoutFactory.fillDefaults().applyTo(comp);
369 for(NavigatorOrderEnum display: NavigatorOrderEnum.values()){
370 orderSelectionCombo.add(display.getLabel());
371 }
372
373 int index = 0;
374 if (PreferencesUtil.getStringValue(PreferencePredicate.TaxonNodeOrder.getKey()) != null){
375 NavigatorOrderEnum orderInNavigator = NavigatorOrderEnum.valueOf(PreferencesUtil.getStringValue(PreferencePredicate.TaxonNodeOrder.getKey()));
376
377 for (String itemLabel : orderSelectionCombo.getItems()){
378 if (itemLabel.equalsIgnoreCase(orderInNavigator.getLabel())){
379 orderSelectionCombo.select(index);
380 setComparatorToConfig();
381 break;
382 }
383 index++;
384 }
385 }
386
387 orderSelectionCombo.addSelectionListener(new SelectionListener() {
388
389 @Override
390 public void widgetSelected(SelectionEvent e) {
391 setComparatorToConfig();
392 }
393
394 @Override
395 public void widgetDefaultSelected(SelectionEvent e) {
396 // TODO Auto-generated method stub
397 }
398 });
399 }
400
401 if (checkUseSelectedtaxonNode != null){
402 if (checkUseSelectedtaxonNode.getSelection()){
403 classificationSelectionCombo.setEnabled(false);
404 }
405 }else{
406 classificationSelectionCombo.setEnabled(true);
407 classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
408 true, false, 2, 1));
409 }
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);
414
415 }
416
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,
420 true, false, 2, 1));
421 folderDialog = new DirectoryDialog(parent.getShell());
422 String exportFolder = PreferencesUtil.getStringValue(EXPORT_FOLDER, true);
423 folderDialog.setFilterPath(exportFolder);
424
425 text_folder = new Text(composite, SWT.BORDER);
426 text_folder.setEditable(false);
427 text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
428 false));
429 if (exportFolder != null){
430 text_folder.setText(exportFolder);
431 setPageComplete(true);
432 }
433
434 Button button = new Button(composite, SWT.PUSH);
435 button.setText("Browse...");
436
437 button.addSelectionListener(new SelectionAdapter() {
438 @Override
439 public void widgetSelected(SelectionEvent e) {
440 super.widgetSelected(e);
441
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);
447 }
448 }
449 });
450 if (!outputModelExport){
451
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,
457 true, false));
458 }
459
460 // make the composite the wizard pages control
461 setControl(composite);
462 }
463
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());
469 }else {
470 ((CdmLightExportConfigurator)configurator).setComparator(new TaxonNodeDtoByRankAndNameComparator());
471 }
472 }
473
474 protected String generateFilename() {
475 StringBuffer buffer = new StringBuffer();
476
477 Calendar cal = Calendar.getInstance();
478 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
479 buffer.append(sdf.format(cal.getTime()));
480
481 buffer.append("-");
482 buffer.append(type +"_");
483
484 String source = ((ICdmRemoteSource)CdmStore.getActiveCdmSource()).toString();
485 source = source.substring(source.indexOf("/")+1);
486 buffer.append(source);
487
488 buffer.append(".");
489 buffer.append(extension);
490
491 return buffer.toString();
492 }
493
494 public String getExportFileName() {
495 return text_exportFileName.getText();
496 }
497
498 public String getFolderText() {
499 return text_folder.getText();
500 }
501
502 public Text getFolderComposite() {
503 return text_folder;
504 }
505
506 public boolean isExportUnpublishedData(){
507 return checkExportUnpublished.getSelection();
508 }
509
510 private void createClassificationSelectionCombo(Composite parent){
511
512 // Composite classificationSelection = new Composite(parent, SWT.NULL);
513 // classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
514
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);
521
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));
526 }
527
528 classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
529
530 // TODO remember last selection
531 classificationSelectionCombo.addSelectionListener(this);
532
533 //return classificationSelection;
534 }
535
536 public Combo getCombo(){
537 return classificationSelectionCombo;
538 }
539
540 @Override
541 public void widgetSelected(SelectionEvent e) {
542 selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
543 }
544
545 public Classification getSelectedClassification(){
546 return selectedClassification;
547 }
548
549 @Override
550 public void widgetDefaultSelected(SelectionEvent e) {
551 //not needed here
552 }
553
554 public UUID getSelectedClassificationUUID() {
555 return selectedClassification.getUuid();
556 }
557
558 public boolean getCheckUseSelectedTaxonNode() {
559 if (checkUseSelectedtaxonNode== null){
560 return false;
561 }
562 return checkUseSelectedtaxonNode.getSelection();
563 }
564 public boolean getCheckUseAllClassifications() {
565 if (checkUseAllClassification== null){
566 return false;
567 }
568 return checkUseAllClassification.getSelection();
569 }
570
571 public Set<UUID> getAllClassificationUuids(){
572 Set<UUID> allClassificationUuids = new HashSet<>();
573 for (Classification classification: this.classifications){
574 allClassificationUuids.add(classification.getUuid());
575 }
576 return allClassificationUuids;
577 }
578
579 @Override
580 public boolean canFlipToNextPage() {
581 return getFolderText() != null;
582 }
583 }