通过单击操作创建 IBM Lotus Web Content Management 站点结构

转自:http://www.ibm.com/developerworks/cn/lotus/wcm-structure/index.html

简介

如果您使用 IBM Lotus Web Content Management 的话,一个经常出现的任务就是定义站点结构。这个任务本身是重复的并且十分耗时;您必须采取 10 多个不同的步骤来创建一个站点或站点区域。而且,站点结构被有意设计为不可移植;您无法将站点结构从一个服务器上的某个库导出到另一个服务器上的不同库。 相反,您需要手动重新创建整个站点结构。

由于构成单一站点结构的站点和站点区域数可能会达到数千个,因此您需要一种方法来自动化完成站点结构的定义并使它易于迁移、导出和导入。在本文中,您将学习一种简单的方法来将站点结构定义为一个 XML 文件并进行相应的部署。

先决条件

您应当熟悉以下这些技术:

  • IBM WebSphere® Portal V6.0 和更高版本,以及它的管理和组件
  • IBM Lotus Web Content Management
  • XML

为 了演示如何在 Lotus Web Content Management 中创建站点结构且无需手动创建所有站点和站点区域,本文使用 Lotus Web Content Management API 和一个包含站点结构的 XML 文件。您可以通过针对本文下载部分提供的 XML 模式对包含站点结构的 XML 文件进行验证来创建它。XML 模式将验证结构和必要字段来预防运行时错误。

您可以定义许多站点、站点区域和默认内容来在包含完整站点结构的 XML 文档中创建站点结构。首先,创建下面这些必要部件:

  • Lotus Web Content Management 库
  • 将在站点结构中使用的编写模板
  • 将在站点结构中使用的演示模板
  • 将在安全设置部分中使用的用户
  • 将在编写模板中使用的工作流、工作流操作和工作流阶段

然 后,以 XML 文件的形式定义站点结构,指定库、站点、站点区域等等。为了确保提供的信息足够用于在 Lotus Web Content Management 中创建站点,XML 文件根据本文下载部分提供的 XSD 文件中定义的模式进行构建。XSD 文件包含创建完整站点结构所需的所有验证,以及包含某些字段的默认内容。

您可以创建一个 JSP 组件来解析 XML 文件并创建其中定义的站点区域。

实现

在这个小节中,我们将描述实现解决方案所需的步骤。我们首先将讨论如何创建 XML 文件和 XSD 文件,随后讨论用于解析文件的 JSP 组件。

XML 创建

XML 文件包含创建站点结构的所有信息。它包含库名、站点结构、站点区域结构和默认内容。需要在与您的需求相对应的结构中创建一个 XML 文件。参见清单 1 中的 XML 示例,它与图 1 所示的站点结构相对应。


图 1. 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构

站点结构由一个站点区域组成;这个站点区域反过来又由另一个站点区域组成。


清单 1. 创建站点结构所使用的 XML 示例
xsi:noNamespaceSchemaLocation="SiteCreation.xsd">




auth_news
pt_newsDetail



[all authenticated portal users]
anonymous portal user




Default_News
Default_News
auth_news

txt_headline
Headline for default Content


rtf_body
Body for the default Content




Default_News
Default_News
auth_news

txt_headline
Headline for default Content


rtf_body
Body for the default Content





Default_News




auth_news
pt_newsDetail



[all authenticated portal users]
anonymous portal user




通过单击操作创建 IBM Lotus Web Content Management 站点结构
文档选项
通过单击操作创建 IBM Lotus Web Content Management 站点结构 通过单击操作创建 IBM Lotus Web Content Management 站点结构

未显示需要 JavaScript. 的文档选项

通过单击操作创建 IBM Lotus Web Content Management 站点结构 通过单击操作创建 IBM Lotus Web Content Management 站点结构

打印本页

通过单击操作创建 IBM Lotus Web Content Management 站点结构 通过单击操作创建 IBM Lotus Web Content Management 站点结构

打印本页

通过单击操作创建 IBM Lotus Web Content Management 站点结构 通过单击操作创建 IBM Lotus Web Content Management 站点结构

将此页作为电子邮件发送

通过单击操作创建 IBM Lotus Web Content Management 站点结构 通过单击操作创建 IBM Lotus Web Content Management 站点结构

将此页作为电子邮件发送

通过单击操作创建 IBM Lotus Web Content Management 站点结构 通过单击操作创建 IBM Lotus Web Content Management 站点结构

讨论

通过单击操作创建 IBM Lotus Web Content Management 站点结构 通过单击操作创建 IBM Lotus Web Content Management 站点结构

样例代码

通过单击操作创建 IBM Lotus Web Content Management 站点结构 通过单击操作创建 IBM Lotus Web Content Management 站点结构

英文原文


级别: 中级

Hebba Soliman, 高级软件工程师, IBM
Ahmed Khairy, IT 专家, IBM

2009 年 12 月 29 日

了解一种使您能够以自动化方式在 IBM® Lotus® Web Content Management 中定义站点结构的简单解决方案。这个解决方案使您能够节省花在既耗时又沉闷的任务上面的时间。

编辑注:您很精通这个主题吗?希望分享您的经验吗?请马上加入到 IBM Lotus 软件 wiki 项目。

Lotus Web Content Management wiki

简介

如 果您使用 IBM Lotus Web Content Management 的话,一个经常出现的任务就是定义站点结构。这个任务本身是重复的并且十分耗时;您必须采取 10 多个不同的步骤来创建一个站点或站点区域。而且,站点结构被有意设计为不可移植;您无法将站点结构从一个服务器上的某个库导出到另一个服务器上的不同库。 相反,您需要手动重新创建整个站点结构。

由于构成单一站点结构的站点和站点区域数可能会达到数千个,因此您需要一种方法来自动化完成站点结构的定义并使它易于迁移、导出和导入。在本文中,您将学习一种简单的方法来将站点结构定义为一个 XML 文件并进行相应的部署。


通过单击操作创建 IBM Lotus Web Content Management 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构
回页首


先决条件

您应当熟悉以下这些技术:

  • IBM WebSphere® Portal V6.0 和更高版本,以及它的管理和组件
  • IBM Lotus Web Content Management
  • XML

为 了演示如何在 Lotus Web Content Management 中创建站点结构且无需手动创建所有站点和站点区域,本文使用 Lotus Web Content Management API 和一个包含站点结构的 XML 文件。您可以通过针对本文下载部分提供的 XML 模式对包含站点结构的 XML 文件进行验证来创建它。XML 模式将验证结构和必要字段来预防运行时错误。

您可以定义许多站点、站点区域和默认内容来在包含完整站点结构的 XML 文档中创建站点结构。首先,创建下面这些必要部件:

  • Lotus Web Content Management 库
  • 将在站点结构中使用的编写模板
  • 将在站点结构中使用的演示模板
  • 将在安全设置部分中使用的用户
  • 将在编写模板中使用的工作流、工作流操作和工作流阶段

然 后,以 XML 文件的形式定义站点结构,指定库、站点、站点区域等等。为了确保提供的信息足够用于在 Lotus Web Content Management 中创建站点,XML 文件根据本文下载部分提供的 XSD 文件中定义的模式进行构建。XSD 文件包含创建完整站点结构所需的所有验证,以及包含某些字段的默认内容。

您可以创建一个 JSP 组件来解析 XML 文件并创建其中定义的站点区域。


通过单击操作创建 IBM Lotus Web Content Management 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构
回页首


实现

在这个小节中,我们将描述实现解决方案所需的步骤。我们首先将讨论如何创建 XML 文件和 XSD 文件,随后讨论用于解析文件的 JSP 组件。

XML 创建

XML 文件包含创建站点结构的所有信息。它包含库名、站点结构、站点区域结构和默认内容。需要在与您的需求相对应的结构中创建一个 XML 文件。参见清单 1 中的 XML 示例,它与图 1 所示的站点结构相对应。


图 1. 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构

站点结构由一个站点区域组成;这个站点区域反过来又由另一个站点区域组成。


清单 1. 创建站点结构所使用的 XML 示例
xsi:noNamespaceSchemaLocation="SiteCreation.xsd">




auth_news
pt_newsDetail



[all authenticated portal users]
anonymous portal user




Default_News
Default_News
auth_news

txt_headline
Headline for default Content


rtf_body
Body for the default Content




Default_News
Default_News
auth_news

txt_headline
Headline for default Content


rtf_body
Body for the default Content





Default_News




auth_news
pt_newsDetail



[all authenticated portal users]
anonymous portal user






图 2 展示了 XML 元素和实现解决方案后生成的输出之间的样例映射。


图 2. XML 与 Lotus Web Content Management 对象和区段之间的映射
通过单击操作创建 IBM Lotus Web Content Management 站点结构

如您所见,实际站点结构与 XML 文件之间的映射非常简单,具有一个表示库的根元素。这个根元素随后包含另一个与站点对应的元素,定义它的名称并显示名称。站点区域以嵌套方式在 XML 文件中定义,用于获得必需的站点结构。

每个站点和站点区域都包含一个设置区段,其中包含安全设置和模板映射区段。下面是必须加以考虑的限制因素:安全设置和模板映射可以在各自的元素或树中的最高级别的站点或站点区域中针对每个站点或站点区域创建,并且其子元素将从父元素中继承。

内容安全设置和工作流被添加到编写模板组件中,而不是 XML 文件中的默认内容区段中。

模板映射和默认内容创建中使用的编写模板和演示模板必须提前创建。

通过定义默认内容的名称,默认内容可以在多个站点区域中重用。当您在 XML 文件中定义了完整的默认内容元素后,您可以通过定义一个只包含名称元素的默认内容标记来使用它。如果默认内容已经存在于库中,也可以使用在多个不同位置重用默认内容这一特性。


通过单击操作创建 IBM Lotus Web Content Management 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构
通过单击操作创建 IBM Lotus Web Content Management 站点结构
回页首


XML 模式验证

如前所述,为了避免丢失 Lotus Web Content Management 用于站点创建的数据,您需要使 XML 文件遵从一个 XSD 文件。这种遵从性可以保证表 1 中列出的验证是有效的。


表 1. XML 模式元素/属性验证
元素 元素/属性 类型 有效范围
Library Sites 元素 0..n NA
Library Name 属性 必要 字符串
Site SiteArea 元素 0..n NA
Site Name 属性 必要 字符串
Site DisplayName 属性 可选 字符串
Site Settings 元素 可选 NA
SiteArea SiteArea 元素 0..n NA
SiteArea Name 属性 必要 字符串
SiteArea DisplayName 属性 可选 字符串
SiteArea Settings 元素 可选 NA
SiteArea DefaultContent 元素 可选 NA
DefaultContent Name 元素 必要 NA
DefaultContent DisplayName 元素 可选 NA
DefaultContent AuthoringTemp 元素 可选(如果已定义了项的话) NA
DefaultContent TextComponent 元素 0..n NA
DefaultContent RichTextComponent 元素 可选 NA
TextComponent FieldName 元素 必要(像在编写模板中一样引用 Lotus Web Content Management TextComponent 字段名) NA
TextComponent TextComponent 元素 必要 NA
TextComponent FieldName 元素 必要(像在编写模板中一样引用 Lotus Web Content Management RichTextComponent 字段名) NA
TextComponent TextComponent 元素 必要 NA
Settings TemplateMapping 元素 0..n NA
Settings SecuritySettings 元素 可选 NA
TemplateMapping AuthoringTemp 元素 必要(引用 Lotus Web Content Management 编写模板名) NA
TemplateMapping PresentationTemp 元素 必要(引用 Lotus Web Content Management 演示模板) NA
SecuritySettings AccessRight 元素 0..n NA
AccessRight Type 属性 必要 addReadAccessMembers/
addEditAccessMembers
or
addDeleteAccessMembers
AccessRight Access 元素 1..n NA

JSP 组件

您 已经在一个遵循 XSD 的 XML 文件中定义了您的站点结构,现在需要创建一个 JSP 组件来处理对 XML 文件的解析,并使用 Lotus Web Content Management API 将站点结构导入到 Lotus Web Content Management 库来执行实际的 Lotus Web Content Management 站点创建工作。

选择 New - JSP 组件。使用名称 jsp_createSiteStructure 创建 JSP 组件,然后将其指向路径为 /jsp/html/createSiteStructure.jsp 的 JSP 文件。将您的 JSP 组件存储在本地 Rendering portlet 中。这个 JSP 文件的物理路径位于一个目录中,类似清单 2 中的代码。


清单 2. 物理路径目录
\installedApps\
WCM_Local_ng_Portlet_PA_dvme0nk.ear\PA_dvme0nk.war\jsp\html\
createSiteStructure.jsp

JSP 文件创建了一个称为 Create Site Structure 的按钮,单击此按钮时将开始解析 XML 文件,首先将解析每一个站点。XML 文件的路径现在可以进行编辑。清单 3 中的文件被认为位于与 JSP 文件相同的位置。代码将进入工作空间并连接到文件中的定义的库。参见清单 3 中的代码。


清单 3. 创建新的评论内容的 JSP 文件
charset=UTF-8" pageEncoding="UTF-8"%>
.
.
.








try {

//if button clicked
if( request.getParameter("buttonName") !=
null && request.getParameter("buttonName").equals("CreateSiteStructure")) {

//path to the XML file containing the site structure
File file = newFile("/opt/app/WebSphere/PortalServer/installedApps/WCM_Local_ng_
Portlet_PA_dm010y1.ear/dm010y1.war/jsp/html/NewsSiteStructure.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);

doc.getDocumentElement().normalize();

Element docEle = doc.getDocumentElement();
String libraryName = docEle.getAttribute("Name");

System.out.println("libraryName = " +libraryName);
workspace = openWorkspaceLibrary(request, libraryName);

//retrieve all site elements
NodeList nl = docEle.getElementsByTagName("Site");

//loop over site elements
if(nl != null) {
for(int i = 0 ; i < nl.getLength();i++) {

Element el = (Element)nl.item(i);
Site site = createSite(el);

DocumentId siteId = site.getId();
System.out.println("site = "+ site.getName());

//for every site element, retrieve site area child elements
NodeList SiteAreaNodeList = el.getChildNodes();
for (Node node = el.getFirstChild(); node != null;
node = node.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE &&
node.getNodeName().equals("SiteArea")) {
Element siteAreaEL = (Element)node;

SiteArea sa = createSiteArea(siteAreaEL, siteId);
if (sa != null){
System.out.println("site area = " + sa.getName());
}
else{
out.println("Error in creation of site area tree,
check logs for more information about the error");
}
}
}
}
}
closeWorkspace();
out.println("Cretion Completed");
}
}catch(FileNotFoundException e){
out.println("XML File not found!");
}catch (org.xml.sax.SAXParseException saxEx){
out.println("Exception In parsing document: "+ saxEx.getMessage());
}
catch (Exception e) {
System.out.println("Exception: "+ e.getClass());
e.printStackTrace();
}
%>

这里使用了一个实用类,它包含了 JSP 代码中用来创建站点结构的所有 helper 方法。基于本文的目的,可以创建一个名为 wcmUtil.jsp 的 JSP 组件来包含所有这些方法。这个 JSP 组件被包含在 createSiteStructure.jsp 中,如清单 3 所示,使用其中的 。

清单 4 - 20 展示了在 helper 类中用于创建站点结构的方法。


清单 4. 连接到工作空间和库
private Workspace workspace;
/*
*Connect to Lotus Web Content Management workspace with current user if
*logged in or anonymous if user is not logged in
*
*Set the Lotus Web Content Management document library to aLibraryName
*/
public Workspace openWorkspaceLibrary(HttpServletRequest request, String aLibraryName)
throws ServiceNotAvailableException, OperationFailedException {
// Connect to WCM Workspace
if (request.getUserPrincipal() != null)
{
workspace = WCM_API.getRepository().getWorkspace
(request.getUserPrincipal());
} else
{
workspace = WCM_API.getRepository().getAnonymousWorkspace();
}

//set library to the passed library name
if (workspace != null) {
DocumentLibrary docLib = workspace.getDocumentLibrary
(aLibraryName);
if(docLib != null){
workspace.setCurrentDocumentLibrary(docLib);
}
}

return workspace;
}


清单 5. 解析和创建站点
/*
*Parse site element
*If site already available, retrieve the site object, edit the site
*If site not already created, create a new site and set the name and
*display name and its settings
*Save the site to the workspace
*/
public Site createSite(Element aSiteElement){

String siteName = aSiteElement.getAttribute("Name");
String siteDisplayName = aSiteElement.getAttribute("DisplayName");
Site site = null;
try{

DocumentId siteId = getWCMDocumentByName(siteName, DocumentTypes.Site);
if (siteId != null){
site = (Site)workspace.getById(siteId);
}
else{
site = workspace.createSite();
site.setName(siteName);
}
site.setTitle(siteDisplayName);
site = (Site)retrieveAddSettings(site, aSiteElement, false);

workspace.save(site);

}catch(Exception e){
System.out.println("Exception = "+e);
e.printStackTrace();

}
return site;
}


清单 6. 解析和创建站点区域
/*
*Parse site area element
*If site area already available, retrieve the site area object, edit the site area
*If site area not already created, create a new site area and set the name
*and display name and its settings
*Add a default content to the site area if available
*Save the site area to the workspace
*/
public SiteArea createSiteArea(Element aSiteAreaElement, DocumentId parentId){
String siteAreaName = aSiteAreaElement.getAttribute("Name");
String siteAreaDisplayName = aSiteAreaElement.getAttribute("DisplayName");

SiteArea sa = null;
try{
String parentPath = workspace.getPathById(parentId, true, true);
String siteAreaPath = parentPath+ "/"+ siteAreaName;
DocumentId siteAreaId = getWCMDocumentByPath(siteAreaPath,
Workspace.WORKFLOWSTATUS_ALL);

if (siteAreaId != null){
sa = (SiteArea)workspace.getById(siteAreaId);
}else{
sa = workspace.createSiteArea(parentId, null, 1);
}

sa = (SiteArea)retrieveAddSettings(sa, aSiteAreaElement, true);
if (sa != null){
sa.setTitle(siteAreaDisplayName);

sa.setName(siteAreaName);
workspace.save(sa);
sa = addDefaultContentToSiteArea(sa, aSiteAreaElement);

if (sa != null){

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

if (!saved){
return null;
}

//checking if site area contains any other site area children
NodeList SiteAreaNodeList = aSiteAreaElement.getChildNodes();

for (Node node = aSiteAreaElement.getFirstChild(); node != null;
node = node.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE &&
node.getNodeName().equals("SiteArea")) {
Element siteAreaEL = (Element)node;

SiteArea childSA = createSiteArea(siteAreaEL, sa.getId());
if (childSA != null){
System.out.println("childSA for = " +sa.getName()+ " =
" + childSA.getName());
}
else{
return null;
}
}
}
}
else{
return null;
}
}
return sa;
}catch(Exception e){
System.out.println("Exception = "+e);
e.printStackTrace();

}
return null;
}


清单 7. 解析和创建默认内容
/*
*Parse the default content element
*If content already available, retrieve the content object, and edit it
*If content not already created, create a new content object and set the
*name and display name and its settings
*If authoring template provided, overwrite if already set
*Add text components and rich text components to the content
*Save the default content to the site area
*/
public SiteArea addDefaultContentToSiteArea(SiteArea aSiteArea,
Element aSiteAreaElement){

DocumentId defaultContentId = null;

Element defaultel = getChildByName(aSiteAreaElement, "DefaultContent");
String contentName = getElementValueByTagName("Name", defaultel);

defaultContentId = getWCMDocumentByName(contentName, DocumentTypes.Content);

Content content = null;

String contentDisplayName = getElementValueByTagName("DisplayName", defaultel);

String authoringTempName = getElementValueByTagName("AuthoringTemp", defaultel);

DocumentId defaultAuthId = null;

if (! authoringTempName.equals("")){
defaultAuthId = getWCMDocumentByName(authoringTempName,
DocumentTypes.AuthoringTemplate);

}
System.out.println("defaultAuthId = "+ defaultAuthId);
try{

//if content already existing in the library, re-use it
if (defaultContentId != null){
content = (Content)workspace.getById(defaultContentId);

//if authoring template provided in the file, overwrite the existing one
if (defaultAuthId != null){
content.setAuthoringTemplateID(defaultAuthId);
}
}
else if (defaultAuthId != null){
//if content not available, create a new one
content = workspace.createContent(defaultAuthId, aSiteArea.getId(),null,
ChildPosition.START);
defaultContentId = content.getId();
content.setName(contentName);
}
else{
System.out.println("Exception = Authoring Template for default content "+
contentName + " is missing");
return null;
}

if (! contentDisplayName.equals("")){
content.setTitle(contentDisplayName);
}

content = retrieveAddTextComponentToContent(content, defaultel);

if (content == null){
return null;
}
content = retrieveAddRichTextComponentToContent(content, defaultel);

if (content == null){
return null;
}

while(!content.isPublished()){
content.nextWorkflowStage();
}
workspace.save(content);

}catch(Exception e){
System.out.println("Exception = "+e);
e.printStackTrace();
}

System.out.println("defaultContent for = " +aSiteArea.getName()+ " = " +
defaultContentId.getName());
aSiteArea.setDefaultContent(defaultContentId);
return aSiteArea;
}


清单 8. 解析文本组件
/*
*Parse DefaultContent element
*Search for text component elements
*Looping over all text component child elements
*Calling method addTextComponent to add the
*text component to the content
*/
public Content retriveAddTextComponentToContent(Content aContent,
Element aContentElement){

NodeList texttnl = aContentElement.getChildNodes();

for (Node node = aContentElement.getFirstChild();
node != null;
node = node.getNextSibling()) {
if (node.getNodeType() ==
Node.ELEMENT_NODE &&
node.getNodeName().equals("TextComponent")) {
Element textCompel = (Element)node;

String textField =
getElementValueByTagName("FieldName", textCompel);
String text =
getElementValueByTagName("Value", textCompel);
aContent =
addTextComponent(aContent, textField, text);

if (aContent != null){
System.out.println("adding textField =
"+ textField+" to content "
+aContent.getName());
}
}
}
if (aContent == null){
return null;
}
else{

return aContent;
}

}


清单 9. 解析富文本组件
/*
*Parse DefaultContent element
*Search for rich text component elements
*Looping over all rich text component child elements
*Calling method addRichTextComponent to add the rich text
*component to the content
*/
public Content retrieveAddRichTextComponentToContent
(Content aContent, Element aContentElement){
NodeList richTexttnl = aContentElement.getChildNodes();
for (Node node = aContentElement.getFirstChild(); node != null;
node = node.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE &&
node.getNodeName().equals("RichTextComponent")) {
Element richtextCompel = (Element)node;

String textField =
getElementValueByTagName("FieldName",
richtextCompel);
String text =
getElementValueByTagName("Value",
richtextCompel);

aContent =
addRichTextComponent(aContent, textField, text);

if (aContent != null){
System.out.println("adding richtextField =
"+ textField+" to content "+
aContent.getName());
}
}
}
if (aContent == null){
return null;
}
else{
return aContent;
}
}


清单 10. 创建和添加文本组件
/*
* Get text component from the content according to
*the text component name
* Set the text component with the value sent
* Add the text component to the content
*/
public Content addTextComponent
(Content aContent, String aTextField, String aValue){

try{
TextComponent txtComp =
(TextComponent) aContent.getComponent(aTextField);
txtComp.setText(aValue);
aContent.setComponent(aTextField, txtComp);
}catch(ComponentNotFoundException ex){
System.out.println
("Exception: "+aTextField+" is not a valid TextComponent");
return null;
}catch(Exception e){
System.out.println("Exception = "+e);
return null;
}

return aContent;
}


清单 11. 创建和添加富文本组件
/*
* Get rich text component from the content according to the RTF name
* Set the rich text component with the value sent
* Add the rich text component to the content
*/
public Content addRichTextComponent(Content aContent,
String aTextField, String aValue){
try{
RichTextComponent richTxtComp =
(RichTextComponent) aContent.getComponent(aTextField);
richTxtComp.setRichText(aValue);
aContent.setComponent(aTextField, richTxtComp);
RichTextComponent rtf =
(RichTextComponent)aContent.getComponent(aTextField);
}catch(ComponentNotFoundException ex){
System.out.println("Exception:
"+aTextField+" is not a valid RichTextComponent");
return null;
}catch(Exception e){
System.out.println("Exception = "+e);
return null;
}

return aContent;
}


清单 12. 解析设置元素
/*
*Parse site area or site element
*Search for settings element
*call method retrieveAddTemplateMapping to add template mapping to site or site area
*call method addSecuritySettings to add security settings to site or site area
*/
public Object retrieveAddSettings(SiteFrameworkContainer aWCMObject,
Element aObjectElement, boolean isSiteArea){

Element settingsel = getChildByName(aObjectElement, "Settings");
if ( settingsel!= null){
if(isSiteArea )
{
aWCMObject = (SiteArea)retrieveAddTemplateMapping
(aWCMObject, settingsel);
aWCMObject = (SiteArea)addSecuritySettings
(aWCMObject, settingsel);
}
else
{
aWCMObject = (Site)retrieveAddTemplateMapping
(aWCMObject, settingsel);
aWCMObject = (Site)addSecuritySettings
(aWCMObject, settingsel);
}
}
return aWCMObject;
}


清单 13. 将 SecuritySettings 添加到 Lotus Web Content Management 对象
/*
*Parse settings element
*Search for SecuritySettings element
*For every SecuritySettings element, find the AccessRight element
*loop over the AccessRight elements and get the type of the access
*According to the access type, a specific method in Lotus
*Web Content Management
*will be called to add the access rights
*/
public Object addSecuritySettings
(SiteFrameworkContainer aWCMObject, Element settingsel){

for (Node node = settingsel.getFirstChild(); node != null;
node = node.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE &&
node.getNodeName().equals("SecuritySettings")) {
Element secel = (Element)node;
NodeList accessRightsnl = secel.getChildNodes();

for (Node accessRightNode = secel.getFirstChild(); accessRightNode !=
null; accessRightNode = accessRightNode.getNextSibling()) {
if (accessRightNode.getNodeType() == Node.ELEMENT_NODE &&
accessRightNode.getNodeName().equals("AccessRight")) {
Element accessRightsel = (Element)accessRightNode;

String accessType = accessRightsel.getAttribute("Type");

NodeList accessnl = accessRightsel.getChildNodes();

if(accessnl != null ) {

for(int y = 0 ; y < accessnl.getLength();y++) {

Node accnode = accessnl.item(y);

if (accnode.getNodeType() ==
accnode.ELEMENT_NODE &&
accnode.getNodeName().equals("Access")) {
Element el = (Element)accnode;
String access = el.getFirstChild().getNodeValue();
if (accessType.equals("addReadAccessMembers")){
aWCMObject.addReadAccessMembers(new String[]{access});
System.out.println("addReadAccessMembers for bject =
"+aWCMObject.getName()+" members = "+access);
}else if(accessType.equals
("addEditAccessMembers")){
aWCMObject.addEditAccessMembers(new String[]{access});
System.out.println("addEditAccessMembers for bject =
"+aWCMObject.getName()+" members =
"+access);

}else if(accessType.equals

("addDeleteAccessMembers")){

aWCMObject.addDeleteAccessMembers
(new String[]{access});
System.out.println
("addDeleteAccessMembers for bject =
"+aWCMObject.getName()+" members = "+access);
}

}
}
}
}
}
}
}
return aWCMObject;
}


清单 14. 解析和添加 Template Mapping SiteArea
/*
*Parse settings element
*Search for TemplateMapping element
*For every TemplateMapping element
*Get the authoring template and presentation template names
*Retrieve the Lotus Web Content Management object from the names
*If item already contains a template mapping with the same authoring template,
*remove it and add the new one
*Else, add the new template mapping
*Save the object, either the site or site area
*/
public Object retrieveAddTemplateMapping
(SiteFrameworkContainer aWCMObject,
Element settingsel){
DocumentId authId = null;
DocumentId presId = null;

NodeList tempnl = settingsel.getChildNodes();

for (Node node = settingsel.getFirstChild(); node !=
null; node = node.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE &&
node.getNodeName().equals("TemplateMapping")) {
Element tempel = (Element)node;

String authoringTemp =
getElementValueByTagName("AuthoringTemp", tempel );
String presTemp =
getElementValueByTagName("PresentationTemp", tempel );

authId = getWCMDocumentByName(authoringTemp,
DocumentTypes.AuthoringTemplate);

presId = getWCMDocumentByName(presTemp,
DocumentTypes.PresentationTemplate);

if (authId != null){
try{
if (aWCMObject.hasTemplateMapping(authId)){
}

if (!aWCMObject.hasTemplateMapping(authId)){
aWCMObject.addTemplateMapping(authId, presId);
}
else{

DocumentIdIterator docIt = aWCMObject.getAuthoringTemplateIds();
if (docIt.hasNext()){
DocumentId dids = (DocumentId)docIt.next();
aWCMObject.removeTemplateMapping(dids);
aWCMObject.addTemplateMapping(authId, presId);
}
}

System.out.println("TemplateMapping authName = "+authoringTemp+"
and presName = "+presTemp +" added to object " + aWCMObject.getName());
workspace.save(aWCMObject);
}catch(Exception e){
System.out.println("Exception ="+e);
e.printStackTrace();
}
}else{
return null;
}
}
}

return aWCMObject;
}


清单 15. 将 Lotus Web Content Management 对象保存到工作空间
/*
* This is a helper method that receives a Lotus Web Content Management
* document object and adds it to the workspace
*/
public boolean saveToWorkspace(com.ibm.workplace.wcm.api.Document wcmDocument){

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

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


清单 16. 按路径检索 Lotus Web Content Management 对象
/*
* This is a helper method that retrieves a Lotus Web Content Management
* object by its path and returns the DocumentId
*/
public DocumentId getWCMDocumentByPath(String wcmPath, int workFlowStatus) {
DocumentId wcmDocumentId = null;
try{


if(wcmPath != null){
DocumentIdIterator docIdIter = workspace.findByPath(wcmPath, workFlowStatus);
if(docIdIter != null && docIdIter.hasNext()){
wcmDocumentId = docIdIter.nextId();
}
}
}catch(Exception e){
System.out.println("Exception = "+e);
e.printStackTrace();
}

return wcmDocumentId;
}


清单 17. 按名称检索子元素
/*
* This is a helper method that gets the child element according to
* its name of another element if
* it contains only one child with that name
*/
private Element getChildByName(Element element, String name) {
Element rEl = null;
for (Node node = element.getFirstChild(); node != null; node =
node.getNextSibling()) {
if (node.getNodeType() == Node.ELEMENT_NODE &&
node.getNodeName().equals(name)) {
rEl = (Element)node;
break;
}
}
return rEl;
}


清单 18. 按名称检索元素值
/*
* This is a helper method that returns a string value of the given element
*/
public String getElementValueByTagName(String aTagName, Element aElement){
String elementValue = "";

NodeList nodeList = aElement.getElementsByTagName(aTagName);
if(nodeList != null && nodeList.getLength() > 0) {
Element el = (Element)nodeList.item(0);
elementValue = el.getFirstChild().getNodeValue();
}

return elementValue;
}


清单 19. 按名称检索 Lotus Web Content Management 对象
/*
* This is a helper method that returns the DocumentId of a Lotus Web
* Content Management object based on its name
*/
public DocumentId getWCMDocumentByName(String wcmName, DocumentType documentTypes){

DocumentId wcmDocumentId = null;
try{
if(wcmName != null){

DocumentIdIterator docIdIter = workspace.findByName(documentTypes, wcmName);
if(docIdIter != null && docIdIter.hasNext()){
wcmDocumentId = docIdIter.nextId();

}
}
}catch(Exception e){
System.out.println("Exception = "+e);
e.printStackTrace();
}
if (wcmDocumentId == null && ( (documentTypes.equals
(DocumentTypes.AuthoringTemplate)) || (documentTypes.equals
(DocumentTypes.PresentationTemplate)) )){
System.out.println("Exception: "+wcmName+" could not be found in the Repository");
}
return wcmDocumentId;
}


清单 20. 关闭 Lotus Web Content Management 工作空间
/**
* Closes the Lotus Web Content Management workspace
*/
public void closeWorkspace() {
WCM_API.getRepository().endWorkspace();

增强

使用本文给出的解决方案可以进一步实现增强。例如,您可以执行下面的操作:

  • 使用 XML 创建内容
  • 向默认内容添加更复杂的组件
  • 通过只更改站点区域的名称,修改 XML 以多次创建相同的结构

    结束语

    在本文中, 我们给出了一个简单的解决方案,它使您能够以一种自动化方式定义站点结构。使用这种解决方案的用例非常多样;例如,您可能需要在多个服务器上定义站点区 域,或者您的站点中具有大量重复的站点结构。这个解决方案帮助您避免将时间浪费在执行大量既耗时又单调的任务上。

}

注意:在这里给出异常处理只是出于演示目的,而不适合用于生产目的。应当在必要时进行修改以满足您的预期和要求。

helper 类给出一些重要警告:

  • 用户将收到一条警告消息,表示出现一些错误并且创建无法成功完成。
  • 错误被记录到 SystemOut.log 文件中,因此您可以从该文件获得更多有关错误的消息。
  • 当在存储库中没有找到用户时将出现错误,这将导致一个错误被记录到日志中,但是也会为用户生成一条 Creation Completed 消息。


图 2 展示了 XML 元素和实现解决方案后生成的输出之间的样例映射。


图 2. XML 与 Lotus Web Content Management 对象和区段之间的映射
通过单击操作创建 IBM Lotus Web Content Management 站点结构

如您所见,实际站点结构与 XML 文件之间的映射非常简单,具有一个表示库的根元素。这个根元素随后包含另一个与站点对应的元素,定义它的名称并显示名称。站点区域以嵌套方式在 XML 文件中定义,用于获得必需的站点结构。

每个站点和站点区域都包含一个设置区段,其中包含安全设置和模板映射区段。下面是必须加以考虑的限制因素:安全设置和模板映射可以在各自的元素或树中的最高级别的站点或站点区域中针对每个站点或站点区域创建,并且其子元素将从父元素中继承。

内容安全设置和工作流被添加到编写模板组件中,而不是 XML 文件中的默认内容区段中。

模板映射和默认内容创建中使用的编写模板和演示模板必须提前创建。

通过定义默认内容的名称,默认内容可以在多个站点区域中重用。当您在 XML 文件中定义了完整的默认内容元素后,您可以通过定义一个只包含名称元素的默认内容标记来使用它。如果默认内容已经存在于库中,也可以使用在多个不同位置重用默认内容这一特性。


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

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