Lotus WCM6.1 API 實現常見擴充套件場景

genusBIT發表於2010-01-20

轉自: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 工件的具體需求,可能需要修改這些標準並輸入適當的值:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14751907/viewspace-625560/,如需轉載,請註明出處,否則將追究法律責任。

相關文章