package eu.etaxonomy.taxeditor.io;
import java.io.BufferedWriter;
-import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
-import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
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 org.eclipse.ui.PlatformUI;
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
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.IImportConfigurator;
import eu.etaxonomy.cdm.io.common.IIoConfigurator;
-import eu.etaxonomy.cdm.io.common.IImportConfigurator.SOURCE_TYPE;
import eu.etaxonomy.cdm.io.dwca.out.DwcaTaxExportConfigurator;
import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
-import eu.etaxonomy.cdm.io.cdmLight.CdmLightExportConfigurator;
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.operation.FeedbackGenerator;
import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
import eu.etaxonomy.taxeditor.store.CdmStore;
-import eu.etaxonomy.taxeditor.store.StoreUtil;
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
/**
* @version 1.0
*/
public class ExportManager extends AbstractIOManager<IExportConfigurator> implements IPostMoniteredOperationEnabled {
-
- private final String successMessage = "The export was successfull";
+
+ private final String successMessage = "The export was successful";
private static final Logger logger = Logger.getLogger(ExportManager.class);
/** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
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;
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);
1000,
ExportManager.this,
feedBackGeneratorList,
- monitor);
-
+ 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)){
- byte[] exportData = (byte[])data.getExportData();
- if(exportData != null){
- FileOutputStream stream = new FileOutputStream(exportFile);
- stream.write(exportData);
- stream.close();
- }
+ 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();
String fileEnding = ".zip";
if (configurator instanceof DwcaTaxExportConfigurator){
DwcaTaxExportConfigurator dwcaConfig = (DwcaTaxExportConfigurator)configurator;
-
- File file = new File(dwcaConfig.getDestination().toURI());
+
+ 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);
+ ZipEntry entry = new ZipEntry( key + fileEnding);
zos.putNextEntry(entry);
zos.write(fileData);
zos.closeEntry();
} 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);
+ showResult(configurator.getClass().getSimpleName(), display, result, successMessage);
} catch (Exception e) {
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)){
+ 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);
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");
+
+ 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);
+ ZipEntry entry = new ZipEntry( key + fileEnding);
zos.putNextEntry(entry);
zos.write(fileData);
zos.closeEntry();
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() {
if (configurator instanceof CdmLightExportConfigurator){
isZip = ((CdmLightExportConfigurator)configurator).isCreateZipFile();
}
- AbstractUtility.executeMoniteredExport("Export: " + configurator.getClass().getSimpleName(),
+ AbstractUtility.executeMoniteredExport(configurator.getUserFriendlyIOName(),
uuid,
1000,
true,
null,
urlString,
isZip);
+
}
});
CdmStore.getContextManager().notifyContextRefresh();
}
});
-
+
}
}