Esperienza con AEM Forms su JEE Process Management, programmazione Java di base e creazione di componenti personalizzati.
Altri prodotti richiesti
Editor Java come Eclipse o Nave IDE
Intermedio
AEM Forms su JEE consente agli sviluppatori di creare un DSC personalizzato (Document Service Container) per creare funzionalità integrate arricchite. La creazione di tali componenti è collegabile all’ambiente di runtime AEM Forms in JEE e ha lo scopo desiderato. Questo articolo spiega come creare un servizio ZIP personalizzato che può essere utilizzato per comprimere un elenco di file in un file .zip e decomprimere un .zip in un elenco di documenti.
Crea un componente DSC personalizzato con due operazioni del servizio per comprimere e decomprimere l’elenco dei documenti. Questo componente utilizza il pacchetto java.util.zip per la compressione e la decompressione. Per creare un componente personalizzato, effettua le seguenti operazioni:
Il codice è disponibile qui:
/*
* Custom DSC : ZIP Utility
* Purpose: This is a LiveCycle ES2 custom component used to Compress & Decompress List of Documents
* Author: Nithiyanandam Dharmadass
* Organization: Ministry of Finance, Kingdom of Bahrain
* Last modified Date: 18/Apr/2011
*/
package nith.lces2.dsc;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.adobe.idp.Document;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipOutputStream;
public class ZIPService {
static final int BUFFER = 2048; // 2MB buffer size
public java.util.List UnzipDocument(com.adobe.idp.Document zipDocument) throws Exception {
ZipInputStream zis = new ZipInputStream(zipDocument.getInputStream());
ZipEntry zipFile;
List resultList = new ArrayList();
while ((zipFile = zis.getNextEntry()) != null) {
ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
int count; // an int variable to hold the number of bytes read from input stream
byte data[] = new byte[BUFFER];
while ((count = zis.read(data, 0, BUFFER)) != -1) {
byteArrayOutStream.write(data, 0, count); // write to byte array
}
com.adobe.idp.Document unzippedDoc = new Document(byteArrayOutStream.toByteArray()); // create an idp document
unzippedDoc.setAttribute("file", zipFile.getName());
unzippedDoc.setAttribute("wsfilename", zipFile.getName()); // update the wsfilename attribute
resultList.add(unzippedDoc);
}
return resultList; // List of uncompressed documents
}
public com.adobe.idp.Document ZipDocuments(java.util.List listOfDocuments,java.lang.String zipFileName) throws Exception {
if (listOfDocuments == null || listOfDocuments.size() == 0) {
return null;
}
ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(byteArrayOutStream); // ZIP Output Stream
for (int i = 0; i < listOfDocuments.size(); i++) {
Document doc = (Document) listOfDocuments.get(i);
InputStream docInputStream = doc.getInputStream();
ZipEntry zipEntry = new ZipEntry(doc.getAttribute("file").toString());
zos.putNextEntry(zipEntry);
int count;
byte data[] = new byte[BUFFER];
while ((count = docInputStream.read(data, 0, BUFFER)) != -1) {
zos.write(data, 0, count); // Read document content and add to zip entry
}
zos.closeEntry();
}
zos.flush();
zos.close();
Document zippedDoc = new Document(byteArrayOutStream.toByteArray());
if(zipFileName==null || zipFileName.equals(""))
{
zipFileName = "CompressedList.zip";
}
zippedDoc.setAttribute("file", zipFileName);
return zippedDoc;
}
}
È necessario creare un file component.xml all’interno della cartella principale del pacchetto che ha definito le operazioni del servizio e i relativi parametri.
Il file component.xml viene mostrato qui:
<?xml version="1.0" encoding="UTF-8"?>
<component xmlns="https://adobe.com/idp/dsc/component/document">
<!-- Unique id identifying this component -->
<component-id>ZipService</component-id>
<!-- Version -->
<version>1.0</version>
<!-- Start of the Service definition -->
<services>
<!-- Unique name for service descriptor.
The value is used as the default name for
deployed services -->
<service name="ZipService">
<!-- service implementation class definition -->
<implementation-class>nith.lces2.dsc.ZIPService</implementation-class>
<!-- description -->
<description>Compress or Decompress list of documents</description>
<!-- You can provide your own icons for a distinct look -->
<small-icon>icons/Zip_icon16.png</small-icon>
<large-icon>icons/Zip_icon32.png</large-icon>
<!-- automatically deploys the service and starts it after installation -->
<auto-deploy service-id="ZipService" />
<operations>
<!-- method name in the interface setSmtpHost-->
<operation name="UnzipDocument">
<!-- input parameters to the "send" method -->
<input-parameter name="zipDocument" title="Input ZIP Document" type="com.adobe.idp.Document">
<hint>A ZIP File to be decompressed</hint>
</input-parameter>
<output-parameter name="resultList" title="Decompressed list of documents" type="java.util.List">
<hint>Decompressed ZIP list</hint>
</output-parameter>
</operation>
<operation name="ZipDocuments">
<!-- input parameters to the "send" method -->
<input-parameter name="listOfDocuments" title="List of Documents" type="java.util.List">
<hint>A list of documents to be Compressed</hint>
</input-parameter>
<input-parameter name="zipFileName" title="Result File Name" type="java.lang.String">
<hint>The name of compressed file (optional)</hint>
</input-parameter>
<output-parameter name="zippedDoc" title="Compressed Zip file" type="com.adobe.idp.Document">
<hint>Compressed ZIP File</hint>
</output-parameter>
</operation>
</operations>
</service>
</services>
</component>
L'operazione UnzipDocument del servizio personalizzato può ora accettare una variabile di documento come input e restituire un elenco di variabili di documento come output.
Analogamente, l'operazione ZipDocuments del componente personalizzato può accettare un elenco di documenti come input, comprimerli come file zip e restituire il documento compresso.
La seguente orchestrazione del flusso di lavoro mostra come decomprimere il file ZIP dato, comprimerlo nuovamente in un altro file ZIP e restituire l'output (vedi la figura seguente).
Puoi utilizzare questo servizio ZIP per i seguenti casi di utilizzo:
Trova tutti i file in una determinata cartella e restituisce i file come documento compresso.
Fornisci un file ZIP contenente una serie di documenti PDF che possono essere prolungati dal lettore dopo la loro decompressione. Questo richiede AEM Forms sul modulo Estensioni Reader JEE.
Fornire un file ZIP contenente un tipo eterogeneo di documento che può essere decompresso e convertito come documento PDF utilizzando il servizio Genera PDF.
I criteri proteggono un elenco di documenti e restituiscono come file ZIP.
Consenti agli utenti di scaricare tutti gli allegati di un'istanza di processo come un singolo file ZIP.