Friday, December 27, 2013

Using BLOB Data Type in Liferay Service Builder

Objective:

Use Blob data type in liferay service builder when we work with liferay Plugin portlet development.

Environment:

Liferay 6.2 +Tomcat 7.x+MySQL 5.1

Note:

The code will work for portal 6.2 version we can also try for other portal versions.

Download Blob Service Builder portlet from following location

You can find source and war file


Portlet Screen:



Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.

Once portlet is deployed successfully you can see the portlet in sample category name as Blob Service Builder.

Blob is one of the data type in relation data bases which is used to store large amount of data in database.

Liferay service builder support the blob data type so that we can use this feature to add large amount of data in database. Generally we want store images or file content then we can use this blob data type in service builder.

When we blob data type in service builder it will generate Util classes and service builder generate SQL script that will execute when we deploy portlet in liferay portal.

Steps to implement blob in Service Builder

  1. Create liferay MVC portlet using Liferay IDE in eclipse
  2. Create service.xml file in portlet WEB-INF directory
  3. Create entity and define blob column for entity
  4. Run service builder check for auto generated java classes.
  5. Create Persistence object and fill data in object
  6. Add Persistence objet to database

Create liferay MVC portlet using Liferay IDE in eclipse

Create one Liferay MVC portlet using liferay IDE in eclipse. This pretty easy because this default portlet in liferay development.

Create service.xml file in portlet WEB-INF directory

Now create one service.xml file in portlet WEB-INF directory




Create entity and define blob column for entity

Once we create service.xml we need define our entity so that we can store data in the database.
And define columns for entity and choose one of the column data type BLOB.

Add following content in service.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN"
"http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.meera.blobsb">
            <author>E5410</author>
            <namespace>meera</namespace>
            <entity name="Photo" local-service="true" remote-service="true">
                        <!-- PK fields -->
                        <column name="photoId" type="long" primary="true" />
                        <!-- Audit fields -->
                        <column name="photoName" type="String" />
                        <column name="owner" type="long" />
                        <column name="data" type="Blob" />
            </entity>
</service-builder>

Note:

Observe above XML file we can see Blob data type for column

Run service builder check for auto generated java classes

Once we define entity in service.xml file now we have run service builder so that it will generate necessary java classes and SQL script which will required interacting with database.

It will generate all model and data base service util classes so that we can interact with data base table to store images or file content.

To run service builder go to ANT view in eclipse and double click on build-service.

From command prompt we can use ant build-service

The following is example screen for ant view in eclipse




Once we run service builder it will generate required java classes and SQL scripts. Here we will some service util classes to interact with data base table.

The following are example for generated java classes after run service builder.





The following is SQL script generated by service build in SQL directory in table.sql

Here our entity is Photo

Name Space is meera

When we create table service build always add name space before entity name which we provided in service.xml

Table Name:

meera_Photo (NameSpace_EntityName  )

create table meera_Photo (
            photoId LONG not null primary key,
            photoName VARCHAR(75) null,
            owner LONG,
            data_ BLOB
);

Create Persistence object and fill data in object

Once we generate classes now we need to create Persistence object to require table and we need pass data to object. This code we have to write wherever you are going to add data to the table generally we will write in action class.

To interact with table we will use XXXLocalServiceUtil class which has many methods to interact with table.

Create Persistence object for entity and we need to set required data to object using persistence object.

Persistence object expect Blob as parameter so we need set Blob using setter method
Here we will get any image or content as file and we will convert to InputStream and then we will convert it to Blog object.

Finally The code as follows


UploadRequest uploadRequest=PortalUtil.getUploadPortletRequest(actionRequest);
File photoImage = uploadRequest.getFile("photo");
String title=ParamUtil.getString(uploadRequest,"title");
InputStream fis =new FileInputStream(photoImage);
OutputBlob dataOutputBlob = new OutputBlob(fis, photoImage.length());
Photo photo=PhotoLocalServiceUtil.createPhoto(CounterLocalServiceUtil.increment());
photo.setPhotoName(title);
photo.setOwner(10153);
photo.setData(dataOutputBlob);
photo=PhotoLocalServiceUtil.addPhoto(photo);


Add Persistence objet to database

Once we ready with persistence object then we will add to database using XXXLocalServiceUtil java class

The following is code for add persistence object to table


Photo photo=PhotoLocalServiceUtil.addPhoto(photo);


The following screen shot of data base table after store data in table



Important Points
  • Liferay serviced build is too to create database service to portlet.
  • Liferay service build will generate necessary java classes and SQL script which we need to interact with database.
  • Liferay service builder will provide Util classes so we can use those classes to do CURD operations on table.
  • Liferay service builder generate classes which internally implemented from Hibernate and Spring frame works.
  • Liferay service builder can do most of the data base operation in some case if requirement is beyond CURD operations then we have to implement ourselves and it provide some custom implementation feature so we can achieve our requirements at maximum level.

Related Links




Author

Meera Prince

Liferay AUI Validator

Objective:

 Use AUI Form Validator to validate form data from client side.

Environment:

Liferay 6.2 +Tomcat 7.x+MySQL 5.1

Note:

The code will work for portal 6.2 version.

Download Liferay AUI Form Validator portlet from following location

You can find source and war file



Portlet Screen:



Procedure for deploy the portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy the portlet.

Once portlet is deployed successfully you can see the portlet in sample category name as AUI Form Validator.

Liferay have given AUI Validator to validate form elements. Liferay have inbuilt Validator tag by using this tag we can validate form data from the client side with minimal java script.

The following is tag


<aui:validator  name="required "  errorMessage="Field is required" >


This tag has two attribute

Name (required):

 This is name of the Validator. This attribute is required attribute for rag. Liferay have provided many default Validator

ErrorMessage:

 This will used to pass error message for Validator. If we not pass then it will display default error messages. The default error message only for default Validator those are provided by liferay.

Note:

This Validator tag we need to pass inside <aui:input/>  tag, you can pass multiple Validators  for one input element and every Validator has its own error message or we can pass our custom messages through  errorMessage attribute.

Example:

<aui:input name="firstName" value='' label="Name">
 <aui:validator name="required"/>
 <aui:validator name="alpha"/>
</aui:input>

The following are liferay provided Default Validators

You can pass following Validators for name attribute value in <aui:validator/> tag.

AcceptFiles:  

It will used to file html element it will check the file valid extension . you have provide file  extension as comma separated.

Default Error Message:

Please enter a value with a valid extension

Example:

<aui:validator name="acceptFiles">'jpg,png,tif,gif'</aui:validator>

Alpha:

This Validator can accept only alphabets for input value.

Default Error Message:  

Please enter only alpha characters.

Example:

<aui:input name="firstName" value='' label="Name">
 <aui:validator name="alpha"/>
</aui:input>

Alphanum:

This will accept alphabets and numbers

Default Error Message:  

Please enter only alphanumeric characters.

Example:

<aui:input name="userName" value='' label="userName">
 <aui:validator name="alphanum"/>
</aui:input>

Date:

Validate date for input.

Default Error Message:  

Please enter a valid date.

Example:

<aui:input name="date" value='' label="Date of Birth">
 <aui:validator name="date"></aui:validator>
</aui:input>

Digits:

Accept digits

Default Error Message:  

Please enter only digits
.
Example:

<aui:input name="phoneNumber" value='' label="Phone Number">
 <aui:validator name="digits"></aui:validator>
</aui:input>

Email:

 Validate email addresses

Default Error Message:  

Please enter a valid email address.

Example:

<aui:input name="email" value='' label="Email">
 <aui:validator name="email"/>
</aui:input>

EqualTo:

This Validator used for to compare two field values example password match

Default Error Message: 

Please enter the same value again.

Example:

<aui:input name="password1" id="password1" value='' label="Password">
 <aui:validator name="required"></aui:validator>
</aui:input>
<aui:input name="password2" value='' label="Re Type Password">
 <aui:validator name="equalTo">'#<portlet:namespace />password1'</aui:validator>
</aui:input>

Max:

This Validator check the value which should be less than to provided value. Like checking age

Default Error Message:  

Please enter a value less than or equal to X

Example:

<aui:input name="age" value='' label="Age">
 <aui:validator name="max">60</aui:validator>
</aui:input>

Min:

This Validator check the value which should be greater than the value provided to Validator. Like checking age

Default Error Message:  

Please enter a value greater than or equal to X

Example:

<aui:input name="age" value='' label="Age">
 <aui:validator name="min">18</aui:validator>
</aui:input>

MaxLength:

Check for the length or number of characters for input value which should less then provided value for Validator.

Default Error Message:  

Please enter no more than X  characters

Example:

<aui:input name="phoneNumber" value='' label="Phone Number">
<aui:validator name="maxLength">10</aui:validator>
</aui:input>

MinLength:

Check for the length or number of characters for input value which shoul more than provided value in Validator.

Default Error Message:  

Please enter at least X characters.

Example:

<aui:input name="phoneNumber" value='' label="Phone Number">
<aui:validator name="minLength">8</aui:validator>
</aui:input>

Number:

Accept only numbers for input value.

Default Error Message:  

Please enter a valid number.

Example:

<aui:input name="age" value='' label="Age">
<aui:validator name="number"></aui:validator>
</aui:input>

Required:

It make field is mandatory to fill.

Default Error Message:  

This field is required..

Example:

<aui:input name="userName" value='' label="userName">
 <aui:validator name="required"/>
</aui:input>

URL:

Check for the valid URL

Default Error Message:  

Please enter a valid URL.

Example:

<aui:input name="webSite" value='' label="Website Address">
 <aui:validator name="url"/>
</aui:input>

Custom Validator

As of now we have seen default Validators those are provided by liferay.
Now we will see how to write custom Validators.

Step: 1

We need use <aui:validator/> tag enclosed by <aui:input> and provide values for name and errorMessage attributes.

Example:

<aui:input name="age" value='' label="Age">
 <aui:validator  name="custom"  errorMessage="You must have 18 years or more" >
</aui:validator>
</aui:input>

Step: 2

Within <aui:validator/> tag  we have to write AUI JavaScript which should have our custom logic and the function should return either true or false.

Example:

<aui:input name="age" value='' label="Age">
 <aui:validator  name="custom"  errorMessage="You must have 18 years or more" >
function (val, fieldNode, ruleValue) {
var result = false;
if (val >=18) {
result = true;
}
return result;
}
</aui:validator>

Assume we need to check the age of user should more than 18 years .

The following is custom Validator to check age should more than 18 years

<aui:input name="age" value='' label="Age">
 <aui:validator  name="custom"  errorMessage="You must have 18 years or more" >
function (val, fieldNode, ruleValue) {
var result = false;
if (val >=18) {
result = true;
}
return result;
}
</aui:validator>
</aui:input>

The following is complete JSP page have all Validators

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<liferay-theme:defineObjects />
<portlet:defineObjects /> <aui:form name="fm2" action="" method="post">
       <aui:input name="userName" value='' label="User Name">
              <aui:validator name="required" />
              <aui:validator name="alphanum" />
       </aui:input>
       <aui:input name="firstName" value='' label="First Name">
              <aui:validator name="required" />
              <aui:validator name="alpha" />
       </aui:input>
       <aui:input name="lastName" value='' label="Last Name">
              <aui:validator name="alpha" />
       </aui:input>
       <aui:input name="email" value='' label="Email">
              <aui:validator name="email" />
       </aui:input>
       <aui:input name="date" value='' label="Date of Birth">
              <aui:validator name="date"></aui:validator>
       </aui:input>
       <aui:input name="phoneNumber" value='' label="Phone Number">
              <aui:validator name="digits"></aui:validator>
              <aui:validator name="minLength">8</aui:validator>
              <aui:validator name="maxLength">10</aui:validator>
       </aui:input>
       <aui:input name="price" value='' label="Price Range">
              <aui:validator name="number"></aui:validator>
              <aui:validator name="min">200</aui:validator>
              <aui:validator name="max">1000</aui:validator>
       </aui:input>
       <aui:input name="password1" id="password1" value='' label="Password"
              type="password">
              <aui:validator name="required"></aui:validator>
       </aui:input>
       <aui:input name="password2" value='' label="Re Type Password"
              type="password">
              <aui:validator name="required" />
              <aui:validator name="equalTo">'#<portlet:namespace />password1'</aui:validator>
       </aui:input>
       <aui:input name="webSite" value='' label="Website Address">
              <aui:validator name="url" />
       </aui:input>
       <aui:input name="age" value='' label="Age">
              <aui:validator name="custom"
                     errorMessage="You must have 18 years or more">
function (val, fieldNode, ruleValue) {
var result = false;
//alert(val);
if (val >=18) {
result = true;
}
return result;
}
</aui:validator>
       </aui:input>
       <aui:button type="submit" name="save" value="Save" />
</aui:form>

Author

Meera Prince

Recent Posts

Recent Posts Widget

Popular Posts