Monday 17 December 2012

disable autogenerating web content Ids

You can disable auto-generating ID’s with the following setting in portal-ext.properties:
journal.article.force.autogenerate.id=false

Tuesday 11 December 2012

Use other database (beside liferay default) for portlet

Introduction: It is a common requirement, that sometimes we are asked to use entirely a different database for all our backend process rather than the one we have for liferay(the one we generally specify in portal-ext.properties).

Ingredients: prior knowledge of service builder, database with name "myDB" username-"root" password-"root", table "myUserTable"with columns "user_id" and "name"

Idea: The idea is to create something that is portlet specific.
Lets create the service builder and since in the <entity> we do have attribute as data-source, so this may help.
To specify the data-source's value we'll use ext-spring.xml..but HOW????

What to do?
1. Create a portlet (http://michi-path.blogspot.in/2012/03/create-new-portlet-in-liferay-ide.html).
2. create service-builder.
3. In Service.xml make the entry
    <entity name="User" table="myUserTable" local-service="true" remote-service="true" data-source="eportalDB">
        <!-- PK fields -->
        <column name="user_id" type="long" primary="true" />
        <!-- Audit fields -->
        <column name="name" type="String" />
     </entity>

 4. Do "ant build-service"
5. Go to docroot/WEB-INF/src/META-INF
create a file ext-spring.xml
6. add the following code in ext-spring.xml:
<?xml version="1.0"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    default-destroy-method="destroy"
    default-init-method="afterPropertiesSet"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="eportalDB" lazy-init="true"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/myDB?useUnicode=true"/>
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
    <bean id="liferayHibernateSessionFactory"   class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration">
         <property name="dataSource" ref="eportalDB" />
    </bean>
    <bean id="liferayTransactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="dataSource" ref="eportalDB" />
         <property name="globalRollbackOnParticipationFailure" value="false" />
         <property name="sessionFactory" ref="liferayHibernateSessionFactory" />
    </bean>
    <bean id="liferaySessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl">
         <property name="sessionFactoryImplementor" ref="liferayHibernateSessionFactory" />
    </bean>
</beans>

7. build-service and deploy

Discussion: In ext-spring.xml
liferayTransactionManager:- is for the DML operations.
if this is not added we wont be able to do operations like insert, update, delete

For only fetching the data from the table, the following code can be removed from the existing ext-spring.xml
<bean id="liferayTransactionManager"   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="dataSource" ref="eportalDB" />
         <property name="globalRollbackOnParticipationFailure" value="false" />
         <property name="sessionFactory" ref="liferayHibernateSessionFactory" />
    </bean>
    <bean id="liferaySessionFactory" class="com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl">
         <property name="sessionFactoryImplementor" ref="liferayHibernateSessionFactory" />
    </bean>


Monday 10 December 2012

UploadPortletRequest getFiles() cannot upload files with size less than 1kb

Introduction: While uploading a file in liferay we use the processAction() method of our MVC portlet class, wherein we use the following kind of code UploadPortletRequest uploadrequest = PortalUtil.getUploadPortletRequest(actionRequest);
File file =  uploadrequest.getFile("fileUpload");

where "fileUpload" is ofcourse in view.jsp

Problem: On upload if the file size was less than 1kb, there is no temporary file created(Whenever an upload operation is done a temporary file in the temp folder of the tomcat is created which is been referred for any operation performed to use the file.

Solution: use input stream instead of getFile(), as follows:-

UploadPortletRequest uploadrequest = PortalUtil.getUploadPortletRequest(actionRequest);
            InputStream[] inputStream = uploadrequest.getFilesAsStream("fileupload");
            for(InputStream fileObj:inputStream){
            File file = FileUtil.createTempFile(fileObj);
            } 

Export to .csv file - Name the file on export

Introduction: It seems easy to make a file downloadable, thats what I presumed when I was assigned this task myself. As soon as I started with this I got stuck on some minor things for which later actually I spent almost a day searching and hence at the end found the solution.


Problem: I was able to create a file even download it but issue was naming the file.
I had a table data that i needed to export to ".csv" format and name the file as "resultdata-randomNo.csv" using javascript
but the issue was when downloading it was in the format of  "xxx.part" and not csv. So I tried out the following way.

Here is the view.jsp
<script type="text/javascript">
function download(){ 
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

        var str = '';

        for (var i = 0; i < array.length; i++) {
            var line = '';

            for (var index in array[i]) {
                line += array[i][index] + ',';
            }

            line.slice(0,line.Length-1); 

            str += line + '\r\n';

            }
var randomnumber= Math.floor(Math.random()* Math.pow(10,6) + Math.pow(10,5));
      var thisForm = document.createElement('form');
      thisForm.style.display = 'none';
      document.body.appendChild(thisForm);
      var fileName = document.createElement('input');
      fileName.type = 'hidden';
      fileName.name = 'fileName';
      fileName.value = 'resultdata-'+randomnumber;
      thisForm.appendChild(fileName);
      var dataTable = document.createElement('input');
      dataTable.type = 'hidden';
      dataTable.name = 'tableData';
      dataTable.value = str;
      thisForm.appendChild(dataTable);

      thisForm.method = 'POST';
      thisForm.action = "<%=renderResponse.encodeURL(renderRequest.getContextPath())%>/tabledata.jsp";
      thisForm.submit();
      document.body.removeChild(thisForm);

}
</script>

tabledata.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@page contentType="application/octet-stream"%>
<%
    String tableData = "No records found";
    String fileName = "datatable";
    if (request.getParameter("fileName") != null
            || request.getParameter("fileName") != "") {
        fileName = request.getParameter("fileName");
    }
    if (request.getParameter("tableData") != null
            || request.getParameter("tableData") != "") {
        tableData = request.getParameter("tableData");
    }
    response.setHeader("Content-Disposition", "attachment;filename=\""
            + fileName + ".csv\"");
    out.print(tableData);
%>


The code is self-explanatory.

Hope it answers your problem also.