Monday, May 30, 2011

Extract all pictures contained in the presentation to image convertion java code

Required jar: jackrabbit-standalone-1.5.0.jar

[sourcecode language="java" wraplines="false"]

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.usermodel.PictureData;
import org.apache.poi.hslf.usermodel.SlideShow;

public class PPttoimg {

/**
* @param args
*/
public static void main(String[] args) {
SlideShow ppt = null;
try {
ppt = new SlideShow(new HSLFSlideShow("Screenshot.ppt"));
} catch (IOException e) {

e.printStackTrace();
}

//extract all pictures contained in the presentation
PictureData[] pdata = ppt.getPictureData();
for (int i = 0; i < pdata.length; i++){
PictureData pict = pdata[i];

// picture data
byte[] data = pict.getData();

int type = pict.getType();
String ext;
switch (type){
case Picture.JPEG: ext=".jpg"; break;
case Picture.PNG: ext=".png"; break;
case Picture.WMF: ext=".wmf"; break;
case Picture.EMF: ext=".emf"; break;
case Picture.PICT: ext=".pict"; break;
default: continue;
}
FileOutputStream out;
try {
out = new FileOutputStream("pict_"+i + ext);
out.write(data);
out.close();
} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}

}

}

[/sourcecode]

Check Point:

Wednesday, May 25, 2011

Browser Detection

Sometimes it can be useful to detect the visitor's browser, and then serve the appropriate information.

The Navigator object contains information about the visitor's browser name, version, and more.

[sourcecode language="html" wraplines="false"]
<html>
<body>
<div id="example"></div>
<script type="text/javascript">
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
document.getElementById("example").innerHTML=txt;
</script>
</body>
</html>

[/sourcecode]

Redirecting Url based on Device

[sourcecode language="html" wraplines="false"]
<script type="text/javascript">
function iPhoneAlert() {
if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent. match(/iPod/i)))
{
var question = confirm(”Would you like to view the iPhone site?”)
if (question){
window.location = “http://m.localhost:8080/web/<span class="Apple-style-span" style="font-family: monospace;">guest/home</span><span class="Apple-style-span" style="font-family: monospace;">”; </span>
}else{
window.location = “http://localhost:8080/web/guest/home<span class="Apple-style-span" style="font-family: monospace;">”; </span>
} } }
</script>
[/sourcecode]



OR

[sourcecode language="html" wraplines="false"]
<script type="text/javascript">
<!--
if (screen.width <= 699) {

window.location = “http://m.localhost:8080/web/guest/home”;

}
//-->
</script>
[/sourcecode]



OR

[sourcecode language="html" wraplines="false"]
<script language=javascript>
<!--
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
location.replace("http://m.localhost:8080/web/guest/home<span class="Apple-style-span" style="font-family: monospace;">");</span>
}
-->
</script>
[/sourcecode]

Monday, May 23, 2011

Creating URL using Liferay tagLib

<%@ taglib uri="http://liferay.com/tld/portlet " prefix="liferay-portlet"%>

Render

<liferay-portlet:renderURL portletName="portletName_WAR_ApplicationName" portletMode="View" windowState="Normal" />

Action

<liferay-portlet:actionURL name="ActionMethodName" var="ActionURLVariable" >
<liferay-portlet:param name="ParamName" value="ParamValue"></liferay-portlet:param>
</liferay-portlet:actionURL>

Resource

<liferay-portlet:resourceURL id="ResourceId" var="ResourceURLVariable" escapeXml="false"/>

Create a URL based on PortletId in Liferay advance

Necessary package need to Import in JSP page
[sourcecode language="html" wraplines="false"]
<%@ page import="com.liferay.portlet.PortletURLFactoryUtil"%>
<%@ page import="com.liferay.portal.kernel.portlet.LiferayPortletURL"%>
<%@ page import="javax.portlet.PortletRequest"%>
<%@ page import="javax.portlet.PortletURL"%>
<%@ page import="javax.servlet.http.HttpServletRequest"%>
[/sourcecode]
Syntax:
[sourcecode language="html" wraplines="false"]
portletURL URL = PortletURLFactoryUtil.create(request, portletId, plid, lifecycle);
[/sourcecode]
OR
[sourcecode language="html" wraplines="false"]
LiferayPortletURL URL = PortletURLFactoryUtil.create(request, portletId, plid, lifecycle);
[/sourcecode]
For creating PortletRequest URL
[sourcecode language="html" wraplines="false"]
<%
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
LiferayPortletURL renderURL = PortletURLFactoryUtil.create(request, portletId, plid, PortletRequest.RENDER_PHASE);
renderURL.setWindowState(WindowState.NORMAL); //to set windowsState
renderURL.setPortletMode(PortletMode.VIEW); //to set portletmode
renderURL.setParameter(arg0,arg1); /to set the parameter ex:ipcActionURL.setParameter("struts_action",/etc/reports/add);

LiferayPortletURL actionURL = PortletURLFactoryUtil.create(request, "portletId", plid, PortletRequest.ACTION_PHASE);
actionURL.setWindowState(WindowState.NORMAL); //to set windowsState
actionURL.setPortletMode(PortletMode.VIEW); //to set portletmode
actionURL.setParameter(arg0,arg1); /to set the parameter ex:ipcActionURL.setParameter("struts_action",/etc/reports/add);

>;
[/sourcecode]
PortletId:
To fetch the current portletId or portletName.
[sourcecode language="java" wraplines="false"]
portletDisplay.getId();
portletDisplay.getPortletName();
[/sourcecode]
plid (Portlet Layout id):
We need to mention plid, based on this plid it will redirect that particular 
page.//for InterPortlet communication(IPC)
[sourcecode language="java" wraplines="false"]
<%= LayoutLocalServiceUtil.getFriendlyURLLayout(themeDisplay.getScopeGroupId(), false, "/layoutName").getPlid() %>
[/sourcecode]
OR
[sourcecode language="java" wraplines="false"]
<%=themeDisplay.getPlid()%>
[/sourcecode]
OR
[sourcecode language="java" wraplines="false"]
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.model.PortletPreferences;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.LayoutLocalServiceUtil;
import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
import java.util.List;

public static long getPortletId(String portletId, long groupId) {

long plid = 0l;
try {
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(PortletPreferences.class);
dynamicQuery.add(PropertyFactoryUtil.forName("portletId").eq(portletId));
List<PortletPreferences&gt; list = (List)PortletPreferencesLocalServiceUtil.dynamicQuery(dynamicQuery);

for (PortletPreferences pfs : list) {
long groupid_ = LayoutLocalServiceUtil.getLayout(pfs.getPlid()).getGroupId();
if (groupid_==groupId) {
plid = pfs.getPlid();
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return plid;
}
[/sourcecode]
Lifecycle/PortletRequest:
Two types of portlet request we can use here.

1. PortletRequest.RENDER_PHASE
2. PortletRequest.ACTION_PHASE

Friday, May 20, 2011

Oracle Commands

Create Tablespace

Syntax


CREATE TABLESPACE
DATAFILE
SIZE
AUTOEXTEND on
MAXSIZE
EXTENT MANAGEMENT LOCAL UNIFORM SIZE ;


Example


create tablespace RAJEEVA
datafile '/usr/lib/oracle/xe/oradata/XE/rajeeva.dft'
size 100M
autoextend on
maxsize 2000M
extent management local uniform size 640K;


Create User

Syntax


create user
identified by
default tablespace
temporary tablespace temp;


Example


create user CLEANGLOBUS
identified by root
default tablespace RAJEEVA
temporary tablespace temp;


Grant Permission for User:

Syntax


grant connect, resource to ;


Example


grant connect, resource to CLEANGLOBUS;


Import Dump

Syntax


imp User/Password File=filename.dmp FULL=y


Export Dump

Syntax


exp User/Password FILE=filename.dmp OWNER=User


Alter Tablespace Size


ALTER dataBaseName DATAFILE '/usr/lib/oracle/xe/oradata/XE/gbdata.dft' RESIZE 3000M


Alter Tablespace Name


ALTER TABLESPACE oldTablespaceName RENAME TO newTablespaceName;

Run liferay on other then default root context

Simple 5 steps to change the context

Step1: Rename the folder ROOT dir to yours own context name(Example: liferay)
Step2: Rename file ROOT.xml to liferay.xml in %LIFERAY-HOME%\conf\Catalina\localhost
Step3: Create empty Directory/Folder ROOT in %LIFERAY-HOME%\webapps\
Step4:

Original Code

[sourcecode language="xml" wraplines="false"]
<Context path="" crossContext="true">
......
....................
</Context>
[/sourcecode]


Modified Code
[sourcecode language="xml" wraplines="false"]
<Context path="/liferay" crossContext="true">
......
....................
</Context>
[/sourcecode]

Step5: Create file portal-ext.properties in %LIFERAY-HOME%\webapps\liferay\WEB_INF\classes\

copy paste the below code
portal.ctx=/liferay

Check Point:
Strat the Liferay server : http://localhost:8080/liferay

Thursday, May 19, 2011

Change Liferay portlet-title icon

How to change portlet-title icon for a different one.

Using plugin hooks we need to customize the existing liferay taglib  jsp file .

file path given below

%LIFERAY_HOME%/webapps/ROOT/html/taglib/ui/icon/page.jsp find this piece of code:

Original code:

[sourcecode language="html" wraplines="false"]
.....
<liferay-util:buffer var="linkContent">
<img class="<%= imgClass %>" src="<%= src %>" <%= details %> />
......
</liferay-util:buffer>
[/sourcecode]

and replace it with this one:

[sourcecode language="html" wraplines="false"]
...
<liferay-util:buffer var="linkContent">
<img class="imgClass" src="<%= src %>" <%= details %> /><div style="float:left" class="customImgClass"></div>
.....
</liferay-util:buffer>
urlIsNotNull %>">

[/sourcecode]

Next, in "Look and Feel" >> "Advanced Styling" >> "Add a CSS rule for just this portlet" and put below code in brackets.

[sourcecode language="html" wraplines="false"]

#portletID .portlet-title .customImgClass{
background-image: url('/theme-name/images/custom/customicon.png');
width: 16px; 
height: 16px;
margin-right: 5px;
}
#portletID .portlet-title .imgClass{
display: none;
}

[/sourcecode]

Hide control panel menu from dock for custom user group

To hide control panel menu from the dock for the customer user group

use this below code in dock.vm file

[sourcecode language="java" wraplines="false"]

#set ($userGroupLocalService = $serviceLocator.findService("com.liferay.portal.service.UserGroupLocalService"))
#set ($userGroupLst = $userGroupLocalService.getUserUserGroups($user_id))
#set($flag=0)

[/sourcecode]

Replace the original code with customise code

Original Code(Liferay default code)

[sourcecode language="java" wraplines="false"]

#if ($show_control_panel)
<li>
<a href="$control_panel_url">$control_panel_text</a>
</li>
#end

[/sourcecode]

Customised Code

[sourcecode language="java" wraplines="false"]

#foreach($userGroup in $userGroupLst )
#if("Customers" == $userGroup.getName().trim())
#set($flag=1)
#if (!$show_control_panel)
<li>
<a href="$control_panel_url">$control_panel_text</a>
</li>
#end

#end
#end
#if($flag==0)
#if ($show_control_panel)
<li>
<a href="$control_panel_url">$control_panel_text</a>
</li>
#end
#end

[/sourcecode]

Configuration the portlet in liferay using portletPreference

In Liferay-portlet.xml we need to specify the configuration action class.
add the below code
[sourcecode language="xml" wraplines="false"]
<portlet>
<portlet-name>Portletname</portlet-name>
<icon>/icon.png</icon>
<configuration-action-class>
com.liferay.config.ConfigurationActionImpl
</configuration-action-class>
......
......
</portlet>
[/sourcecode]
create ConfigurationActionImpl.java file in the com.liferay.. package.
aad the below code
[sourcecode language="java" wraplines="false"]
package com.liferay.config;

import com.liferay.portal.kernel.portlet.ConfigurationAction;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
* <a href="ConfigurationActionImpl.java.html"><b><i>View Source</i></b></a>
*
* @author Mark Wong
*
*/
public class ConfigurationActionImpl implements ConfigurationAction {

public void processAction(PortletConfig portletConfig,
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {

String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

if (!cmd.equals(Constants.UPDATE)) {
return;
}

String email = ParamUtil.getString(actionRequest, "email");
String subject = ParamUtil.getString(actionRequest, "subject");

String portletResource = ParamUtil.getString(actionRequest,"portletResource");
PortletPreferences preferences = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);

preferences.setValue("email", email);
preferences.setValue("subject", subject);

preferences.store();

PortletSession portletSession = actionRequest.getPortletSession();
SessionMessages.add(actionRequest, portletConfig.getPortletName()+ ".doConfigure");

}

public String render(PortletConfig portletConfig,
RenderRequest renderRequest, RenderResponse renderResponse)
throws Exception {

return "/html/config.jsp";
}

}
[/sourcecode]
Create file Config.jsp in html folder
add the below code
[sourcecode language="html" wraplines="false"]
<%@include file="/html/init.jsp" %>

<form action="<liferay-portlet:actionURL portletConfiguration="true" />"
method="post" name="<portlet:namespace />fm">

<input name="<portlet:namespace /><%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />

<table class="lfr-table">
<tr>
<td>Email</td>
<td><input type="text" name="<portlet:namespace />email" value="<%=email %>"/>
</td>

<td>Subject</td>
<td><input type="text" name="<portlet:namespace />subject" value="<%=subject %>" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="<liferay-ui:message key="save" />"
onClick="submitForm(document.<portlet:namespace />fm);" />
</td>
</tr>
</table>
</form>
[/sourcecode]
create init.jsp file in htmlfolder add the below code
[sourcecode language="html" wraplines="false"]
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>

<%@ page import="com.liferay.portal.kernel.util.Constants" %>
<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %>
<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%@ page import="com.liferay.portal.kernel.util.StringPool" %>
<%@ page import="com.liferay.portal.util.PortalUtil" %>
<%@ page import="com.liferay.portlet.PortletPreferencesFactoryUtil" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<%@ page import="javax.portlet.WindowState" %>
<%@ page import="javax.portlet.PortletURL"%>
<%@ page import="javax.portlet.ActionRequest" %>
<%@ page import="javax.portlet.PortletPreferences" %>

<%@ page import="com.liferay.portal.kernel.language.LanguageUtil" %>
<%@ page import="com.liferay.portal.security.permission.ActionKeys"%>

<liferay-theme:defineObjects />
<portlet:defineObjects />

<%
String currentURL = PortalUtil.getCurrentURL(request);

PortletPreferences preferences = renderRequest.getPreferences();

String portletResource = ParamUtil.getString(request, "portletResource");

if (Validator.isNotNull(portletResource)) {
preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
}

String email = preferences.getValue("email", StringPool.BLANK);
String subject = preferences.getValue("subject", StringPool.BLANK);

%>
[/sourcecode]
Craete view.jsp file to display the configuration value use the below code
[sourcecode language="html" wraplines="false"]
<%@include file="/html/init.jsp" %>
<%=email %>
<%=subject %>
[/sourcecode]

Redirect URL and also passing parameter in URL using java Script

[sourcecode language="html" wraplines="false"] 
<html>
<head>
<script language="JavaScript">
<!--
function WinOpen() {
var ap_arrival_val=document.redirect.carIn.value
var ap_departure_val=document.redirect.carOut.value
document.location.href='/web/guest/reservierung?
ap_arrival='+ap_arrival_val+'&ap_departure='+ap_departure_val;

}
// -->
</script>
</head>
<body>
<form name="redirect">
<input type="text"name="carIn"/>  
<input type="text"name="carOut"/>
<input type=button value="Submit" onClick="WinOpen();">
</form>
</body>
</html>
[/sourcecode]

render the current user portrait in my JSP File

How to render the current user portrait in my JSP File?

[sourcecode language="html" wraplines="false"]

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<liferay-theme:defineObjects />
<portlet:defineObjects />

<liferay-ui:user-display userId="<%= user.getUserId() %>" />

[/sourcecode]

Creating Tabs in Liferay Portlets with spaces in tab names

In view.jsp file add the below code.
[sourcecode language="html" wraplines="false"] 
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>

 
<%@ page import="com.liferay.portal.kernel.util.ParamUtil" %>
<%@ page import="javax.portlet.PortletURL" %>
<%@ page import="javax.portlet.ActionRequest" %>
<%@ page import="javax.portlet.PortletRequest" %>

<liferay-portlet:renderURL var="portletURL"/>
<%
String tabValue = ParamUtil.getString(request, "tab", "sampleTab1");
String includeURL = "/html/.../jsp" + tabValue.trim() + ".jsp";
%>

<liferay-ui:tabs names="Sample Tab 1,Sample Tab 2,Sample Tab 3" 
tabsValues="sampleTab1,sampleTab2,sampleTab3" 
param="tab" url="<%= portletURL %>" />

<c:import url="<%= includeURL %>"/>
[/sourcecode]

and create the files with name give as in the 
"tabsValues"(Ex:sampleTab1.jsp).