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
		String exportFolder = PreferencesUtil.getStringValue(EXPORT_FOLDER, true);
430
		folderDialog.setFilterPath(exportFolder);
431

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

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

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

    
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);
454
				}
455
			}
456
		});
457
		if (!outputModelExport){
458

    
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,
464
	    				true, false));
465
		}
466

    
467

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

    
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());
477
        }else {
478
            ((CdmLightExportConfigurator)configurator).setComparator(new TaxonNodeDtoByRankAndNameComparator());
479
        }
480
    }
481

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

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

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

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

    
496

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

    
500
		return buffer.toString();
501
	}
502

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

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

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

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

    
519
	private void createClassificationSelectionCombo(Composite parent){
520

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

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

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

    
536
		}
537

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

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

    
543

    
544

    
545
		//return classificationSelection;
546
	}
547

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

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

    
556
	}
557

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

    
562
	}
563

    
564
	public UUID getSelectedClassificationUUID() {
565

    
566
		return selectedClassification.getUuid();
567
	}
568

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

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

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

    
595
}
(17-17/30)