Total Pageviews

Monday, November 29, 2010

Difference between a Java interface and a Java abstract class?

Hello every one,
There are few points that I think distinguishes Java interface from a Java Abstract Class. have a look -
1. Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
2. Variables declared in a Java interface is by default final. A Java abstract class may contain non-final variables.
3. Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
4. Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
5. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
6. A Java class can implement multiple interfaces but it can extend only one abstract class.
7. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
8. In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

Wednesday, November 24, 2010

User Registration (create account) Portlet customization

The registration or create account portlet in Liferay portal can be customized to add/remove some of its field without any coding.

This blog explain some of these customization properties which developers can add them in portal-ext to change the behavior of user registration portlet.

users.screen.name.always.autogenerate - If you do not want your users to provide screen name, you can use this property hide screen name text field. The default value is false. If it is changed to true, the screen name field will not appear on the registration page and screen name will be auto generated by the portal.

login.create.account.allow.custom.password - The default registration portlet in Liferay doesn't provide option of password selection to user. If this property is set to true, the registration screen will show password fields to user and user can provide password of his/her choice.

field.enable.com.liferay.portal.model.Contact.male - If your site is not interested in knowing the gender of the user, you can remove gender selection from the page by setting false value of this property.

field.enable.com.liferay.portal.model.Contact.birthday - Last configuration is for birth date field. You can remove bith date selection by setting false value to this property.
captcha.check.portal.create_account - This propery can be used to tell whether or not to use captcha checks for the account creation.

users.email.address.required - Set this to false if you want to be able to create users without an email address. An email address will be automatically assigned to a user based on the property "users.email.address.auto.suffix". If user is entering his email id, the system will not generate a new one. Usually, setting this property to false will make sense when you don't want user to use email address for login.

Sunday, November 21, 2010

Security Questions customization

I've seen various time people asking for disabling security question and creating custom questions. That's why I am writing this post. However its quite easy.


For disabling the queries, we have to set the following properties false in portal-ext.properties
users.reminder.queries.enabled=false

When you press the drop down list box of security questions, a entry is shown -"write my own question", that can be disabled by setting the following property false in portal-ext.properties as follows:
users.reminder.queries.custom.question.enabled=false

However, if you wish to display your own custom question in the drop down list box, that's a bit tricky, but not too difficult at all. One important thing to note about this - they are not stored in an any database table, but in language_en.properties (and in other locales) file, and whose key is provided in the portal.properties file's following property:
users.reminder.queries.questions= <list of question keys>
Now, for creating your own question, you have to create a (or more) question(s) with unique key(s), and list those keys as the value os above mentioned property in portal-ext.properties file.

Don't forget to add your custom language-ext.properties file in you stack in ext-impl/src/content, so that it can also be overridden. In the same way, you can create property file for all other supported locales too.

see the following example:

portal-ext.properties
users.reminder.queries.questions= question-1,question-2

Language-ext.properties
question-1=this is the first question
question-2=this is the second question
question-3=this is the third question


Disabling Terms and Condition
set the following property false in the portal-ext.properties for disabling terms and condition:
terms.of.use.required=false

Wednesday, November 17, 2010

Custom Attributes in Liferay


Follow the steps for making custom attributes-
Go to Control Panel -> User - > Custom Attribute -> Add Custom Attribute

An Attribute is a key and type of the field (once saved) cannot be changed at all...

Accessing Custom Attribute Pragmatically
ThemeDisplay td=(ThemeDisplay)request.getAttribute(Web_Keys.THEME_DISPLAY);
User u=td.getUser();
System.out.println(u.getExpendoBridge().getAttribute("CustomAttributeName"));


Setting Attribute Pragmatically
ThemeDisplay td=(ThemeDisplay)request.getAttribute(Web_Keys.THEME_DISPLAY);
User u=td.getUser();
System.out.println(u.getExpendoBridge().setAttribute("CustomAttributeName","Value"));


Adding custom Attribute form element at Sign In Portlet

write the following code in /root/html/portlet/login/create_account.jsp
<liferay ui:custom-attribute-list
                  className="com.liferay.portal.model.user"
                  editable="<%=true%>"
                  label="<%=true%>">

but this will not visible to due to permission. So, login as admin and provide permission to guest to see and modify custom field through control panel.

Sunday, November 14, 2010

Redirect to another Portlet

This is a very short post, but big enough in its work.
The following code is capable of redirecting to some other portlet.


ThemeDisplay themeDisplay = (ThemeDisplay)liferayPortletRequest.getAttribute(WebKeys.THEME_DISPLAY);
String url =  themeDisplay.getPortalURL() + themeDisplay.getPathMain() + "/message_boards/find_message?messageId=" + _message.getMessageId();

Wednesday, November 3, 2010

Portlet Definition in EXT Environment

The file liferay-portlet.xml may be placed in the WEB-INF directory of any portlet application to configure Liferay Portal specific features. Following is an example of what this file may look like:

<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 4.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_4_2_0.dtd">

<liferay-portlet-app>
 <portlet>
  <portlet-name>1</portlet-name>
  <struts-path>mail</struts-path>
  <preferences-unique-per-layout>false</preferences-unique-per-layout>
  <preferences-owned-by-group>false</preferences-owned-by-group>
  <use-default-template>false</use-default-template>
  <restore-current-view>false</restore-current-view>
  <maximize-edit>true</maximize-edit>
  <private-request-attributes>false</private-request-attributes>
  <render-weight>0</render-weight>
 </portlet>
...
</liferay-portlet-app>

The portlet-name element must be equal to the portlet name specified in the portlet.xml file. Here is a complete list of all the available options.

struts-path
This option is only useful when using the Liferay StrutsPortlet framework. Suppose the struts-path value is "mail". This tells the portal that all requests with the path mail/* are considered part of this portlet's scope. Users who request paths that match mail/* will only be granted access if they also have access to this portlet. This is true for both portlet requests and regular servlet requests.
configuration-path
The configuration-path value is a Struts path that allows users to configure the portlet at runtime. The Struts path must reference a class that extends com.liferay.portal.struts.PortletAction.
indexer-class
The indexer-class value must be a class that implements com.liferay.util.lucene.Indexer and is called to create or update a search index for the portlet.
scheduler-class
The scheduler-class value must be a class that implements com.liferay.portal.job.Scheduler and is called to schedule Quartz jobs for this portlet.
portlet-url-class
The portlet-url-class value must be a class that extends com.liferay.portlet.PortletURLImplWrapper. Set this class to override the default portlet URL implementation.
portlet-url-class
The portlet-url-class value must be a class that implements com.liferay.portal.servlet.FriendlyURLPortletPlugin. Use this if content inside a portlet needs to have a friendly URL. See the Message Boards portlet source code for an example of its uses.
portlet-data-handler-class
The portlet-data-handler-class value must be a class that implements com.liferay.portal.kernel.lar.PortletDataHandlerand is called when archiving tasks are run.
smtp-message-listener-class
The smtp-message-listener-class value must be a class that implementscom.liferay.portal.kernel.smtp.MessageListener and is called when processing emails.
preferences-company-wide
Set the preferences-company-wide value to true if the preferences for the portlet are across the entire company. Setting this value to true means the value for preferences-unique-per-layout and preferences-owned-by-group are not used. The default value is false. For example, an administrator could set the preferences to an Announcements portlet that would save a message in the portlet's preferences. This message would then be used across all pages for that company. The portlet must not be instanceable because instanceable portlets have uniquely generated portlet ids. The default behavior of the bundled Announcements portlet sets the instanceable value to true so that normal users cannot create company wide messages. A future release would include permissions for the edit mode versus the view mode which would allow an administrator to set the message while users would just view the message.
preferences-unique-per-layout
Set the preferences-unique-per-layout value to true if the preferences for the portlet are unique for each page. If set to false, the preferences for the portlet are shared across all pages. The default value is true. The preferences-unique-per-layout element is used in combination with the preferences-owned-by-group element. See the comments for the preferences-owned-by-group element for more information.
preferences-owned-by-group
Set the preferences-owned-by-group value to true if the preferences for the portlet are owned by the group when the portlet is shown in a group page. If set to false, the preferences are owned by the user at all times. The default value is true. Suppose the Stocks portlet has preferences-unique-per-layout set to true and preferences-owned-by-group set to false. Users can set a different list of stocks for every personal page. Users can set a different list of stocks for every community page. Suppose the Stocks portlet has preferences-unique-per-layout set to false and preferences-owned-by-group set to false. Users can set one list of stocks to be shared across all personal pages. Users can set one list of stocks to be shared across a community's set of pages. Suppose the Stocks portlet has preferences-unique-per-layout set to true and preferences-owned-by-group set to true. Users can set a different list of stocks for every personal page. Administrators set the portlet preferences for users in a community page. Administrators can set a different list of stocks for every community page that are then shared by all users within a community. Suppose the Stocks portlet has preferences-unique-per-layout set to false and preferences-owned-by-group set to true. Users can set one list of stocks to be shared across all personal pages. Administrators set the portlet preferences for users in a community page. Administrators can set one list of stocks to be shared by all users across a community's set of pages.
use-default-template
Set the use-default-template value to true if the portlet uses the default template to decorate and wrap content. Setting this to false allows the developer to own and maintain the portlet's entire outputted content. The default value is true. The most common use of this is if you want the portlet to look different from the other portlets or if you want the portlet to not have borders around the outputted content.
show-portlet-access-denied
Set the show-portlet-access-denied value to true if users are shown the portlet with an access denied message if they do not have access to the portlet. If set to false, users are never shown the portlet if they do not have access to the portlet. The default value is set in portal.properties.
show-portlet-inactive
Set the show-portlet-inactive value to true if users are shown the portlet with an inactive message if the portlet is inactive. If set to false, users are never shown the portlet if the portlet is inactive. The default value is set in portal.properties.
action-url-redirect
Set the action-url-redirect value to true if an action URL for this portlet should cause an auto redirect. This helps prevent double submits. The default value is false.
restore-current-view
Set the restore-current-view value to true if the portlet restores to the current view when toggling between maximized and normal states. If set to false, the portlet will reset the current view when toggling between maximized and normal states. The default value is true.
maximize-edit
Set the maximize-edit value to true if the portlet goes into the maximized state when the user goes into the edit mode. This only affects the default portal icons and not what may be programmatically set by the portlet developer. The default value is false.
maximize-help
Set the maximize-help value to true if the portlet goes into the maximized state when the user goes into the edit mode. This only affects the default portal icons and not what may be programmatically set by the portlet developer. The default value is false.
maximize-print
Set the maximize-print value to true if the portlet goes into the maximized state when the user goes into the edit mode. This only affects the default portal icons and not what may be programmatically set by the portlet developer. The default value is false.
layout-cacheable
Set the layout-cacheable flag to true if the data contained in this portlet will never change unless the layout or portlet entry is changed.
instanceable
Set the instanceable value to true if the portlet can appear multiple times on a page. If set to false, the portlet can only appear once on a page. The default value is false.
private-request-attributes
Set the private-request-attributes value to true if the portlet does not share request attributes with any other portlet. The default value is true.
render-weight
The default value of render-weight is 1. If set to a value less than 1, the portlet is rendered in parallel. If set to a value of 1 or greater, then the portlet is rendered serially. Portlets with a greater render weight have greater priority and will be rendered before portlets with a lower render weight. If the ajaxable value is set to false, then render-weight is always set to 1if it is set to a value less than 1. This means ajaxable can override render-weight if ajaxable is set to false.
ajaxable
The default value of ajaxable is true. If set to false, then this portlet can never be displayed via Ajax.
add-default-resource
If the add-default-resource value is set to false, and the portlet does not belong to the page but has been dynamically added, then the user will see that he does not have permissions to view the portlet. If the add-default-resource value is set to true, the default portlet resources and permissions are added to the page. The user can then view the portlet. Most portlets are harmless and can benefit from this flexibility. However, to prevent security loop holes, the default value is false.
system
Set the system value to true if the portlet is a system portlet that a user cannot manually add to their page. The default value is false.
active
Set the active value to true if the portlet is active and available to users. If set to false, the portlet will not be active or available to users. The default value is true. This value can be changed at runtime via the Admin portlet.
include
Set the include value to true to if the portlet is available to the portal. If set to false, the portlet is not available to the portal. The default value is true. Portlets that are not included as part of the portal are never available to the user to be made active or inactive. As far the user knows, the portlets do not even exist in the system. This allows the Liferay developers to bundle a lot of portlets in one core package, and yet allow custom deployments to turn on or off individual portlets or sets of portlets. This follows the Siebel and Microsoft model of bundling everything in one core package, but using XML configuration or registry settings to turn on and off features or sets of features. We do not recommend that custom deployers modify the core source by removing specific portlets because this prevents an easy upgrade process in the future. The best way to turn on and off portlets is to set the include element. The advantage of this way of doing things is that it becomes very easy to deploy Liferay. All features are available in one package. The disadvantage is that by not utilizing all of the portlets, you are wasting disk space and may even take a small but static memory footprint. However, we feel that the extra disk space and memory usage is a cheap price to pay in order to provide an easy installation and upgrade path.
In addition to specifying the above parameters specific to each portlet, the liferay-portlet.xml file can also be used to specify role mappings and custom user attributes global to the whole portlet application. Here is an example:

<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 4.2.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_4_2_0.dtd">

<liferay-portlet-app>
...
 <role-mapper>
  <role-name>user</role-name>
  <role-link>User</role-link>
 </role-mapper>
 <custom-user-attribute>
  <name>user.name.random</name>
  <custom-class>com.liferay.portlet.CustomUserAttributes</custom-class>
 </custom-user-attribute>
</liferay-portlet-app>

Here is a more detailed description of these elements:

role-mapper
The role-mapper contains two names specified by role-name and role-link. The role-name value must be a role specified in portlet.xml. The role-link value must be the name of a Liferay role that exists in the database. The role-mapper element pairs up these two values to map roles from portlet.xml to roles in the Liferay database. This is needed because Liferay roles may contain spaces whereas roles in portlet.xml cannot contain spaces. This also adds extra flexibility where the portlet vendor does not need to have any knowledge about Liferay's roles.
custom-user-attribute
The custom-user-attribute contains a list of names that are retrieved using a custom class that extendscom.liferay.portlet.CustomUserAttributes. For a usage example, download the sample hot deployable portlet WAR named test.war. Look for the classcom.liferay.portlet.teststruts.TestStrutsUserAttributes to see how it associates the custom user attribute "user.name.test" with the value "Test Name". This class could be modified to read custom user attributes from another datasource that may be a database, a LDAP server, or a web service.

Monday, November 1, 2010

Modifying E-mail Notification

There are two methods to achieve it...
1. You can change the e mail notification by modifying the template at following breadcrumb :

Control Panel >> Settings >> E-mail Notification

This will change the default template in the following path:


com/liferay/portlet/admin/dependencies/


This is the easiest way to achieve. However, there is one more way which is more recommended.




2. In EXT environment, you have freedom of writing your own template and set it in portal-ext.properties file. The default one can be seen by following the mentioned path. However same variable(string literals) can be used in custom templates too.



#
# Configure email notification settings.
#
admin.email.from.name=Apoorva Prakash
admin.email.from.address=apoorvashubhi@gmail.com

admin.email.user.added.enabled=true
admin.email.user.added.subject=com/liferay/portlet/admin/dependencies/CUSTOM_email_user_added_subject.tmpl
admin.email.user.added.body=com/liferay/portlet/admin/dependencies/CUSTOM_email_user_added_body.tmpl

admin.email.password.sent.enabled=true
admin.email.password.sent.subject=com/liferay/portlet/admin/dependencies/CUSTOM_email_password_sent_subject.tmpl
admin.email.password.sent.body=com/liferay/portlet/admin/dependencies/CUSTOMemail_password_sent_body.tmpl


In the above mentioned code, I have created a new template, stored in the same path with the different name.
Available Values:
In this template few string tokens are available, which can be used.

New string tokens can be create by overriding "liferay-portal-src-5.2.3\portal-impl\src\com\liferay\portal\service\impl\UserLocalServiceImpl.java" class.

AN IMPORTANT NOTE:

When you start the the server first time, it will pickup the custom template you've created. As soon as you make some modification from Rich Text Editor window, and save it, the modified template is saved in the database in portletpreferences table. Next time, the template is picked up from the database instead custom template. Basically, liferay looks for template from the database first, if it exists there then populates the template from database, else reads the path from portal-ext.properties and loads the custom template. Database is always given priority over properties file.

Showing error and success messages in Liferay

In JSP:

<liferay-ui:error key="ErrorKey" message="ErrorMessage" />
<liferay-ui:success key="SuccessKey" message="Success Message" />

In Action Class:
SessionErrors.add(req, "ErrorKey");
SessionMessages.add(req, "SuccessKey");