Service
Builder in PluginSDK (Lifera 5.2)
- Create a portlet > create calculatedeductible “Calculate Deductible”
- Create portlet-ext.xml and place it in root folder of liferay ie Liferay-Portal folder which contains workspace and tomcat etc.
##MySql
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=lportal
jdbc.default.password=lportal
- Create service.xml in WEB-INF folder as below.
<?xml
version="1.0"
encoding="UTF-8"
?>
<!DOCTYPE
service-builder
PUBLIC
"-//Liferay//DTD
Service Builder 5.2.0//EN"
"http://www.liferay.com/dtd/liferay-service-builder_5_2_0.dtd">
<service-builder
package-path="com.plugin.Deductible">
<author>Harshal
Shah</author>
<namespace>calculate</namespace>
<entity
name="Deductible"
local-service="true"
remote-service="false">
<column
name="productId"
type="long"
primary="true"
/>
<column
name="make"
type="String"
primary="true"/>
<column
name="model"
type="String"
primary="true"
/>
<column
name="deductible"
type="long"
/>
<order
by="asc">
<order-column
name="make"
/>
</order>
</entity>
</service-builder>
- Now double click on build.xml>build services target of that portlet…by now you know what I mean .
- Modify portlet.xml to
<?xml
version="1.0"?>
<portlet-app
xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
version="2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
<portlet>
<portlet-name>calculatedeductible</portlet-name>
<display-name>Calculate
Deductible</display-name>
<portlet-class>com.plugin.Deductible.controller.CalculatePortlet</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/view.jsp</value>
</init-param>
<init-param>
<name>edit-jsp</name>
<value>/edit.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>edit</portlet-mode>
</supports>
<portlet-info>
<title>Calculate
Deductible</title>
<short-title>Calculate
Deductible</short-title>
<keywords>Calculate
Deductible</keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>
</portlet-app>
Here
we have modified portlet-class since we want to include the edit and
view modes also added <portlet-mode>
- Create a class Calculate Portlet which implements different modes. Create this package under the source folder under WEB-INF.
package
com.plugin.Deductible.controller;
import
com.plugin.Deductible.action.CalculateLocalServiceUtil;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import
javax.portlet.PortletPreferences;
import
javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
public class CalculatePortlet extends
GenericPortlet {
public void init() throws
PortletException {
editJSP =
getInitParameter("edit-jsp");
viewJSP =
getInitParameter("view-jsp");
}
public void doEdit(RenderRequest
renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {
renderResponse.setContentType("text/html");
PortletURL addName =
renderResponse.createActionURL();
addName.setParameter("addName",
"addName");
renderRequest.setAttribute("addNameUrl",
addName.toString());
include(editJSP, renderRequest,
renderResponse);
}
public void doView(RenderRequest
renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {
include(viewJSP, renderRequest,
renderResponse);
}
public void
processAction(ActionRequest actionRequest,
ActionResponse actionResponse)
throws IOException, PortletException {
String addName =
actionRequest.getParameter("addName");
if (addName != null) {
try {
String make =
actionRequest.getParameter("Make");
String model =
actionRequest.getParameter("Model");
String deductible =
actionRequest.getParameter("Deductible");
long ded=0;
if (make!=null &&
model!=null && deductible != null ) {
System.out.println(deductible);
Thread.sleep(5000);
try{
ded = Long.parseLong(deductible);}
catch(NumberFormatException e){
System.out.println("deductible
is not a number");
actionResponse.setPortletMode(PortletMode.VIEW);
}
CalculateLocalServiceUtil.addProduct(make,
model, ded);
}
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
actionResponse.setPortletMode(PortletMode.VIEW);
}
}
protected void include(String path,
RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {
PortletRequestDispatcher
portletRequestDispatcher = getPortletContext()
.getRequestDispatcher(path);
if (portletRequestDispatcher == null)
{
_log.error(path + " is not a
valid include");
} else {
portletRequestDispatcher.include(renderRequest,
renderResponse);
}
}
protected String editJSP;
protected String viewJSP;
private static Log _log =
LogFactory.getLog(CalculatePortlet.class);
}
- Now different modes are set we will implement the wrapper for the service builder classess.
Create CalculateLocalService Util under
WEB-INF action folder
package com.plugin.Deductible.action;
import
com.plugin.Deductible.service.DeductibleLocalServiceUtil;
import java.util.List;
import
com.plugin.Deductible.service.persistence.DeductiblePK;
import
com.plugin.Deductible.service.impl.DeductibleLocalServiceImpl;
import
com.plugin.Deductible.model.Deductible;
import
com.plugin.Deductible.service.persistence.DeductiblePersistence;
import java.util.List;
import
com.plugin.Deductible.service.base.DeductibleLocalServiceBaseImpl;
import java.util.Date;
import java.util.List;
import javax.portlet.ActionRequest;
import javax.transaction.Transaction;
import
com.liferay.counter.service.CounterLocalServiceUtil;
import
com.liferay.portal.PortalException;
import
com.liferay.portal.SystemException;
import
com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import
com.liferay.portal.service.UserLocalServiceUtil;
import
com.liferay.portal.theme.ThemeDisplay;
public class CalculateLocalServiceUtil
{
public static Deductible
addProduct(String make,String model,long deductible) throws
SystemException,
PortalException {
DeductiblePK dPk = new
DeductiblePK();
try {
long productId =
CounterLocalServiceUtil.increment(Deductible.class
.getName());
dPk.setProductId(productId);
dPk.setMake(make);
dPk.setModel(model);
Deductible product =
DeductibleLocalServiceUtil
.createDeductible(dPk);
DeductibleLocalServiceUtil.updateDeductible(product,
false);
return product;
} catch (Exception e) {
System.out.println("Junk piece
of crap..Caught you");
}
return null;
}
public static List<Deductible>
getAllProducts() throws SystemException,
PortalException {
try {
int end =
DeductibleLocalServiceUtil.getDeductiblesCount();
return
DeductibleLocalServiceUtil.getDeductibles(0,end);
} catch (Exception e) {
System.out.println("Junk piece
of crap..Caught you");
}
return null;
}
}
- Now create the jsp’s under docroot.
View.jsp;
<%@
include
file="init.jsp"
%>
<%@
taglib
uri="http://java.sun.com/portlet_2_0"
prefix="portlet"
%>
<portlet:defineObjects
/>
<%
List
products =(List)CalculateLocalServiceUtil.getAllProducts();
Deductible
product =null;%>
<table
style="border:
lpx solid #CCC; width; 100%;padding:5px;">
<tr>
<td
style="color:
#12558E;">Make</td>
<td
style="color:
#12558E;">
Model</td>
<td
style="color:
#12558E;">
Deductible</td>
</tr>
<%System.out.println("In
view.jsp");
%>
<c:if
test="<%=products!=null
%>">
<%
for
(int
i=0; i<products.size(); i++ ){
product=(Deductible)products.get(i);
%>
<tr>
<td><%=product.getMake()
%></td>
<td>
<%=product.getModel()
%></td>
<td>
<%=product.getDeductible()
%></td>
</tr>
<%}%>
</c:if>
</table>
Edit.jsp:
<%@
taglib
uri="http://java.sun.com/portlet"
prefix="portlet"
%>
<jsp:useBean
class="java.lang.String"
id="addNameUrl"
scope="request"
/>
<portlet:defineObjects
/>
<form
id
=
"<portlet:namespace
/>addForm"
action="<%=addNameUrl
%>"
method="post">
<table>
<tr>
<td>Make:</td>
<td><input
type="text"
name="Make"></td>
<td>Model:</td>
<td>
<input
type="text"
name="Model"></td>
<td>Deductible:</td>
<td>
<input
type="text"
name="Deductible"></td>
</tr>
</table>
<input
type="submit"
id="nameButton"
title="Add
Name"
value="Add
Name">
</form>
Init.jsp:
<%@
page
import="java.io.ByteArrayInputStream"
%>
<%@
page
import="java.io.Serializable"
%>
<%@
page
import="java.io.StringReader"
%>
<%@
page
import="java.math.BigDecimal"
%>
<%@
page
import="java.net.InetAddress"
%>
<%@
page
import="java.net.UnknownHostException"
%>
<%@
page
import="java.text.DateFormat"
%>
<%@
page
import="java.text.DecimalFormat"
%>
<%@
page
import="java.text.MessageFormat"
%>
<%@
page
import="java.text.NumberFormat"
%>
<%@
page
import="java.text.SimpleDateFormat"
%>
<%@
page
import="java.util.ArrayList"
%>
<%@
page
import="java.util.Arrays"
%>
<%@
page
import="java.util.Calendar"
%>
<%@
page
import="java.util.Collection"
%>
<%@
page
import="java.util.Collections"
%>
<%@
page
import="java.util.Currency"
%>
<%@
page
import="java.util.Date"
%>
<%@
page
import="java.util.Enumeration"
%>
<%@
page
import="java.util.HashMap"
%>
<%@
page
import="java.util.HashSet"
%>
<%@
page
import="java.util.Iterator"
%>
<%@
page
import="java.util.LinkedHashMap"
%>
<%@
page
import="java.util.LinkedHashSet"
%>
<%@
page
import="java.util.List"
%>
<%@
page
import="java.util.Locale"
%>
<%@
page
import="java.util.Map"
%>
<%@
page
import="java.util.Properties"
%>
<%@
page
import="java.util.ResourceBundle"
%>
<%@
page
import="java.util.Set"
%>
<%@
page
import="java.util.Stack"
%>
<%@
page
import="java.util.TimeZone"
%>
<%@
page
import="java.util.TreeMap"
%>
<%@
page
import="java.util.TreeSet"
%>
<%@
page
import
="com.plugin.Deductible.model.Deductible"
%>
<%@
page
import
="com.plugin.Deductible.action.CalculateLocalServiceUtil"%>
- Compile, deploy from ant target and start the tomcat.
No comments:
Post a Comment