Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2014
1,528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. // This file was generated by Mendix Business Modeler.
  2. //
  3. // WARNING: Only the following code will be retained when actions are regenerated:
  4. // - the import list
  5. // - the code between BEGIN USER CODE and END USER CODE
  6. // - the code between BEGIN EXTRA CODE and END EXTRA CODE
  7. // Other code you write will be lost the next time you deploy the project.
  8. // Special characters, e.g., é, ö, à, etc. are supported in comments.
  9.  
  10. package hourregistration.actions;
  11.  
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.util.List;
  18. import java.util.zip.ZipEntry;
  19. import java.util.zip.ZipOutputStream;
  20. import org.apache.commons.io.IOUtils;
  21. import system.proxies.FileDocument;
  22. import com.mendix.core.Core;
  23. import com.mendix.systemwideinterfaces.core.IContext;
  24. import com.mendix.webui.CustomJavaAction;
  25. import com.mendix.systemwideinterfaces.core.IMendixObject;
  26.  
  27. /**
  28. *
  29. */
  30. public class Java_ZipDocuments extends CustomJavaAction<IMendixObject>
  31. {
  32. private java.util.List<IMendixObject> __ListOfDocuments;
  33. private java.util.List<system.proxies.FileDocument> ListOfDocuments;
  34. private String NameOfZip;
  35.  
  36. public Java_ZipDocuments(IContext context, java.util.List<IMendixObject> ListOfDocuments, String NameOfZip)
  37. {
  38. super(context);
  39. this.__ListOfDocuments = ListOfDocuments;
  40. this.NameOfZip = NameOfZip;
  41. }
  42.  
  43. @Override
  44. public IMendixObject executeAction() throws Exception
  45. {
  46. this.ListOfDocuments = new java.util.ArrayList<system.proxies.FileDocument>();
  47. if (__ListOfDocuments != null)
  48. for (IMendixObject __ListOfDocumentsElement : __ListOfDocuments)
  49. this.ListOfDocuments.add(system.proxies.FileDocument.initialize(getContext(), __ListOfDocumentsElement));
  50.  
  51. // BEGIN USER CODE
  52.  
  53.  
  54.  
  55. if(NameOfZip == null || NameOfZip ==""){
  56. throw new Exception("Filename not defined.");
  57. }
  58.  
  59. IMendixObject newFileDocument = Core.instantiate(getContext(), "System.FileDocument");
  60. zipFileDocuments(newFileDocument, this.ListOfDocuments, getContext());
  61. return newFileDocument;
  62. // END USER CODE
  63. }
  64.  
  65. /**
  66. * Returns a string representation of this action
  67. */
  68. @Override
  69. public String toString()
  70. {
  71. return "Java_ZipDocuments";
  72. }
  73.  
  74. // BEGIN EXTRA CODE
  75. private void zipFileDocuments(IMendixObject destinationZipFile, List<system.proxies.FileDocument> filesToZip, IContext context)
  76. throws IOException {
  77. File tempFile = File.createTempFile("BulkZipFileDocument"+System.currentTimeMillis(), "tmp");
  78. ZipOutputStream zipfile = new ZipOutputStream(new FileOutputStream(tempFile));
  79. int i = 1;
  80. for (FileDocument file : filesToZip) {
  81. file.setName(context, i + file.getName());
  82. i++;
  83. InputStream fileInputStream = Core.getFileDocumentContent(context, file.getMendixObject());
  84. zipfile.putNextEntry(new ZipEntry(file.getName()));
  85. IOUtils.copy(fileInputStream, zipfile);
  86. fileInputStream.close();
  87. }
  88. zipfile.close();
  89. InputStream zipInputStream = new FileInputStream(tempFile);
  90. Core.storeFileDocumentContent(context, destinationZipFile, NameOfZip, zipInputStream);
  91. tempFile.delete();
  92. }
  93. // END EXTRA CODE
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement