Project

General

Profile

Download (5.46 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2007 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10

    
11
package eu.etaxonomy.taxeditor.io;
12

    
13
import org.eclipse.core.runtime.Assert;
14
import org.eclipse.core.runtime.IProgressMonitor;
15
import org.eclipse.core.runtime.IStatus;
16
import org.eclipse.core.runtime.Status;
17
import org.eclipse.core.runtime.jobs.Job;
18
import org.eclipse.swt.widgets.Display;
19
import org.eclipse.ui.IWorkbenchPart;
20

    
21
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
22
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
23
import eu.etaxonomy.cdm.io.common.CdmDefaultExport;
24
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
25
import eu.etaxonomy.cdm.io.common.IIoConfigurator;
26
import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
27
import eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator;
28
import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
29
import eu.etaxonomy.taxeditor.model.MessagingUtils;
30
import eu.etaxonomy.taxeditor.store.StoreUtil;
31

    
32
/**
33
 * <p>
34
 * ExportHandler class.
35
 * </p>
36
 *
37
 * @author n.hoffmann
38
 * @created Sep 11, 2009
39
 * @version 1.0
40
 */
41
public class ExportManager extends AbstractIOManager<IExportConfigurator> {
42

    
43
	/**
44
	 *
45
	 * @param applicationConfiguration
46
	 */
47
	private ExportManager(ICdmApplicationConfiguration applicationConfiguration) {
48
		super(applicationConfiguration);
49
	}
50

    
51
	/**
52
	 * <p>
53
	 * NewInstance
54
	 * </p>
55
	 *
56
	 * @param applicationConfiguration
57
	 *            a
58
	 *            {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
59
	 *            object.
60
	 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
61
	 */
62
	public static ExportManager NewInstance(
63
			ICdmApplicationConfiguration applicationConfiguration) {
64
		return new ExportManager(applicationConfiguration);
65
	}
66

    
67
	/**
68
	 * <p>
69
	 * createIOJob
70
	 * </p>
71
	 *
72
	 * @param configurator
73
	 *            a {@link eu.etaxonomy.cdm.io.common.IExportConfigurator}
74
	 *            object.
75
	 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
76
	 */
77
	@Override
78
	protected Job createIOJob(final IExportConfigurator configurator) {
79

    
80
		Assert.isNotNull(configurator, "Configuration may not be null");
81

    
82
		final Display display = Display.getCurrent();
83

    
84
		Job job = new Job("Export: " + configurator.getClass().getSimpleName()) {
85
			@Override
86
			protected IStatus run(IProgressMonitor monitor) {
87
				monitor.beginTask(
88
						"Exporting database. This will take some time.", 100);
89
				monitor.worked(10);
90

    
91
			    /** see ticket # 4456 and */
92
//				display.asyncExec(new Runnable() {
93
//
94
//					@Override
95
//					public void run() {
96
//
97
//					    // terminate any open transactions
98
//						IConversationEnabled activePart = (IConversationEnabled) StoreUtil
99
//								.getActivePage().getActivePart();
100
//						activePart.getConversationHolder().commit(false);
101
//					}
102
//				});
103

    
104
//				monitor.worked(10);
105

    
106
				CdmDefaultExport<IExportConfigurator> exporter = new CdmDefaultExport<IExportConfigurator>();
107
				configurator.setProgressMonitor(CdmProgressMonitorAdapter
108
						.CreateSubMonitor(monitor, 80));
109
				exporter.setCdmAppController(applicationConfiguration);
110
				monitor.worked(10);
111

    
112
				try {
113
					exporter.invoke(configurator);
114
					monitor.worked(60);
115
				} catch (RuntimeException e) {
116
					MessagingUtils.messageDialog("Error exporting data", this,
117
							"An error occured while"
118
									+ "exporting to destination '"
119
									+ configurator.getDestinationNameString()
120
									+ "'.\n"
121
									+ "Please check error log for details.", e);
122
				}
123

    
124
				display.asyncExec(new Runnable() {
125

    
126
					@Override
127
					public void run() {
128
						IWorkbenchPart activePart = StoreUtil.getActivePage()
129
								.getActivePart();
130
						if (activePart instanceof IConversationEnabled) {
131
							// terminate any open transactions
132
							IConversationEnabled conversationEnabled = (IConversationEnabled) activePart;
133
							// restarting transaction and committing it to
134
							// trigger
135
							// change listener
136
							// TODO verify correct behaviour
137

    
138
							try{
139
								conversationEnabled.getConversationHolder()
140
									.startTransaction();
141

    
142
								conversationEnabled.getConversationHolder()
143
									.commit();
144
							}catch(RuntimeException e){
145
								MessagingUtils.messageDialog("Error starting conversation handling", this, "" +
146
										"Conversation Error: "+ e);
147
							}
148
						}
149
					}
150
				});
151
				monitor.worked(10);
152

    
153
				return Status.OK_STATUS;
154
			}
155
		};
156

    
157
		return job;
158
	}
159

    
160
	/**
161
	 * @param jaxb
162
	 * @return
163
	 */
164
	private IIoConfigurator getConfigurator(TYPE type) {
165
		Assert.isNotNull(type, "Type should not be null");
166

    
167
		switch (type) {
168
		case Jaxb:
169
			return JaxbExportConfigurator.NewInstance(null, null);
170
		case Sdd:
171
			return SDDExportConfigurator.NewInstance(null, null, null);
172
		default:
173
			MessagingUtils.notImplementedMessage(this);
174
			throw new IllegalArgumentException("Export not supported yet");
175
		}
176
	}
177

    
178
	/**
179
	 * <p>
180
	 * JaxbConfigurator
181
	 * </p>
182
	 *
183
	 * @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator} object.
184
	 */
185
	public final JaxbExportConfigurator JaxbConfigurator() {
186
		return (JaxbExportConfigurator) getConfigurator(TYPE.Jaxb);
187
	}
188

    
189
	/**
190
	 * <p>
191
	 * SddConfigurator
192
	 * </p>
193
	 *
194
	 * @return a {@link eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator}
195
	 *         object.
196
	 */
197
	public final SDDExportConfigurator SddConfigurator() {
198
		return (SDDExportConfigurator) getConfigurator(TYPE.Sdd);
199
	}
200

    
201
}
(2-2/3)