Thursday, November 7, 2013

WAR file built Using Liferay Plugins SDK contains java source files

The standard Liferay SDK build process includes all java sources to the deployment units (war file).

As Liferay is Open Source, it makes sense to include the code also in the war, if someone just download the plugin war and installs it. This holds good for the plugins developed to be shared among the community. But it is not acceptable while building it for any enterprise project.

It is possible to override the war target in individual build.xml of the portlet or you could just replace the target with your own in the build-common-plugin.xml so it applies to all portlets in your SDK. Below is the snippet.

<target name="war" depends="compile">
<mkdir dir="${sdk.dir}/dist" />

<if>
<available file="tmp" />
<then>
<property name="docroot.dir" value="tmp" />
</then>
<else>
<property name="docroot.dir" value="docroot" />
</else>
</if>

<delete file="${plugin.file}" />

<antcall target="clean-portal-dependencies" />

<if>
<available file="docroot/WEB-INF/liferay-hook.xml.processed" />
<then>
<property name="liferay-hook.xml.excludes" value="WEB-INF/liferay-hook.xml*" />
</then>
<else>
<property name="liferay-hook.xml.excludes" value="" />
</else>
</if>

<if>
<contains string="${app.server.dir}" substring="glassfish" />
<then>
<zip
basedir="${docroot.dir}"
destfile="${plugin.file}"
excludes="**/META-INF/context.xml,${liferay-hook.xml.excludes},${plugins.war.excludes}, WEB-INF/src/**"
>
<zipfileset
dir="${docroot.dir}"
fullpath="WEB-INF/liferay-hook.xml"
includes="WEB-INF/liferay-hook.xml.processed"
/>
</zip>
</then>
<else>
<zip
basedir="${docroot.dir}"
destfile="${plugin.file}"
excludes="${liferay-hook.xml.excludes},${plugins.war.excludes}, WEB-INF/src/**"
>
<zipfileset
dir="${docroot.dir}"
fullpath="WEB-INF/liferay-hook.xml"
includes="WEB-INF/liferay-hook.xml.processed"
/>
</zip>
</else>
</if>

<if>
<and>
<equals arg1="${plugins.src.zip.enabled}" arg2="true" />
</and>
<then>
<zip destfile="${plugin.src.file}">
<zipfileset
dir="${docroot.dir}"
excludes="${liferay-hook.xml.excludes}"
prefix="${plugin.name}-src-${plugin.full.version}"
/>
<zipfileset
dir="${docroot.dir}"
fullpath="${plugin.name}-src-${plugin.full.version}/WEB-INF/liferay-hook.xml"
includes="WEB-INF/liferay-hook.xml.processed"
/>
</zip>
</then>
</if>
</target>

Adding Documents to liferay Document Library through API

While adding documents to documents library there are two ways of doing it using the liferay API,  DLFileEntryLocalServiceUtil or DLAppLocalServiceUtil


Difference between DLFileEntryLocalServiceUtil and DLAppLocalServiceUtil

DLFileEntry services and DLFolderEntry services are specifically for storing file and folder entries in liferay's database and are totally unaware of the new repository concept introduced in 6.1. The user-guide & this wiki explains how to add a new repository.

Where as DLApp (DLAppService & DLAppLocalService) services take into account these things i.e. to say that they take care of syncing documents between liferay database and other repositories, and not just store entries in Liferay database.

Adding documents to Documents Library

1. Get the File object (using input stream) of the file which is being inserting to the document library.

2. Get the FileInputStream 

InputStream inputStream = new FileInputStream( file );

3. Get the ThemeDisplay from the request. This is used for retrieving the user id and group id while adding the documents which is depicted in the further steps down.

ThemeDisplay themeDisplay = ( ThemeDisplay ) actionRequest.getAttribute( WebKeys.THEME_DISPLAY );

4. Get the Service Context object

ServiceContext serviceContext = ServiceContextFactory.getInstance( actionRequest );

5. Get the access folder in which the document would be placed.

Folder folder = DLAppLocalServiceUtil.getFolder( themeDisplay.getScopeGroupId(), 0, "<<Name of the folder>>" );

Here the first argument is the repository ID,  If the repository is a default Liferay repository, the repositoryId is the groupId or scopeGroupId. Otherwise, the repositoryId will correspond to values obtained from RepositoryLocalServiceUtil.

6. Now finally the inserting to the document library using DLAppLocalServiceUtil

FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry( themeDisplay.getUserId(), folder.getRepositoryId(), folder.getFolderId(), fileName,
contentType, fileName, fileName, "changeLog", inputStream, file.length(), serviceContext );


That is all we need to do, document is now inserted in the document library.

Monday, November 4, 2013

Defining custom Velocity Variables for Liferay Themes

There are number of predefined variables that can be accessed from with in the liferay theme. For elaborated list of those, below is the url

http://www.liferay.com/community/wiki/-/wiki/Main/Access+Objects+from+Velocity?_36_pageResourcePrimKey=2905717

Apart from those variables, if at all we end up needing a custom variable, which define a custom functionality/value as per the specific business need, we can define them in two places.

1. init_custom.vm. 

init.vm file, which is present in the liferay theme, contains all the liferay defined velocity variables. As a best practice, all the custom variables should be defined in the init_custom.vm file. 
This file allows you to override and define new Velocity variables.

#set($background_url = $theme_display.getThemeSetting('background-url'))

In the above example we are reading the background-url value provided by the user, while configuring the themes.

2. Setting the variables in request scope under attribute WebKeys.VM_VARIABLES

Create a hook, either Service Pre action or Login Post Action hook and inside your extended pre or post Action class, create a Map object with the key and value of the vm variables you want to insert, and then set that into the attribute under the key WebKeys.VM_VARIABLES. The variables added to the map are directly accessible in the Theme. This method provide more flexibility than the earlier one as there are no restrictions for defining the variables in this method.

Here is the code snippet for the same


Map< String, Object > vmVariables = ( Map< String, Object > ) request.getAttribute( WebKeys.VM_VARIABLES );

if ( vmVariables == null ) {
         vmVariables = new HashMap< String, Object >();
}
vmVariables.put("someVariable", "someValue");


request.setAttribute( WebKeys.VM_VARIABLES, vmVariables );

Now the variable added to the Map is directly accessible from with in the velocity template file in the theme.

For example it can be directly accessible from portal_normal.vm as below

<a id="siteLogo" class="logo $logo_css_class" href="$someVariable" title="#language("go-to") $site_name"/>