Project

General

Profile

Download (20.3 KB) Statistics
| Branch: | Tag: | Revision:
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.CdmRemoteSource;
58
import eu.etaxonomy.taxeditor.store.CdmStore;
59

    
60
/**
61
 * <p>
62
 * ExportToFileDestinationWizardPage class.
63
 * </p>
64
 *
65
 * @author n.hoffmann
66
 * @created 15.06.2009
67
 * @version 1.0
68
 */
69
public class ExportToFileDestinationWizardPage extends WizardPage implements SelectionListener {
70

    
71
	/** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
72
	public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
73

    
74
	/** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
75
	public static final String JAXB_EXPORT = "JAXB_EXPORT";
76

    
77
	/** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
78
	public static final String TCS_EXPORT = "TCS_EXPORT";
79

    
80
	/** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
81
	public static final String SDD_EXPORT = "SDD_EXPORT";
82

    
83
	public static final String DWCA_EXPORT = "DWCA_EXPORT";
84
	public static final String OUTPUT_MODEL_EXPORT = "CDM_LIGHT(CSV)";
85
	public static final String CSV_EXPORT = "CSV_EXPORT";
86
	public static final String CSV_NAME_EXPORT = "CSV_NAME_EXPORT";
87
	private static final String CSV_PRINT_EXPORT ="CSV_PRINT_EXPORT";
88
	private static final String EXPORT_FOLDER ="EXPORT_FOLDER";
89
	public static final String XML = "xml";
90

    
91
	public static final String CSV = "csv";
92

    
93
	public static final String ZIP = "zip";
94

    
95
    private boolean csvExport = false;
96

    
97
    private boolean csvNameExport = false;
98

    
99
	private boolean csvPrintExport = false;
100

    
101
	private boolean outputModelExport = false;
102
	private boolean dwcaExport = false;
103

    
104
	private DirectoryDialog folderDialog;
105
	private Text text_exportFileName;
106

    
107
	private Text text_folder;
108

    
109
	private final String type;
110

    
111
	private final String extension;
112

    
113
    private Combo classificationSelectionCombo;
114

    
115
    private List<Classification> classifications;
116

    
117
    private Classification selectedClassification;
118

    
119
    private Label classificationLabel;
120

    
121
    private ExportConfiguratorBase configurator;
122
    private Button checkUseSelectedtaxonNode;
123

    
124
    private Button checkUseSelectedClassification;
125
    private Button checkUseAllClassification;
126

    
127
    private Button checkExportUnpublished;
128
    Combo orderSelectionCombo;
129

    
130

    
131
	protected ExportToFileDestinationWizardPage(String pageName, String type,
132
			String title, String description, String extension, ExportConfiguratorBase configurator) {
133
		super(pageName);
134
		this.configurator = configurator;
135
		this.type = type;
136
		switch(type) {
137
		   case CSV_EXPORT :
138
			   csvExport = true;
139
			   break;
140
		   case CSV_NAME_EXPORT:
141
			   csvNameExport = true;
142
			   break;
143
		   case OUTPUT_MODEL_EXPORT:
144
			   outputModelExport = true;
145
			   break;
146
		   case CSV_PRINT_EXPORT:
147
			   csvPrintExport = true;
148
			   break;
149
		   case DWCA_EXPORT:
150
			   dwcaExport = true;
151
			   break;
152

    
153
		}
154
		this.extension = extension;
155
		this.setTitle(title);
156
		this.setDescription(description);
157
	}
158

    
159
	public static ExportToFileDestinationWizardPage Jaxb(JaxbExportConfigurator configurator) {
160
		return new ExportToFileDestinationWizardPage(
161
				JAXB_EXPORT,
162
				"jaxb",
163
				"JAXB Export",
164
				"Exports the content of the currently selected database into the cdm jaxb format.",
165
				XML, configurator);
166
	}
167

    
168
	public static ExportToFileDestinationWizardPage Tcs(TcsXmlExportConfigurator config) {
169
		return new ExportToFileDestinationWizardPage(
170
				TCS_EXPORT,
171
				"tcs",
172
				"Tcs Export",
173
				"Export the content of the currently selected database into TCS format.",
174
				XML, config);
175
	}
176

    
177
	public static ExportToFileDestinationWizardPage Sdd(SDDExportConfigurator config) {
178
		return new ExportToFileDestinationWizardPage(
179
				SDD_EXPORT,
180
				"sdd",
181
				"Sdd Export",
182
				"Export the content of the currently selected database into SDD format.",
183
				XML, config);
184
	}
185

    
186
	public static ExportToFileDestinationWizardPage Dwca(DwcaTaxExportConfigurator config) {
187
		return new ExportToFileDestinationWizardPage(
188
				DWCA_EXPORT,
189
				DWCA_EXPORT,
190
				"DwC-Archive Export",
191
				"Export the content of the currently selected database into Darwin Core Archive format.",
192
				ZIP, config);
193
	}
194

    
195
    public static ExportToFileDestinationWizardPage OutputModel(CdmLightExportConfigurator config) {
196

    
197
        return new ExportToFileDestinationWizardPage(
198
                OUTPUT_MODEL_EXPORT,
199
                OUTPUT_MODEL_EXPORT,
200
                "CDM Light Export (csv)",
201
                "Export the content of the currently selected database into the CDM light (csv) format.",
202
                CSV,config);
203
    }
204

    
205
    public static ExportToFileDestinationWizardPage Csv(CsvDemoExportConfigurator config) {
206

    
207
        return new ExportToFileDestinationWizardPage(
208
                CSV_EXPORT,
209
                CSV_EXPORT,
210
                "CSV Export",
211
                "Export the content of the currently selected database into Comma Separated Value format.",
212
                CSV, config);
213
    }
214

    
215
    public static ExportToFileDestinationWizardPage CsvNames(CsvNameExportConfigurator config) {
216

    
217
        return new ExportToFileDestinationWizardPage(
218
                CSV_NAME_EXPORT,
219
                CSV_NAME_EXPORT,
220
                "CSV Name Export",
221
                "Export the names of the currently selected database into Semicolon Separated Value format.",
222
                CSV, config);
223
    }
224

    
225
    public static ExportToFileDestinationWizardPage CsvPrint(CsvNameExportConfigurator config) {
226

    
227
        return new ExportToFileDestinationWizardPage(
228
                CSV_PRINT_EXPORT,
229
                CSV_NAME_EXPORT,
230
                "CSV Print Export",
231
                "Export the content of the currently selected database into Semicolon Separated Value format.",
232
                CSV, config);
233
    }
234

    
235
	/** {@inheritDoc} */
236
	@Override
237
    public void createControl(Composite parent) {
238

    
239
		setPageComplete(false);
240

    
241
		Composite composite = new Composite(parent, SWT.NONE);
242
		GridLayout gridLayout = new GridLayout();
243

    
244
		gridLayout.numColumns = 2;
245
		TaxonNode node = null;
246

    
247
		composite.setLayout(gridLayout);
248
		if (outputModelExport || dwcaExport){
249
            checkExportUnpublished = new Button(composite,  SWT.CHECK);
250
            checkExportUnpublished.setText("Export unpublished taxa");
251
            Label label = new Label(composite, SWT.NONE);
252

    
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);
257
        }
258
		if(classifications == null){
259
			classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
260
			Collections.sort(classifications, new Comparator<Classification>() {
261

    
262
                @Override
263
                public int compare(Classification o1, Classification o2) {
264
                    if (o1.equals(o2)){
265
                        return 0;
266
                    }
267
                    int result = o1.getTitleCache().compareTo(o2.getTitleCache());
268
                    if (result == 0){
269
                        return o1.getUuid().compareTo(o2.getUuid());
270
                    }
271
                    return result;
272
                }
273
            });
274
			if (!configurator.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
275
				selectedClassification = CdmStore.getService(IClassificationService.class).load(configurator.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
276
			}else{
277

    
278
				selectedClassification = classifications.iterator().next();
279
			}
280
		}
281

    
282

    
283
		if(csvExport || csvPrintExport || outputModelExport|| csvNameExport || dwcaExport){
284

    
285
//		    Label comboBoxLabel = new Label(composite, SWT.NONE);
286
//		    comboBoxLabel.setText("Classification");
287
			Composite selectNodeOrClassification = new Composite(composite, SWT.NONE);
288
			GridLayout grid = new GridLayout();
289
            grid.numColumns = 1;
290
            selectNodeOrClassification.setLayout(grid);
291
			if ((outputModelExport || dwcaExport)&& !configurator.getTaxonNodeFilter().getSubtreeFilter().isEmpty()){
292
				checkUseSelectedtaxonNode= new Button(selectNodeOrClassification,  SWT.RADIO);
293
				String taxonStr = "";
294
				int count = configurator.getTaxonNodeFilter().getSubtreeFilter().size();
295
				for (LogicFilter<TaxonNode> filter: configurator.getTaxonNodeFilter().getSubtreeFilter()){
296
					node = CdmApplicationState.getCurrentAppConfig().getTaxonNodeService().load(filter.getUuid());
297
					count--;
298
					if (node.hasTaxon()){
299
						taxonStr += node.getTaxon().getName().getTitleCache();
300
						if (count>0){
301
							taxonStr += ", ";
302
						}
303
					}
304
				}
305

    
306
				checkUseSelectedtaxonNode.setText("Export selected subtree(s) ("+  taxonStr+")");
307
				checkUseSelectedtaxonNode.addListener(SWT.Selection, new Listener() {
308
					@Override
309
					public void handleEvent(Event e) {
310
						Button b = (Button) e.widget;
311
		                GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
312
		                data.exclude = b.getSelection();
313
		                classificationSelectionCombo.setEnabled(!data.exclude);
314

    
315
		            	}
316
		        	});
317
				  checkUseSelectedtaxonNode.setSelection(true);
318
			}
319
			if(outputModelExport || dwcaExport){
320
				  if (dwcaExport){
321
					  checkUseAllClassification= new Button(selectNodeOrClassification,  SWT.RADIO);
322
					  checkUseAllClassification.setText("Export all classifications");
323
					  checkUseAllClassification.addListener(SWT.Selection, new Listener() {
324
			               @Override
325
			               public void handleEvent(Event e) {
326
			            	   Button b = (Button) e.widget;
327
				               GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
328
				               data.exclude = b.getSelection();
329
				               classificationSelectionCombo.setEnabled(!data.exclude);
330

    
331
			               }
332
					  	});
333
				  }
334
				  checkUseSelectedClassification= new Button(selectNodeOrClassification,  SWT.RADIO);
335
				  checkUseSelectedClassification.setText("Export selected classification");
336
				  checkUseSelectedClassification.addListener(SWT.Selection, new Listener() {
337
		               @Override
338
		               public void handleEvent(Event e) {
339
		            	   Button b = (Button) e.widget;
340
		                   GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
341
		                   data.exclude = b.getSelection();
342
		                   classificationSelectionCombo.setEnabled(data.exclude);
343

    
344
		                  }
345
				  	});
346
			}
347

    
348
			if (node!= null){
349
				selectedClassification = node.getClassification();
350
			}
351
			createClassificationSelectionCombo(selectNodeOrClassification);
352
			if (outputModelExport){
353
			    Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
354
			    GridData gridDataSep = new GridData();
355
			    gridDataSep = new GridData(GridData.BEGINNING, GridData.CENTER, true, true);
356
			    gridDataSep.horizontalSpan = 2;
357
                separator.setLayoutData(gridDataSep);
358

    
359
                Composite comp = new Composite(composite, SWT.NONE);
360

    
361
                GridData gridData = new GridData();
362
	            gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
363
	            gridData.horizontalSpan = 2;
364
	            comp.setLayoutData(gridData);
365

    
366
	            Label label = new Label(comp, SWT.NONE);
367
                label.setText("Sorting of taxa");
368

    
369
			    orderSelectionCombo = new Combo(comp, SWT.BORDER| SWT.READ_ONLY);
370

    
371
			    GridLayoutFactory.fillDefaults().applyTo(comp);
372
	            for(NavigatorOrderEnum display: NavigatorOrderEnum.values()){
373
	                orderSelectionCombo.add(display.getLabel());
374
	            }
375

    
376
	            int index = 0;
377
	            if (PreferencesUtil.getStringValue(PreferencePredicate.TaxonNodeOrder.getKey()) != null){
378
	                NavigatorOrderEnum orderInNavigator = NavigatorOrderEnum.valueOf(PreferencesUtil.getStringValue(PreferencePredicate.TaxonNodeOrder.getKey()));
379

    
380
	                for (String itemLabel : orderSelectionCombo.getItems()){
381
	                    if (itemLabel.equalsIgnoreCase(orderInNavigator.getLabel())){
382
	                        orderSelectionCombo.select(index);
383
	                        setComparatorToConfig();
384
	                        break;
385
	                    }
386
	                    index++;
387
	                }
388
	            }
389

    
390
	            orderSelectionCombo.addSelectionListener(new SelectionListener() {
391

    
392
                    @Override
393
                    public void widgetSelected(SelectionEvent e) {
394
                        setComparatorToConfig();
395
                    }
396

    
397
                    @Override
398
                    public void widgetDefaultSelected(SelectionEvent e) {
399
                        // TODO Auto-generated method stub
400

    
401
                    }
402
                });
403

    
404
			}
405

    
406

    
407
			if (checkUseSelectedtaxonNode != null){
408

    
409
				if (checkUseSelectedtaxonNode.getSelection()){
410
					 classificationSelectionCombo.setEnabled(false);
411
				}
412
			}else{
413
			    classificationSelectionCombo.setEnabled(true);
414
			    classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
415
                      true, false, 2, 1));
416
			}
417
			 Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
418
            GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
419
            gridData.horizontalSpan = 2;
420
            separator.setLayoutData(gridData);
421

    
422
		}
423

    
424
		Label folderLabel = new Label(composite, SWT.NONE);
425
		folderLabel.setText("Select Folder for exported files");
426
		folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
427
        true, false, 2, 1));
428
		folderDialog = new DirectoryDialog(parent.getShell());
429
		folderDialog.setFilterPath(PreferencesUtil.getStringValue(EXPORT_FOLDER));
430

    
431
		text_folder = new Text(composite, SWT.BORDER);
432
		text_folder.setEditable(false);
433
		text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
434
				false));
435
		if (PreferencesUtil.getStringValue(EXPORT_FOLDER) != null){
436
			text_folder.setText(PreferencesUtil.getStringValue(EXPORT_FOLDER));
437
			setPageComplete(true);
438
		}
439

    
440
		Button button = new Button(composite, SWT.PUSH);
441
		button.setText("Browse...");
442

    
443
		button.addSelectionListener(new SelectionAdapter() {
444
			@Override
445
			public void widgetSelected(SelectionEvent e) {
446
				super.widgetSelected(e);
447

    
448
				String path = folderDialog.open();
449
				if (path != null) { // a folder was selected
450
					text_folder.setText(path);
451
					PreferencesUtil.setStringValue(EXPORT_FOLDER, path);
452
					setPageComplete(true);
453
				}
454
			}
455
		});
456
		if (!outputModelExport){
457

    
458
			 Label fileLabel = new Label(composite, SWT.NONE);
459
		        fileLabel.setText("File");
460
	    		text_exportFileName = new Text(composite, SWT.BORDER);
461
	    		text_exportFileName.setText(generateFilename());
462
	    		text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
463
	    				true, false));
464
		}
465

    
466

    
467
		// make the composite the wizard pages control
468
		setControl(composite);
469
	}
470

    
471
    protected void setComparatorToConfig() {
472
        if (orderSelectionCombo.getText().equals(NavigatorOrderEnum.NaturalOrder.getLabel())){
473
            ((CdmLightExportConfigurator)configurator).setComparator(new TaxonNodeDtoNaturalComparator());
474
        } else if (orderSelectionCombo.getText().equals(NavigatorOrderEnum.AlphabeticalOrder.getLabel())){
475
            ((CdmLightExportConfigurator)configurator).setComparator(new TaxonNodeDtoByNameComparator());
476
        }else {
477
            ((CdmLightExportConfigurator)configurator).setComparator(new TaxonNodeDtoByRankAndNameComparator());
478
        }
479
    }
480

    
481
	protected String generateFilename() {
482
		StringBuffer buffer = new StringBuffer();
483

    
484
		Calendar cal = Calendar.getInstance();
485
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
486
		buffer.append(sdf.format(cal.getTime()));
487

    
488
		buffer.append("-");
489
		buffer.append(type +"_");
490

    
491
		String source = ((CdmRemoteSource)CdmStore.getActiveCdmSource()).toString();
492
		source = source.substring(source.indexOf("/")+1);
493
		buffer.append(source);
494

    
495

    
496
		buffer.append(".");
497
		buffer.append(extension);
498

    
499
		return buffer.toString();
500
	}
501

    
502
	public String getExportFileName() {
503
		return text_exportFileName.getText();
504
	}
505

    
506
	public String getFolderText() {
507
		return text_folder.getText();
508
	}
509

    
510
	public Text getFolderComposite() {
511
		return text_folder;
512
	}
513

    
514
	public boolean isExportUnpublishedData(){
515
	    return checkExportUnpublished.getSelection();
516
	}
517

    
518
	private void createClassificationSelectionCombo(Composite parent){
519

    
520
//		Composite classificationSelection = new Composite(parent, SWT.NULL);
521
//		classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
522

    
523
//		GridLayout layout = new GridLayout();
524
//		classificationSelection.setLayout(layout);
525
		GridData gridData = new GridData();
526
		gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
527
		gridData.horizontalIndent = 5;
528
//		classificationSelection.setLayoutData(gridData);
529

    
530
		classificationSelectionCombo = new Combo(parent, SWT.BORDER| SWT.READ_ONLY);
531
		classificationSelectionCombo.setLayoutData(gridData);
532
		for(Classification tree : classifications){
533
			classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
534

    
535
		}
536

    
537
		classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
538

    
539
		// TODO remember last selection
540
		classificationSelectionCombo.addSelectionListener(this);
541

    
542

    
543

    
544
		//return classificationSelection;
545
	}
546

    
547
	public Combo getCombo(){
548
	    return classificationSelectionCombo;
549
	}
550

    
551
	@Override
552
	public void widgetSelected(SelectionEvent e) {
553
		selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
554

    
555
	}
556

    
557
	@Override
558
	public void widgetDefaultSelected(SelectionEvent e) {
559
		//not needed here
560

    
561
	}
562

    
563
	public UUID getSelectedClassificationUUID() {
564

    
565
		return selectedClassification.getUuid();
566
	}
567

    
568
	public boolean getCheckUseSelectedTaxonNode() {
569
		if (checkUseSelectedtaxonNode== null){
570
			return false;
571
		}
572
		return checkUseSelectedtaxonNode.getSelection();
573
	}
574
	public boolean getCheckUseAllClassifications() {
575
		if (checkUseAllClassification== null){
576
			return false;
577
		}
578
		return checkUseAllClassification.getSelection();
579
	}
580

    
581
	public Set<UUID> getAllClassificationUuids(){
582
		Set<UUID> allClassificationUuids = new HashSet();
583
		for (Classification classification: this.classifications){
584
			allClassificationUuids.add(classification.getUuid());
585
		}
586
		return allClassificationUuids;
587
	}
588

    
589
	@Override
590
	public boolean canFlipToNextPage() {
591
        return  getFolderText() != null;
592
    }
593

    
594
}
(17-17/30)