Thursday, 18 September 2014

Exploring wcm8.0 api in IBM websphere portal

(C:\IBM\WebSphere\wp_profile\installedApps\192Cell\wcm.ear\ilwwcm.war\webinterface\api-javadoc)




 // ********************** Finding presentation template*********************


templates DocumentIdIterator  presentationTemplateIterator = workspace.findByType(DocumentTypes.PresentationTemplate);
DocumentId presentationTemplateId = null;
if(presentationTemplateIterator.hasNext()){
while(presentationTemplateIterator.hasNext())
{
presentationTemplateId = presentationTemplateIterator.nextId();
out.println("Found Presentation Template : " + presentationTemplateId.getName() + "<br>");
}
} else {
out.println("Could not find any Presentation Template");
}


  // ********************** Find Library Component example *********************
// find all library components
DocumentIdIterator libraryComponentIterator = workspace.findByType(DocumentTypes.LibraryComponent);
DocumentId libraryComponentId = null;
if(libraryComponentIterator.hasNext()){
while(libraryComponentIterator.hasNext())
{
libraryComponentId = libraryComponentIterator.nextId();
out.println("Found Library component : " + libraryComponentId.getName() + "<br>");
}
} else {
out.println("Could not find any Library component");
}

// ************************** Find Workflow example *************************

// find all workflows
DocumentIdIterator workflowIterator = workspace.findByType(DocumentTypes.Workflow); DocumentId workflowId = null;
if(workflowIterator.hasNext()){
while(workflowIterator.hasNext())
{
workflowId = workflowIterator.nextId();
out.println("Found Workflow : " + workflowId.getName() + "<br>");
}
} else {
out.println("Could not find any Workflow");
}


// ************************ Find Workflow Stage example **********************
// find all workflow stages
DocumentIdIterator workflowStageIterator = workspace.findByType(DocumentTypes.WorkflowStage);
DocumentId workflowStageId = null;
if(workflowStageIterator.hasNext()){
while(workflowStageIterator.hasNext())
{
workflowStageId = workflowStageIterator.nextId();
out.println("Found Workflow Stage : " + workflowStageId.getName() + "<br>");
}
} else {
out.println("Could not find any Workflow Stage");
}


   // ************************** Find Taxonomy example ************************
// find all taxonomies
DocumentIdIterator taxonomyIterator = workspace.findByType(DocumentTypes.Taxonomy); DocumentId taxonomyId = null;
if(taxonomyIterator.hasNext()){
while(taxonomyIterator.hasNext())
{
taxonomyId = taxonomyIterator.nextId();
out.println("Found Taxonomy : " + taxonomyId.getName() + "<br>");
}
} else {
out.println("Could not find any Taxonomy");
- 11 -
}

// ************************** Find Category example *************************
// find all categories
DocumentIdIterator categoryIterator = workspace.findByType(DocumentTypes.Category); DocumentId categoryId = null;
if(categoryIterator.hasNext()){
while(categoryIterator.hasNext())
{
categoryId = categoryIterator.nextId();
out.println("Found Category : " + categoryId.getName() + "<br>");
}
} else {
out.println("Could not find any Category");
}
%>


Scenario 3: Find content by various criteria
To find content by Authoring Template, Category, path, Workflow Stage, modified since date, and modified between dates, you can select and use the appropriate API code in listing 3, which uses the following APIs:
workspace.findContentByAuthoringTemplate
workspace.findContentByCategory
workspace.findContentByPath
workspace.findContentByWorkflowStage
workspace.findContentModifiedSince
workspace.findContentModifiedBetween

Listing 3. Example code to find content by criteria
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
- 12 -
DocumentIdIterator contentIterator = null;
DocumentId contentId = null;


// **************** Find Content by authoring template example ******************

// define authoring template
String authoringTemplateName = new String("AuthoringTemplate");
// find authoring template by name
DocumentIdIterator authoringTemplateIterator = workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName);
DocumentId authoringTemplateId = null;
if (authoringTemplateIterator.hasNext()){
out.println("Authoring Template found : " + authoringTemplateName + "<br>");
authoringTemplateId = authoringTemplateIterator.nextId();
// find all the contents having above authoring template
contentIterator = workspace.findContentByAuthoringTemplate(authoringTemplateId);
if(contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("List of Content uses " + authoringTemplateName );
while(contentId!=null) {
out.println( "<BR>" + contentId.getName());
contentId = contentIterator.nextId();
}
} else {
out.println("Could not find any Content that uses " + authoringTemplateName );
}
}else {
out.println("Could not find Authoring Template: " + authoringTemplateName + "<br>");
}

// ******************* Find Content by Category example ***********************
// define category
String categoryName = new String("Category");
DocumentId categoryId = null;
// find category by name
DocumentIdIterator categoryIterator = workspace.findByName(DocumentTypes.Category,categoryName);
- 13 -
if (categoryIterator.hasNext()){
out.println("Category found : " + categoryName + "<br>");
categoryId = categoryIterator.nextId();
// find all the content having above category
contentIterator = workspace.findContentByCategory(categoryId);
if(contentIterator.hasNext()){
out.println("List of Content uses " + categoryName );
while(contentIterator.hasNext()) {
contentId = contentIterator.nextId();
out.println( "<BR>" + contentId.getName());
}
} else {
out.println("Could not find any Content that uses " + categoryName );
}
}else {
out.println("Could not find Category: " + categoryName + "<br>");
}


// ******************* Find Content by Path example **************************
// define content path
String contentPath = "/Web content/Site/SiteArea/Content";
// find content by path
contentIterator = workspace.findContentByPath(contentPath);
if (contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("Content found on Path : " + contentPath + " : Content name : " + contentId.getName() + "<br>");
}else {
out.println("Could not find Content on Path : " + contentPath + " <BR>");
}

// ******************* Find Content by Workflow Stage example ******************
// define workflow stage
String workflowStageName = new String("WorkflowStageName");
// find workflow stage by name
- 14 -
DocumentIdIterator workflowStageIterator = workspace.findByName(DocumentTypes.WorkflowStage,workflowStageName);
if (workflowStageIterator.hasNext()){
DocumentId workflowStageId = workflowStageIterator.nextId();
out.println("Workflow Stage found : " + workflowStageName + "<br>");
// find all the content having above workflow stage
contentIterator = workspace.findContentByWorkflowStage(workflowStageId);
if(contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("List of contents under Workflow Stage " + workflowStageName );
while(contentId!=null) {
out.println( "<BR>" + contentId.getName());
contentId = contentIterator.nextId();
}
} else {
out.println("Could not find any content under Workflow Stage " + workflowStageName );
}
}else {
out.println("Could not find Workflow Stage: " + workflowStageName + "<br>");
}


// ******************* Find Content since modified date example *****************

// define start date in dd/mm/yyyy format
Date startDate = new Date("01/01/2008");
// find content modified since start date
contentIterator = workspace.findContentModifiedSince(startDate);
if(contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("List of contents modified since " + startDate );
while(contentId!=null) {
out.println( "<BR>" + contentId.getName());
contentId = contentIterator.nextId();
}
} else {

out.println("Could not find any content modified since " + startDate );
}
%>


// **************** Find Content modified between two dates example *************
// define start date in dd/mm/yyyy format
startDate = new Date("01/01/2008");
// define end date in dd/mm/yyyy format
Date endDate = new Date("12/31/2009");
// find all the contents modified between start date and end date
contentIterator = workspace.findContentModifiedBetween(startDate,endDate);
if(contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("List of contents modified between " + startDate + "and" + endDate );
while(contentId!=null) {
out.println( "<BR>" + contentId.getName());
contentId = contentIterator.nextId();
}
} else {
out.println("Could not find any content modified between " + startDate + "and" + endDate );
}
%>


Scenario 4: Search for content based on Authoring Template, Site Area, Category, and Keywords criteria
To search for content based on criteria that are a combination of Authoring Template, Site Area, Category, or Keywords, you can select and use the appropriate API code in listing 4. You can do this by using the ContentSearch method, which returns an iterator of IDs of objects that match the given search criteria.
The ContentSearch method behaves in a manner similar to the Menu Component. If Site Areas have been specified, then all the ancestor and descendant Site Areas are also included in the search.
This performs an "or" search, not an "and" search. This means that a search for two different Categories and a Content Template will return content profiled with at least one of each profile type (one Category and one Template), not just content that matches all the parameters. Content that matches only one criteria type (Template only) will not be returned.

Note that the order of results is not guaranteed, and all parameters are optional except matchAllKeys, the last parameter.

Listing 4. Example code to search for content by criteria
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
DocumentId contentId = null;
DocumentId authoringTemplateId = null;
DocumentId siteAreaId = null;
DocumentId categoryId = null;
// define authoring template
String authoringTemplateName = new String("AuthoringTemplate");
// find authoring template by name
DocumentIdIterator authoringTemplateIterator = workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName);
if (authoringTemplateIterator.hasNext()){
authoringTemplateId = authoringTemplateIterator.nextId();
out.println("Authoring Template found : " + authoringTemplateName + "<br>");
}else {
out.println("Could not find Authoring Template: " + authoringTemplateName + "<br>");
}
// define siteare
String siteArea = new String("SiteArea");
// find sitearea by name
DocumentIdIterator siteAreaIterator = workspace.findByName(DocumentTypes.SiteArea,siteArea);
if (siteAreaIterator.hasNext()){
siteAreaId = siteAreaIterator.nextId();
out.println("Sitearea Found : " + siteArea + "<br>");
}else {
out.println("Could not find Sitearea : " + siteArea + "<br>");
}
// define category
String categoryName = new String("Category");
// find category by name
DocumentIdIterator categoryIterator = workspace.findByName(DocumentTypes.Category,categoryName);
if (categoryIterator.hasNext()){
categoryId = categoryIterator.nextId();
out.println("Category Found : " + categoryName + "<br>");
}else {
out.println("Could not find Category : " + categoryName + "<br>");
}
// define keywords
String[] keywords = new String[]{"Keyword"};
// search all the contents based on authoring template , sitearea , category and keywords
DocumentIdIterator contentIterator = workspace.contentSearch(authoringTemplateId, new DocumentId [] {siteAreaId}, new DocumentId [] {categoryId}, keywords,true);
if(contentIterator.hasNext()){
contentId = contentIterator.nextId();
while(contentId!=null)
{
out.println("Found Content : " + contentId.getName() + "<br>");
contentId = contentIterator.nextId();
}
} else {
out.println("Could not find any Content using the contentSearch Method");
}
%>


Scenario 5: Creating a new Site
You can create a new site using Web Content Management API methods, after which you can also use all the methods under Site Object once the site has been created. You can select and

use the appropriate code provided in listing 5, which demonstrates only the creation of a new site, setting its name, title, description, and then saving it to the repository.
We use workspace.createSite to achieve this, which returns the new Site object.

Listing 5. Example code to create a new Site

< %@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
// create new site
Site newSite = workspace.createSite();
newSite.setName("NewSite");
newSite.setTitle("NewSite");
newSite.setDescription("New Site Created using WCM API");
String[] saveMessage = workspace.save((Document)newSite);
if (saveMessage.length==0){
out.println("Could not create new Site..<BR>");
}else{
out.println("New Site created successfully.");
}
%>


Scenario 6: Creating a new Site Area
You can also create a new Site Area by using Web Content Management API methods, after which you can use all the methods under the Site Area Object.
Listing 6 demonstrates only creating a new Site Area, setting its name, title, description, and saving it to the repository. We use workspace.createSiteArea here, which creates a new Site
Area under the given parent. The parent can be a Site or a Site Area, and the method returns the new Site Area object.
Listing 6. Example code to create new Site Area
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
DocumentId documentId = null;
Site parentSite = null;
String parentSiteName = "Site";
DocumentId siblingId = null;
// find parent site
DocumentIdIterator siteIterator = workspace.findByName(DocumentTypes.Site,parentSiteName);
if (siteIterator.hasNext()){
documentId = siteIterator.nextId();
parentSite = (Site) workspace.getById(documentId);
// create new sitearea
SiteArea newSiteArea = workspace.createSiteArea((DocumentId)parentSite.getId(),siblingId,ChildPosition.END);
newSiteArea.setName("NewSiteArea");
newSiteArea.setTitle("NewSiteArea");
newSiteArea.setDescription("New Sitearea Created using WCM API
String[] saveMessage = workspace.save((Document)newSiteArea);
if (saveMessage.length==0) {
out.println (" <BR> Created new Sitearea under " + parentSiteName );
}
} else {
out.println (" <BR> Could not find parent Site " + parentSiteName + ". Could not create a new Sitearea." );

}
%>


Scenario 7: Creating new Content
You can create new Content under a specific Site Area at a specific position, using a Web Content Management API. You can select and use the appropriate provided code in listing 7, which demonstrates the creation of Content under a specific Site Area at the last position, with a specific Authoring Template.
The code also configures the Workflow while saving the content. We use workspace.createContent for this purpose, which returns a new content object based on the specified Authoring Template. The API code can be changed based on your requirements.

Listing 7. Example code to create a new Content
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
// define authoring template
String authoringTemplateName = new String("AuthoringTemplate");
// define parent sitearea
String parentSiteAreaName = new String("SiteArea");
// define workflow
String workflowName = new String("Workflow");
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();
out.println("Authoring Template found : " + authoringTemplateName + "<br>");
}else {
out.println("Could not find Authoring Template: " + authoringTemplateName + "<br>");
}
// find sitearea by name
DocumentIdIterator siteAreaIterator = workspace.findByName(DocumentTypes.SiteArea,parentSiteAreaName );
if (siteAreaIterator.hasNext()){
parentSiteAreaId = siteAreaIterator.nextId();
out.println("Sitearea Found : " + parentSiteAreaName + "<br>");
}else {
out.println("Could not find Sitearea : " + parentSiteAreaName + "<br>");
}
// find workflow by name
DocumentIdIterator workflowIterator = workspace.findByName(DocumentTypes.Workflow,workflowName);
if (workflowIterator.hasNext()){
workflowId = workflowIterator.nextId();
out.println("Workflow found : " + workflowName + "<br>");
}else {
out.println("Could not find Workflow: " + workflowName + "<br>");
}
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);
String[] saveMessage = workspace.save((Document)newContent);
if (saveMessage.length==0) {
out.println (" <BR> Created new Content under " + parentSiteAreaName );
}
}else {
out.println ("Could not create new content");
}
%>


Scenario 8: Creating a new Content Link that is linked to existing Content
You can create a new Content Link that is linked to existing Content under a specific Site Area. The Content must be saved before creating a link to it, while the Content Link is created immediately, and should not be saved.
We use workspace.createContentLink to achieve this, which returns a new Content Link object. The code in listing 8 assumes that the Content Link is not present under the Site Area.

Listing 8. Example code to create a new Content Link
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
// define content
String contentName = new String("Content");
// define sitearea where the content link needs to be created
String parentSiteArea = new String("NewSiteArea");
DocumentId contentId = null;
DocumentId parentSiteAreaId =null;
DocumentId siblingId = null;
// find content by name
DocumentIdIterator contentIterator = workspace.findByName(DocumentTypes.Content,contentName);
if (contentIterator.hasNext())
contentId = contentIterator.nextId();
// find sitearea by name
DocumentIdIterator parentSiteAreaIterator = workspace.findByName(DocumentTypes.SiteArea,parentSiteArea);
if (parentSiteAreaIterator.hasNext())
parentSiteAreaId = parentSiteAreaIterator.nextId();
if((contentId!=null) && (parentSiteAreaId!=null)) {
// create content link
ContentLink contentLink = workspace.createContentLink(contentId,parentSiteAreaId,siblingId,ChildPosition.END);
out.println("New Content link of Content " + contentName + " is created under new Sitearea " + parentSiteArea);
} else{
out.println("Could not create Content link of Content " + contentName + " under new Sitearea " + parentSiteArea);
}
%>


Scenario 9: Creating various Library Components
You can create Library Components such as DateComponent, DocumentManagerComponent, FileComponent, HTMLComponent, ImageComponent, JSPComponent, LinkComponent, RichTextComponent, ShortTextComponent, TextComponent, UserSelectionComponent.
The code in listing 9 demonstrates only creation, setting the name, title, description, and then saving them in the Web Content Management repository. The code can be directly used to create a File Component, Image Component, and Rich Text Component:
workspace.createFileComponent
workspace.createImageComponent
workspace.createRichTextComponent
Other APIs can be used once the components are created.

Listing 9. Example code to create various library components
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
String[] saveMessage = new String[1];


// ***************** Create File Resource Component example ********************
LibraryFileComponent libraryFileComponent = workspace.createFileComponent();
libraryFileComponent.setName("NewFileComponent");
libraryFileComponent.setTitle("NewFileComponent");
libraryFileComponent.setDescription("New File resource component created using API");
saveMessage = workspace.save((Document)libraryFileComponent);
out.println ("<BR> New File resource component created :" + libraryFileComponent.getName());



// ******************* Create Image Component example ***********************
LibraryImageComponent libraryImageComponent = workspace.createImageComponent();
libraryImageComponent.setName("NewImageComponent");
libraryImageComponent.setTitle("NewImageComponent");
libraryImageComponent.setDescription("New Image component created using API");
saveMessage = workspace.save((Document)libraryImageComponent);
out.println ("<BR> New Image component created :" + libraryImageComponent.getName());


// ******************* Create Rich Text Component example **********************
LibraryRichTextComponent libraryRichTextComponent = workspace.createRichTextComponent();
libraryRichTextComponent.setName("NewRichTextComponent");
libraryRichTextComponent.setTitle("NewRichTextComponent");
libraryRichTextComponent.setDescription("New Rich text component created using API");
saveMessage = workspace.save((Document)libraryRichTextComponent);
out.println ("<BR> New Rich text component created :" + libraryRichTextComponent.getName());
%>


Scenario 10: Creating a new Taxonomy and a new Category
You can create a new Taxonomy as well as a new Category under a specific Category or Taxonomy. The code in listing 10 demonstrates creating a new Taxonomy and new Category under a specific parent Taxonomy, and you can use this code directly in your code. We use:
workspace.createTaxonomy, which returns the new Taxonomy object
workspace.createCategory, which returns the new Category object
Listing 10. Example code to create a new Taxonomy and new Category
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
String[] saveMessage = new String[1];


// *********************** Create Taxonomy example **************************

Taxonomy parentTaxonomy = workspace.createTaxonomy();
parentTaxonomy.setName("NewParentTaxonomy");
parentTaxonomy.setTitle("NewParentTaxonomy");
parentTaxonomy.setDescription("New Taxonomy created using WCM API");
saveMessage = workspace.save((Document)parentTaxonomy);
if (saveMessage.length==0) {
out.println (" <BR> Created new Taxonomy : " + parentTaxonomy.getName());
}


// *********************** Create Category example **************************
Category newCategory = workspace.createCategory((DocumentId)parentTaxonomy.getId());
newCategory.setName("NewCategory");
newCategory.setTitle("NewCategory");
newCategory.setDescription("New Category Created using WCM API");
saveMessage = workspace.save((Document)newCategory);
if (saveMessage.length==0) {
out.println (" <BR> Created new Category : " + newCategory.getName());
}
%>


Scenario 11: Creating and canceling drafts on published Content items
You can create as well as cancel drafts on published content, using a Web Content Management API. The code in listing 11 can be used directly to create a draft of existing content and also to cancel an existing draft on published content.

Listing 11. Example code to create and cancel drafts
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
// define the publish content
String contentName = new String("PublishedContent");
DocumentId contentId = null;
// find content by name
DocumentIdIterator contentIterator = workspace.findByName(DocumentTypes.Content,contentName);
if (contentIterator.hasNext()){
contentId = contentIterator.nextId();
Content content = (Content) workspace.getById(contentId);
// create draft from publish content
Document draftContent = content.createDraftDocument();
out.println("Draft created for content " + contentName);
// cancel draft
if (content. hasDraft()) {
draftContent = content.cancelDraftDocument();
out.println("Draft cancled for " + contentName);
} else {
out.println("Draft does not exist for content : " + contentName);
}
}
%>


Scenario 12: Moving content to Next Stage of a Workflow, Restarting the Workflow, setting another Workflow on specific content
You can move content to the next Workflow stage, restart the Workflow, and set another workflow on specific content. You can use the code in listing 12 to move content from the draft stage to the publish stage, restart the workflow, and to set another Workflow on a specific content item.
Listing 12. Example code to move Content
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
// define the draft content
String contentName = new String("Content");
Content content = null;
DocumentId contentId = null;
// define new workflow to be set on content
String newWorkflowName = new String("Workflow");
DocumentId newWorkflowId = null;
// find content by name
DocumentIdIterator contentIterator = workspace.findByName(DocumentTypes.Content,contentName);
if (contentIterator.hasNext()){
contentId = contentIterator.nextId();
content = (Content) workspace.getById(contentId);
// below code may not be required if the next workflow stage does not exist
// moving content to next workflow stage
content.nextWorkflowStage();
out.println("Draft content" + contentName + "is moved to next workflow stage");
// restarting workflow
content.restartWorkflow();
out.println("<BR> Workflow restarted for content " + contentName);
}


// find new workflow by name
DocumentIdIterator workflowIterator = workspace.findByName(DocumentTypes.Workflow,newWorkflowName);
if (workflowIterator.hasNext())
newWorkflowId = workflowIterator.nextId();
if((contentId!=null) && (newWorkflowId !=null)) {
// set new \ another workflow on content
content.setWorkflowId(newWorkflowId);
String[] saveMessage = workspace.save((Document)content);
out.println("<BR> New Workflow set on content : " + contentName);
} else {
out.println("<BR> Could not set new Workflow on content : " + contentName);
}
%>


Scenario 13: Render Sites, Site Areas, Contents, Content components, or Library Components
You can render Sites, Site Areas, Contents, Content components, or Library components. The given Web Content Management content component is rendered using the specified rendering context.
Note that it’s not enough to specify only the ContentComponent to be rendered; the path to the item containing the ContentComponent must also be specified in the RenderingContext.
The code in listing 13 demonstrates rendering Content, Content component, and the Library component. You can always add or remove more components per your requirements.
Listing 13. Example code to render various artifacts
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());

// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));


// ******************* Render Site\ Sitearea or Content example ******************
// define path of the site , sitearea or content to be rendered.
String contentPath = new String ("/WebContent/Site/SiteArea/Content");
Map parametermap = new HashMap();
parametermap.put("srv", "page");
// create rendering context
RenderingContext renderingContext = workspace.createRenderingContext(request, response, parametermap);
if(renderingContext!=null) {
renderingContext.setRenderedContent(contentPath);
out.println("Rendering content " + contentPath );
out.println ("<BR>" + workspace.render(renderingContext));
} else {
out.println("Could not render content");
}


    // ********************* Render Content Component example ********************
// define content name
String contentName = new String("Content");
// define siteArea name
String siteAreaName = new String("SiteArea");
Content content = null;
ContentComponent component=null;
SiteArea siteArea = null;
// find content by name
DocumentIdIterator contentIterator = workspace.findByName(DocumentTypes.Content,contentName);
if (contentIterator.hasNext()){
content = (Content) workspace.getById(contentIterator.nextId());
component = content.getComponent("MyComponent");
}
// find sitearea by name
DocumentIdIterator siteAreaIterator = workspace.findByName(DocumentTypes.SiteArea,siteAreaName);
if (siteAreaIterator.hasNext())
siteArea = (SiteArea) workspace.getById(siteAreaIterator.nextId());
if((content!=null) && (siteArea!=null)){
renderingContext.setRenderedContent(content,siteArea);
out.println("<BR> Rendering content component : " + component.getName());
out.println ("<BR>" + workspace.render(renderingContext,component));
} else {
out.println("Could not render Content component");
}


// ********************* Render Library Component example ********************
// define library component
String libraryComponentName = new String("LibraryComponent");
// find library component by name
DocumentIdIterator libraryComponentIterator = workspace.findByName(DocumentTypes.LibraryComponent,libraryComponentName);
if (libraryComponentIterator.hasNext()){
LibraryComponent libraryComponent = (LibraryComponent) workspace.getById(libraryComponentIterator.nextId());
renderingContext.setRenderedContent(contentPath);
out.println("<BR> Rendering library component " + libraryComponentName );
out.println ("<BR>" + workspace.render(renderingContext,libraryComponent));
} else {
out.println("<BR> Could not render Library component" + libraryComponentName);
}
%>

          Scenario 14: Moving or copying an entire Site Area within/between libraries
To move or copy an entire Site Area with all its children to another parent site within the same library or to another library, you can use the API code in listing 14. To move the Site Area, we use the workspace.moveSiteFrameworkDocument method.
To copy the Site Area, you can use the workspace.copySiteFrameworkDocument method to replace the workspace.moveSiteFrameworkDocument method, using the same code below.

                      Listing 14. Example code to move or copy Site Area
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// define sitearea to be move
String siteArea = new String("SiteArea");
// define new parent site
String newParentSite = new String("Site");
DocumentId siteAreaId = null;
// define sibling id
DocumentId siblingId = null;.
DocumentId newParentsiteId = null;
// set source library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("Source Library"));
// find sitearea to be moved
DocumentIdIterator siteAreaIterator = workspace.findByName(DocumentTypes.SiteArea,siteArea);
if (siteAreaIterator.hasNext()){
siteAreaId = siteAreaIterator.nextId();
out.println("Sitearea Found : " + siteArea + "<br>");
}else {
out.println("Could not find Sitearea : " + siteArea + "<br>");
}
// target library. Not required while moving Sitearea to the same library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("Target Library"));
// find new parent site
DocumentIdIterator siteIterator = workspace.findByName(DocumentTypes.Site,newParentSite);
if (siteIterator.hasNext()){
newParentsiteId = siteIterator.nextId();
out.println("New parent site found : " + newParentSite + "<br>"); - 32 -
}else {
out.println("Could not find new parent site : " + newParentSite + "<br>");
}
if ((newParentsiteId!=null) && (siteAreaId!=null)) {
out.println(" Moving Sitearea <b> " + siteArea + "</b> to new parent site <b>" + newParentSite + "</b><br>");
// move sitearea to new parent site
workspace.moveSiteFrameworkDocument(siteAreaId,newParentsiteId,siblingId,ChildPosition.START);
out.println(" Moved Sitearea <b>" + siteArea + " </b> to new parent site <b>" + newParentSite + "</b><br>");
}else{
out.println(" Moving Sitearea <b> " + siteArea + "</b> to new parent site <b>" + newParentSite + "</b> failed <br>");
}
%>


Scenario 15: Moving or copying content within/between libraries


You can move or copy the content to another Site Area either within the same library or to a different library, using the code in listing 15. To move the content, we use the workspace.moveSiteFrameworkDocument method.
To copy the Site Area, you can use the workspace.copySiteFrameworkDocument method to replace the workspace.moveSiteFrameworkDocument method, using the same code below.
Listing 15. Example code to move or copy content
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// define content to be move
String contentName = new String("Content");
// define new parent siteArea
String newParentSitearea = new String("SiteArea");
// sibling id
DocumentId siblingId = null;
DocumentId contentId = null;
DocumentId newParentsiteareaId = null;
// set source library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("Source Library"));
// find content to be moved
DocumentIdIterator contentIterator = workspace.findByName(DocumentTypes.Content,contentName);
if (contentIterator.hasNext()){
contentId = contentIterator.nextId();
out.println("Content Found : " + contentName + "<br>");
}else {
out.println("Could not find Content : " + contentName + "<br>");
}
// set target library. Not required when moving content to the same library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("Target Library"));
// find new parent sitearea
DocumentIdIterator newParentSiteareaIterator = workspace.findByName(DocumentTypes.SiteArea,newParentSitearea);
if (newParentSiteareaIterator.hasNext()){
newParentsiteareaId = newParentSiteareaIterator.nextId();
out.println("New parent Sitearea found : " + newParentSitearea + "<br>");
}else {
out.println("Could not find new parent Sitearea : " + newParentSitearea + "<br>");
}
if ((contentId!=null) && (newParentsiteareaId!=null)) {
out.println(" Moving content <b> " + contentName + "</b> to new parent Sitearea <b>" + newParentSitearea + "</b><br>");
// move content to new sitearea
workspace.moveSiteFrameworkDocument(contentId,newParentsiteareaId,siblingId,ChildPosition.START);
- 34 -
out.println(" Moved content <b>" + contentName + " </b> to new parent Sitearea <b>" + newParentSitearea + "</b><br>");
}else{
out.println(" Moving content <b> " + contentName + "</b> to new parent Sitearea <b>" + newParentSitearea + "</b> failed.<br>");
}
%>
Scenario 16: Moving or copying non-hierarchical or root items to another library
To move or copy any non-hierarchical items (Authoring Templates, Presentation Templates, Library Components, Workflows, Workflow Stages), or root items (Sites and Taxonomies) to another library, use the code in listing 16. To move the item to another library, we use the workspace.moveToLibrary method, which moves non-hierarchical or root items to another library.
To copy the item, we use the workspace.copyToLibrary method to replace the workspace.moveToLibrary method, using the same code below.
The code in listing 16 shows how we can achieve this for Authoring Templates, Presentation Templates, Library components, Workflows, Workflow Stages, Sites, and Taxonomies.
Listing 16. Example code to move or copy root items to another library
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// set source library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("Source Library"));
// get target library
DocumentLibrary targetDocumentLibrary = workspace.getDocumentLibrary("Target Library");


// ********************** Move Authoring Template example ******************
// define authoring template
String authoringTemplateName = new String("AuthoringTemplate");
// find authoring template by name
DocumentIdIterator authoringTemplateIterator = workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName);
if (authoringTemplateIterator.hasNext()){
DocumentId authoringTemplateId = authoringTemplateIterator.nextId(); out.println("Authoring Template found : " + authoringTemplateName + "<br>");
out.println(" Moving Authoring Template <b> " + authoringTemplateName + "</b> to new library <b> Target Library </b><br>");
// move authoring template to another library
workspace.moveToLibrary(targetDocumentLibrary ,authoringTemplateId ); out.println(" Moved Authoring Template <b>" + authoringTemplateName + " </b> to another library <b> Target Library </b><br>");
}else {
out.println("Could not find Authoring Template: " + authoringTemplateName + "<br>");
}


      // ********************** Move Presentation Template example ******************
// define presentation template
String presentationTemplateName = new String("PresentationTemplate");
// find presentation template by name
DocumentIdIterator presentationTemplateIterator = workspace.findByName(DocumentTypes.PresentationTemplate,presentationTemplateName);
if (presentationTemplateIterator.hasNext()){
DocumentId presentationTemplateId = presentationTemplateIterator.nextId(); out.println("Presentation Template found : " + presentationTemplateName + "<br>");
out.println(" Moving Presentation Template <b> " + presentationTemplateName + "</b> to new library <b> Target Library </b><br>");
// move presentation template to another library
workspace.moveToLibrary(targetDocumentLibrary ,presentationTemplateId ); // Moving presentation tempate to another library
out.println(" Moved Presentation Template <b>" + presentationTemplateName + " </b> to another library <b> Target Library </b><br>");
}else {
out.println("Could not find Presentation Template: " + presentationTemplateName + "<br>");
}



// ********************** Move Library Component example *********************

// define library component
String libraryComponentName = new String("LibraryComponent");
// find library component by name
DocumentIdIterator libraryComponentIterator = workspace.findByName(DocumentTypes.LibraryComponent,libraryComponentName);
if (libraryComponentIterator.hasNext()){
DocumentId libraryComponentId = libraryComponentIterator.nextId(); out.println("Library Component found : " + libraryComponentName + "<br>");
out.println(" Moving Library Component <b> " + libraryComponentName + "</b> to new library <b> Target Library </b><br>");
// move library component to new library
workspace.moveToLibrary(targetDocumentLibrary ,libraryComponentId );
out.println(" Moved Library Component <b>" + libraryComponentName + " </b> to another library <b> Target Library </b><br>");
}else {
out.println("Could not find Library Component: " + libraryComponentName + "<br>");
}


// ************************* Move Workflow example *************************
// define workflow
String workflowName = new String("Workflow");
// find workflow by name
DocumentIdIterator workflowIterator = workspace.findByName(DocumentTypes.Workflow,workflowName);
if (workflowIterator.hasNext()){
DocumentId workflowId = workflowIterator.nextId();
out.println("Workflow found : " + workflowName + "<br>");
out.println(" Moving Workflow <b> " + workflowName + "</b> to new library <b> Target Library </b><br>");
// move workflow to another library
workspace.moveToLibrary(targetDocumentLibrary ,workflowId );
out.println(" Moved workflow <b>" + workflowName + " </b> to another library <b> Target Library </b><br>");
}else {
out.println("Could not find Workflow: " + workflowName + "<br>");
}

  // ************************ Move Site example ******************************
// define site to be move
String siteName = new String("Site");
// find site by name
DocumentIdIterator siteIterator = workspace.findByName(DocumentTypes.Site,siteName); DocumentId siteId = null;
if (siteIterator.hasNext()){
out.println("Site found : " + siteName + "<br>");
out.println(" Moving Site <b> " + siteName + "</b> to new library <b> Target Library </b><br>");
siteId = siteIterator.nextId();
// move site to another library
workspace.moveToLibrary(targetDocumentLibrary ,siteId );
out.println(" Moved Site <b>" + siteName + " </b> to another library <b> Target Library </b><br>");
}else {
out.println("Could not find Site : " + siteName + "<br>");
}


// ********************** Move Workflow Stage example ***********************
// define workflow stage
String workflowStageName = new String("WorkflowStageName");
// find workflow stage by name
DocumentIdIterator workflowStageIterator = workspace.findByName(DocumentTypes.WorkflowStage,workflowStageName);
if (workflowStageIterator.hasNext()){
DocumentId workflowStageId = workflowStageIterator.nextId();
out.println("Workflow Stage found : " + workflowStageName + "<br>");
out.println(" Moving Workflow Stage<b> " + workflowStageName + "</b> to new library <b> Target Library </b><br>");
// move workflowstage to another library
workspace.moveToLibrary(targetDocumentLibrary ,workflowStageId );
out.println(" Moved Workflow Stage<b>" + workflowStageName + " </b> to another library <b> Target Library </b><br>");
}else {
out.println("Could not find Workflow Stage: " + workflowStageName + "<br>");
}


 // ************************ Move Taxonomy example *************************
// define taxonomy
String taxonomyName = new String("Taxonomy");
// find taxonomy by name
DocumentIdIterator taxonomyIterator = workspace.findByName(DocumentTypes.Taxonomy,taxonomyName);
if (taxonomyIterator.hasNext()){
DocumentId taxonomyId = taxonomyIterator.nextId();
out.println("Taxonomy found : " + taxonomyName + "<br>");
out.println(" Moving Taxonomy <b> " + taxonomyName + "</b> to new library <b> Target Library </b><br>");
// move taxonomy to another library
workspace.moveToLibrary(targetDocumentLibrary ,taxonomyId );
out.println(" Moved Taxonomy <b>" + taxonomyName + " </b> to another library <b> Target Library </b><br>");
}else {
out.println("Could not find Taxonomy: " + taxonomyName + "<br>");
}
%>
Scenario 17:  Moving or copying a Category within/between libraries
To move or copy a Category in the same library or between libraries, use the code in lisitng 17 below. To move the item in the same or to another library, we use the workspace.moveCategory method, which essentially moves a category and adds it under a new parent.
To copy the category, we use the workspace.copyCategory method to replace the workspace.moveToLibrary method, using the same code below.
Note that the example below moves a category to another taxonomy; you can also move the category to another parent category.
Listing 17. Example code to move or copy a Category
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));
// define category to be move
String categoryName = new String("Category");
// define parent taxonomy
String newTaxonomy = new String("Taxonomy");
DocumentId categoryId = null;
DocumentId taxonomyId = null;
// find category to be moved
DocumentIdIterator categoryIterator = workspace.findByName(DocumentTypes.Category,categoryName);
if (categoryIterator.hasNext()){
categoryId = categoryIterator.nextId();
out.println("Category Found : " + categoryName + "<br>");
}else {
out.println("Could not find Category : " + categoryName + "<br>");
}
// find taxonomy by name
DocumentIdIterator taxonomyIterator = workspace.findByName(DocumentTypes.Taxonomy,newTaxonomy);
if (taxonomyIterator.hasNext()){
taxonomyId = taxonomyIterator.nextId();
out.println("Taxonomy found : " + newTaxonomy + "<br>");
}else {
out.println("Could not find Taxonomy: " + newTaxonomy + "<br>");
}
if ((categoryId!=null) && (taxonomyId!=null)) {
out.println(" Moving Category <b> " + categoryName + "</b> to new parent taxonomy <b>" + newTaxonomy + "</b><br>");
// move category

workspace.moveCategory(categoryId,taxonomyId);
out.println(" Moved Category <b>" + categoryName + " </b> to new parent taxonomy <b>" + newTaxonomy + "</b><br>");
}else{
out.println(" Moving Category <b> " + categoryName + "</b> to new parent taxonomy <b>" + newTaxonomy + "</b> failed <br>");
}
%>
Scenario 18:
                             Deleting and purging Objects
We can delete and purge all types of objects, using Web Content Management APIs. It’s best to clear all the references of the object being deleted before deleting the actual object; otherwise, the deletion will fail.
We can also purge (permanently delete) the object from the repository. The code in listing 18 demonstrates deleting as well as purging a content item and as well as a Library Component.
To delete a Site / Site Area / Taxonomy, you must delete all the children prior to deleting the Sites / Site Areas / and Taxonomies.
Listing

                   Example code to delete and purge Objects
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="java.util.*"%>
<%
// retrieve repository
Repository repository = WCM_API.getRepository();
// get workspace for current user
Workspace workspace = repository.getWorkspace(request.getUserPrincipal());
// Set library
workspace.setCurrentDocumentLibrary(workspace.getDocumentLibrary("WebContent"));


// ********************** Delete \ Purge Content example **********************
// define content
String contentName = new String("Content");
String[] deleteMessage = new String[1];
// find content by name
DocumentIdIterator contentIterator = workspace.findByName(DocumentTypes.Content,contentName);
DocumentId contentId = null;
if (contentIterator.hasNext()){
contentId = contentIterator.nextId();
// clear all the references of the content
if(workspace.getReferences(contentId)!=null)
workspace.clearReferences(workspace.getReferences(contentId));
// delete content
deleteMessage = workspace.delete(contentId);
out.println("Content deleted :" + deleteMessage);
// purge content
deleteMessage = workspace.purge(contentId);
out.println("<BR> Content purged:" + deleteMessage);
} else {
out.println("Could not delete content :" + contentName);
}


      // ****************** Delete \ Purge Library Component example ******************
// define library component
String libraryComponentName = new String("LibraryComponent");
// find library component by name
DocumentIdIterator libraryComponentIterator = workspace.findByName(DocumentTypes.LibraryComponent,libraryComponentName);
DocumentId libraryComponentId = null;
if (libraryComponentIterator.hasNext()){
libraryComponentId = libraryComponentIterator.nextId();
// clear all the references of library component
if(workspace.getReferences(libraryComponentId)!=null)
workspace.clearReferences(workspace.getReferences(libraryComponentId));
// delete library component
deleteMessage = workspace.delete(libraryComponentId);
out.println("Library Component deleted :" + deleteMessage);
// purge library component
deleteMessage = workspace.purge(libraryComponentId);
out.println("<BR> Library Component purged:" + deleteMessage);
} else {
out.println("Could not delete Library Component :" + libraryComponentName);

}
%>

No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...