-// $Id$
/**
* Copyright (C) 2007 EDIT
* European Distributed Institute of Taxonomy
package eu.etaxonomy.taxeditor.io;
+import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+import org.apache.log4j.Logger;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.IWorkbenchPart;
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
-import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
+import eu.etaxonomy.cdm.api.application.ICdmRepository;
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
+import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
+import eu.etaxonomy.cdm.io.cdmLight.CdmLightExportConfigurator;
import eu.etaxonomy.cdm.io.common.CdmDefaultExport;
+import eu.etaxonomy.cdm.io.common.ExportDataWrapper;
import eu.etaxonomy.cdm.io.common.ExportResult;
+import eu.etaxonomy.cdm.io.common.ExportResultType;
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
import eu.etaxonomy.cdm.io.common.IIoConfigurator;
+import eu.etaxonomy.cdm.io.dwca.out.DwcaTaxExportConfigurator;
import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
import eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator;
import eu.etaxonomy.cdm.io.service.IIOService;
+import eu.etaxonomy.taxeditor.event.EventUtility;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
import eu.etaxonomy.taxeditor.model.MessagingUtils;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
+import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
+import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
+import eu.etaxonomy.taxeditor.store.CdmStore;
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
/**
* @created Sep 11, 2009
* @version 1.0
*/
-public class ExportManager extends AbstractIOManager<IExportConfigurator> {
+public class ExportManager extends AbstractIOManager<IExportConfigurator> implements IPostMoniteredOperationEnabled {
+ private final String successMessage = "The export was successful";
+
+ private static final Logger logger = Logger.getLogger(ExportManager.class);
+ /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
+ public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm";
/**
*
* @param applicationConfiguration
*/
- private ExportManager(ICdmApplicationConfiguration applicationConfiguration) {
+ private ExportManager(ICdmRepository applicationConfiguration) {
super(applicationConfiguration);
}
* @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
*/
public static ExportManager NewInstance(
- ICdmApplicationConfiguration applicationConfiguration) {
+ ICdmRepository applicationConfiguration) {
return new ExportManager(applicationConfiguration);
}
monitor.worked(60);
} catch (RuntimeException e) {
MessagingUtils.messageDialog("Error exporting data", this,
- "An error occured while"
+ "An error occurred while"
+ "exporting to destination '"
+ configurator.getDestinationNameString()
+ "'.\n"
@Override
public void run() {
- IWorkbenchPart activePart = StoreUtil.getActivePage()
- .getActivePart();
+ Object activePart = EventUtility.getActivePart();
if (activePart instanceof IConversationEnabled) {
// terminate any open transactions
IConversationEnabled conversationEnabled = (IConversationEnabled) activePart;
return Status.OK_STATUS;
}
+
+
};
return job;
final Display display = Display.getCurrent();
- Job job = new Job("Export: " + configurator.getClass().getSimpleName()) {
+ Job job = new Job("Export: " + configurator.getClass().getSimpleName()) { //$NON-NLS-1$
@Override
protected IStatus run(IProgressMonitor monitor) {
- monitor.beginTask("Exporting database. This will take some time.", IProgressMonitor.UNKNOWN);
+ SubMonitor subMonitor = SubMonitor.convert(monitor, 100);
+ subMonitor.beginTask("Exporting database. This will take some time.", IProgressMonitor.UNKNOWN);
try {
IIOService ioService = CdmApplicationState.getIOService();
+ UUID uuid = ioService.monitExportData(configurator);
+ IRemotingProgressMonitor remotingMonitor;
+ List<IFeedbackGenerator> feedBackGeneratorList = new ArrayList<IFeedbackGenerator>();
+ try {
+ remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor("Export Data",
+ uuid,
+ 1000,
+ ExportManager.this,
+ feedBackGeneratorList,
+ subMonitor);
+
+ } catch (Exception ex) {
+ return new Status(Status.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Operation Interrupted", ex);
+ }
+
+ ExportResult result = (ExportResult)remotingMonitor.getResult();
+ ExportDataWrapper data = result.getExportData();
+ try{
+ if (result.getExportData().getType().equals(ExportResultType.BYTE_ARRAY) || result.getExportData().getType().equals(ExportResultType.LIST_BYTE_ARRAY)){
+ if (data.getExportData() instanceof byte[]){
+ byte[] exportData = (byte[])data.getExportData();
+ if(exportData != null){
+ FileOutputStream stream = new FileOutputStream(exportFile);
+ Writer out = new BufferedWriter(new OutputStreamWriter(
+ stream, "UTF8"));
+
+ stream.write(exportData);
+ out.flush();
+ stream.close();
+ }
+ }else if (data.getExportData() instanceof List<?>){
+ //TODO:This needs to be improved
+ List<byte[]> exportData = (List<byte[]>)data.getExportData();
+ if(exportData != null){
+ FileOutputStream stream = new FileOutputStream(exportFile);
+ Writer out = new BufferedWriter(new OutputStreamWriter(
+ stream, "UTF8"));
+
+ stream.write(exportData.get(0));
+ out.flush();
+ stream.close();
+ }
+ }
+
+ } else if (result.getExportData().getType().equals(ExportResultType.MAP_BYTE_ARRAY)){
+ Map<String, byte[]> resultMap = (Map<String, byte[]>)data.getExportData();
+ Set<String> keySet = resultMap.keySet();
+ SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
+ Calendar cal = Calendar.getInstance();
+ String fileEnding = ".zip";
+ if (configurator instanceof DwcaTaxExportConfigurator){
+ DwcaTaxExportConfigurator dwcaConfig = (DwcaTaxExportConfigurator)configurator;
+
+ File file = new File(dwcaConfig.getDestination().toURI());
+ FileOutputStream stream = new FileOutputStream(file);
+ ZipOutputStream zos = new ZipOutputStream(stream);
+ for (String key: keySet){
+ byte[] fileData = resultMap.get(key);
+ ZipEntry entry = new ZipEntry( key + fileEnding);
+ zos.putNextEntry(entry);
+ zos.write(fileData);
+ zos.closeEntry();
+ }
+ zos.close();
+ }
+ } else{
+ logger.error("This kind of result data is not supported yet." + result.getExportData().getType().toString());
+ }
+
+ } catch(Exception e){
+ logger.error(e.getStackTrace());
+ }
- ExportResult result = ioService.export(configurator);
- List<byte[]> dataList = result.getExportData();
- byte[] exportData = dataList.get(0);
+ showResult(configurator.getClass().getSimpleName(), display, result, successMessage);
- FileOutputStream stream = new FileOutputStream(exportFile);
- try {
- stream.write(exportData);
- } finally {
- stream.close();
- }
} catch (Exception e) {
MessagingUtils.errorDialog("Error exporting data",
return (SDDExportConfigurator) getConfigurator(TYPE.Sdd);
}
+ /**
+ * @param configurator
+ * @param urlString
+ * @return
+ */
+ public Job createIOServiceJob(CdmLightExportConfigurator configurator, String urlString) {
+ Assert.isNotNull(configurator, "Configuration may not be null");
+ final Display display = Display.getDefault();
+
+ Job job = new Job("Export: " + configurator.getClass().getSimpleName()) { //$NON-NLS-1$
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ monitor.beginTask("Exporting database. This will take some time.", IProgressMonitor.UNKNOWN);
+ try {
+ IIOService ioService = CdmApplicationState.getIOService();
+ monitor.beginTask(
+ "Exporting database. This will take some time.", 100);
+ monitor.worked(10);
+ configurator.setProgressMonitor(CdmProgressMonitorAdapter
+ .CreateSubMonitor(monitor, 80));
+ ExportResult result = ioService.export(configurator);
+ ExportDataWrapper data = result.getExportData();
+ try{
+ if (result.getExportData().getType().equals(ExportResultType.BYTE_ARRAY) || result.getExportData().getType().equals(ExportResultType.LIST_BYTE_ARRAY)){
+ byte[] exportData = (byte[])data.getExportData();
+ if(exportData != null){
+ File file = new File(urlString);
+ FileOutputStream stream = new FileOutputStream(file);
+ Writer out = new BufferedWriter(new OutputStreamWriter(
+ stream, "UTF8"));
+
+ stream.write(exportData);
+ out.flush();
+ stream.close();
+ }
+ } else if (result.getExportData().getType().equals(ExportResultType.MAP_BYTE_ARRAY)){
+ Map<String, byte[]> resultMap = (Map<String, byte[]>)data.getExportData();
+ Set<String> keySet = resultMap.keySet();
+ SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
+ Calendar cal = Calendar.getInstance();
+ String fileEnding = ".csv";
+
+ if (configurator.isCreateZipFile()){
+ File file = new File(urlString+File.separator + "csv_light_" + sdf.format(cal.getTime())+ ".zip");
+ FileOutputStream stream = new FileOutputStream(file);
+ ZipOutputStream zos = new ZipOutputStream(stream);
+ for (String key: keySet){
+ byte[] fileData = resultMap.get(key);
+ ZipEntry entry = new ZipEntry( key + fileEnding);
+ zos.putNextEntry(entry);
+ zos.write(fileData);
+ zos.closeEntry();
+ }
+ zos.close();
+ }else{
+ for (String key: keySet){
+ byte[] fileData = resultMap.get(key);
+ File file = new File(urlString+File.separator + key + fileEnding);
+ FileOutputStream stream = new FileOutputStream(file);
+ Writer out = new BufferedWriter(new OutputStreamWriter(
+ stream, "UTF8"));
+ stream.write(fileData);
+ stream.close();
+ }
+ }
+ }else{
+ logger.error("This kind of result data is not supported yet." + result.getExportData().getType().toString());
+ }
+ } catch(Exception e){
+ logger.error(e.getStackTrace());
+ }
+ showResult(configurator.getClass().getSimpleName(), display, result, successMessage);
+
+
+ } catch (Exception e) {
+ MessagingUtils.errorDialog("Error exporting data",
+ this,
+ e.getMessage(),
+ TaxeditorStorePlugin.PLUGIN_ID,
+ e,
+ true);
+ }
+
+ return Status.OK_STATUS;
+ }
+ };
+ return job;
+ }
+
+ public void runMoniteredOperation(final IExportConfigurator configurator, String urlString) {
+ IIOService ioService = CdmApplicationState.getIOService();
+ final UUID uuid = ioService.monitExportData(configurator);
+
+ Display.getDefault().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ boolean isZip = false;
+ if (configurator instanceof CdmLightExportConfigurator){
+ isZip = ((CdmLightExportConfigurator)configurator).isCreateZipFile();
+ }
+ AbstractUtility.executeMoniteredExport(configurator.getUserFriendlyIOName(),
+ uuid,
+ 1000,
+ true,
+ ExportManager.this,
+ null,
+ urlString,
+ isZip);
+
+ }
+ });
+
+ }
+ @Override
+ public void postOperation(IRemotingProgressMonitor monitor) {
+ Display.getDefault().asyncExec(new Runnable() {
+
+ @Override
+ public void run() {
+ CdmStore.getContextManager().notifyContextRefresh();
+ }
+ });
+
+ }
}