Wednesday 4 September 2013

Get specific column (more than 1) using Dynamic Query Liferay - ProjectionList

Introduction : I had been struggling to execute this kind of query using the Dynamic Query

Select id, name from group where id=2;
So, thought may be useful to anyone looking for it.

Solution : Use ProjectionList

Map<String,Integer> resultsMap = new HashMap< String,Integer>();
ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
DynamicQuery dynaQuery = DynamicQueryFactoryUtil
.forClass(Group.class);
projectionList.add(ProjectionFactoryUtil.property("id"));
projectionList.add(ProjectionFactoryUtil.property("name"));
dynaQuery.setProjection(projectionList);
dynaQuery.add(RestrictionsFactoryUtil.eq(
"id", 2));
try {
List<Object[]> ids = (GroupLocalServiceUtil
.dynamicQuery(dynaQuery));
 for (Object[] o : ids) {
   resultsMap.put((String) o[0], ((Long) o[1]).intValue());
 }
} catch (SystemException e) {
e.printStackTrace();
}

Wednesday 21 August 2013

Set JDK in Ubuntu (LINUX)

Run the following commands in your terminal if you have the tar.gz

1. tar -xvf jdk-7u21-linux-i586.tar.gz

2. sudo mv ./jdk1.7.0_21 /usr/lib/jvm/jdk1.7.0_21

3. sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0_21/bin/java" 1

4. sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0_21/bin/javac" 1

5. sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0_21/bin/javaws" 1

6. sudo chmod a+x /usr/bin/java

7. sudo chmod a+x /usr/bin/javac

8. sudo chmod a+x /usr/bin/javaws

9. sudo chown -R root:root /usr/lib/jvm/jdk1.7.0_21

10.sudo update-alternatives --config java

11.export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_21/

12.export PATH=$PATH:/usr/lib/jvm/jdk1.7.0_21/bin

Sunday 11 August 2013

While using Dynamic Query getting "QueryException: could not resolve property"

Here is the solution.......

DynamicQuery dynaQuery = DynamicQueryFactoryUtil
                .forClass(User_Customer.class);
dynaQuery.setProjection(ProjectionFactoryUtil
.property("primaryKey.id_customer");

dynaQuery.add(RestrictionsFactoryUtil.eq("primaryKey
.id_userId", userId));

List<Integer> customerIds = new ArrayList<Integer>();
customerIds = (List<Integer>)user_CustomerPersistence.findWithDynamicQuery(dynaQuery);


The case is...you have a composite key and whenever you try using it like this
dynaQuery.setProjection(ProjectionFactoryUtil.property("id_customer");

You are getting an exception, something like this:-

06:34:05,450 ERROR [http-bio-8080-exec-88][BasePersistenceImpl:186] 
Caught unexpected exception org.hibernate.QueryException
com.liferay.portal.kernel.exception.SystemException: 
org.hibernate.QueryException: could not resolve property: userId of:
com.test.model.impl.User_CustomerImpl
 
 
Use "primaryKey.columnName" instead of "columnName"

Tuesday 2 July 2013

Liferay Maven Hook(Custom JSP)

Introduction:

Starting with maven brings another pivotal feature implementation i.e. Hooks. Let us do with the easier Login-hook and change the login.jsp

Prerequisites:-
1. Download maven-support.zip from here.
 In your command prompt/Terminal window, type mvn archetype:generate
2. Install maven on eclipse http://m2eclipse.sonatype.org/sites/m2e 


Steps:-

1. Create a new Maven Project
 
2. Click Next

3.Select liferay-hook-archetype for creating a portlet.

4. Set GroupId(your package), artifact Id(your hook name), version(1.1) and package
 
 5. Following is the project structure

6. Inside /src/main/webapp/WEB-INF/liferay-hook.xml add the following:-

<hook>
<custom-jsp-dir>/META-INF/custom_jsps</custom-jsp-dir>
</hook>


7. Copy the login.jsp following the folder structure, and make the changes in login.jsp as per your requirement.
 
8. In your pom.xml replace
${liferay.version} with your liferay version, I am using 6.1.1 so I replaced with the same. So, finally pom.xml looks something like the following

<?xml version="1.0"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>me.pd.test.portlet</groupId>
    <artifactId>mylogin</artifactId>
    <packaging>war</packaging>
    <name>mylogin Hook</name>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>com.liferay.maven.plugins</groupId>
                <artifactId>liferay-maven-plugin</artifactId>
                <version>${liferay.maven.plugin.version}</version>
                <configuration>
                    <autoDeployDir>\home\priyanka\pfiles\bundles\deploy</autoDeployDir>
                    <appServerDeployDir>\home\priyanka\pfiles\bundles\tomcat-7.0.27\webapps</appServerDeployDir>
                    <appServerLibGlobalDir>\home\priyanka\pfiles\bundles\tomcat-7.0.27\lib\ext</appServerLibGlobalDir>
                    <appServerPortalDir>\home\priyanka\pfiles\bundles\tomcat-7.0.27\webapps\ROOT</appServerPortalDir>

                    <liferayVersion>6.1.1</liferayVersion>

                    <pluginType>hook</pluginType>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.liferay.portal</groupId>
            <artifactId>portal-service</artifactId>
            <version>6.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.liferay.portal</groupId>
            <artifactId>util-java</artifactId>
            <version>6.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.portlet</groupId>
            <artifactId>portlet-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>


9. Go to your command prompt or Terminal(Linux)
Type:-
 mvn clean install liferay:deploy

Your hook is finally deployed successfully :)