Revision 23443827
Added by Katja Luther over 6 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/AbstractUtility.java | ||
---|---|---|
9 | 9 |
|
10 | 10 |
package eu.etaxonomy.taxeditor.model; |
11 | 11 |
|
12 |
import java.io.BufferedWriter; |
|
13 |
import java.io.File; |
|
14 |
import java.io.FileOutputStream; |
|
15 |
import java.io.OutputStreamWriter; |
|
16 |
import java.io.Writer; |
|
12 | 17 |
import java.lang.reflect.InvocationTargetException; |
18 |
import java.text.SimpleDateFormat; |
|
13 | 19 |
import java.util.ArrayList; |
20 |
import java.util.Calendar; |
|
14 | 21 |
import java.util.Collection; |
15 | 22 |
import java.util.LinkedHashMap; |
16 | 23 |
import java.util.List; |
24 |
import java.util.Map; |
|
25 |
import java.util.Set; |
|
17 | 26 |
import java.util.TreeSet; |
18 | 27 |
import java.util.UUID; |
28 |
import java.util.zip.ZipEntry; |
|
29 |
import java.util.zip.ZipOutputStream; |
|
19 | 30 |
|
20 | 31 |
import org.apache.commons.lang.StringUtils; |
21 | 32 |
import org.apache.log4j.Logger; |
... | ... | |
65 | 76 |
import eu.etaxonomy.cdm.api.application.CdmApplicationState; |
66 | 77 |
import eu.etaxonomy.cdm.api.service.IProgressMonitorService; |
67 | 78 |
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor; |
79 |
import eu.etaxonomy.cdm.io.common.ExportDataWrapper; |
|
68 | 80 |
import eu.etaxonomy.cdm.io.common.ExportResult; |
81 |
import eu.etaxonomy.cdm.io.common.ExportResultType; |
|
69 | 82 |
import eu.etaxonomy.cdm.model.common.IEnumTerm; |
70 | 83 |
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation; |
71 | 84 |
import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator; |
... | ... | |
92 | 105 |
|
93 | 106 |
/** Constant <code>statusLineManager</code> */ |
94 | 107 |
protected static IStatusLineManager statusLineManager; |
95 |
|
|
108 |
/** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */ |
|
109 |
public static final String DATE_FORMAT_NOW = "yyyyMMddHHmm"; |
|
96 | 110 |
|
97 | 111 |
public static boolean closeAll() { |
98 | 112 |
if(getActivePage()!=null){ |
... | ... | |
341 | 355 |
* @param postOp callback for running post operation logic |
342 | 356 |
* @return |
343 | 357 |
*/ |
344 |
public static IStatus executeMoniteredOperation(final String label,
|
|
358 |
public static IStatus executeMoniteredExport(final String label,
|
|
345 | 359 |
final UUID uuid, |
346 | 360 |
final int pollInterval, |
347 | 361 |
final boolean cancelable, |
348 | 362 |
final IPostMoniteredOperationEnabled postOp, |
349 |
final IFeedbackGenerator feedbackGenerator) { |
|
363 |
final IFeedbackGenerator feedbackGenerator, |
|
364 |
String urlString, |
|
365 |
boolean createZip) { |
|
350 | 366 |
|
351 | 367 |
try { |
352 | 368 |
// get the remoting monitor the first time to make sure that the |
... | ... | |
376 | 392 |
return new Status(Status.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Operation Interrupted", ex); |
377 | 393 |
} |
378 | 394 |
final StringBuilder reportSb = new StringBuilder(); |
379 |
// collect reports |
|
380 |
// for(String report : remotingMonitor.getResult()) { |
|
381 |
reportSb.append(((ExportResult)remotingMonitor.getResult()).createReport()); |
|
382 |
// } |
|
383 |
if(!StringUtils.isBlank(reportSb.toString())) { |
|
384 |
Display.getDefault().asyncExec(new Runnable() { |
|
385 |
@Override |
|
386 |
public void run() { |
|
387 |
// display reports with possibility to save |
|
388 |
ReportTextDialog dialog = new ReportTextDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
|
389 |
dialog.setTitle(label + " Report"); |
|
390 |
dialog.setReportText(reportSb.toString()); |
|
391 |
dialog.open(); |
|
392 |
} |
|
393 |
}); |
|
395 |
if (remotingMonitor.getResult() instanceof ExportResult){ |
|
396 |
ExportResult result = (ExportResult)remotingMonitor.getResult(); |
|
397 |
|
|
398 |
reportSb.append(result.createReport()); |
|
399 |
|
|
400 |
if(!StringUtils.isBlank(reportSb.toString())) { |
|
401 |
Display.getDefault().asyncExec(new Runnable() { |
|
402 |
@Override |
|
403 |
public void run() { |
|
404 |
// display reports with possibility to save |
|
405 |
ReportTextDialog dialog = new ReportTextDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
|
406 |
dialog.setTitle(label + " Report"); |
|
407 |
dialog.setReportText(reportSb.toString()); |
|
408 |
dialog.open(); |
|
409 |
} |
|
410 |
}); |
|
411 |
} |
|
412 |
|
|
413 |
if (urlString != null){ |
|
414 |
ExportDataWrapper data = result.getExportData(); |
|
415 |
try{ |
|
416 |
if (result.getExportData().getType().equals(ExportResultType.BYTE_ARRAY)){ |
|
417 |
byte[] exportData = (byte[])data.getExportData(); |
|
418 |
if(exportData != null){ |
|
419 |
File file = new File(urlString); |
|
420 |
FileOutputStream stream = new FileOutputStream(file); |
|
421 |
Writer out = new BufferedWriter(new OutputStreamWriter( |
|
422 |
stream, "UTF8")); |
|
423 |
|
|
424 |
stream.write(exportData); |
|
425 |
out.flush(); |
|
426 |
stream.close(); |
|
427 |
} |
|
428 |
} else if (result.getExportData().getType().equals(ExportResultType.MAP_BYTE_ARRAY)){ |
|
429 |
Map<String, byte[]> resultMap = (Map<String, byte[]>)data.getExportData(); |
|
430 |
Set<String> keySet = resultMap.keySet(); |
|
431 |
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); |
|
432 |
Calendar cal = Calendar.getInstance(); |
|
433 |
String fileEnding = ".csv"; |
|
434 |
|
|
435 |
if (createZip){ |
|
436 |
File file = new File(urlString+File.separator + sdf.format(cal.getTime())+ ".zip"); |
|
437 |
FileOutputStream stream = new FileOutputStream(file); |
|
438 |
ZipOutputStream zos = new ZipOutputStream(stream); |
|
439 |
for (String key: keySet){ |
|
440 |
byte[] fileData = resultMap.get(key); |
|
441 |
ZipEntry entry = new ZipEntry( key + fileEnding); |
|
442 |
zos.putNextEntry(entry); |
|
443 |
zos.write(fileData); |
|
444 |
zos.closeEntry(); |
|
445 |
} |
|
446 |
zos.close(); |
|
447 |
}else{ |
|
448 |
for (String key: keySet){ |
|
449 |
byte[] fileData = resultMap.get(key); |
|
450 |
File file = new File(urlString+File.separator + key + fileEnding); |
|
451 |
FileOutputStream stream = new FileOutputStream(file); |
|
452 |
Writer out = new BufferedWriter(new OutputStreamWriter( |
|
453 |
stream, "UTF8")); |
|
454 |
stream.write(fileData); |
|
455 |
stream.close(); |
|
456 |
} |
|
457 |
} |
|
458 |
}else{ |
|
459 |
logger.error("This kind of result data is not supported yet." + result.getExportData().getType().toString()); |
|
460 |
} |
|
461 |
} catch(Exception e){ |
|
462 |
logger.error(e.getStackTrace()); |
|
463 |
} |
|
464 |
} |
|
394 | 465 |
} |
395 | 466 |
return Status.OK_STATUS; |
396 | 467 |
} |
... | ... | |
776 | 847 |
} |
777 | 848 |
return selectionToSet; |
778 | 849 |
} |
850 |
|
|
851 |
/** |
|
852 |
* Executes a remoting monitored import |
|
853 |
* |
|
854 |
* @param label for the import |
|
855 |
* @param uuid of the remoting monitor already started on the server |
|
856 |
* @param pollInterval in milliseconds |
|
857 |
* @param cancelable flag which determines whether the operation can be cancelled |
|
858 |
* @param postOp callback for running post operation logic |
|
859 |
* @return |
|
860 |
*/ |
|
861 |
public static IStatus executeMoniteredOperation(final String label, |
|
862 |
final UUID uuid, |
|
863 |
final int pollInterval, |
|
864 |
final boolean cancelable, |
|
865 |
final IPostMoniteredOperationEnabled postOp, |
|
866 |
final IFeedbackGenerator feedbackGenerator) { |
|
867 |
|
|
868 |
try{ |
|
869 |
// get the remoting monitor the first time to make sure that the |
|
870 |
// operation is valid |
|
871 |
final IProgressMonitorService progressMonitorService = CdmApplicationState.getCurrentAppConfig().getProgressMonitorService(); |
|
872 |
final IRemotingProgressMonitor firstRemotingMonitor = progressMonitorService.getRemotingMonitor(uuid); |
|
873 |
if(firstRemotingMonitor == null) { |
|
874 |
throw new IllegalStateException("Remoting progress monitor is null"); |
|
875 |
} |
|
876 |
|
|
877 |
Job job = new Job(label) { |
|
878 |
|
|
879 |
|
|
880 |
@Override |
|
881 |
public IStatus run(IProgressMonitor monitor) { |
|
882 |
// run the monitor until the operation is finished |
|
883 |
monitor.beginTask("Start", 100); |
|
884 |
IRemotingProgressMonitor remotingMonitor; |
|
885 |
try { |
|
886 |
remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor(label, |
|
887 |
uuid, |
|
888 |
pollInterval, |
|
889 |
postOp, |
|
890 |
feedbackGenerator, |
|
891 |
monitor); |
|
892 |
} catch (Exception ex) { |
|
893 |
return new Status(Status.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Operation Interrupted", ex); |
|
894 |
} |
|
895 |
final StringBuilder reportSb = new StringBuilder(); |
|
896 |
// collect reports |
|
897 |
// for(String report : remotingMonitor.getResult()) { |
|
898 |
reportSb.append(((ExportResult)remotingMonitor.getResult()).createReport()); |
|
899 |
// } |
|
900 |
if(!StringUtils.isBlank(reportSb.toString())) { |
|
901 |
Display.getDefault().asyncExec(new Runnable() { |
|
902 |
@Override |
|
903 |
public void run() { |
|
904 |
// display reports with possibility to save |
|
905 |
ReportTextDialog dialog = new ReportTextDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
|
906 |
dialog.setTitle(label + " Report"); |
|
907 |
dialog.setReportText(reportSb.toString()); |
|
908 |
dialog.open(); |
|
909 |
} |
|
910 |
}); |
|
911 |
} |
|
912 |
return Status.OK_STATUS; |
|
913 |
} |
|
914 |
|
|
915 |
@Override |
|
916 |
protected void canceling() { |
|
917 |
CdmStore.getCurrentApplicationConfiguration().getProgressMonitorService().cancel(uuid); |
|
918 |
} |
|
919 |
}; |
|
920 |
|
|
921 |
// configure the job |
|
922 |
job.setProperty(IProgressConstants.KEEP_PROPERTY, true); |
|
923 |
job.setUser(true); |
|
924 |
// schedule job |
|
925 |
job.schedule(); |
|
926 |
|
|
927 |
} catch (Exception e) { |
|
928 |
MessagingUtils.errorDialog("Error executing operation", |
|
929 |
AbstractUtility.class, |
|
930 |
"An error occured while executing " + label, |
|
931 |
TaxeditorStorePlugin.PLUGIN_ID, |
|
932 |
e, |
|
933 |
true); |
|
934 |
} |
|
935 |
|
|
936 |
return Status.OK_STATUS; |
|
937 |
} |
|
938 |
|
|
779 | 939 |
} |
Also available in: Unified diff
fix #6799: the destination file should not be passed to the server