Thursday, February 28, 2013

Struts2+Liferay Service Builder +JqGrid Liferay portlet


Struts2+Liferay Service Builder +JqGrid   Liferay portlet

Objective:
Create liferay portlet using Struts2, struts jqGrid   and Liferay Service builder.
Before read this article we need to have good understanding in spring frame work struts framework and hibernate and have knowledge on annotation.

The following are the reference links to get knowledge.


Here Struts is used as controllers and liferay service layer  for persistence logic.
Steps to run port let.
1.     Take the portlet from following location.

2.     Place portlet into your plugins/portlets directory
3.     If you are use Eclipse ide create portle as existing source.
5.     Run ant build-service and ant deploy commands
6.     See in samples category
7.     Drag and drop into page and add the user using UI.
8.     And Users View users screens.
Screen shots: 
Screen:1

Screen:2
Screen:3

Screen:4

Note: This portlet done in liferay 6.0.6 version.
IF you want run this in liferay 6.1 version first create one mvc port let thorough the Liferay IDE and copy manually all files to newly created one.
Please make sure dtd version of liferay xml files.
For liferay xml  xml files copy content to respective xml files then there no effect in dtd files of liferay.

Wednesday, February 27, 2013

Spring3+Struts2+Hibernate4+Liferay portlet


Spring3+Struts2+Hibernate4+Liferay portlet

Objective:
Create iferay portlet usinf Spring3, Struts2 and Hiberate4.
Before read this article we need to have good understanding in spring frame work struts framework and hibernate and have knowledge on annotation.
The following are the reference links to get knowledge.


Here we are using spring for Business logic and Struts is used as controllers and hibernate for persistence logic.

Steps to run port let.
1.     Create data base in mysql name as userdb
2.     Take the portlet from following location.

4.     Place portlet into your plugins/portlets directory
5.     If you are use Eclipse ide create portle as existing source.
6.     For data base configuration you have to modify the following file according to your local environment.
7.     Sprin-hibernate.xml
8.     Run ant  deploy command
9.     See in samples category
10.    Drag and drop into page and add the user using UI.
11.    See the added users in user table..
Screen Shots:
Screen:1
Screen:2

Note: This portlet done in liferay 6.0.6 version.
IF you want run this in liferay 6.1 version first create one mvc portlet thorough the  Liferay IDE and copy manually all files to newly created one.
Please make sure dtd version of liferay xml files.
For liferay xml  xml files copy content to respective xml files then there no effect in dtd files of liferay.

Saturday, February 23, 2013

Best way to add data to life ray tables.



Best way to add data to life ray tables.
In life ray when we add data to tables we are using service layer add method.
Whenever we use add method it will expect object that going to add in data base.
Example: 
Table Name: XXXXXXX
In service.xml we write like this
<entity name="XXXXXXXX" table="XXXXXXXXX" local-service="true"
                        remote-service="true">
                        <!-- PK fields -->
                        <column name="column1" type="long" primary="true"type="increment" />
                        <!-- Audit fields -->
                        <column name="column2" type="String" />
                        <column name="column3" type="String" />
                        <column name="column4" type="boolean" />
                        <finder name=" column2" return-type=" XXXXXXXX">
                                    <finder-column name="column2" />
                        </finder>
            </entity>


The service builder generated some java classes from that we will add data tables.
Main java classes we uses is
·         XXXXXXXLocalServiceUtil
·         XXXXXXXXServiceUtil
·         XXXXXXUtil
Note: XXXX is any entity name
XXXXXXLocalServiceUtil:
Generally most of the scenarios we use XXX LocalServiceUtil to do CURD operation such as Add, Update and Delete.
If we use this class we won’t get any principle exceptions.


XXXXXXXXServiceUtil:
Whenever we want does some security check then we have to use XXXXServiceUtil java class to do CURD operations.
It having some security checks points so that it wills throw Principle Exception
Example:
When we add role to using RoleServiceUtil from our application then it will throw principle exception.
If login as Admin and run same scenarios it won’t throw any exception.
Because admin have privatization to add user.
So whenever we want perform some kind of permissions check or security check then we have to use this class.
Some time we need to use XXXServiceUtil methods. whenever we get this kind of requirement we have do following
First implement our own custom method in XXXLocalServiceImpl.java when we run service builder custom method syntax automatically created in XXXLocalServiceUtil
Take Example: RoleServiceUtil we have one method
public static com.liferay.portal.model.Role addRole(
                        long companyId, java.lang.String name,
                        java.util.Map<java.util.Locale, java.lang.String> titleMap,
                        java.lang.String description, int type)
                        throws com.liferay.portal.kernel.exception.PortalException,
                                    com.liferay.portal.kernel.exception.


When we call this method in the following way we will get principle exception until we login as admin and use this method.
RoleServiceUtil.addRole(10132,”Student”,null,”this is student”,3);
So first we have to implement custom method in RoleLocalServiceImpl.java from our custom method then we have to use RoleServiceUtil.addRole(….) method in our implementaion.



Public class RoleLocalServiceImpl extends ….{
public static com.liferay.portal.model.Role customeAddRole(
                        long companyId, java.lang.String name,
                        java.util.Map<java.util.Locale, java.lang.String> titleMap,
                        java.lang.String description, int type)
                        throws com.liferay.portal.kernel.exception.PortalException,
                                    com.liferay.portal.kernel.exception{
RoleServiceUtil.addRole(companyId, name, titleMap, description, type);
}}


Once we implement this run service builder then we will this method in RoleLocalServiceUtil class
Now we have call like this
RoleLocalServiceUtil.customAddRole(…..);

Then we won’t get any principle exceptions.
XXXUtil:
All finder methods will be generated in this class.
Like findByName, finByPrimaryKye like this.
But here we need to remember one thing we can’t call this methods directly from XXXXXUtil.
XXXUtil.findByName(..)
When we call directly we will get Exception like Hibernate session bound exception.
We have to implement custom methods in XXXLocalServiceImpl and we have to call from
LocalServiceUtil
RoleLolacServiceImpl extends …{
 Public Role getRoleByName(…){
XXXUtil.findByName(..)
}}

After we have to call like this
RoleLolacServiceUtil.getRoleByName(…..);

Generating Primary Key Value from Counter Service.
Adding Object to data base in Liferay.
Case: 1
Example  Role
Role role=new RoleImpl();
Role.setNeme(“Student”);
Role.setTitle(“Student”)
Role.setDescription(“Stuent”);
Role.setType(1);
RoleLocalServiceUtil.addRole(role);

Observe above scenario we are not set the primary key value to the object so hibernate itself generating primary key and insert into table.
Hera we are setting property increment in service.xml file.
Note: When we run above scenario we need RoleImpl java class in current class path.
Case: 2
We can use method which in XXXServiceUtil
This is not 100% reliable foe when we execute application as guest. That time it will throw principle exception.
RoleServiceUtil.addRole(…….);

The above method throws exception


Assume Following scenarios.

1.      I want insert data in table which in Portal level.
2.      I want insert data from PluginB-Portlet the table in PluginA-Portlet.
I want insert data in table which in Portal level.
For example I want add role to role table.
If I want add role to role table from my plugin portle I need RoleImpl class to create new object. But RoleImpl class not available in Current plugin class path.
Because service layer can share only service classes like XXXUtil,XXXServiceUtil, XXXLocalSericeUtil and interfaces not implementation classes Like XXXServiceImpl, XXXModelImpl, XXXImpl.
If we want use case: 1 we need RoleImpl object but RoleImpl class not available in current class path so we can’t use case 1.
If we want use case: 2 it will throw principle exception if application run as Guest user.
Solution: 1
If we want use Case: 2 we need implement custom method in one of our XXXLocalServiceImpl and from that we have to call XXXLocalServiceUtil.addRole(); but this process also sometimes throws exception.
Solution: 2
We have to load RoleImpl class using Portal class loader and from that can get RoleImpl object. But when I use this process I got exception duplicate primary key exceptions. Here role is not incremented.
Class<?> portalclassObject=PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.RoleImpl");
Role role=(Role)classObject.newInstance();
Role.setNeme(“Student”);
Role.setTitle(“Student”)
Role.setDescription(“Stuent”);
Role.setType(1);
RoleLocalServiceUtil.addRole(role);


I want insert data from PluginB-Portlet but the table in PluginA-Portlet.
Solution: 1
If we want use Case: 2 we need implement custom method in one of our XXXLocalServiceImpl and from that we have to call XXXServiceUtil.addXX(..); but this process also sometimes throws exception.
Solution: 2
We have to load XXXImpl class using Portlet Beans locator class and from that can get XXXImpl object..
This is working between 2 Pluign portlets.
ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.SERVLET_CONTEXT_NAME,"portletClassLoader");
Class<?> classObject=classLoader.loadClass("com.vidyayug.global.model.impl.XXXImpl");Role semeObject=(XXX)classObject.newInstance();
semeObject .XXX(“Student”);
semeObject.XXX(“Student”)
semeObject.setXXX(“Stuent”);
semeObject.setXXX(1);
XXXLocalServiceUtil.addXXX(semeObject);


Optimal Solution:
Use Counter LocalServe class to generate Primary key value.
We have Counter table from the table we can generate primary key value based on entity or global counter.
This counter table based on model class it will maintain counter  and each time when create object it will be incremented.
Adding Object to database using Counter increment.
long roleId=CounterLocalServiceUtil.increment();
Role role=RoleLocalServiceUtil.createRole(roleId);
                                    role.setCompanyId(themeDisplay.getCompanyId());
                                    role.setClassNameId(PortalUtil.getClassNameId(Role.class));
                                    role.setClassPK(role.getRoleId());
                                    role.setName(roleName);
                                    role.setDescription(roleName);
                                    role.setTitle(roleName);
                                    role.setType(RoleConstants.TYPE_REGULAR);
                                    role=RoleLocalServiceUtil.addRole(role);

Note: When we observe above code we need not RoleImpl class anywhere.
We just use XXXLocalServiceUtil.create(counterInceremnt); so it will give object after that we can set the data to the object and we can use XXXLocalServiceUtil.addXXX(xxxObject);
We can use counter increment in two ways.
1.      CounterLocalServiceUtil.increment();
2.      CounterLocalServiceUtil.increment(modelClassName);
Example:
Long id=CounterLocalServiceUtil.increment(com.meera.test.Employeee.class);
For more methods you can see CounterLocalServiceUtil java class.

Note: Whenever we are using portal related insertion like user, role, and organization from our plugin portlet please see the increment how they written in liferay means they used global increment or object specific increment. Mostly they may use global increment for portal entity insertions.
CounterLocalServiceUtil.increment();//Global increment
CounterLocalServiceUtil.increment(modelClassName);//entity specific increment

When we use counter increment we need not to use XXXImpl class to create object or we need not load ant XXXImpl class from other class path using either PortalCalssLoader or Portlet Bean locator.
XXXLocalServiceUtil.create(primaryKey) give object and we can set the data to object and we can XXXLocalServiceUtil.addXXx()  to add object to the table.
Note:
If we want use other port let service or portal services service jar file should available in tomcat global class path i.e. tomcat/lib/ext.




Friday, February 22, 2013

Liferay Permission System Detailed Concept


Define Permission System Detailed Concept

In the liferay we have permission system so that we can define permissions system for the role.
We already know how to define permission system from Admin screens in liferay.
In the permission system there is involved two tables
1.      Resource Action table
2.      Resource Permissions table

Resource Action table:
 In this table we are maintaining action for each resource. Actions are divided into two types
1.      Portlet Resource
2.      Model Resource
Portlet Resource:
Portlets resource action like what is the permission system defines for the portlet like VIEW, EDIT, CONFIGURATION and ADD TO PAGE.
Example  Entries for Prtlets Resource.


If we observe name column we can see the portletId, in the name column we can see relevant actions for portlet. Like this way portlets resource action are defined in the table.
Model Resource:
Model resource action is nothing but if we want do some functionality in the portlet then we will define actions for model resource.
Example:
If we take Organization portlet we have different action  like MANAGE TEAMS,MANAGE USERS like this . If the portlet inside having any functionality then we need to defined action for model resource.


Note:
Here model resource name model java class name. That is why we can observe model resource name for organization is com.liferay.portal.model.Organization.
How permission system working:
In the liferay 6.x they have introduced algorithm called Advanced Permission Algorithm 6.
When we use this algorithm then this entire functionality uses only two tables are Resource Action and Resource Permission.

This permission system working based on bitwise operator OR and bitwise value.
If we observe table resource action we can find column bitWiseValue for each action.
For each action we have bitwise value from this permission system is working.
Example:
If take any resource either model resource or portlet resource for each they have their own ActionIds
Example:
Assume Organization model resource called com.liferay.portal.model.Organization
We have different actions like MANGE_USERS, MANAGE_TEAM etc..
This bitwise value is  multiple of 2
APPROVE_PROPOSAL------2
ASSIGN_MEMBERS-------4
ASSIGN_REVIEWER------8
ASSIGN_USER_ROLES----16
If we add new action then the value id multiple of 2
Assume if we give permission for role on organization we have to calculate the total bitwise value.
So setting permissions is simply a matter of OR ing all the action masks together.

i.e.

Permissions = VIEW | ADD_MESSAGE | SUBSCRIBE;


For all you java programmers not up to speed with low level bitwise operations | is bitwise OR and & is bitwise AND.

Checking for VIEW permission is as simple as
if ((permissions & 1) == 1) {
      Has permission
 }


Checking for any permission is simply

1if ((permissions & ACTION) == ACTION) {
          Has permission
 }

Example:
Take one portlet  MyPortlet I want give ADD TO PAGE ,VIEW and CONFURATION permissions for this portlet on particular role
Then apply bitwise operator like this
VIEW                         --------------------1
CONFIGURATION ---------------------4
ADD TO PAGE    -----------------------2

1-------------      001             apply OR operation
2------------       010            apply OR operation
4---------           100             apply OR operation
                         111---------------7   is value
If want get ADD TO PAGE, VIEW AND CONFUGURATION role should have the value 7 then we will get all permissions.

Where we store all these details?
We have one table call Resource Permissions there we store all these information.

Here all the permissions stored with respect to role. And if observe last column actionIds there total bitwise value is stored.
Like in the above example we will store value 7.
We will see the each column in the resource permissions.
resourcePermissionId:  this is just a primary key.
CompanyId: this represent for which  liferay instance we are defining permission system.
Name: we already see this in resource action table name column this also same. This represents whether this is Model Resource or portlet Resource.
PrimyKey: this very important value this value vary bases on model resource and portlet resrce.
Scope: scope represent effect of this permission mean only for organization or community or individual resource.
PRIMKEY_DNE                                                             = -1;
SCOPE_INDIVIDUAL                                                     =4;
SCOPE_GROUP                                                               =2;
SCOPE_GROUP_TEMPLATE /OORGANIZATION = 3;
SCOPE_COMPANY                                                          =1;
actionIds:
 This is total bitwise value of all actions that is assigned to the role on resource.
How prime key change resource to resource?

CASE: 1 when we set permissions to layout:
Example if we set some permission to page means this page view by this role like that. Then entry following like this



Observe above primKey value. When we set permission to the page then primKey value is plid.
CASE: 2 when we set permission to the portlet and that  portlet in particular page.

Observe prime key this is combination string
plid_LAYOUT_PortletId
56 is web content display portlet name. This is instanciable portlet that’s why portlet id contain _INSTANCE_aec   like that.
Here scope is individual means we are setting permission for web content display portlet that is in particular page.
CASE: 3 Set permission system in the port let functionality or we can say for model resource.
Example we taka organization portlet we have many activities like manage users, manage teams and manage pages like this


Observe prime key this is 0. Means when we apply permissions for model resource then it will become 0.
Total bitwise for all action is 65535.
Here scope 3 means organizations level.
Important points:
When we use define permission system if define any permission there
The following are the entries
Regular Role:
Primekey=CompanyId,
Scope=1
Organization Role:
PrimKey=0
Scope=3
When we set permission system for individual portlets or articles
Regular Role:
PrimKey=layout_portletID
Scope=4
Organization Role:
PrimKey=layout_portletID
Scope=4
Journal articles
Regular Role/Organization Role:
PrimKey=Article Resource key (available in journalarticleresource table)
Scope=4
Set Permission system for Layouts/pages
Regular Role:
PrimKey= plid
Scope=4
Organization Role:
PrimKey=plid
Scope=4




Concept for Bitwise Permission System goes through the following link.
Implementing Custom permission system for our own portlets goes through the following link.

Recent Posts

Recent Posts Widget

Popular Posts