Sunday, February 13, 2011

Content Creation with the IBM WCM API - works for version 6, 6.1,7

<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>

<% // retrieve repository Repository repository = WCM_API.getRepository(); //Creating a workspace Workspace workspace = repository.getWorkspace("username","password"); // Set library workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("LibraryName")); // define authoring template String authoringTemplateName = new String("Authoring Template"); // define parent sitearea String parentSiteAreaName = new String("SiteArea Name"); // define workflow String workflowName = new String("Workflow Name"); DocumentId authoringTemplateId = null; DocumentId parentSiteAreaId = null; DocumentId siblingId = null; DocumentId workflowId = null; // find authoring template by name DocumentIdIterator authoringTemplateIterator = workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName); if (authoringTemplateIterator.hasNext()){ authoringTemplateId = authoringTemplateIterator.nextId(); System.out.println("Authoring Template found : " + authoringTemplateName + "
");
}else {
out.println("Could not find Authoring Template: " + authoringTemplateName + "
");
}

// find sitearea by name
DocumentIdIterator siteAreaIterator = workspace.findByName(DocumentTypes.SiteArea,parentSiteAreaName );
if (siteAreaIterator.hasNext()){
parentSiteAreaId = siteAreaIterator.nextId();
out.println("Sitearea Found : " + parentSiteAreaName + "
");
}else {
out.println("Could not find Sitearea : " + parentSiteAreaName + "
");
}

// find workflow by name
DocumentIdIterator workflowIterator = workspace.findByName(DocumentTypes.Workflow,workflowName);
if (workflowIterator.hasNext()){
workflowId = workflowIterator.nextId();
out.println("Workflow found : " + workflowName + "
");
}else {
out.println("Could not find Workflow: " + workflowName + "
");
}
if((authoringTemplateId!=null) && (parentSiteAreaId!=null) && (workflowId!=null)){
// create new content
Content newContent = workspace.createContent(authoringTemplateId,parentSiteAreaId,siblingId,ChildPosition.END);
newContent.setName("NewContent");
newContent.setTitle("NewContent");
newContent.setDescription("New Content created using WCM API"); newContent.setWorkflowId(workflowId);

workspace.save(newContent);

DocumentIdIterator contentIterator = workspace.findByName(DocumentTypes.Content,"Almost there");
DocumentId contentId = null;
if(contentIterator.hasNext()) {
contentId = contentIterator.nextId();
}

//Get the content using getById
Content content = (Content)workspace.getById(contentId);

//Approving the Content
newContent.nextWorkflowStage();

System.out.println("Content page '" + parentSiteAreaName + newContent + "' moved to the approved state
");
String[] saveMessage = workspace.save((Document)newContent);

workspace.save(content);


if (saveMessage.length==0) {
out.println ("
Created new Content under " + parentSiteAreaName );
}

}else {
out.println ("Could not create new content");
}

%>

Thursday, February 10, 2011

LWCM Utility

Attached is a utility for creating a full website.The attached code creates the site, site areas,content ,sets the security permissions and adds the workflow to the content.

I have set this up as it is extremely useful.Consider this,if you add a controller plus an online form you now have a website creator :-)

I will continue posting some great LWCM work but will spice it up with some Portal.

Yours in Websphere
T.J.S.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import com.ibm.workplace.wcm.api.ChildPosition;
import com.ibm.workplace.wcm.api.Content;
import com.ibm.workplace.wcm.api.DocumentId;
import com.ibm.workplace.wcm.api.DocumentIdIterator;
import com.ibm.workplace.wcm.api.DocumentLibrary;
import com.ibm.workplace.wcm.api.DocumentType;
import com.ibm.workplace.wcm.api.DocumentTypes;
import com.ibm.workplace.wcm.api.LibraryComponent;
import com.ibm.workplace.wcm.api.LibraryImageComponent;
import com.ibm.workplace.wcm.api.ReferenceComponent;
import com.ibm.workplace.wcm.api.RichTextComponent;
import com.ibm.workplace.wcm.api.Site;
import com.ibm.workplace.wcm.api.SiteArea;
import com.ibm.workplace.wcm.api.SiteFrameworkContainer;
import com.ibm.workplace.wcm.api.TextComponent;
import com.ibm.workplace.wcm.api.WCM_API;
import com.ibm.workplace.wcm.api.Workspace;
import com.ibm.workplace.wcm.api.exceptions.ComponentNotFoundException;
import com.ibm.workplace.wcm.api.exceptions.OperationFailedException;
import com.ibm.workplace.wcm.api.exceptions.ServiceNotAvailableException;

private Workspace workspace;
private final String libraryName;
private final Map contentFieldsForSiteArea = new HashMap();

public WcmApiUtilBean(String libraryName) throws Exception {
super();

this.libraryName = libraryName;
this.initWorkspace();
}

private void initWorkspace() throws Exception {
ResourceBundle resource = ResourceBundle.getBundle("xxxxxxxxxx");
String wcmUserName = resource.getString("xxxxxxxx").trim();
String wcmUserPassword = resource.getString("xxxxxxxx").trim();
this.workspace = WCM_API.getRepository().getWorkspace(wcmUserName, wcmUserPassword);

DocumentLibrary docLib = this.workspace.getDocumentLibrary(this.libraryName);
this.workspace.setCurrentDocumentLibrary(docLib);
}

public void closeWorkspace() {
WCM_API.getRepository().endWorkspace();
this.workspace = null;
}

private DocumentId[] getWCMDocumentsByName(String wcmName, DocumentType documentTypes) {
List documentIds = new ArrayList();
DocumentIdIterator docIdIter = workspace.findByName(documentTypes, wcmName);
if (docIdIter.hasNext()) {
DocumentId wcmDocumentId = docIdIter.nextId();
documentIds.add(wcmDocumentId);
}
return (DocumentId[]) documentIds.toArray(new DocumentId[documentIds.size()]);
}

private DocumentId[] getWCMDocumentsByPath(String wcmPath, int workFlowStatus) {
List documentIds = new ArrayList();

DocumentIdIterator docIdIter = workspace.findByPath(wcmPath, workFlowStatus);
if (docIdIter != null && docIdIter.hasNext()) {
DocumentId wcmDocumentId = docIdIter.nextId();
documentIds.add(wcmDocumentId);
}
return (DocumentId[]) documentIds.toArray(new DocumentId[documentIds.size()]);
}

private SiteFrameworkContainer setTemplateMapping(SiteFrameworkContainer aWCMObject, String presTemp, String authoringTemp) throws Exception {

DocumentId[] authIds = null;
DocumentId[] presIds = null;

authIds = getWCMDocumentsByName(authoringTemp, DocumentTypes.AuthoringTemplate);

presIds = getWCMDocumentsByName(presTemp, DocumentTypes.PresentationTemplate);

if ((authIds.length == 1) && (presIds.length == 1)) {

if (!aWCMObject.hasTemplateMapping(authIds[0])) {
aWCMObject.addTemplateMapping(authIds[0], presIds[0]);
} else {

DocumentIdIterator docIt = aWCMObject.getAuthoringTemplateIds();
if (docIt.hasNext()) {
} else {
aWCMObject.addTemplateMapping(authIds[0], presIds[0]);
}
System.out.println("TemplateMapping authName = " + authoringTemp + " and presName = " + presTemp + " added to object " + aWCMObject.getName());
workspace.save(aWCMObject);
}
} else {
throw new Exception("Expected to find only one Authoring Template with Name:" + authoringTemp + " and Presentation Template with Name:" + presTemp);
}
return aWCMObject;
}

private SiteFrameworkContainer setSecuritySettings(SiteFrameworkContainer aWCMObject) throws Exception {

String accessType = "addReadAccessMembers";

String[] accessnl = new String[] { "[all authenticated portal users]", "anonymous portal user" };
for (int y = 0; y < accessnl.length; y++) { String access = accessnl[y]; if (accessType.equals("addReadAccessMembers")) { aWCMObject.addReadAccessMembers(new String[] { access }); System.out.println("addReadAccessMembers for Object = " + aWCMObject.getName() + " members = " + access); } else if (accessType.equals("addEditAccessMembers")) { aWCMObject.addEditAccessMembers(new String[] { access }); System.out.println("addEditAccessMembers for Object = " + aWCMObject.getName() + " members = " + access); } else if (accessType.equals("addDeleteAccessMembers")) { aWCMObject.addDeleteAccessMembers(new String[] { access }); System.out.println("addDeleteAccessMembers for Object = " + aWCMObject.getName() + " members = " + access); } } workspace.save(aWCMObject); return aWCMObject; } private SiteFrameworkContainer setTemplates(SiteFrameworkContainer aWCMObject, String presTemp, String authoringTemp) throws Exception { aWCMObject = setTemplateMapping(aWCMObject, presTemp, authoringTemp); aWCMObject = setSecuritySettings(aWCMObject); return aWCMObject; } private Content addTextComponent(Content aContent, String aTextField, String aValue) throws Exception { TextComponent txtComp = (TextComponent) aContent.getComponent(aTextField); txtComp.setText(aValue); aContent.setComponent(aTextField, txtComp); return aContent; } private Content addComponentReference(Content aContent, String fieldName, String value) throws Exception { DocumentId[] documentIds = this.getWCMDocumentsByName(value,DocumentTypes.LibraryImageComponent); ReferenceComponent refComp = (ReferenceComponent) aContent.getComponent(fieldName); LibraryComponent libComp = (LibraryComponent)workspace.getById(documentIds[0]); refComp.setComponentRef(libComp); aContent.setComponent(fieldName,refComp); return aContent; } private Content addRichTextComponent(Content aContent, String aTextField, String aValue) throws Exception { RichTextComponent txtComp = (RichTextComponent) aContent.getComponent(aTextField); txtComp.setRichText(aValue); aContent.setComponent(aTextField, txtComp); return aContent; } private Content setTextComponentForContent(Content aContent, String name, String value) throws Exception { String textField = name; String text = value; aContent = addTextComponent(aContent, textField, text); if (aContent != null) { System.out.println("adding textField = " + textField + " to content " + aContent.getName()); } if (aContent == null) { return null; } else { return aContent; } } private Content setComponentReferenceForContent(Content aContent, String name, String value) throws Exception { aContent = addComponentReference(aContent, name, value); if (aContent != null) { System.out.println("adding Component Reference = " + name + " to content " + aContent.getName()); } if (aContent == null) { return null; } else { return aContent; } } private Content setRichTextComponentForContent(Content aContent,String name,String value) throws Exception { aContent = addRichTextComponent(aContent, name, value); return aContent; } private SiteArea addDefaultContentToSiteArea(SiteArea aSiteArea, String authoringTempName, String contentTitleValue,String contentBodyValue) throws Exception { String contentName = aSiteArea.getParent().getName() + "_" + aSiteArea.getName() + "_c"; DocumentId[] defaultAuthIds = getWCMDocumentsByName(authoringTempName, DocumentTypes.AuthoringTemplate); if (defaultAuthIds.length > 0) {
if (defaultAuthIds.length == 1) {
System.out.println("defaultAuthId = " + defaultAuthIds[0]);
Content content = null;
DocumentId[] defaultContentIds = getWCMDocumentsByName(contentName, DocumentTypes.Content);
DocumentId defaultContentId;
if (defaultContentIds.length > 0) {
if (defaultContentIds.length == 1) {
content = (Content) workspace.getById(defaultContentIds[0]);
} else {
throw new Exception("Found more than on Content Item with the Same name:" + contentName);
}
defaultContentId = defaultContentIds[0];
} else {
content = workspace.createContent(defaultAuthIds[0], aSiteArea.getId(), null, ChildPosition.START);
defaultContentId = content.getId();
content.setName(contentName);
content.setTitle(contentName);
}

content = setTextComponentForContent(content, "Title", contentTitleValue);
content = setRichTextComponentForContent(content,"Body",contentBodyValue);


String workflowName = new String("xxxxxx");

DocumentIdIterator workflowIterator = workspace.findByName(DocumentTypes.Workflow, workflowName);
if (workflowIterator.hasNext()) {
DocumentId workflowId = workflowIterator.nextId();
System.out.println("Workflow found : " + workflowName + "
");

if (!content.isPublished()) {
content.setWorkflowId(workflowId);
workspace.save(content);
content.nextWorkflowStage();
System.out.println("Moved to next workflowstage");
}

} else {
System.out.println("Could not find Workflow: " + workflowName + "
");
}

aSiteArea.setDefaultContent(defaultContentId);
return aSiteArea;

} else {
throw new Exception("Found more than one Authoring Template with the same name:" + authoringTempName);
}
} else {
throw new Exception("Did not find more than one Authoring Template with the same name:" + authoringTempName);
}

}

private boolean saveToWorkspace(com.ibm.workplace.wcm.api.Document wcmDocument) throws Exception {

String[] errors = workspace.save(wcmDocument);

for (int i = 0; i < errors.length; i++) { System.out.println("Error while saving " + wcmDocument.getName() + " : " + errors[i]); } if (errors.length > 0) {
return false;
}
return true;
}

protected void finalize() throws Throwable {
this.closeWorkspace();
super.finalize();
}

public Site obtainSite(String siteName, String presTemp, String authoringTemp) throws Exception {
Site site = null;
DocumentId[] siteIds = getWCMDocumentsByName(siteName, DocumentTypes.Site);
if (siteIds.length > 0) {
if (siteIds.length == 1) {
site = (Site) workspace.getById(siteIds[0]);
} else {
throw new Exception("More then one Site found with the Same name:" + siteName);
}
} else {
site = workspace.createSite();
site.setName(siteName);
site.setTitle(siteName);
String[] messages = workspace.save(site);

for (int i = 0; i < messages.length; i++) { System.out.println("Message = " + messages[i]); } site = (Site) setTemplates(site, presTemp, authoringTemp); } return site; } public SiteArea obtainSiteArea(String siteAreaName, String presTemp, String authoringTemp, DocumentId parentId, String siteContentTitleValue,String siteContentBodyValue) throws Exception { SiteArea sa = null; String parentPath = workspace.getPathById(parentId, true, true); String siteAreaPath = parentPath + "/" + siteAreaName; DocumentId[] siteAreaIds = getWCMDocumentsByPath(siteAreaPath, Workspace.WORKFLOWSTATUS_ALL); if (siteAreaIds.length > 0) {
// if (siteAreaIds.length == 1) {
// sa = (SiteArea) workspace.getById(siteAreaIds[0]);
// } else {
throw new Exception("Found more than one Sitearea with the same name:" + siteAreaName);
// }
} else {
sa = workspace.createSiteArea(parentId, null, 1);
sa.setTitle(siteAreaName);
sa.setName(siteAreaName);
workspace.save(sa);
}

sa = (SiteArea) setTemplates(sa, presTemp, authoringTemp);

boolean saved = saveToWorkspace(sa);

sa = addDefaultContentToSiteArea(sa, authoringTemp, siteContentTitleValue,siteContentBodyValue);

saved = saveToWorkspace(sa);
System.out.println("site area = " + sa.getName());

return sa;
}

}

Generate reports from Opportunities using a Visualforce Page in Salesforce

  Step 1: Create a Visualforce Page Go to the Setup menu in Salesforce. Search for "Visualforce Pages" in the Quick Find box and c...