Integrating WCM Using Web Scripts [ 332 ] <title><![CDATA[${newsItem.shortTitle}]]></title> <header><![CDATA[${newsItem.contentHeader}]]></header> <date>${newsItem.newsDate}</date> </newsItem> </#if> </#if> </#list> </#if> </news> Storing/registering a web script in Alfresco Once done with the development of a web script with all of the three mentioned les, we need to store it in an appropriate place. Let's store it through Alfresco Explorer. We will store the web script les in the Company Home | Data Dictionary | Web Scripts Extensions folder. Create the folder structure as org/cignex/news inside the Web Scripts Extensions folder and upload all of the three web script-related les here, as mentioned in a previous section on storing a web script. After uploading, you should see all of the three les in the news folder, as shown in the next screenshot: Once done with this, register the web scripts by navigating to http://localhost:8080/alfresco/service/index and clicking on the Refresh Web Scripts button as mentioned before in the section on registering a web script. Now that the new web script is registered, we can start using it with the URL. If you want to check it, you can click on any of the browse options. Clicking on Browse by Web Script URI will browse all of the web scripts' URI. By doing so, you can verify your web script is there. Now we will see how to use this web script in Liferay. Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 333 ] Portlet in Liferay Now in Liferay, you need to create a new portlet and call our web script from that portlet. The following screenshot shows a snap of a portal page where one of the portlets is News & Press. The content of this portlet is fetched from the Alfresco WCM: You can download the code samples for web script-related les and the news portlet .war le for Liferay from the Packt website. Integrating Alfresco WCM and Drupal with monthly blogs In this case study, we will see the integration between Alfresco WCM and Drupal. This will be an integration of two different technologies, Java and PHP. We are going to take an example from monthly blogs. As in Chapter 4, Web Content Production with Web Forms, we have a web form for creating different blog items in the WCM. So we do have some blog items available with us in the WCM, which we will display on a Drupal page. Web script for getting monthly blogs Here we will have a web script that will fetch the blog content from Alfresco WCM for a particular month based on the published date of the blog content. We will use JavaScript as a controller and will return the response in HTML format. So in Drupal, we do not need to do any complex processing; just display the HTML as it is. Download from Wow! eBook <www.wowebook.com> Integrating WCM Using Web Scripts [ 334 ] Description document The description document, getMonthlyBlogs.get.desc.xml, which describes the URL and the authentication mechanism for the web script is as follows: <webscript> <shortname>Listing Of Blogs</shortname> <description>Listing Of Blogs for Cignex home page thru Webscript </description> <url>/org/cignex/blogs/getBlogs.xml?storeId={storeId}&month= {monthnumber}</url> <authentication>user</authentication> </webscript> Execution script The JavaScript controller, getMonthlyBlogs.get.js, executes the search query to fetch the blog items' nodes from the WCM repository. This is done using the Lucene Search API to search the contents of a specic web form; here it's the blog's web form. Keep in mind that this Lucene Search will only be applied to the Staging Sandbox of a web project. Here we have storeId as one of the web script parameters, but that should point to the Staging Sandbox of a web project: var storeId = args["storeId"]; var blogNodes = new Array(); if(storeId != null) { var webProjectStore = avm.lookupStore(storeId); if(webProjectStore != null) { var queryForBlogs = "@wca\\:parentformname:blog AND @ cm\\:name:\"*.xml\""; var resultNodes = webProjectStore.luceneSearch(queryForBlogs); if(resultNodes != null) { logger.log("Result nodes: " + resultNodes.length); for (var i=0; i<resultNodes.length; i++) { blogNodes[i] = resultNodes[i]; } } } if(blogNodes != null) { model.m_blogNodes=blogNodes; } } else{ logger.log("ERROR: 'store' argument not specified."); } Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 335 ] Response template The rendering template for the HTML response, getMonthlyBlogs.get.html.ftl le, is as follows: <#ftl ns_prefixes={"D", "http://www.alfrescobook.com/webforms"}> <#if args["month"]?exists> <#assign monthnumber=args["month"]> <#else> <#assign dateformat="yyyy-MM-dd"> <#assign monthnumber=date?string(dateformat)?substring(5,7)> </#if> <#switch monthnumber> <#case '1'> <#assign month="January"> <#break> <#case '2'> <#assign month="Febuary"> <#break> <#case '3'> <#assign month="March"> <#break> <#case '4'> <#assign month="April"> <#break> <#case '5'> <#assign month="May"> <#break> <#case '6'> <#assign month="June"> <#break> <#case '7'> <#assign month="July"> <#break> <#case '8'> <#assign month="August"> <#break> <#case '9'> <#assign month="September"> <#break> <#case '10'> <#assign month="October"> <#break> <#case '11'> <#assign month="November"> <#break> <#case '12'> <#assign month="December"> <#break> </#switch> Download from Wow! eBook <www.wowebook.com> Integrating WCM Using Web Scripts [ 336 ] <html> <body> <#if m_blogNodes?exists> <font face="Verdana" color="CC3333"> <h3>Blogs for Month - ${month} </h3> </font> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <#list m_blogNodes as blogNode> <#assign blogItemDom = blogNode.xmlNodeModel/> <#if blogItemDom?exists> <#assign blogItem = blogItemDom.blog> <#if blogItem?exists> <#if blogItem.publishedDate?substring(5,7) == monthnumber> <tr> <td> <table> <tr> <td> <p> <a href="#"> <b>${blogItem.mainTitle} </a> </p> </td> </tr> <tr> <td> <p> ${blogItem.publishedDate?substring(0,10)} </p> </td> </tr> </table> </td> </tr> </#if> </#if> </#if> </#list> </table> </#if> </body> </html> Here we are fetching the blog items for a particular month with the parameter as month in the web script URL. But if you don't specify this parameter, then the current month will be considered to fetch the blog items. Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 337 ] Storing/registering the web script in Alfresco Once done with the development of a web script with all of the mentioned les, we need to store it to the Web Scripts Extensions folder in Alfresco Explorer. Navigate to the Company Home | Data Dictionary | Web Scripts Extensions folder. Create the folder structure as org/cignex/blogs. Upload all of the three mentioned les here in this folder, as shown in the following screenshot: Once done with this, we need to register our web script. Navigate to http://localhost:8080/alfresco/service/index and click on the Refresh Web Scripts button. Now that our web script is registered, we can start using it with the URL. Calling the web script in Drupal Now in Drupal, we need to create a new page where we will display this month's blog entries. For that we will create a new module: Download from Wow! eBook <www.wowebook.com> Integrating WCM Using Web Scripts [ 338 ] You can download the code samples for web script-related les and blog module-related les for Drupal from the Packt website. Integrating Alfresco WCM with any J2EE web application In the previous example, we saw how we can display the news headlines on the news portlet. Now, in this case study, we will learn to view the details about a particular news item. Suppose you have any J2EE-based application and you want to display the news page with some specic news item. This can be integrated with the earlier portlet example as well, like say, on the portal home page, you have one news portlet, which displays the news headlines, but when you click on any news item, you should be able to see that particular news item. However, here we will take the example of any J2EE web application and we will call this web script from a normal JSP page. Web script for getting the details of a particular news item In this case study, as mentioned before, we will talk about the web script that will fetch the details of a particular news item. In this example, we will develop a Java- backed web script so that you can have an idea about how to create Java-backed web scripts. The response format will be HTML. Description document The description document, getNewsItem.get.desc.xml, which describes the URL and the authentication mechanism for the web script is as follows: <webscript> <shortname>Listing Of News Item</shortname> <description>Listing Of News Item for Cignex News page thru Webscript</description> <url>/org/cignex/news/getNewsItem.html?storeId={storeId}& newsId={newsId}</url> <authentication>user</authentication> </webscript> Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 339 ] Java-backed Bean for a web script In this example, instead of using JavaScript as the execution script controller, we will use Java Bean as a controller and perform our business logic to fetch the particular content and process that is in this Java class. We will create a Java Bean class named GetNewsItem, which extends the DeclarativeWebScript class and will override the execute method as follows: public class GetNewsItem extends DeclarativeWebScript { protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) { // Perform the business logic here in this method } } Now, we need to congure this Bean in a Spring conguration le as a controller for a web script. For that we will create a conguration XML le named web-script-custom-context.xml in tomcat/webapps/alfresco/WEB- INF/classes/alfresco/extension . Make an entry for this Java class as a Spring Bean in web-script-custom-context.xml as follows: <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'> <beans> <! Web Script Storage > <bean id="webscript.org.cignex.news.getNewsItem.get" class="com.cignex.web.scripts.bean.news.GetNewsItem" parent="webscript"> <property name="contentService" ref="ContentService" /> <property name="avmService" ref="AVMService" /> </bean> </beans> Response template The rendering template for the HTML response, getNewsItem.get.html.ftl le, is as follows: <#ftl ns_prefixes={"D", "http://www.alfrescobook.com/news"}> <#if m_news?exists> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr> <#assign newsItem = m_news> Download from Wow! eBook <www.wowebook.com> Integrating WCM Using Web Scripts [ 340 ] <td> <h2>${newsItem.contentHeader}</h2> <#if newsItem.newsDate != ""> <strong>News Date:</strong> ${newsItem.newsDate} </#if> <#if newsItem.contentSubHeader != "" > <h4>${newsItem.contentSubHeader}</h4> </#if> <table width="50%" cellspacing="0" cellpadding="5" border="0"> <tr> <td> <tr> <h4> ${newsItem.imageTitle}</h4> <#if newsItem.contentGraphic != "" > <td> <img src="${newsItem.contentGraphic}" border=0/> </td> </#if> </tr> </td> <td> <strong> ${newsItem.imageCaption}</strong> </td> </tr>0 <tr> <td> ${newsItem.contentText} </td> </tr> </table> </td> </tr> </table> </#if> </html> Here we are fetching the details of a news item for a particular news ID, based on the ID passed; the details for that news item will be returned from Alfresco. Download from Wow! eBook <www.wowebook.com> Chapter 10 [ 341 ] In this example, we have not used the Lucene Search API; rather we have used AVM APIs and this web script is exible enough to search in any of the sandboxes for any web project. Suppose you want to search for some content in your own User Sandbox (the content is still not promoted to the Staging Sandbox); you can pass the store ID as wwwcignex—admin. Here 'wwwcignex' is the web project name and 'admin' is the username, so wwwcignex—admin will be the sandbox of the 'wwwcignex' web project for a user 'admin'. This will help in testing the content, which you have created, before submitting this change to the Staging Sandbox. Storing/registering the web script in Alfresco Once done with development of the web script with all of the three mentioned les, we need to store it to the Web Scripts Extensions folder in Alfresco Explorer. Navigate to Company Home | Data Dictionary | Web Scripts Extensions and create the folder structure as org/cignex/news. Upload all of the three mentioned les here in this folder, as shown in the next screenshot: Once we have done this, we need to register the web scripts. Go to http://localhost:8080/alfresco/service/index and click on the Refresh Web Scripts button. Now that our new web script is registered, we can start using it with the URL. Download from Wow! eBook <www.wowebook.com> . conguration le as a controller for a web script. For that we will create a conguration XML le named web- script-custom-context.xml in tomcat/webapps/alfresco /WEB- INF/classes/alfresco/extension . Make. the web script that will fetch the details of a particular news item. In this example, we will develop a Java- backed web script so that you can have an idea about how to create Java-backed web. <www.wowebook.com> Integrating WCM Using Web Scripts [ 338 ] You can download the code samples for web script-related les and blog module-related les for Drupal from the Packt website. Integrating