Scenario: Create a portfolio using the API

This scenario describes how to create a portfolio using the Correspondence Management Solution Accelerator API.

Correspondence Management objects used to create a portfolio

The main Java objects used in the creation of a portfolio are:

  • Document object - com.adobe.icc.dbforms.obj.PortfolioDocument

  • Folder Object - com.adobe.icc.dbforms.obj.PortfolioFolder

  • Portfolio Object - com.adobe.icc.dbforms.obj.Portfolio

These objects reference the other managed assets of Correspondence Management such as Category, Letter, and so on.

Creating a document object

The Document object can be created by using the constructor of the PortfolioDocument, as specified below:

public PortfolioDocument(String id, String name, String desc, String comment, int state, DocumentSource docSource, byte[] docContent, String docReference, Category ucat, Category lcat, int version, Date activeStartDate, Date activeEndDate, String lastChangeBy, String mimeType)

An alternative is to use the setter methods of the PortfolioDocument to set the different properties of the object. A portfolio document can either be a document from a File or from the Correspondence Letter that was created in the Correspondence Management system. A few parameters in the PortfolioDocument class enable us to create either of the two. The following are the parameters which specify what type of Document object is being created:

  • DocumentSource: Specifies whether the document object is created from a File or from a Letter. Thus, it can either take the value DocumentSource.FILE or DocumentSource.LETTER

  • DocumentReference: Set to NULL if the DocumentSource is a File. Otherwise it references the new document object’s letter ID.

  • DocumentContent: Set to NULL if the DocumentSource is a Letter. If the DocumentSource is a File, then it references the byte array of the PDF file.

The following code snippet illustrates how the portfolioDocument can be created:

PortfolioDocument doc = new PortfolioDocument(); 
doc.setActiveEndDate(getDate()); 
doc.setActiveStartDate(getDate()); 
doc.setComment("Sample document"); 
doc.setDesc("Sample document"); 
doc.setDocContent(getdocumentContent()); 
doc.setDocReference(null); 
doc.setDocSource(DocumentSource.FILE); 
doc.setLcat(subCategoryObject); 
doc.setUcat(categoryObject); 
doc.setName(documentName); 
doc.setState(iState); 
doc.setVersion(0);

While creating the Portfolio, you can organize the Documents in a folder structure by creating the PortfolioFolder object. Similar to the portfolioDocument you can either use the constructor or a different setter method.

public PortfolioFolder(String id, String name, String desc, List<PortfolioFolder> subFolders, List<PortfolioDocument> documents) 

Finally, the portfolio object is created using the Folder objects and the Document objects and also references the other managed assets of CM.

public Portfolio(String id, String name, String desc, String comment, int state, Category ucat, Category lcat, int version, Date activeStartDate, Date activeEndDate, String lastChangeBy, PortfolioDocument navigator, PortfolioDocument cover, PortfolioFolder rootFolder) 

There are a couple of special documents that are mentioned above in the Portfolio constructor.

  • Navigator: The Document Object is created using Adobe’s special file format (.nav) for navigating the portfolio

  • Cover Page: The Document Object which is set as the Portfolio’s cover page.

Note: It is mandatory to have the Navigator, Cover page, and a source Document while creating the Portfolio.

Creating and rendering the portfolio

To create the portfolio, perform the following operations:

  1. Create and persist the different Document objects such as Navigator, Cover page, other source documents.

  2. Create and persist the portfolio object by associating the documents appropriately.

  3. Render the portfolio.

Create and persist Document objects

Creating the necessary document objects for the Portfolio.

/* Creating a document object for the Navigator */ 
PortfolioDocument navDoc = new PortfolioDocument(); 
navDoc.setActiveEndDate(getDate()); 
navDoc.setActiveStartDate(getDate()); 
navDoc.setComment("Navigator document"); 
navDoc.setDesc("Navigator document"); 
navDoc.setDocContent(getdocumentContent()); 
navDoc.setDocReference(null); 
navDoc.setDocSource(DocumentSource.FILE); 
navDoc.setLcat(getCategoryObject("SubCategory",1)); 
navDoc.setUcat(getCategoryObject("MainCategory",0)); 
navDoc.setName("SampleNavigator"); 
navDoc.setState(0); 
navDoc.setVersion(0);

Where:

  • getDate() is a custom method that returns the current date.

  • getdocumentContent(String docSource) is again a custom method, which returns the file content as a byte array.

  • getCategoryObject(String categoryName, int categoryType) is a custom method which returns the Category object.

Similarly, create the cover page document object coverDoc.

/* Creating a document object from a Letter */ 
PortfolioDocument ltrDoc = new PortfolioDocument(); 
ltrDoc.setActiveEndDate(getDate()); 
ltrDoc.setActiveStartDate(getDate()); 
ltrDoc.setComment("Sample document"); 
ltrDoc.setDesc("Sample document"); 
ltrDoc.setDocContent(null); 
ltrDoc.setDocReference(getLetterID("SampleLetter",0)); 
ltrDoc.setDocSource(DocumentSource.LETTER); 
ltrDoc.setLcat(getCategoryObject("SubCategory",1)); 
ltrDoc.setUcat(getCategoryObject("MainCategory",0)); 
ltrDoc.setName("SampleDocFromLetter"); 
ltrDoc.setState(0); 
ltrDoc.setVersion(0);
Note: getLetterID(String LetterName, int State) is a custom method which returns the Letter ID.

The document’s navDoc, coverDoc, and ltrDoc objects are created but are not yet persisted. To do so, use the Correspondence Management API createDocument().

public PortfolioDocument createDocument(PortfolioDocument document)

This method takes in a document object and persists it. It returns a PortfolioDocument object with the ID with which it is referenced in the Content Space.

Persist the document objects created:

/* Persisting the document objects */ 
navDoc = documentService.createDocument(navDoc); 
coverDoc = documentService.createDocument(coverDoc); 
ltrDoc = documentService.createDocument(ltrDoc);

These assets can then be activated using the following activateDocument API.

public PortfolioDocument activateDocument(String documentID, boolean updateDependencies)

Creating the Portfolio object

Create the portfolio object associating the different document objects created.

/* Creating the portfolio object */ 
Portfolio pf = new Portfolio(); 
pf.setName("SamplePortfolio"); 
pf.setActiveEndDate(getDate()); 
pf.setActiveStartDate(getDate()); 
pf.setComment("Sample portfolio"); 
pf.setDesc("Sample portfolio "); 
pf.setLcat(getCategoryObject("SubCategory",1)); 
pf.setUcat(getCategoryObject("MainCategory",0)); 
pf.setState(0); 
pf.setVersion(0); 
pf.setNavigator(navDoc); 
pf.setCover(coverDoc); 
/* Create the folders and associate the documents with it */ 
PortfolioFolder folder = new PortfolioFolder(); 
List<PortfolioDocument> docList = new ArrayList<PortfolioDocument>(); 
docList.add(ltrDoc); 
folder.setName("root"); 
folder.setSubFolders(null); 
folder.setDesc("root folder"); 
folder.setDocuments(docList); 
pf.setRootFolder(folder);

Now that the portfolio object is created with all its properties set, persist the object using the createPortfolio API.

public Portfolio createPortfolio(Portfolio portfolio)

Persist the portfolio object.

/* Persisting the portfolio object */ 
pf = portfolioService.createPortfolio(pf);

Similar to the PortfolioDocument, activate the portfolio asset using the activatePortfolio() API.

public Portfolio activatePortfolio(String portfolioID)

Render the Portfolio Object

Now that all the required assets of the portfolio and the portfolio object itself have been created to render the portfolio. Correspondence Management provides the following API:

public PDFResponseType renderPortfolio(Portfolio portfolio, String dataXML)

This method takes in the portfolio object and the dataXML and returns a PDFResponseType object from which the Portfolio can be extracted and saved.

The Data XML is used when a document created out of a letter, which in turn references some Data dictionary elements. When the portfolio is rendered, the letter document is rendered with all the references to the data dictionary elements resolved with the XML data.

Note: The letter document in the portfolio can be rendered interactively only. Flattening the letter and rendering is not available.

This code snippet shows how to render the final portfolio object and extract the portfolio file that can be saved to the local disk.

byte[] pdfDoc = null; 
PDFResponseType resp = portfolioRenderService.renderPortfolio(pf, xmlData); 
pdfDoc = resp.getFile().getDocument();

// Ethnio survey code removed