Thursday, August 13, 2009

Customizing url and changing default landing after login

Introduction:
Currently when ever we are login in we are getting path like

http://localhost:8080/web/guest/home

First we will try to simplify this url

eg:
Suppose I made a community with name Fquad then I want to access
it as http://fquad.com rather than http://fquad.com:8080/web/guest/home

How to achieve it??

we need to follow few simple steps for it

Step 1: Go to c:\WINDoWS\System32\drivers\etc or search for host file

in host file add a line like this

127.0.0.1 www.fquad.com

Step 2: Stop the sever and start it and try to access the application with

url http://fquad:8080/web/guest/home , if working fine go to step 3

Step 3: Now create a community with name Fquad.

Step 4: Create 2 pages in Fquad community 1 public page and other private page.

Step 5: Go to manage pages of Fquad community click the setting tab and then click

the Virtual Host tab then you can see Public Virtual Host text field fill it with

www.fquad.com

Step 6: Now try to access application using url http://fquad:8080/

Step 7: To remove :8080 is simple just change connector port in tomcat/conf/server.xml

search for line Connector port="8080" protocol="HTTP/1.1 and change 8080 to 80 thats it.

Sometimes it is required to land directly to private page of a community then in portal-ext.properties

add 1 line , portal-ext.properties exist under tomcat\webapps\ROOT\WEB-INF\classes if not there create it

default.landing.page.path=/group/fquad

Wednesday, August 12, 2009

Give a try to ContactListener in ext environment

step 1. create a class that extends ModelListener .

eg. If want to extend ContactListener and want to do some action onAfterCreate then

public class CustomContactListener extends ContactListener {
public void onAfterCreate(BaseModel model) throws ModelListenerException {
try {
super.onAfterCreate(model)

//your code
System.out.println("Conatct info after update: " + contact.getContactId() + ":" + contact.getFirstName());
}
catch (Exception e) {
throw new ModelListenerException(e);
}
}
}

step 2. Make an entry in portal-ext.properties

value.object.listener.com.liferay.portal.model.Contact=\
com.liferay.portal.model.ContactListener,\
com.liferay.portal.model.CustomContactListener

do ant deploy and update contact table entry and check in console whether updated data is coming or not.

Tuesday, August 11, 2009

Using Json in Liferay

Introduction:
We have 2 Service tiers..

The Local tier - this tier has no permission checks and is simply a clean cut implementation of the business logic of the service.

i.e. you set local-service="true" when you created the entity.

e.g.
UserLocalServiceUtil.updateActive(long userId, boolean active) (re, no permission check is made, use with caution).

The Remote Tier - this tier is designed to include your permission checks and finally calls the underlying Local service to actually perform the service operation. This tier is also the one on which ALL web-service methods are generated. All the HTTP, JSON, and SOAP are backed by the methods found in this tier.

i.e. you set remote-service="true" when you created the entity AND you implemented methods in [Entity]ServiceImpl.java rather than leaving it blank.

e.g.
UserServiceImpl.updateActive(long userId, boolean active) (i.e., makes a permission check internally):
public User updateActive(long userId, boolean active)
throws PortalException, SystemException {

if ((getUserId() == userId) && !active) {
throw new RequiredUserException();
}

UserPermissionUtil.check(
getPermissionChecker(), userId, ActionKeys.DELETE);

return userLocalService.updateActive(userId, active);
}


SO, all webservice methods which call their implementation of "updateActive" on the UserService are backed by this method. Also, this means that the caller must be authenticated in order to be tested for permissions... otherwise the call will fail. So, you need to be authenticated, AND you need to have permission.

How does a JSON method get authenticated when it's called through and ajax call? All our JSON services are passed through an AutoLogin Filter which handles the authentication based on authorization cookies.

Abstraction for using Json in Liferay:

Step 1. generate service layer for your entity with remote-service="true"
eg.


Step 2. In [entity]ServiceImpl write a method which you want to call using Json.
eg. updateName(userName) throws Exception{//your code goes here}.

Step 3. Do ant build-service -Dservice.file=your_service_xml_path from ext/ext-impl

Step 4. Do ant deploy from ext.

Step 5. Check file ext-web/docroot/html/js/liferay/ext_service_unpacked.js is updated with your method or not.

Note: In liferay version 5.2.x onwards this file is "ext_service.js".

Step 6. Include js like this in file in which you want to call json.

Step 7. Call method like this
eg:
function updateName(userName) {
var params = {
userName: userName
};
Liferay.Service.your_entity_namespace.entity_name.updateName(params);
}

Monday, August 10, 2009

Creating simple struts Portlet in liferay in 5 min

In struts portlet as compare to Jsp portlet we need to add 2 extra xml files

1.struts-config.xml
2.tiles-defs.xml

We have to modify follwing xml files.

These xml files are located under "ext/ext-web/docroot/WEB-INF"

2.1. portlet-ext.xml
--------------------

<portlet>
<portlet-name>Sample</portlet-name>
<display-name>Sample Struts Portlet</display-name>
<portlet-class>com.liferay.portlet.StrutsPortlet</portlet-class>
<init-param>
<name>view-action</name>
<value>/ext/sample/view</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>

2.2. liferay-portlet-ext.xml
----------------------------

<portlet>
<portlet-name>Sample</portlet-name>
<struts-path>ext/sample</struts-path>
<use-default-template>false</use-default-template>
</portlet>

2.3. liferay-display.xml
------------------------

add

<portlet id="Sample" />

inside

<category name="category.sample">

2.4. struts-config.xml
----------------------

<action path="/ext/sample/view" forward="portlet.ext.sample.view" />

2.5. tiles-defs.xml
-------------------

<definition name="portlet.ext.sample" extends="portlet" />
<definition name="portlet.ext.sample.view" extends="portlet.ext.sample">
<put name="portlet_content" value="/portlet/ext/sample/view.jsp" />
</definition>


(create the following two jsp files under "/ext/ext-web/docroot/html/portlet/ext/sample")

2.6. init.jsp
-------------

<%@ include file="/html/portlet/init.jsp" %>

2.7. view.jsp
-------------

<%@ include file="/html/portlet/ext/sample/init.jsp" %>
Simple Struts Portlet!

2.8 Language-ext.properties
---------------------------

Add a new entry in the above file,

javax.portlet.title.Sample=Sample Struts Portlet

creating simple jsp portlet in Liferay

For creating simple Jsp portlet in Liferay we have to modify following files.

Following xml files are located under "ext/ext-web/docroot/WEB-INF"

1.1. portlet-ext.xml
--------------------
Add entry for your portlet

<portlet>
<portlet-name>Sample</portlet-name>
<display-name>JSP Portlet Introduction</display-name>
<portlet-class>com.liferay.util.bridges.jsp.JSPPortlet</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/html/portlet/ext/jsp_portlet/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
</supports>
<resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>

1.2. liferay-portlet-ext.xml
----------------------------
Make an entry for your portlet.

<portlet>
<portlet-name>Sample</portlet-name>
</portlet>

1.3. liferay-display.xml
------------------------
Make an entry for the category in which you want to display your portlet.

<category name="category.sample">
<portlet id="Sample" />
</category>

1.4. view.jsp
-------------

create this file under "ext/ext-web/docroot/html/portlet/ext/jsp_portlet"
and put some contents


do "ant deploy" from "ext-web"

1.5 Language-ext.properties
---------------------------

(under /ext/ext-impl/src/content)

Add two new entries in the above file, this helps us in I18N

javax.portlet.title.Sample=Sample JSP Portlet

category.sample=Example

do "ant deploy" from "ext-impl"