Lotus WCM6.1 API 实现常见扩展场景

转自:http://www.ibm.com/developerworks/cn/lotus/documentation/d-ls-wcmapi/index.html

IBM Lotus Web Content Management(此后简称为 “Web Content Management”)API 为 Web Content Management 的标准特性提供了扩展。本文将介绍此 API 的使用和解决方案,以及用户在使用它时执行的最常见实现的代码样例。您可以对本文的代码样例加以选择,并将其按照原样放入到您的系统中。

1 简介

您可以使用 Web Content Management 产品的 API 和 JavaServer PagesTM (JSP) 文件扩展它的标准特性。Lotus Web Content Management API 主要关注以编程方式处理内容,它是访问 Web Content Management 站点的另一种方式。它允许完全使用 JavaTM 代码创建、编辑、删除和呈现内容和其他 Web Content Management 对象。

此外,您可以以一种全新的方式操作站点并将 Web Content Management 与其他应用程序集成在一起。

使用 Web Content Management API 可以实现许多功能;下面是其中的一些:

  • Web 内容库:
    • 在库内部移动或复制项。
    • 在库与库之间移动或复制项。
  • 项 ID 的搜索迭代器:
    • 按名称查找给定类型的项。
    • 查找具有给定类型的项。
    • 按名称查找库组件。
    • 按编写模板查找内容。
    • 按类别查找内容。
    • 按路径查找内容。
    • 按工作流阶段查找内容。
    • 查找在某个日期之后修改的内容。
    • 修改在两个日期之间修改的内容。
  • 内容搜索(类似于 Menu Component 搜索项,但是使用与 id 相同的项参数)。
  • 通过项 ID 检索。
  • 创建、删除和保存以下项的能力:
    • 内容
    • 站点
    • 站点区域
    • 文件资源组件
    • HTML 组件
    • 图像组件
    • 日期和时间组件
    • 链接组件
    • 数值组件
    • 富文本组件
    • 样式表组件
    • 短文本组件
    • 文本组件
    • 用户选择组件
    • 分类
    • 类别
  • 检索以下项的能力:
    • 内容
    • 站点
    • 站点区域
    • 分类和类别
    • 工作流
    • 组件
  • 从搜索中检索以下项的能力(但是不是以项的形式):
    • 编写模板 ID(编写模板)
    • 演示模板 ID(演示模板)
    • 工作流阶段 ID
  • 在工作流阶段中批准或拒绝内容项的能力。其他项类型不支持这种功能。

Javadocs 应当被视为一个使用 API 的完整的可用特性集。Javadoc HTML 文件位于 was profile root\instal ledApps\nodename\wcm .ear\ilwwcm .war\webinterface\ 文件夹下。

下面提供的所有样例都可以直接应用于或合并到您的代码,以根据环境中必须实现的各种场景来获得所需的结果。我们为大部分代码添加了注释(以斜体 显示),以方便理解代码和流程。

注意:有关更多信息,请参考 IBM Lotus Web Content Management API 信息中心主题,本小节中的内容就是从其中摘取而来。

2 假设和考虑

在开始之前,注意以下问题:

  • 用户必须访问以下代码样例中提到的所有对象。
  • 我们不会使用 try-catch 方式获取异常。
  • moveToLibrary 的 Javadoc 中这样陈述:“这种方法将非层次化的或项移动到另一个库”。
  • 站点和分类被认为是根项。
  • 参考本文档的第 4 小节“在所有示例中指定的名称的标准”,其中解释了代码样例中使用的标准。

    3 针对每种需求使用 API 实现不同场景的代码样例

    以下的所有场景均在 6.1 版本的 Web Content Management 中测试过;然而,同样的逻辑可以应用于版本 6.0,只需在需要时对 API 进行修改。

    场景 1:按名称查找给定类型的项

    要按名称查找给定类型的项,您可以选择并使用清单 1 中的相应 API 代码。通过这些代码,您可以使用 workspace.findbyName API 搜索 Site、Site Area、Content、Content Link、Authoring 模板、Presentation 模板、所有库组件、工作流、工作流阶段、分类和类别等等。

    这实际上将返回一个具有指定名称的特定类型的所有对象的 ID 迭代器。


    清单 1. 按名称查找项的示例代码
    				
    
    
    ");
    }else {
        out.println("Could not find site : " + siteName + "
    "); } // ************************* Find Sitearea example ************************** // define sitearea name String siteArea = new String("SiteArea"); // find sitearea by name DocumentIdIterator siteAreaIterator = workspace.findByName(DocumentTypes. SiteArea,siteArea); if (siteAreaIterator.hasNext()) { out.println("Sitearea found : " + siteArea + "
    "); }else { out.println("Could not find Sitearea : " + siteArea + "
    "); } // ************************* Find Content example *************************** // define content name String contentName = new String("Content"); // find content by name DocumentIdIterator contentIterator = workspace .findByName(DocumentTypes.Content,contentName); if (contentIterator.hasNext()) { out.println("Content found : " + contentName + "
    "); }else { out.println("Could not find Content : " + contentName + "
    "); } // *********************** Find Authoring Template example ******************* // define authoring template name String authoringTemplateName = new String("AuthoringTemplate"); // find authoring template by name DocumentIdIterator authoringTemplateIterator = workspace.findByName(DocumentTypes.AuthoringTemplate,authoringTemplateName); if (authoringTemplateIterator.hasNext()) { out.println("Authoring template found : " + authoringTemplateName + "
    "); }else { out.println("Could not find Authoring template: " + authoringTemplateName + "
    "); } // *********************** Find Presentation Template example ***************** // define presentation template name String presentationTemplateName = new String("PresentationTemplate"); // find presentation template by name DocumentIdIterator presentationTemplateIterator = workspace.findByName(DocumentTypes.PresentationTemplate,presentationTemplateName); if (presentationTemplateIterator.hasNext()) { out.println("Presentation template found : " + presentationTemplateName + "
    "); }else { out.println("Could not find Presentation template: " + presentationTemplateName + "
    "); } // *********************** Find Library Component example ******************* // define library component name String libraryComponentName = new String("LibraryComponent"); // find library component by name DocumentIdIterator libraryComponentIterator = workspace.findByName(DocumentTypes.LibraryComponent,libraryComponentName); if (libraryComponentIterator.hasNext()) { out.println("Library component found : " + libraryComponentName + "
    "); }else { out.println("Could not find Library component: " + libraryComponentName + "
    "); } // *************************** Find Workflow example *********************** // define workflow name String workflowName = new String("Workflow"); // find workflow by name DocumentIdIterator workflowIterator = workspace .findByName(DocumentTypes.Workflow,workflowName); if (workflowIterator.hasNext()) { out.println("Workflow found : " + workflowName + "
    "); }else { out.println("Could not find Workflow: " + workflowName + "
    "); } // ************************* Find Workflow Stage example ******************** // define workflow stage name String workflowStageName = new String("WorkflowStageName"); // find workflow stage by name DocumentIdIterator workflowStageIterator = workspace.findByName(DocumentTypes.WorkflowStage,workflowStageName); if (workflowStageIterator.hasNext()) { out.println("Workflow Stage found : " + workflowStageName + "
    "); }else { out.println("Could not find Workflow Stage: " + workflowStageName + "
    "); } // ************************** Find Taxonomy example ************************ // define taxonomy name String taxonomyName = new String("Taxonomy"); // find taxonomy by name DocumentIdIterator taxonomyIterator = workspace.findByName(DocumentTypes.Taxonomy,taxonomyName); if (taxonomyIterator.hasNext()) { out.println("Taxonomy found : " + taxonomyName + "
    "); }else { out.println("Could not find Taxonomy: " + taxonomyName + "
    "); } // ************************** Find Category example ************************* // define category name String categoryName = new String("Category"); // find category by name DocumentIdIterator categoryIterator = workspace.findByName(DocumentTypes.Category,categoryName); if (categoryIterator.hasNext()) { out.println("Category Found : " + categoryName + "
    "); }else { out.println("Could not find Category : " + categoryName + "
    "); } %>

    场景 2:根据项类型查找项

    要根据特定类型查找项,可以直接选择和使用清单 2 中的相应的 API 代码。通过这些代码,您可以使用workspace.findbyType API 搜索所有 Sites、Site Areas、Contents、Content Links、Authoring templates、Presentation 模板、所有库组件、工作流、工作流阶段、分类和类别等等。

    这将返回给定类型的所有对象的 ID 迭代器。


    清单 2. 按类型查找项的示例代码
    				
    
    
    ");
    } else {
    out.println("Could not find any Site");
    }
    // ************************** Find Sitearea example *************************
    // find all siteareas
    DocumentIdIterator siteAreaIterator 
        = workspace.findByType(DocumentTypes. SiteArea); DocumentId siteAreaId = null;
    if(siteAreaIterator.hasNext()) {
    while(siteAreaIterator.hasNext())
    {
    siteAreaId = siteAreaIterator.nextId();
    out.println("Found Sitearea : " + siteAreaId.getName() + "
    "); } } else { out.println("Could not find any Sitearea"); } // ************************** Find Content example ************************** // find all contents DocumentIdIterator contentIterator = workspace.findByType(DocumentTypes.Content); DocumentId contentId = null; if(contentIterator.hasNext()) { while(contentIterator.hasNext()) { contentId = contentIterator.nextId(); out.println("Found Content : " + contentId.getName() + "
    "); } } else { out.println("Could not find any Content"); } // ********************** Find Authoring Template example ********************* // find all authoring templates DocumentIdIterator authoringTemplateIterator = workspace.findByType(DocumentTypes.AuthoringTemplate); DocumentId authoringTemplateId = null; if(authoringTemplateIterator.hasNext()) { while(authoringTemplateIterator.hasNext()) { authoringTemplateId = authoringTemplateIterator.nextId(); out.println("Found Authoring Template : " + authoringTemplateId.getName() + "
    "); } } else { out.println("Could not find any Authoring Template"); } // ********************* Find Presentation Template example ******************** // find all presentation 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() + "
    "); } } 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() + "
    "); } } 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() + "
    "); } } 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() + "
    "); } } 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() + "
    "); } } else { out.println("Could not find any Taxonomy"); } // ************************** 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() + "
    "); } } else { out.println("Could not find any Category"); } %>

    场景 3:根据各种条件查找内容

    要按照 Authoring Template、Category、路径、Workflow Stag 和修改日期查找内容,可以选择和使用清单 3 中的相应的 API 代码,使用以下 API:

    • workspace.findContentByAuthoringTemplate
    • workspace.findContentByCategory
    • workspace.findContentByPath
    • workspace.findContentByWorkflowStage
    • workspace.findContentModifiedSince
    • workspace.findContentModifiedBetween

    清单 3. 按条件查找内容的示例代码
    				
    
    
    "); 
    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( "
    " + 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 + "
    "); } // ******************* 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); if (categoryIterator.hasNext()) { out.println("Category found : " + categoryName + "
    "); 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( "
    " + contentId.getName()); } } else { out.println("Could not find any Content that uses " + categoryName ); } }else { out.println("Could not find Category: " + categoryName + "
    "); } // ******************* 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() + "
    "); }else { out.println("Could not find Content on Path : " + contentPath + "
    "); } // ******************* Find Content by 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 + "
    "); // 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( "
    " + 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 + "
    "); } // ******************* 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( "
    " + 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("0 1/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( "
    " + contentId.getName()); contentId = contentIterator.nextId(); } } else { out.println("Could not find any content modified between " + startDate + "and" + endDate ); } %>

    场景 4:根据 Authoring Template、Site Area、Category 和 Keywords 条件搜索内容:

    要根据由 Authoring Template、Site Area、Category 或 Keywords 组成的条件组合搜索内容,可以选择并使用清单 4 中的相应 API。您可以通过 ContentSearch 方法实现此搜索,该方法将返回匹配给定搜索条件的对象的 ID 迭代器。

    ContentSearch 方法的行为类似于 Menu Component。如果已经指定了 Site Areas,那么所有父类和子类 Site Areas 也将包括在搜索中。

    这将执行 “or” 搜索,而不是 “and” 搜索。这意味着对两个不同 Categories 和一个 Content Template 的搜索将返回至少使用两种 profile 类型(一个 Category 和一个 Template)的其中之一描述的内容,而不是匹配所有参数的内容。只匹配一种条件类型(只匹配 Template )的内容不会被返回。

    注意,返回的结果的顺序是不确定的,并且所有的参数都是可选的,最后一个参数 matchAllKeys 除外。


    清单 4. 根据条件搜索代码的示例代码
    				
     
    
    ");
    }else {
    out.println("Could not find Authoring Template: " + authoringTemplateName + "
    "); } // 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 + "
    "); }else { out.println("Could not find Sitearea : " + siteArea + "
    "); } // 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 + "
    "); }else { out.println("Could not find Category : " + categoryName + "
    "); } // 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() + "
    "); contentId = contentIterator.nextId(); } } else { out.println("Could not find any Content using the contentSearch Method"); } %>

    场景 5:创建一个新站点

    您可以使用 Web Content Management API 方法创建一个新站点,创建好站点之后,还可以在 Site Object 下使用所有方法。您可以选择和使用清单 5 中的相应代码,它们只演示了新站点的创建、设置站点名、标题和描述,并将其保存到存储库中。

    我们使用 workspace.createSite 实现这点,它将返回新的 Site 对象。


    清单 5. 创建新站点的示例代码
    				
    
    
    ");
    }else{
        out.println("New Site created successfully.");
    }
    %>
    

    场景 6:创建新的 Site Area

    还可以使用 Web Content Management API 方法创建新的站点区域,之后可以在 Site Area Object 下使用所有这些方法。

    清单 6 仅展示了新站点区域的创建,设置名称、标题、描述并将其保存到存储库中。我们在这里使用 workspace.createSiteArea,它将在给定父项下创建一个新站点区域。该父项可能是站点或站点区域,并且方法将返回新的 Site Area 对象。


    清单 6. 创建新站点区域的示例代码
    				
    				
    
    
     Created new Sitearea under " + parentSiteName );
    }
    } else {
    out.println (" 
    Could not find parent Site " + parentSiteName + ". Could not create a new Sitearea." ); } %>

    场景 7:创建新内容

    通过使用 Web Content Management API,您可以在特定的位置在特定的站点区域下创建新的内容。您可以选择和使用清单 7 中的相应代码,这些代码演示了使用特定的 Authoring Template 在最后一个位置以及特定站点区域下创建内容。

    代码还对工作流进行了配置,并保存了内容。我们使用 workspace.createContent 实现此目的,它将根据指定的 Authoring Template 返回新内容对象。API 代码可以根据需求更改。


    清单 7. 创建新内容的示例代码
    				
    
    
    ");
    }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); String[] saveMessage = workspace. save((Document)newContent); if (saveMessage.length==0) { out.println ("
    Created new Content under " + parentSiteAreaName ); }else { out.println ("Could not create new content"); } %>

    场景 8:创建一个链接到现有内容的新内容链接

    您可以在指定站点空间下创建一个链接到现有内容的新内容链接。在创建内容链接之前,必须对内容进行保存,而内容链接会被立即创建,因此不应该保存。

    我们使用 workspace.createContentLink 实现此目的,它将返回一个新的 Content Link 对象。清单 8 中的代码假设 Content Link 没有在站点区域下给出。


    清单 8. 创建新内容链接的示例代码
    				
    
    
    
    

    场景 9:创建各种库组件

    您可以创建库组件,比如 DateComponent、DocumentManagerComponent、FileComponent、HTMLComponent、ImageComponent、JSPComponent、LinkComponent、RichTextComponent、ShortTextComponent、TextComponent 和 UserSelectionComponent。

    清单 9 中的代码演示了内容创建、设置名称、标题和描述,以及将内容保存到 Web Content Management 存储库中。可以直接使用这些代码创建 文件组件、图像组件和富文本组件:

    • workspace.createFi leComponent
    • workspace.createImageComponent
    • workspace.createRichTextComponent

    创建好组件后可以使用其他 API。


    清单 9. 创建各种库组件的示例代码
    				
    
    
     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 ("
    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"); saveMes sage = workspace.save((Document)libraryRichTextComponent); out.println ("
    New Rich text component created :" + libraryRichTextComponent.getName()); %>

    场景 10:创建新分类和新类别

    您可以在某个特定类别或分类下创建新的分类以及新的类别。清单 10 的代码演示了在特定的父分类下创建新的分类和新的类别,并且您可以直接在自己的代码中使用这些代码。我们使用:

    • workspace.createTaxonomy,返回新的分类对象
    • workspace.createCategory,返回新的类别对象

    清单 10. 创建新分类和新类别的示例代码
    				
    
    
     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 (" 
    Created new Category : " + newCategory.getName()); } %>

    场景 11:在已发布的内容项上创建和取消草稿

    您可以使用一个 Web Content Management API 在已发布的内容项上创建和取消草稿。清单11 中的代码可以用来直接创建现有内容的草稿,也可以用来为已发布的内容取消现有草稿。


    清单 11. 创建和取消草稿的示例代码
    				
    
    
    
    

    场景 12:将内容移动到工作流的下一阶段,重启工作流,对特定内容设置另一个工作流

    您可以将内容移动到工作流的下一阶段,重启工作流,对特定内容设置另一个工作流。您可以使用清单 12 中的代码将内容从草稿阶段进入发布阶段,重新启动工作流,然后为特定的内容项设置另一个工作流。


    清单 12. 移动内容的示例代码
    				
    
    
     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("
    New Workflow set on content : " + contentName); } else { out.println("
    Could not set new Workflow on content : " + contentName); } %>

    场景 13:呈现 Sites、Site Areas、Contents、Content 组件或 Library 组件

    您可以呈现 Sites、Site Areas、Contents、Content 组件或 Library 组。给定的 Web Content Management 内容组件将使用指定的呈现上下文呈现。

    注意,在 RenderingContext 中指定将要呈现 ContentComponent 是不够的,还需要指定包含 ContentComponent 的项的路径。

    清单 13 中的代码演示了如何呈现 Content、Content 组件和 Library 组件。您可以始终添加或删除更多的组件,这将根据您的需求而定。


    清单 13. 呈现各种工件的示例代码
    				
    
    
    " + 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("
    Rendering content component : " + component.getName()); out.println ("
    " + 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("
    Rendering library component " + libraryComponentName ); out.println ("
    " + workspace.render(renderingContext,libraryComponent)); } else { out.println("
    Could not render Library component" + libraryComponentName); } %>

    场景 14:在库的内部或库之间移动或复制整个站点区域

    要将整个站点区域及其所有子内容移动或复制到同一库中的父站点或另一个库,可以使用清单 14 的代码。要移动站点区域,我们将使用 workspace.moveSiteFrameworkDocument 方法。

    要复制站点区域,可以使用下面的相同代码,用 workspace.copySiteFrameworkDocument 方法来替换 workspace.moveSiteFrameworkDocument 方法。


    清单 14. 移动或复制站点区域的示例代码
    				
    
    
    ");
    }else {
    out.println("Could not find Sitearea : " + siteArea + "
    "); } // 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 + "
    "); }else { out.println("Could not find new parent site : " + newParentSite + "
    "); } if ((newParentsiteId!=null) && (siteAreaId!=null)) { out.println(" Moving Sitearea " + siteArea + " to new parent site " + newParentSite + "
    "); // move sitearea to new parent site workspace.moveSiteFrameworkDocument(siteAreaId, newParentsiteId, siblingId, ChildPosition.START); out.println(" Moved Sitearea " + siteArea + " to new parent site " + newParentSite + "
    "); }else{ out.println(" Moving Sitearea " + siteArea + " to new parent site " + newParentSite + " failed
    "); } %>

    场景 15:在库的内部或库之间移动或复制内容

    使用清单 15 的代码,您可以将内容移动或复制到另一个站点区域,该站点区域可以位于同一个库,或位于不同的库。要移动内容,我们将使用 workspace.moveSiteFrameworkDocument 方法。

    要复制站点区域,可以使用下面的相同代码,用 workspace.copySiteFrameworkDocument 方法替代 workspace.moveSiteFrameworkDocument 方法。


    清单 15. 移动或复制内容的示例代码
    				
    
    
    ");
    }else {
    out.println("Could not find Content : " + contentName + "
    "); } // 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 + "
    "); }else { out.println("Could not find new parent Sitearea : " + newParentSitearea + "
    "); } if ((contentId!=null) && (newParentsiteareaId!=null)) { out.println(" Moving content " + contentName + " to new parent Sitearea " + newParentSitearea + "
    "); // move content to new sitearea // workspace.moveSiteFrameworkDocument(contentId, // newParentsiteareaId,siblingId,ChildP osition.START); out.println(" Moved content " + contentName + " to new parent Sitearea " + newParentSitearea + "
    "); }else{ out.println(" Moving content " + contentName + " to new parent Sitearea " + newParentSitearea + " failed.
    "); } %>

    场景 16:将非分层的项或根内容移动或复制到另一个库

    要将非分层的项(Authoring Templates、Presentation Templates、Library Components、Workflows、Workflow Stages)或根项(Sites 和 Taxonomies)移动或复制到另一个库,可以使用清单 16 中的代码。要将项移动到另一个库,我们将使用 workspace.moveToLibrary 方法,它将非分层项或根项移动到另一个库。

    要复制内容项,我们将使用下面的相同代码,用 workspace.copyToLibrary 方法替换 workspace.moveToLibrary 方法。

    清单 16 中的代码展示了如何针对 Authoring Templates、Presentation Templates、Library 组件、Workflows、Workflow Stages、Sites 和 Taxonomies 实现这个操作。


    清单 16. 将根项移动或复制到另一个库
    				
     
    
    "); 
    out.println(" Moving Authoring Template  " + authoringTemplateName 
        + " to new library  Target Library 
    "); // move authoring template to another library workspace.moveToLibrary(targetDocumentLibrary ,authoringTemplateId ); out.println(" Moved Authoring Template " + authoringTemplateName + " to another library Target Library
    "); }else { out.println("Could not find Authoring Template: " + authoringTemplateName + "
    "); } // ********************** 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 + "
    "); out.println(" Moving Presentation Template " + presentationTemplateName + " to new library Target Library
    "); // move presentation template to another library // workspace.moveToLibrary(targetDocumentLibrary ,presentationTemplateId ); // Moving presentation tempate to another library out.println(" Moved Presentation Template " + presentationTemplateName + " to another library Target Library
    "); }else { out.println("Could not find Presentation Template: " + presentationTemplateName + "
    "); } // ********************** 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 + "
    "); out.println(" Moving Library Component " + libraryComponentName + " to new library Target Library
    "); // move library component to new library // workspace.moveToLibrary(targetDocumentLibrary ,libraryComponentId ); out.println(" Moved Library Component " + libraryComponentName + " to another library Target Library
    "); }else { out.println("Could not find Library Component: " + libraryComponentName + "
    "); } // ************************* 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 + "
    "); out.println(" Moving Workflow " + workflowName + " to new library Target Library
    "); // move workflow to another library workspace.moveToLibrary(targetDocumentLibrary ,workflowId ); out.println(" Moved workflow " + workflowName + " to another library Target Library
    "); }else { out.println("Could not find Workflow: " + workflowName + "
    "); // ************************ 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 + "
    "); out.println(" Moving Site " + siteName + " to new library Target Library
    "); siteId = siteIterator.nextId(); // move site to another library workspace.moveToLibrary(targetDocumentLibrary ,siteId ); out.println(" Moved Site " + siteName + " to another library Target Library
    "); }else { out.println("Could not find Site : " + siteName + "
    "); } // ********************** 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 + "
    "); out.println(" Moving Workflow Stage " + workflowStageName + " to new library Target Library
    "); // move workflowstage to another library // workspace.moveToLibrary(targetDocumentLibrary ,workflowStageId ); out.println(" Moved Workflow Stage" + workflowStageName + " to another library Target Library
    "); }else { out.println("Could not find Workflow Stage: " + workflowStageName + "
    "); } // ************************ 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 + "
    "); out.println(" Moving Taxonomy " + taxonomyName + " to new library Target Library
    "); // move taxonomy to another library workspace.moveToLibrary(targetDocumentLibrary ,taxonomyId ); out.println(" Moved Taxonomy " + taxonomyName + " to another library Target Library
    "); }else { out.println("Could not find Taxonomy: " + taxonomyName + "
    "); } %>

    场景 17:在库的内部或库之间移动或复制类别

    要在同一个库的内部或库与库之间移动或复制类别,可以使用清单 17 中的代码。要在同一个库中移动项或将项移动到另一个库,我们将使用 workspace.moveCategory 方法。该方法实际上移动一个类别并将其添加到一个新的父类别下。

    要复制类别,我们将使用下面的相同代码,用 workspace.copyCategory 方法替换 workspace.moveToLibrary 方法。

    注意,下面的示例将一个类别移动到另一个分类中;您也可以将类别移动到另一个父类别中。


    清单 17. 移动或复制类别的示例代码
    				
    
    
    ");
    }else {
    out.println("Could not find Category : " + categoryName + "
    "); } // find taxonomy by name DocumentIdIterator taxonomyIterator = workspace.findByName(DocumentTypes.Taxonomy,newTaxonomy); if (taxonomyIterator.hasNext()) { taxonomyId = taxonomyIterator.nextId(); out.println("Taxonomy found : " + newTaxonomy + "
    "); }else { out.println("Could not find Taxonomy: " + newTaxonomy + "
    "); } if ((categoryId!=null) && (taxonomyId!=null)) { out.println(" Moving Category " + categoryName + " to new parent taxonomy " + newTaxonomy + "
    "); // move category workspace.moveCategory(categoryId,taxonomyId); out.println(" Moved Category " + categoryName + " to new parent taxonomy " + newTaxonomy + "
    "); }else{ out.println(" Moving Category " + categoryName + " to new parent taxonomy " + newTaxonomy + " failed
    "); } %>

    场景 18:删除和清除对象

    我们可以使用 Web Content Management API 删除和清除所有对象。在删除实际对象之前,最好清除被删除对象的所有引用;否则,删除将失败。

    我们还可以清除(永久删除)来自库的对象。清单 18 的代码演示了如何删除和清除内容项,以及一个库组件。

    要删除站点 / 站点区域 / 分类,您必须在删除它们之前清除所有子内容。


    清单 18. 删除和清除对象的示例代码
    				
    
    
     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("
    Library Component purged:" + deleteMessage); } else { out.println("Could not delete Library Component :" + libraryComponentName); } %>

    4 在所有示例中指定的名称的标准

    如本红皮书开头所述,我们为大部分代码行添加了以下划线表示的注释,以方便理解代码和流程。

    在为提供的 API 样例编写代码时,设定了一些需要遵循的标准。根据您的系统上的 Web Content Management 工件的具体需求,可能需要修改这些标准并输入适当的值:

    • WebContent。被作为将在所有场景中设置的标准库
    • Site。将被搜索的已定义的站点名,或已经为站点设置好的名称
    • NewSite。创建的新站点的名称
    • SiteArea。已定义的站点区域
    • NewSiteArea。创建的新站点区域的名称
    • Content。已定义的内容名称
    • NewContent。创建的新内容的名称
    • PublishedContent。已发布的内容的名称
    • AuthoringTemplate。 已定义的 Authoring Template
    • PresentationTemplate。已定义的 Presentation
  • LibraryComponent。已定义的库组件名
MyComponent。已定义的组件名 NewImageComponent。创建的新图像组件的名称 NewRichTextComponent。创建的新富文本组件的名称 Workflow。已定义的工作流名 WorkflowStageName。已定义的工作流阶段名 NewFileComponent。新创建的文件资源的名称 Taxonomy。已定义的分类名 NewParentTaxonomy。新创建的分类的名称 Category。将被搜索的已定义的类别名 NewCategory。新创建的类别的名称 Keyword。已定义的关键字 "/Web content/Site/SiteArea/Content"。内容路径 Source Library。已定义的源库的名称 Target Library。已定义的目标库的名称 01/01/2008。选择作为起始日期的样例日期 12/31/2009。选择作为终止日期的样例日期

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14751907/viewspace-625560/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14751907/viewspace-625560/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值