Web script for getting the details of a particular news item

Now let's look at the web script changes for this. In this example, we will see how we can fetch the details of an individual news item.

Description document

The description document, getNewsItem.get.desc.xml, will be the same as used in the previous example.

Java-backed Bean for web scripts

We use the same Java Bean class, GetNewsItem, which extends the DeclarativeWebScript class, but we will modify this class as follows:

  1. Render the intermediate template model (getNewsItem.get.html.model.ftl) with the renderTemplate method of template service as:
    renderTemplate(templatePath, map, out);
    

    where templatePath is the path for the filegetNewsItem.get.html.model.ftl, map is the model map of the news item, and out is used to store the intermediate result of this rendering.

  2. Create a tag model map by processing the TagMapping.xml file.
  3. Then process this tag model over the result of that intermediate template rendering with the processTagging method of template service as:
    result = templateService.processTemplateString(
    "freemarker", template, modelMap);
    

    where freemarker indicates the use of FreeMarker as the template processor, template is the intermediate result generated by rendering of the template in the previous step, modelMap is the model map created by processing the tag mapping file, and result will hold the final result after replacing all of the tags with actual values.

    Here we are using the template service to render and process the FreeMarker template.

  4. Now in the Spring Bean configuration file, web-script-custom-context.xml, in the tomcat/webapps/alfresco/WEB-INF/classes/alfresco/extension folder, we need to add template service as a property for that Bean as we are using this new service for our example as:
    <?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" />
    <property name="templateService" ref="TemplateService" />
    </bean>
    </beans>
    

Response template

The intermediate rendering template for the HTML response, the getNewsItem.get.html.model.ftl file 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>
<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>
<tr>
<td>
${newsItem.contentText}
</td>
</tr>
</table>
</td>
</tr>
</table>
</#if>
</html>

And finally, the rendering template, the getNewsItem.get.html.ftl file, is:

<html>
${finalXML}
</html>

Storing / registering the web script in Alfresco

There is no change in the description document, but upload the new template file getNewsItem.get.html.model.ftl in the Company Home | Data Dictionary | Web Scripts Extensions | org | cignex | news folder. Also, modify the getNewsItem.get.html.ftl file that already exists there accordingly.

Navigate to http://localhost:8080/alfresco/service/index and click on the Refresh Web Scripts button to view the changes.

Calling the web script from a JSP page

There will not be any changes for this. The same JSP that was used in the previous case can be used to call this web script, and the same result will be displayed:

Calling the web script from a JSP page

As shown in the previous screenshot, the actual book page URL being displayed in the status bar comes from the tag mapping file.

Note

You can download the code samples for web script-related and blog module-related files for this case study from the Packt website.

Integrating Alfresco WCM and a Surf-based web application

In this case study, we will refer to the same web script we used in the first case study (Integrating Alfresco WCM and Liferay with a news portlet). For integrating this with a Surf-based web application, we need to create a JSON response format for the same web script.

Refer to the Integrating Alfresco WCM and Liferay with a news portlet section for more details on the web script for getting a news headline.

Response template

The rendering template for the JSON response, the getNewsItem.get.json.ftl file, is as follows:

<#escape x as jsonUtils.encodeJSONString(x)>
{
"newsItems": [
<#if m_newsNodes?exists>
<#list m_newsNodes as newsNode>
<#assign newsItemDom = newsNode.xmlNodeModel/>
<#if newsItemDom?exists>
<#assign newsItem = newsItemDom.news>
<#if newsItem?exists>
{
"Headline":"${newsItem.contentHeader}"
}
</#if>
</#if>
<#if newsNode_has_next>,</#if>
</#list>
</#if>
]
}
</#escape>

Integrating web scripts with a SURF application

In the Communication with Web Content Management section of Chapter 9, we have already explained the configuration required at Surf side to call web scripts from Alfresco, along with a UI web script that will be responsible for rendering the page. And the web script we mentioned earlier in this section is a data web script, which will be responsible for fetching content from the repository.

In Surf, we have already created a header web script to display the main page. We have also created a web script to display the news header in Surf. Now we will call the news web script in the same (header) web script to integrate news headline on the same page.

The following screenshot shows the main page from the Surf application where we have integrated the WCM web script. Content is fetched from the repository:

Integrating web scripts with a SURF application

Note

You can download the code samples from the Packt website.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset