Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This file was generated by Mendix Business Modeler.
- //
- // WARNING: Only the following code will be retained when actions are regenerated:
- // - the import list
- // - the code between BEGIN USER CODE and END USER CODE
- // - the code between BEGIN EXTRA CODE and END EXTRA CODE
- // Other code you write will be lost the next time you deploy the project.
- // Special characters, e.g., é, ö, à, etc. are supported in comments.
- package hourregistration.actions;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
- import org.apache.commons.io.IOUtils;
- import system.proxies.FileDocument;
- import com.mendix.core.Core;
- import com.mendix.systemwideinterfaces.core.IContext;
- import com.mendix.webui.CustomJavaAction;
- import com.mendix.systemwideinterfaces.core.IMendixObject;
- /**
- *
- */
- public class Java_ZipDocuments extends CustomJavaAction<IMendixObject>
- {
- private java.util.List<IMendixObject> __ListOfDocuments;
- private java.util.List<system.proxies.FileDocument> ListOfDocuments;
- private String NameOfZip;
- public Java_ZipDocuments(IContext context, java.util.List<IMendixObject> ListOfDocuments, String NameOfZip)
- {
- super(context);
- this.__ListOfDocuments = ListOfDocuments;
- this.NameOfZip = NameOfZip;
- }
- @Override
- public IMendixObject executeAction() throws Exception
- {
- this.ListOfDocuments = new java.util.ArrayList<system.proxies.FileDocument>();
- if (__ListOfDocuments != null)
- for (IMendixObject __ListOfDocumentsElement : __ListOfDocuments)
- this.ListOfDocuments.add(system.proxies.FileDocument.initialize(getContext(), __ListOfDocumentsElement));
- // BEGIN USER CODE
- if(NameOfZip == null || NameOfZip ==""){
- throw new Exception("Filename not defined.");
- }
- IMendixObject newFileDocument = Core.instantiate(getContext(), "System.FileDocument");
- zipFileDocuments(newFileDocument, this.ListOfDocuments, getContext());
- return newFileDocument;
- // END USER CODE
- }
- /**
- * Returns a string representation of this action
- */
- @Override
- public String toString()
- {
- return "Java_ZipDocuments";
- }
- // BEGIN EXTRA CODE
- private void zipFileDocuments(IMendixObject destinationZipFile, List<system.proxies.FileDocument> filesToZip, IContext context)
- throws IOException {
- File tempFile = File.createTempFile("BulkZipFileDocument"+System.currentTimeMillis(), "tmp");
- ZipOutputStream zipfile = new ZipOutputStream(new FileOutputStream(tempFile));
- int i = 1;
- for (FileDocument file : filesToZip) {
- file.setName(context, i + file.getName());
- i++;
- InputStream fileInputStream = Core.getFileDocumentContent(context, file.getMendixObject());
- zipfile.putNextEntry(new ZipEntry(file.getName()));
- IOUtils.copy(fileInputStream, zipfile);
- fileInputStream.close();
- }
- zipfile.close();
- InputStream zipInputStream = new FileInputStream(tempFile);
- Core.storeFileDocumentContent(context, destinationZipFile, NameOfZip, zipInputStream);
- tempFile.delete();
- }
- // END EXTRA CODE
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement