Use Blob data type in liferay service builder when we work with liferay Plugin portlet development.
Liferay 6.2 +Tomcat 7.x+MySQL 5.1
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
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
- Create liferay MVC portlet using Liferay IDE in eclipse
- Create service.xml file in portlet WEB-INF directory
- Create entity and define blob column for entity
- Run service builder check for auto generated java classes.
- Create Persistence object and fill data in object
- 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"
<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" />
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
meera_Photo (NameSpace_EntityName )
create table meera_Photo (
photoId LONG not null primary key,
photoName VARCHAR(75) null,
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
File photoImage = uploadRequest.getFile("photo");
InputStream fis =new FileInputStream(photoImage);
OutputBlob dataOutputBlob = new OutputBlob(fis, photoImage.length());
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
The following screen shot of data base table after store data in table
- 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.