Thursday, January 20, 2011

portlet render url

 1. In JSP
<portlet:renderURL var="sampleURL">
<portlet:param name="jspPage" value="jsp file path"/>
</portlet:renderURL>



 2. In Java
PortletURL sampleURL = renderResponse.createRenderURL();
sampleURL .setParameter("jspPage", " jsp file path ");






Portlets in Action

Wednesday, January 12, 2011

Jasper Report with Liferay integration

JASPER REPORT
===============
1) step-1
~~~~~~~~~~~

package com.ext.portlet.<some>.service.impl;

public class <some>LocalServiceImpl extends <some>LocalServiceBaseImpl {

public void downloadReport() throws SystemException, PortalException{


UserLocalService service = UserLocalServiceUtil.getService();

DetachedCriteria dCriteria = DetachedCriteria.forClass(User.class);

dCriteria.add(Restrictions.eq("active", true));

DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);

List<user> userList = (List)service.dynamicQuery(dynamicQuery);
List<userandcontactinfo> userandContactInfoList = new ArrayList<userandcontactinfo>();
for(int i=0; i<userlist.size(); classloader="" contact);="" contact="" i++){="" user="" userandcontactinfo(user,="" userandcontactinfo="" userandcontactinfolist.add(userandcontactinfo);="" }="">LocalServiceImpl.class.getClassLoader();
JasperDesign jasperDesign;
JasperReport jasperReport;
JasperPrint jasperPrint;
try {

InputStream inputStream = classloader.getResourceAsStream("profiles.jrxml");

jasperDesign = JRXmlLoader.load(inputStream);


jasperReport = JasperCompileManager.compileReport(jasperDesign);
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
userandContactInfoList);
jasperPrint = JasperFillManager.fillReport(
jasperReport, null, ds);

String dest ;
File file = new File("");
dest = file.getAbsolutePath();

JasperExportManager.exportReportToPdfFile(jasperPrint, dest+"/profiles.pdf");

} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

------------------------------------------------------------------------------------

step-2
~~~~~~~~~~

package com.ext.portlet.<some>.action;

import java.io.Serializable;
import java.util.Date;

import com.ext.portlet.<some>.model.UserInfo;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.User;


public class UserandContactInfo implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
public UserandContactInfo(User user, Contact contact){

this.screenName = user.getScreenName();
this.firstName = user.getFirstName();
this.lastName = user.getLastName();
this.dob = user.getBirthday();
this.gender = (contact.getMale()?"Male":"Female");
this.emailAddress = user.getEmailAddress();
}


public String getEmailAddress() {
return emailAddress;
}
public String getFullName() {
return fullName;
}
public String getScreenName() {
return screenName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}

private String emailAddress;
private String fullName;
private String screenName;
private String firstName;
private String lastName;


}

---------------------------------------------------------------------------------------------------------------------

step-3
~~~~~~~~~~~~


package com.ext.portlet.<some>.action;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ext.portlet.<some>.service.<some>LocalServiceUtil;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.servlet.ServletResponseUtil;

public class DownloadPdf extends PortletAction {

public ActionForward strutsExecute(
ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response)
throws Exception {

try {
<some>ServiceUtil.downloadReport();
InputStream is = new BufferedInputStream(
new FileInputStream("profiles.pdf"));

ServletResponseUtil.sendFile(response, "Profiles.pdf", is, "application/pdf");

return null;
}
catch (Exception e) {
PortalUtil.sendError(e, request, response);
return null;
}
}
}

--------------------------------------------------------------------------------------------------------------------

step-4
~~~~~~~~~~
jrxml file should be >>>>>>>>> ext-impl/src/profiles.jrxml
==============

<jasperreport bottommargin="20" columncount="1" columnspacing="0" columnwidth="535" issummarynewpage="false" istitlenewpage="false" leftmargin="30" name="profiles" orientation="Portrait" pageheight="842" pagewidth="595" printorder="Vertical" rightmargin="30" topmargin="20" whennodatatype="NoPages">
<property name="ireport.scriptlethandling" value="0">
<property name="ireport.encoding" value="UTF-8">
<import value="java.util.*">
<import value="net.sf.jasperreports.engine.*">
<import value="net.sf.jasperreports.engine.data.*">


<field class="java.lang.String" name="emailAddress">
<field class="java.lang.String" name="fullName">
<field class="java.lang.String" name="screenName">
<field class="java.lang.String" name="firstName">
<field class="java.lang.String" name="lastName">


<background>
<band height="0" issplitallowed="true">
</band>
</background>
< title > <br /> <band height="12" isSplitAllowed="true" ><br /> </band><br />
<pageheader>
<band height="19" issplitallowed="true">
<textfield evaluationtime="Now" hyperlinktarget="Self" hyperlinktype="None" isblankwhennull="false" isstretchwithoverflow="false">
<reportelement forecolor="red" height="18" key="textField" width="100" x="404" y="1">
<box></box>
<textelement>

</textelement>
<textfieldexpression class="java.lang.Integer"></textfieldexpression>
</reportelement>

</textfield>
</band>
<columnheader>
<band height="0" issplitallowed="true">
</band>
</columnheader>
<detail>
<band height="783" issplitallowed="true">
<textfield evaluationtime="Now" hyperlinktarget="Self" hyperlinktype="None" isblankwhennull="false" isstretchwithoverflow="false">
<reportelement height="18" key="textField" width="205" x="133" y="136">
<box></box>
<textelement>

</textelement>
<textfieldexpression class="java.lang.String"></textfieldexpression>
</reportelement>
<textfield evaluationtime="Now" hyperlinktarget="Self" hyperlinktype="None" isblankwhennull="false" isstretchwithoverflow="false">
<reportelement height="18" key="textField" width="271" x="133" y="37">
<box></box>
<textelement>

</textelement>
<textfieldexpression class="java.lang.String"></textfieldexpression></reportelement>
-------------
-----------
------------
---------
</textfield>
</textfield>
<columnfooter>
<band height="0" issplitallowed="true">
</band>
</columnfooter>
<pagefooter>
<band height="0" issplitallowed="true">
</band>
</pagefooter>
<lastpagefooter>
<band height="16" issplitallowed="true">
</band>
</lastpagefooter>
<summary>
<band height="15" issplitallowed="true">
</band>
</summary>
</band>

--------------------------------------------------------------------------------------------------------------------


step-5
~~~~~~~~~

Required jars


a) jasperreports-3.0.0.jar >>>>>>>>tomcat/webapps/ROOT/WEB-INF/lib
b) itext-1.3.1.jar >>>>>>> tomcat/webapps/ROOT/WEB-INF/lib

JasperReports 3.5 for Java Developers

Liferay Alloy Popup

In Liferay's previous versions, we are using Liferay.Popup for popup window using jQuery.

As Liferay is moved to Alloy UI , Here is the code for Alloy Popup:


To Display some Html content in Alloy Popup:


< aui:script >

function callPopup(){

AUI().ready('aui-dialog', 'aui-overlay-manager', 'dd-constrain', function(A) {

var dialog = new A.Dialog({

title: 'DISPLAY CONTENT',

centered: true,

modal: true,

width: 500,

height: 400,

bodyContent: "This is testing content inside the popup"

}).render();

});

}

</aui:script >




Passing URL : Passing URL into Alloy Popup

<aui:script>

Liferay.provide(window,'<portlet:namespace />callPopup',

function(url1) {

var A = AUI();

var data = {};

var dialog = new A.Dialog(

{

centered: true,

destroyOnClose: true,

modal: true,

title: Liferay.Language.get('Display-Content'),

width: 600

}

).render();

dialog.plug(

A.Plugin.IO,

{

data: data,

uri: url1

}

);

},

['aui-dialog', 'aui-io']

);

</aui:script >

Liferay in Action: The Official Guide to Liferay Portal Development

Friday, August 6, 2010

Inter Portlet Communication (IPC) for Portlets on Different Page

Use the below code for Inter Portlet Communication (IPC) for Portlets on Different Page:-

String plidName = "Sample_WAR_Sampleportlet";
long plidForward = getPortletId(plidName);
PortletURL url = new PortletURLImpl(request,plidName, plidForward, PortletRequest.ACTION_PHASE);

---------------------------------------------------------------------------------

public static long getPortletId(String portletId) {

PortletPreferences prfs = null;
try {
DetachedCriteria dCriteria = DetachedCriteria
.forClass(PortletPreferences.class);
dCriteria.add(Restrictions.eq("portletId", portletId));
DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);

List list = PortletPreferencesLocalServiceUtil.dynamicQuery(dynamicQuery);

if (!list.isEmpty()) {
prfs = (PortletPreferences) list.get(0);
}

} catch (Exception e) {
e.printStackTrace();
}
return prfs.getPlid();
}

Friday, July 30, 2010

Liferay Service Builder

Liferay has very good service builder
utility. We can specify our domain model in
a service.xml file and run an ant task. The
utility will create Java files, Hibernate files,
Service Builder
and service classes

• Service Builder is a RAD tool, we may
choose to use our own service framework
and layer instead

Liferay supports Web Services

Liferay supports Web Services :-

• JSON
• Hessian
• Burlap
• REST
• RMI
• Spring HTTP
• WSRP
• WebDAV

Thursday, July 29, 2010

HashMap sort by key

HashMap map = new HashMap();

map.put("05", "500");
map.put("04", "400");
map.put("03", "600");
map.put("01", "100");

Object[] key = map.keySet().toArray();
Arrays.sort(key);

for (int i = 0; i < key.length; i++) {
System.out.println(map.get(key[i]));
}

Thursday, March 11, 2010

Inter Portlet Communictaion For Session Attributes

If we set any value in session it will be available until session available.

Include Following Line in liferay-portlet.xml:
<private-session-attributes>false</private-session-attributes>

Create session attribute in any file:
PortletSession ps = req.getPortletSession();
ps.setAttribute(key, value, PortletSession.APPLICATION_SCOPE);

Example:
ps.setAttribute("SAMPLE_ATTRIBUTE", "sample", PortletSession.APPLICATION_SCOPE);

Access in other Portlet File:
String name = ps.getAttribute("SAMPLE_ATTRIBUTE", PortletSession.APPLICATION_SCOPE);
Now name contains sample.

Programmatically import LAR file into Liferay.

public static void addDefaultLayoutsByLAR(
long userId, long groupId, boolean privateLayout, File larFile)
throws PortalException, SystemException {

Map parameterMap = new HashMap();

parameterMap.put(
PortletDataHandlerKeys.PERMISSIONS,
new String[] {Boolean.TRUE.toString()});
parameterMap.put(
PortletDataHandlerKeys.PORTLET_DATA,
new String[] {Boolean.TRUE.toString()});
parameterMap.put(
PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT,
new String[] {Boolean.TRUE.toString()});
parameterMap.put(
PortletDataHandlerKeys.PORTLET_SETUP,
new String[] {Boolean.TRUE.toString()});
parameterMap.put(
PortletDataHandlerKeys.USER_PERMISSIONS,
new String[] {Boolean.FALSE.toString()});

LayoutLocalServiceUtil.importLayouts(
userId, groupId, privateLayout, parameterMap, larFile);
}

Installing Liferay 5.2.3 on Existing Apache Tomcat 6.0.24

1. Download and install Apache Tomcat 6.0.24 into your preferred directory.

2. Download and install JDK 5 . Set an environment variable called %JAVA_HOME% (in Windows) or $JAVA_HOME (in Linux/UNIX) to point to your JDK directory.


3. Create and edit $TOMCAT_HOME/conf/Catalina/localhost/ROOT.xml to set up the portal web application.
<Context path="" debug="0" reloadable="true" cookies="true" crossContext="false" privileged="false" />


4. Download liferay-portal-5.2.3.war.


5. Download Liferay's Portal 5.2.3 Dependencies.

5.1. Create a $TOMCAT_HOME/lib/ext directory and unzip the dependencies ZIP in there.

6. Edit $TOMCAT_HOME/conf/catalina.properties. common.loader= ${catalina.home}/common/classes,\ ...\ ${catalina.home}/common/lib/ext/*.jar


7. Make sure your database server is installed and is working.
If it's installed in a different machine make sure that it's accessible from the one where Liferay is being installed.


8. Configure data sources for your database. Make sure the JDBC driver for your database is accessible by Tomcat.
8.1. Obtain the JDBC driver for your version of the database server. In the case of MySQL use mysql-connector-java-{$version}-bin.jar.
8.2. Copy the JAR file to $TOMCAT_HOME/lib/ext

9. Create a database for Liferay.
For example: create database lportal character set utf8; Liferay will automatically create the tables and populate it the first time it starts.

10. To add support for accessing Liferay's services remotely and to access the document library using WebDAV follow these steps:
10.1. Download tunnel-web.war
10.2. Create a directory called $TOMCAT_HOME/webapps/tunnel and unzip the WAR contents inside

11. Create $TOMCAT_HOME/conf/jaas.config.

PortalRealm { com.liferay.portal.kernel.security.jaas.PortalLoginModule required; };


12. Edit $TOMCAT_HOME/bin/catalina.bat so that Tomcat can reference the login module.
set JAVA_OPTS=-Xms128m -Xmx512m -Dfile.encoding=UTF8 -Duser.timezone=GMT -Djava.security.auth.login.config=%CATALINA_HOME%/conf/jaas.config

13. Delete contents $TOMCAT_HOME/webapps/ROOT directory.


14. Unpack liferay-portal-5.2.3.war to $TOMCAT_HOME/webapps/ROOT.


15. For supporting UTF-8 UIRIEncoding, edit $TOMCAT_HOME/conf/server.xml.


16. Run Tomcat, point browser to http://localhost:8080. Sign in as test@liferay.com and password test


NOTE: ---- ************copy the following jar files in lib/ext folder to avoid startup problem:********
activation.jar annotations.jar ccpp.jar container.jar hsql.jar jms.jar jta.jar jutf7.jar mail.jar mysql.jar portal-kernel.jar
portal-service.jar portlet-container.jar portlet.jar postgresql.jar serializer.jar support-tomcat.jar xalan.jar xercesImpl.jar

xml-apis.jar