Friday 28 August 2015

Using the "liferay-ui:input-date" tag

Just a simple example to use the liferay-ui:input-date tag in our forms.

1. Firtly we need to import the required tag.
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

2. If you need to show the default selected date as Today. We need a Calendar object to get these values for our form.
<%
// To set today's date as default for the calendar.
Calendar today = Calendar.getInstance();
%>

3. Now for the actual form. The Liferay UI Date element is created here with the necessary parameters.
<label>
    Date of Birth
    <liferay-ui:input-date name="dobDate" 
        dayValue="<%= today.get(Calendar.DAY_OF_MONTH) %>" dayParam="dobDay"
        monthValue="<%= today.get(Calendar.MONTH) %>" monthParam="dobMonth"
        yearValue="<%= today.get(Calendar.YEAR) %>" yearParam="dobYear" />
</label>
The dayValue, monthValue and yearValue attributes are displayed to the user as soon as the form is loaded.
dayParam, monthParam and yearParam are the attributes representing the data when it is sent to the server side.
I have also set the name attribute which will send the selected date in MM/dd/yyyy format to the server.

4. Now for the server side, to retrieve the data from the user. This is the first way to fetch the data, below I have provided another way to do the same.
// Method 1
// We can fetch the date either by separate Day, Month and Year parameter.
int dobDay = ParamUtil.getInteger(actionRequest, "dobDay");
int dobMonth = ParamUtil.getInteger(actionRequest, "dobMonth");
int dobYear = ParamUtil.getInteger(actionRequest, "dobYear");

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, dobDay);
calendar.set(Calendar.MONTH, dobMonth);
calendar.set(Calendar.YEAR, dobYear);

System.out.println("Method 1: " + calendar.getTime());
In our Portlet class, the dobDay, dobMonth and dobYear will provide the Day, Month and Year respectively. We can set this to a Calendar object and retrieve the Date object with the calendar.getTime() method.

5. Now for method 2.
// Method 2
// With the input String from the selected date.
try {
    String dobDateString = ParamUtil.getString(actionRequest, "dobDate");
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    Date dobDate = sdf.parse(dobDateString);

    System.out.println("Method 2: " + dobDate);
}
catch (ParseException pe) {
    pe.printStackTrace();
}
Here we'll use the name parameter to fetch the same value. Using the SimpleDateFormat to parse the string we can retrieve the Date object.

Please find attached the entire source for the same in the below link.
calendar-portlet.zip

Hope all of this makes sense.

Saturday 8 August 2015

Setting up Liferay mail server with Gmail

To setup Liferay with Gmail to send mail to users. (Eg. When they register, forget their password, etc.) We need to configure the Mail server setting in the Control Panel.

Firstly, Liferay is considered as a Less secure app in Gmail. To allow Liferay to use Gmail as a mail server, we need to enable the feature. Below link is from Google Support that shows how to enable this feature. It is as simple as selecting a radio button.
Gmail Support - Allowing less secure apps to access your account

Now continuing with the configuration. Go to
Control Panel -> Server Administration -> Mail
Here we need to fill details for the Outgoing SMTP Server to send mail from Liferay.

SMTP settings for Gmail can be found in the below page.
Google Apps SMTP settings to send mail from a printer, scanner, or app

Here's how to use the settings in liferay:

Outgoing SMTP Server: smtp.gmail.com
Outgoing Port: 465
Use a Secure Network Connection: Checked
User Name: Your Gmail Mail ID
Password: Your Gmail password

Save the settings and you're done.

Below is a screenshot of my mail settings for reference.

Friday 10 July 2015

jQuery Datepicker project

I had created a simple jQuery datepicker plugin couple of months back, just wanted to share it.

The plugin converts any input box to a 3 drop down date picker. It gets the name of the input element and attaches a Day, Month and Year respectively. You can access the original field to retrieve the date which will, by default, be in the format yyyy/mm/dd.

So, for example, if your input fields name was dob, then, to retrieve the value for the day, you would use dobDay. For the month it would be dobMonth and year with dobYear. For all event handlers, you will need to use the new name that will be generated.

Find the example JSFiddle link below.
JSFiddle - jQuery Datepicker

Datepicker Source File
Complete Source, (jquery.datepicker.js)

Below is the minified JS file for the datepicker. Copy this to a JavaScript file and include it in the page.
Minified file, (jquery.datepicker.min.js)

Supported Parameters:

startDate: JavaScript Date object to restrict the user to a specific Start date.
endDate: JavaScript Date object to restrict the user to a specific End date.
dateFormat: a function takes (day, month, year) as parameters and that returns a string that will be used to fill the hidden input box.


Usage:
HTML
<body>
    <input type="text" name="dob" id="dob" />
</body>

Javascript Code
$(function () {
    $('#dob').datepicker();

    // Example: Create 'onChange' handlers.
    $('#dobMonth').on('change', function () {
        alert($(this).val());
    });

    // Example: Display the String value on changin the date.
    $('#dobDay, #dobMonth, #dobYear').on('change', function () {
        alert($('#dob').val());
    });
});

Thursday 4 June 2015

Modifying Liferay Database Connection


Example of the connection to MySQL Database
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=gautham
jdbc.default.password=passw0rd

The username and Password is common for all the connections and has to be set.
jdbc.default.username=
jdbc.default.password=

Below is the list of Databases that liferay can connect to. Use the appropriate properties for your database.
DB2
jdbc.default.driverClassName=com.ibm.db2.jcc.DB2Driver
jdbc.default.url=jdbc:db2://localhost:50000/lportal:deferPrepares=false;fullyMaterializeInputStreams=true;fullyMaterializeLobData=true;progresssiveLocators=2;progressiveStreaming=2;
Derby
jdbc.default.driverClassName=org.apache.derby.jdbc.EmbeddedDriver
jdbc.default.url=jdbc:derby:lportal
Hypersonic
jdbc.default.driverClassName=org.hsqldb.jdbcDriver
jdbc.default.url=jdbc:hsqldb:${liferay.home}/data/hsql/lportal
Ingres
jdbc.default.driverClassName=com.ingres.jdbc.IngresDriver
jdbc.default.url=jdbc:ingres://localhost:II7/lportal
MySQL
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
Oracle
jdbc.default.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.default.url=jdbc:oracle:thin:@localhost:1521:xe
P6Spy
jdbc.default.driverClassName=com.p6spy.engine.spy.P6SpyDriver
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
PosgreSQL
jdbc.default.driverClassName=org.postgresql.Driver
jdbc.default.url=jdbc:postgresql://localhost:5432/lportal
SQL Server
jdbc.default.driverClassName=net.sourceforge.jtds.jdbc.Driver
jdbc.default.url=jdbc:jtds:sqlserver://localhost/lportal
Sybase
jdbc.default.driverClassName=net.sourceforge.jtds.jdbc.Driver
jdbc.default.url=jdbc:jtds:sybase://localhost:5000/lportal

All the above details are available in the portal.properties file.
Search for "# MySQL" to find MySQL connection properties. Similarly you can search for your own database name to get the properties.

Feel free to post a comment.

Sunday 31 May 2015

Liferay, Reset Admin password.

If you forget or lose your Liferay Admin user and have access to your Liferay Schema / Database, you can execute the below query to reset your password.

Note: You will have to restart the server for the changes to take place.
UPDATE 
  user_ 
SET 
  password_ = '1234',
  passwordEncrypted = '0',
  passwordReset = '1',
  status = 0 
WHERE 
  emailaddress = 'test@liferay.com';
password_ is the new password that you are setting. You can use this to log in with the Admin credentials.
emailaddress should be the email Address of the Admin User.

Hope this worked for you. Do post a comment if you face any issues.