Skip to content

Commit

Permalink
multiple doc supports (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentleseigneur authored Oct 30, 2024
1 parent 6a80225 commit cd69a70
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 16 deletions.
31 changes: 23 additions & 8 deletions src/main/java/com/bonitasoft/presales/connector/ConnectorZip.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bonitasoft.presales.connector;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

Expand All @@ -18,33 +19,47 @@

public class ConnectorZip extends AbstractConnector {

static final String DOCUMENTS_INPUT = "documentsToZip";
static final String SINGLE_DOCUMENTS_INPUT = "documentsToZip";
static final String MULTIPLE_DOCUMENTS_INPUT = "multipleDocumentsToZip";
static final String ZIP_FILE_NAME = "zipFileName";
static final String OUTPUT_ZIP = "outputZipFile";

Logger logger = Logger.getLogger("org.bonitasoft");

List<Document> allDocs;

@Override
public void validateInputParameters() throws ConnectorValidationException {
if (getInputParameter(DOCUMENTS_INPUT) == null || !(getInputParameter(DOCUMENTS_INPUT) instanceof List)) {
throw new ConnectorValidationException("Input documentsToZip should be a list of Bonita documents.");
}
if (getInputParameter(ZIP_FILE_NAME) == null || !(getInputParameter(ZIP_FILE_NAME) instanceof String)) {
throw new ConnectorValidationException("You must provide a name for the output ZIP file.");
}
allDocs = new ArrayList<>();
List<Document> allSingleDocs = (List<Document>) getInputParameter(SINGLE_DOCUMENTS_INPUT);
List<List<Document>> allMultipleDocs = (List<List<Document>>) getInputParameter(MULTIPLE_DOCUMENTS_INPUT);
if (allSingleDocs != null && !allSingleDocs.isEmpty()) {
allDocs.addAll(allSingleDocs);
}
if (allMultipleDocs != null && !allMultipleDocs.isEmpty()) {
for (List<Document> allMultipleDoc : allMultipleDocs) {
allDocs.addAll(allMultipleDoc);
}
}
if (allDocs.isEmpty()) {
throw new ConnectorValidationException("Should provide a list one Document to zip.");
}

}

@Override
public void executeBusinessLogic() throws ConnectorException {
@SuppressWarnings("unchecked")
List<Document> documentsToZip = (List<Document>) getInputParameter(DOCUMENTS_INPUT);
String zipFileName = (String) getInputParameter(ZIP_FILE_NAME);
logger.info("Input validated");

// Create a temporary zip file
File zipFile;

try {
zipFile = createZipFile(documentsToZip, zipFileName);
zipFile = createZipFile(allDocs, zipFileName);
DocumentValue documentValue;
try {
documentValue = convertFileToDocumentValue(zipFile, zipFileName);
Expand All @@ -69,7 +84,7 @@ private File createZipFile(List<Document> documentsToZip, String zipFileName)
File tempZipFile = File.createTempFile(zipFileName, ".zip");
ProcessAPI processAPI = getAPIAccessor().getProcessAPI();
try (FileOutputStream fos = new FileOutputStream(tempZipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {
ZipOutputStream zos = new ZipOutputStream(fos)) {

for (Document document : documentsToZip) {
byte[] documentContent = processAPI.getDocumentContent(document.getContentStorageId());
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources-filtered/connector-zip.def
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<!-- Connectors inputs -->
<input mandatory="true" name="documentsToZip" type="java.lang.List"/>
<input mandatory="true" name="multipleDocumentsToZip" type="java.lang.List"/>
<input mandatory="true" name="zipFileName" type="java.lang.String"/>

<!-- Connectors outputs -->
Expand All @@ -16,6 +17,7 @@
<!-- Pages -->
<page id="configurationPage">
<widget xsi:type="definition:List" id="documentsToZipWidget" inputName="documentsToZip"/>
<widget xsi:type="definition:List" id="multipleDocumentsToZip" inputName="multipleDocumentsToZip"/>
<widget xsi:type="definition:Text" id="zipFileNameWidget" inputName="zipFileName"/>
</page>
</definition:ConnectorDefinition>
6 changes: 4 additions & 2 deletions src/main/resources-filtered/connector-zip.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ connectorDefinitionDescription= Connector to zip multiple files
connectorDefinitionLabel=${project.artifactId}
configurationPage.pageTitle=Configuration Page
configurationPage.pageDescription=Configure the files to be uploaded and output file.
documentsToZipWidget.label = Documents
documentsToZipWidget.description = List of Bonita documents to zip
documentsToZipWidget.label = Single Documents
documentsToZipWidget.description = List of Bonita single documents to zip
multipleDocumentsToZip.label = Multiple Documents
multipleDocumentsToZip.description = List of Bonita multiple documents to zip
zipFileNameWidget.label = Filename
zipFileNameWidget.description = Name for the output ZIP file (without extension)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bonitasoft.engine.bpm.document.Document;
import org.bonitasoft.engine.bpm.document.DocumentNotFoundException;
import org.bonitasoft.engine.bpm.document.DocumentValue;
import org.bonitasoft.engine.bpm.document.impl.DocumentImpl;
import org.bonitasoft.engine.connector.ConnectorException;
import org.bonitasoft.engine.connector.ConnectorValidationException;
import org.junit.Test;
Expand All @@ -13,10 +14,7 @@
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
//
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -65,7 +63,7 @@ public void testSuccessfulZipCreation() throws Exception {
List<Document> documentsToZip = new ArrayList<>();
documentsToZip.add(mockDocument);
final HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put(ConnectorZip.DOCUMENTS_INPUT, documentsToZip);
parameters.put(ConnectorZip.SINGLE_DOCUMENTS_INPUT, documentsToZip);
parameters.put(ConnectorZip.ZIP_FILE_NAME, "testZip");
connector.setInputParameters(parameters);

Expand Down Expand Up @@ -110,12 +108,63 @@ public void testInvalidInputParameters() throws Exception {

// Set invalid input
final HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put(ConnectorZip.DOCUMENTS_INPUT, null);
parameters.put(ConnectorZip.SINGLE_DOCUMENTS_INPUT, null);
parameters.put(ConnectorZip.MULTIPLE_DOCUMENTS_INPUT, null);
parameters.put(ConnectorZip.ZIP_FILE_NAME, "testZip");
connector.setInputParameters(parameters);

connector.validateInputParameters();
}

@Test
public void testSingleInputParameters() throws Exception {
connector = Mockito.spy(new ConnectorZip());
processAPI = mock(ProcessAPI.class);
apiAccessor = mock(APIAccessor.class);

doReturn(apiAccessor).when(connector).getAPIAccessor();
doReturn(processAPI).when(apiAccessor).getProcessAPI();

// Set invalid input
final HashMap<String, Object> parameters = new HashMap<String, Object>();
List<Document> singleDocs = aListOfDocuments();
parameters.put(ConnectorZip.SINGLE_DOCUMENTS_INPUT, singleDocs);
parameters.put(ConnectorZip.MULTIPLE_DOCUMENTS_INPUT, null);
parameters.put(ConnectorZip.ZIP_FILE_NAME, "testZip");
connector.setInputParameters(parameters);

connector.validateInputParameters();
}

@Test
public void testMultipleInputParameters() throws Exception {
connector = Mockito.spy(new ConnectorZip());
processAPI = mock(ProcessAPI.class);
apiAccessor = mock(APIAccessor.class);

doReturn(apiAccessor).when(connector).getAPIAccessor();
doReturn(processAPI).when(apiAccessor).getProcessAPI();

// Set invalid input
final HashMap<String, Object> parameters = new HashMap<String, Object>();
List<Document> singleDocs = aListOfDocuments();

parameters.put(ConnectorZip.SINGLE_DOCUMENTS_INPUT, null);
parameters.put(ConnectorZip.MULTIPLE_DOCUMENTS_INPUT, List.of(singleDocs));
parameters.put(ConnectorZip.ZIP_FILE_NAME, "testZip");
connector.setInputParameters(parameters);

connector.validateInputParameters();
}

private List<Document> aListOfDocuments() {
List<Document> docs = new ArrayList<>();
DocumentImpl d = new DocumentImpl();
d.setContentMimeType("text");
d.setName(UUID.randomUUID().toString() + ".txt");
docs.add(d);
return docs;
}


}

0 comments on commit cd69a70

Please sign in to comment.