Project

General

Profile

Download (17.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.wizard.WizardPage;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.events.SelectionAdapter;
24
import org.eclipse.swt.events.SelectionEvent;
25
import org.eclipse.swt.events.SelectionListener;
26
import org.eclipse.swt.layout.GridData;
27
import org.eclipse.swt.layout.GridLayout;
28
import org.eclipse.swt.widgets.Button;
29
import org.eclipse.swt.widgets.Combo;
30
import org.eclipse.swt.widgets.Composite;
31
import org.eclipse.swt.widgets.DirectoryDialog;
32
import org.eclipse.swt.widgets.Event;
33
import org.eclipse.swt.widgets.Label;
34
import org.eclipse.swt.widgets.Listener;
35
import org.eclipse.swt.widgets.Text;
36

    
37
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
38
import eu.etaxonomy.cdm.api.service.IClassificationService;
39
import eu.etaxonomy.cdm.filter.LogicFilter;
40
import eu.etaxonomy.cdm.io.cdmLight.CdmLightExportConfigurator;
41
import eu.etaxonomy.cdm.io.common.ExportConfiguratorBase;
42
import eu.etaxonomy.cdm.io.csv.caryophyllales.out.CsvNameExportConfigurator;
43
import eu.etaxonomy.cdm.io.csv.redlist.demo.CsvDemoExportConfigurator;
44
import eu.etaxonomy.cdm.io.dwca.out.DwcaTaxExportConfigurator;
45
import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
46
import eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator;
47
import eu.etaxonomy.cdm.io.tcsxml.out.TcsXmlExportConfigurator;
48
import eu.etaxonomy.cdm.model.taxon.Classification;
49
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
50
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
51
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
52
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
53
import eu.etaxonomy.taxeditor.store.CdmStore;
54

    
55
/**
56
 * <p>
57
 * ExportToFileDestinationWizardPage class.
58
 * </p>
59
 *
60
 * @author n.hoffmann
61
 * @created 15.06.2009
62
 * @version 1.0
63
 */
64
public class ExportToFileDestinationWizardPage extends WizardPage implements SelectionListener {
65

    
66
	/** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
67
	public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
68

    
69
	/** Constant <code>JAXB_EXPORT="JAXB_EXPORT"</code> */
70
	public static final String JAXB_EXPORT = "JAXB_EXPORT";
71

    
72
	/** Constant <code>TCS_EXPORT="TCS_EXPORT"</code> */
73
	public static final String TCS_EXPORT = "TCS_EXPORT";
74

    
75
	/** Constant <code>SDD_EXPORT="SDD_EXPORT"</code> */
76
	public static final String SDD_EXPORT = "SDD_EXPORT";
77

    
78
	public static final String DWCA_EXPORT = "DWCA_EXPORT";
79
	public static final String OUTPUT_MODEL_EXPORT = "CDM_LIGHT(CSV)";
80
	public static final String CSV_EXPORT = "CSV_EXPORT";
81
	public static final String CSV_NAME_EXPORT = "CSV_NAME_EXPORT";
82
	private static final String CSV_PRINT_EXPORT ="CSV_PRINT_EXPORT";
83
	private static final String EXPORT_FOLDER ="EXPORT_FOLDER";
84
	public static final String XML = "xml";
85

    
86
	public static final String CSV = "csv";
87

    
88
	public static final String ZIP = "zip";
89

    
90
    private boolean csvExport = false;
91

    
92
    private boolean csvNameExport = false;
93

    
94
	private boolean csvPrintExport = false;
95

    
96
	private boolean outputModelExport = false;
97
	private boolean dwcaExport = false;
98

    
99
	private DirectoryDialog folderDialog;
100
	private Text text_exportFileName;
101

    
102
	private Text text_folder;
103

    
104
	private final String type;
105

    
106
	private final String extension;
107

    
108
    private Combo classificationSelectionCombo;
109

    
110
    private List<Classification> classifications;
111

    
112
    private Classification selectedClassification;
113

    
114
    private Label classificationLabel;
115

    
116
    private ExportConfiguratorBase configurator;
117
    private Button checkUseSelectedtaxonNode;
118

    
119
    private Button checkUseSelectedClassification;
120
    private Button checkUseAllClassification;
121

    
122
    private Button checkExportUnpublished;
123

    
124
	protected ExportToFileDestinationWizardPage(String pageName, String type,
125
			String title, String description, String extension, ExportConfiguratorBase configurator) {
126
		super(pageName);
127
		this.configurator = configurator;
128
		this.type = type;
129
		switch(type) {
130
		   case CSV_EXPORT :
131
			   csvExport = true;
132
			   break;
133
		   case CSV_NAME_EXPORT:
134
			   csvNameExport = true;
135
			   break;
136
		   case OUTPUT_MODEL_EXPORT:
137
			   outputModelExport = true;
138
			   break;
139
		   case CSV_PRINT_EXPORT:
140
			   csvPrintExport = true;
141
			   break;
142
		   case DWCA_EXPORT:
143
			   dwcaExport = true;
144
			   break;
145

    
146
		}
147
		this.extension = extension;
148
		this.setTitle(title);
149
		this.setDescription(description);
150
	}
151

    
152
	public static ExportToFileDestinationWizardPage Jaxb(JaxbExportConfigurator configurator) {
153
		return new ExportToFileDestinationWizardPage(
154
				JAXB_EXPORT,
155
				"jaxb",
156
				"JAXB Export",
157
				"Exports the content of the currently selected database into the cdm jaxb format.",
158
				XML, configurator);
159
	}
160

    
161
	public static ExportToFileDestinationWizardPage Tcs(TcsXmlExportConfigurator config) {
162
		return new ExportToFileDestinationWizardPage(
163
				TCS_EXPORT,
164
				"tcs",
165
				"Tcs Export",
166
				"Export the content of the currently selected database into TCS format.",
167
				XML, config);
168
	}
169

    
170
	public static ExportToFileDestinationWizardPage Sdd(SDDExportConfigurator config) {
171
		return new ExportToFileDestinationWizardPage(
172
				SDD_EXPORT,
173
				"sdd",
174
				"Sdd Export",
175
				"Export the content of the currently selected database into SDD format.",
176
				XML, config);
177
	}
178

    
179
	public static ExportToFileDestinationWizardPage Dwca(DwcaTaxExportConfigurator config) {
180
		return new ExportToFileDestinationWizardPage(
181
				DWCA_EXPORT,
182
				DWCA_EXPORT,
183
				"DwC-Archive Export",
184
				"Export the content of the currently selected database into Darwin Core Archive format.",
185
				ZIP, config);
186
	}
187

    
188
    public static ExportToFileDestinationWizardPage OutputModel(CdmLightExportConfigurator config) {
189

    
190
        return new ExportToFileDestinationWizardPage(
191
                OUTPUT_MODEL_EXPORT,
192
                OUTPUT_MODEL_EXPORT,
193
                "CDM Light Export (csv)",
194
                "Export the content of the currently selected database into the CDM light (csv) format.",
195
                CSV,config);
196
    }
197

    
198
    public static ExportToFileDestinationWizardPage Csv(CsvDemoExportConfigurator config) {
199

    
200
        return new ExportToFileDestinationWizardPage(
201
                CSV_EXPORT,
202
                CSV_EXPORT,
203
                "CSV Export",
204
                "Export the content of the currently selected database into Comma Separated Value format.",
205
                CSV, config);
206
    }
207

    
208
    public static ExportToFileDestinationWizardPage CsvNames(CsvNameExportConfigurator config) {
209

    
210
        return new ExportToFileDestinationWizardPage(
211
                CSV_NAME_EXPORT,
212
                CSV_NAME_EXPORT,
213
                "CSV Name Export",
214
                "Export the names of the currently selected database into Semicolon Separated Value format.",
215
                CSV, config);
216
    }
217

    
218
    public static ExportToFileDestinationWizardPage CsvPrint(CsvNameExportConfigurator config) {
219

    
220
        return new ExportToFileDestinationWizardPage(
221
                CSV_PRINT_EXPORT,
222
                CSV_NAME_EXPORT,
223
                "CSV Print Export",
224
                "Export the content of the currently selected database into Semicolon Separated Value format.",
225
                CSV, config);
226
    }
227

    
228
	/** {@inheritDoc} */
229
	@Override
230
    public void createControl(Composite parent) {
231

    
232
		setPageComplete(false);
233

    
234
		Composite composite = new Composite(parent, SWT.NONE);
235
		GridLayout gridLayout = new GridLayout();
236

    
237
		gridLayout.numColumns = 2;
238
		TaxonNode node = null;
239

    
240
		composite.setLayout(gridLayout);
241
		if (outputModelExport || dwcaExport){
242
            checkExportUnpublished = new Button(composite,  SWT.CHECK);
243
            checkExportUnpublished.setText("Export unpublished taxa");
244
            Label label = new Label(composite, SWT.NONE);
245
            Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
246
            GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
247
            gridData.horizontalSpan = 2;
248
            separator.setLayoutData(gridData);
249
        }
250
		if(classifications == null){
251
			classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
252
			Collections.sort(classifications, new Comparator<Classification>() {
253

    
254
                @Override
255
                public int compare(Classification o1, Classification o2) {
256
                    if (o1.equals(o2)){
257
                        return 0;
258
                    }
259
                    int result = o1.getTitleCache().compareTo(o2.getTitleCache());
260
                    if (result == 0){
261
                        return o1.getUuid().compareTo(o2.getUuid());
262
                    }
263
                    return result;
264
                }
265
            });
266
			if (!configurator.getTaxonNodeFilter().getClassificationFilter().isEmpty()){
267
				selectedClassification = CdmStore.getService(IClassificationService.class).load(configurator.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
268
			}else{
269

    
270
				selectedClassification = classifications.iterator().next();
271
			}
272
		}
273

    
274

    
275
		if(csvExport || csvPrintExport || outputModelExport|| csvNameExport || dwcaExport){
276

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

    
298
				checkUseSelectedtaxonNode.setText("Export selected subtree(s) ("+  taxonStr+")");
299
				checkUseSelectedtaxonNode.addListener(SWT.Selection, new Listener() {
300
					@Override
301
					public void handleEvent(Event e) {
302
						Button b = (Button) e.widget;
303
		                GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
304
		                data.exclude = b.getSelection();
305
		                classificationSelectionCombo.setVisible(!data.exclude);
306

    
307
		            	}
308
		        	});
309
				  checkUseSelectedtaxonNode.setSelection(true);
310
			}
311
			if(outputModelExport || dwcaExport){
312
				  if (dwcaExport){
313
					  checkUseAllClassification= new Button(selectNodeOrClassification,  SWT.RADIO);
314
					  checkUseAllClassification.setText("Export all classifications");
315
					  checkUseAllClassification.addListener(SWT.Selection, new Listener() {
316
			               @Override
317
			               public void handleEvent(Event e) {
318
			            	   Button b = (Button) e.widget;
319
				               GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
320
				               data.exclude = b.getSelection();
321
				               classificationSelectionCombo.setVisible(!data.exclude);
322

    
323
			               }
324
					  	});
325
				  }
326
				  checkUseSelectedClassification= new Button(selectNodeOrClassification,  SWT.RADIO);
327
				  checkUseSelectedClassification.setText("Export selected classification");
328
				  checkUseSelectedClassification.addListener(SWT.Selection, new Listener() {
329
		               @Override
330
		               public void handleEvent(Event e) {
331
		            	   Button b = (Button) e.widget;
332
		                   GridData data = (GridData)  classificationSelectionCombo.getLayoutData();
333
		                   data.exclude = b.getSelection();
334
		                   classificationSelectionCombo.setVisible(data.exclude);
335

    
336
		                  }
337
				  	});
338
			}
339

    
340
			if (node!= null){
341
				selectedClassification = node.getClassification();
342
			}
343
			createClassificationSelectionCombo(selectNodeOrClassification);
344

    
345

    
346

    
347
			if (checkUseSelectedtaxonNode != null){
348

    
349
				if (checkUseSelectedtaxonNode.getSelection()){
350
					 classificationSelectionCombo.setVisible(false);
351

    
352
				}
353
			}else{
354
			    classificationSelectionCombo.setVisible(true);
355
			    classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
356
                      true, false, 2, 1));
357
			}
358
			 Label separator = new Label(composite, SWT.HORIZONTAL | SWT.SEPARATOR);
359
            GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
360
            gridData.horizontalSpan = 2;
361
            separator.setLayoutData(gridData);
362

    
363
		}
364

    
365
		Label folderLabel = new Label(composite, SWT.NONE);
366
		folderLabel.setText("Select Folder for exported files");
367
		folderLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
368
        true, false, 2, 1));
369
		folderDialog = new DirectoryDialog(parent.getShell());
370
		folderDialog.setFilterPath(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
371

    
372
		text_folder = new Text(composite, SWT.BORDER);
373
		text_folder.setEditable(false);
374
		text_folder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
375
				false));
376
		if (PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER) != null){
377
			text_folder.setText(PreferencesUtil.getPreferenceStore().getString(EXPORT_FOLDER));
378
			setPageComplete(true);
379
		}
380

    
381
		Button button = new Button(composite, SWT.PUSH);
382
		button.setText("Browse...");
383

    
384
		button.addSelectionListener(new SelectionAdapter() {
385
			@Override
386
			public void widgetSelected(SelectionEvent e) {
387
				super.widgetSelected(e);
388

    
389
				String path = folderDialog.open();
390
				if (path != null) { // a folder was selected
391
					text_folder.setText(path);
392
					PreferencesUtil.getPreferenceStore().setValue(EXPORT_FOLDER, path);
393
					setPageComplete(true);
394
				}
395
			}
396
		});
397
		if (!outputModelExport){
398

    
399
			 Label fileLabel = new Label(composite, SWT.NONE);
400
		        fileLabel.setText("File");
401
	    		text_exportFileName = new Text(composite, SWT.BORDER);
402
	    		text_exportFileName.setText(generateFilename());
403
	    		text_exportFileName.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
404
	    				true, false));
405
		}
406

    
407

    
408
		// make the composite the wizard pages control
409
		setControl(composite);
410
	}
411

    
412
	protected String generateFilename() {
413
		StringBuffer buffer = new StringBuffer();
414

    
415
		Calendar cal = Calendar.getInstance();
416
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
417
		buffer.append(sdf.format(cal.getTime()));
418

    
419
		buffer.append("-");
420

    
421
		buffer.append(type + "_export-");
422
		try {
423
			buffer.append(CdmDataSourceRepository.getCurrentCdmSource());
424
		} catch (CdmRemoteSourceException e) {
425
			buffer.append("Unknown");
426
		}
427

    
428
		buffer.append(".");
429
		buffer.append(extension);
430

    
431
		return buffer.toString();
432
	}
433

    
434
	public String getExportFileName() {
435
		return text_exportFileName.getText();
436
	}
437

    
438
	public String getFolderText() {
439
		return text_folder.getText();
440
	}
441

    
442
	public Text getFolderComposite() {
443
		return text_folder;
444
	}
445

    
446
	public boolean isExportUnpublishedData(){
447
	    return checkExportUnpublished.getSelection();
448
	}
449

    
450
	private void createClassificationSelectionCombo(Composite parent){
451

    
452
//		Composite classificationSelection = new Composite(parent, SWT.NULL);
453
//		classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
454

    
455
//		GridLayout layout = new GridLayout();
456
//		classificationSelection.setLayout(layout);
457
		GridData gridData = new GridData();
458
		gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
459
		gridData.horizontalIndent = 5;
460
//		classificationSelection.setLayoutData(gridData);
461

    
462
		classificationSelectionCombo = new Combo(parent, SWT.BORDER| SWT.READ_ONLY);
463
		classificationSelectionCombo.setLayoutData(gridData);
464
		for(Classification tree : classifications){
465
			classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
466

    
467
		}
468

    
469
		classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
470

    
471
		// TODO remember last selection
472
		classificationSelectionCombo.addSelectionListener(this);
473

    
474

    
475

    
476
		//return classificationSelection;
477
	}
478

    
479
	public Combo getCombo(){
480
	    return classificationSelectionCombo;
481
	}
482

    
483
	@Override
484
	public void widgetSelected(SelectionEvent e) {
485
		selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
486

    
487
	}
488

    
489
	@Override
490
	public void widgetDefaultSelected(SelectionEvent e) {
491
		//not needed here
492

    
493
	}
494

    
495
	public UUID getSelectedClassificationUUID() {
496

    
497
		return selectedClassification.getUuid();
498
	}
499

    
500
	public boolean getCheckUseSelectedTaxonNode() {
501
		if (checkUseSelectedtaxonNode== null){
502
			return false;
503
		}
504
		return checkUseSelectedtaxonNode.getSelection();
505
	}
506
	public boolean getCheckUseAllClassifications() {
507
		if (checkUseAllClassification== null){
508
			return false;
509
		}
510
		return checkUseAllClassification.getSelection();
511
	}
512

    
513
	public Set<UUID> getAllClassificationUuids(){
514
		Set<UUID> allClassificationUuids = new HashSet();
515
		for (Classification classification: this.classifications){
516
			allClassificationUuids.add(classification.getUuid());
517
		}
518
		return allClassificationUuids;
519
	}
520

    
521
	@Override
522
	public boolean canFlipToNextPage() {
523
        return  getFolderText() != null;
524
    }
525

    
526
}
(17-17/30)