Tuesday, October 8, 2013

Alfresco Portlets in Liferay

Yesterday I wrote a short article about the integration of Alfresco into Liferay. One part of it focused on the Portlets those are part of Alfresco Share. This new article provides some deeper insights.

Let's assume that you deployed the Alfresco portlets to Liferay. So you now have a new web application ${LIFERAY_HOME}/tomcat/webapps/share (Let's name this path ${SHARE}). Under ${SHARE}/WEB-INF the file which is named 'portlet.xml' can be found. A portlet definition looks as the following one:



As you can see Alfresco provides a ProxyPortlet class in order to view a Share Web Script. The next interesting file is the one which is named 'liferay-portlet.xml'. It references the ShareMyDocLibs portlet:

<liferay-portlet-app>
        <portlet>
                <portlet-name>ShareMyDocLibs</portlet-name>
                <user-principal-strategy>screenName</user-principal-strategy>
        </portlet>
        ...
</liferay-portlet-app>

There is a third configuration file which is named 'liferay-display.xml'. It defines the Alfresco application category:

<display>
        <category name="Alfresco">
                <portlet id="ShareMyDocLibs"></portlet>
                ...
        </category>
</display>
To access the portlet also a servlet definition in the 'web.xml' configuration file is required:

...
       <servlet>
                <servlet-name>ShareMyDocLibs Servlet</servlet-name>
                <servlet-class>com.liferay.portal.kernel.servlet.PortletServlet</servlet-class>
                <init-param>
                        <param-name>portlet-class</param-name>
                        <param-value>org.alfresco.web.portlet.ProxyPortlet</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
        </servlet>
...
       <servlet-mapping>
                <servlet-name>ShareMyDocLibs Servlet</servlet-name>
                <url-pattern>/ShareMyDocLibs/*</url-pattern>
        </servlet-mapping>


So in order to include your own Alfresco portlet, it should be sufficient to add the entries to the just mentioned descriptor files. But let's now investigate the ShareMyDocLibs script. It is located at '${SHARE}/WEB-INF/classes/alfresco/site-webscripts/org/alfresco/components/my-documentlibraries' and just follows the WebScript pattern. (More about WebScripts: http://ecmgeek.blogspot.de/2012/06/simple-pinboard-dashlet.html ). So it has a descriptor, a controller and in this case an HTML view. Let's create a very simple 'Hello World' Dashlet (Do not mix it up with Portlet: A dashlet is a mini app which is living on Alfresco Share's Dashboard. A Dashlet is also only a Web Script in Share. A Portlet is a mini app which is living in a Portlet Container, like Liferay. An JSR168 portlet can run in other Portlet Containers, too. Unlike Dashlets, Portlets are a kind of standardized. However, Alfresco provides this ProxyPortlet which allows to show Dashlets in Liferay.). So let's start by adding a Dashlet to '/WEB-INF/classes/alfresco/site-webscripts/org/alfresco/components/dashlets'. Let's name it 'my-liferay-portlet':

  • Create a WebScript descriptor file 'my-liferay-portlet.get.desc.xml'
<webscript>
    <shortname>A simple Liferay portlet</shortname>
    <description>Says Hello world!</description>
    <family>site-dashlet</family>
    <format default="html">extension</format>
    <url>/components/dashlets/my-liferay-portlet</url>
</webscript>
  • Create a Freemarker HTML template which has the name 'my-liferay-portlet.get.html.ftl'
<div>
<h3> My Liferay portlet </h3>
<br/>
<b> ${result} </b>
</div>
  • Create a JavaScript controller which sets the model. It should be named 'my-liferay-portlet.get.js'
model.result = "Hello world!";

  •  Navigate to 'http://localhost:8080/share/service/index' and click on the 'Refresh Web Scripts' button. If this does not work for you, just restart your Liferay installation.
  •  Your Liferay embedded Share is available via 'http://localhost:8080/share/' whereby 'http://localhost:8080' is the URL to reach your Liferay installation. (Further details in my previous article: http://ecmgeek.blogspot.de/2013/10/how-to-access-alfresco-repository-from.html ). So log-in to Share and add your Dashlet to a Site Dashboard to make sure that it works. The result in Share looks like that (As you can see, I did not add the Alfresco specific Dashlet border because that would look a little bit confusing in Liferay):
  • Now let's add the Dashlet as a portlet by editing the following files as mentioned above (Just copy the blocks of the ShareMyDocLibs portlet by mapping to the new Web Script's URL. Do not forget to restart your Liferay installation afterwards):
    • liferay-portlet.xml
 <portlet>
                <portlet-name>MyLiferayAlfrescoPortlet</portlet-name>
                <user-principal-strategy>screenName</user-principal-strategy>
 </portlet>
    • liferay-portlet.xml
 <portlet>
                <description>Alfresco: My Liferay Portlet</description>
                <portlet-name>MyLiferayAlfrescoPortlet</portlet-name>
                <portlet-class>org.alfresco.web.portlet.ProxyPortlet</portlet-class>
                <init-param>
                        <name>viewScriptUrl</name>
                        <value>/page/components/dashlets/my-liferay-portlet</value>
                </init-param>
                <supports>
                        <mime-type>text/html</mime-type>
                        <portlet-mode>VIEW</portlet-mode>
                </supports>
                <portlet-info>
                        <title>Share: My Document Libraries</title>
                        <short-title>My Document Libraries</short-title>
                </portlet-info>
                <security-role-ref>
                        <role-name>administrator</role-name>
                </security-role-ref>
                <security-role-ref>
                        <role-name>guest</role-name>
                </security-role-ref>
                <security-role-ref>
                        <role-name>power-user</role-name>
                </security-role-ref>
                <security-role-ref>
                        <role-name>user</role-name>
                </security-role-ref>
 </portlet>
    • liferay-display.xml
<display>
        <category name="Alfresco">
                <portlet id="ShareMyDocLibs"></portlet>
                <portlet id="ShareSiteDocLib"></portlet>
                <portlet id="ShareRepoBrowser"></portlet>
                <portlet id="MyLiferayAlfrescoPortlet"></portlet>
        </category>
</display>

    • web.xml

  • Then add the new portlet to a Liferay page. The resulting portlet looks like that:


So finally I can say, that integrating Alfresco with Liferay works good for you if you are an Alfresco Developer. Then Alfresco's ProxyPortlet already solves problems like the authentication via Liferay for you. If you know how to develop Alfresco Dashlets, then you also know how to develop Alfresco Portlets out of the box. I think that I would not recommend to use the existing Portlets like the 'MyDocumentLibraries' one, because it just breaks with Liferay at some points. It forwards you to the Alfresco Document Library and as soon as you click the wrong link, it then forwards you to standalone Share application. So a compromise could be:
  • Deploy Share to Liferay, but do not use the example Portlets
  • Create Dashlets for Share those work without back links to the rest of the Share UI
  • Make these Dashlets available as Portlets in Liferay

2 comments:

  1. Hello , Thank you for this post. I m working now on Liferay 6.2 integration with Alfresco 4.2 , I have a problem with the portlet : (Share : Site Document Library ), when I change its settings by choosing a site , nothing displays :( Do you have any idea about how to solve it ?! Help please

    ReplyDelete
    Replies
    1. Hi Sofia. I'm doing smthg similar. Have u solved the problem?

      Delete