|
First Name,
Last Name
Employee Designation
Email Address
Age
Phone Number
|
|
Column Name
|
Type
|
|
First Name,
|
String
|
|
Last Name
|
String
|
|
Employee Designation
|
String
|
|
Email Address
|
String
|
|
Age
|
Integer
|
|
Phone Number
|
String
|
|
ExpandoTableLocalServiceUtil.addTable(companyId, XXXX.class.getName(), tableName);
ExpandoColumnLocalServiceUtil.addColumn(tableId,fieldName,ExpandoColumnConstants.STRING);
ExpandoColumnLocalServiceUtil.addColumn(tableId,fieldName,ExpandoColumnConstants.Integer);
ExpandoValueLocalServiceUtil.addValue(
companyId, XXXX.class.getName(), databaseTableName,
fieldLabel, classPK, fieldValue);
|
|
long classPK = CounterLocalServiceUtil.increment(XXXX.class.getName());
OR
long classPK = CounterLocalServiceUtil.increment();
|
|
<%@ taglib uri= "http://liferay.com/tld/portlet" prefix= "liferay-portlet" %>
<%@ taglib uri= "http://liferay.com/tld/theme" prefix= "liferay-theme" %>
<%@ taglib uri= "http://liferay.com/tld/ui" prefix= "liferay-ui" %>
<%@ taglib uri= "http://java.sun.com/portlet_2_0" prefix= "portlet" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<h1>Liferay Expando API Example</h1>
<portlet:renderURL var= "addEmployee">
<portlet:param name= "mvcPath" value= "/html/jsps/add_employee.jsp"/>
</portlet:renderURL>
<portlet:renderURL var= "dislayEmployees">
<portlet:param name= "mvcPath" value= "/html/jsps/display_employeed.jsp"/>
</portlet:renderURL>
<br/>
<a href= "<%=addEmployee.toString()%> ">Add Employee</a><br/>
<a href= "<%=dislayEmployees.toString()%> ">Display Employees</a><br/>
|
|
<%@page import= "com.liferay.portal.kernel.servlet.SessionErrors"%>
<%@page import= "com.liferay.portal.kernel.servlet.SessionMessages"%>
<%@ taglib uri= "http://liferay.com/tld/portlet" prefix= "liferay-portlet" %>
<%@ taglib uri= "http://liferay.com/tld/theme" prefix= "liferay-theme" %>
<%@ taglib uri= "http://liferay.com/tld/ui" prefix= "liferay-ui" %>
<%@ taglib uri= "http://java.sun.com/portlet_2_0" prefix= "portlet" %>
<portlet:defineObjects />
<portlet:renderURL var= "homeURL"></portlet:renderURL>
<portlet:actionURL var= "addEmployeeActionURL" windowState= "normal"
name= "addEmployee">
</portlet:actionURL>
<% if(SessionMessages.contains(renderRequest.
getPortletSession(),"employee-add-success")){%>
<liferay-ui:success key= "employee-add-success" message= "Employee information
have been added successfully." />
<%} %>
<% if(SessionErrors.contains(renderRequest.getPortletSession(),
"employee-add-error")){%>
<liferay-ui:error key= "employee-add-error" message= "There is an
Error occured while adding employee and please try again" />
<%} %>
<h2>Add Employee</h2>
<a href= "<%=homeURL.toString() %> ">Home</a><br/><br/>
<form action= "<%=addEmployeeActionURL%> " name= "employeeForm" method= "POST">
<b>First Name</b><br/>
<input type= "text" name= "<portlet:namespace/> employeeFirstName" id= "<portlet:namespace/> employeeFirstName"/><br/>
<b>Last Name</b><br/>
<input type= "text" name= "<portlet:namespace/> employeeLastName" id= "<portlet:namespace/> employeeLastName"/><br/>
<b>Employee Designation</b><br/>
<input type= "text" name= "<portlet:namespace/> employeeDesignation" id= "<portlet:namespace/> employeeDesignation"/><br/>
<b>Email Address</b><br/>
<input type= "text" name= "<portlet:namespace/> emailAddress" id= "<portlet:namespace/> emailAddress"/><br/>
<b>Age</b><br/>
<input type= "text" name= "<portlet:namespace/> employeeAge" id= "<portlet:namespace/> employeeAge"/><br/>
<b>Phone Number</b><br/>
<input type= "text" name= "<portlet:namespace/> phoneNumber" id= "<portlet:namespace/> phoneNumber"/><br/>
<input type= "submit" name= "addStudent" id= "addStudent" value= "Add Employee"/>
</form>
|
|
<%@page import= "com.liferay.portlet.expando.NoSuchTableException"%>
<%@page import= "com.liferay.portlet.expando.model.ExpandoTable"%>
<%@page import= "com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil"%>
<%@page import= "com.liferay.portal.kernel.util.StringPool"%>
<%@page import= "com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil"%>
<%@page import= "com.liferay.portlet.expando.model.ExpandoRow"%>
<%@page import= "com.liferay.portal.kernel.dao.orm.QueryUtil"%>
<%@page import= "com.meera.liferay.expandoapi.LiferayExpandoAPIAction"%>
<%@page import= "java.util.List"%>
<%@page import= "com.liferay.portlet.expando.service.ExpandoRowLocalServiceUtil"%>
<%@ taglib uri= "http://liferay.com/tld/portlet" prefix= "liferay-portlet" %>
<%@ taglib uri= "http://liferay.com/tld/theme" prefix= "liferay-theme" %>
<%@ taglib uri= "http://liferay.com/tld/ui" prefix= "liferay-ui" %>
<%@ taglib uri= "http://java.sun.com/portlet_2_0" prefix= "portlet" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<portlet:renderURL var= "homeURL"></portlet:renderURL>
<h1>Liferay Expando API Display Employees</h1>
<a href= "<%=homeURL.toString() %> ">Home</a><br/><br/>
<table style="width: 100%" border= "1">
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Designation</th>
<th>EmailAddress</th>
<th>Age</th>
<th>PhoneNumber</th>
</tr>
<%
ExpandoTable expandoTable= null;
String message= null;
try {
expandoTable = ExpandoTableLocalServiceUtil.getTable(
themeDisplay.getCompanyId(), LiferayExpandoAPIAction. class.getName(),LiferayExpandoAPIAction.expandoTableName);
}
catch (NoSuchTableException nste) {
message="Table not existed to show the data. please add data first and comeback to to see the data";
}
if(expandoTable!= null){
List<ExpandoRow> rows = ExpandoRowLocalServiceUtil.getRows(
themeDisplay.getCompanyId(), LiferayExpandoAPIAction. class.getName(),
LiferayExpandoAPIAction.expandoTableName, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
for (ExpandoRow row : rows) {
String data = ExpandoValueLocalServiceUtil.getData(
themeDisplay.getCompanyId(),
LiferayExpandoAPIAction. class.getName(), LiferayExpandoAPIAction.expandoTableName,
LiferayExpandoAPIAction.columnNames[0], row.getClassPK(), StringPool.BLANK);
%>
<tr>
<td><%=ExpandoValueLocalServiceUtil.getData(
themeDisplay.getCompanyId(),
LiferayExpandoAPIAction. class.getName(), LiferayExpandoAPIAction.expandoTableName,
LiferayExpandoAPIAction.columnNames[0], row.getClassPK(), StringPool.BLANK) %></td>
<td><%=ExpandoValueLocalServiceUtil.getData(
themeDisplay.getCompanyId(),
LiferayExpandoAPIAction. class.getName(), LiferayExpandoAPIAction.expandoTableName,
LiferayExpandoAPIAction.columnNames[1], row.getClassPK(), StringPool.BLANK) %></td>
<td><%=ExpandoValueLocalServiceUtil.getData(
themeDisplay.getCompanyId(),
LiferayExpandoAPIAction. class.getName(), LiferayExpandoAPIAction.expandoTableName,
LiferayExpandoAPIAction.columnNames[2], row.getClassPK(), StringPool.BLANK) %></td>
<td><%=ExpandoValueLocalServiceUtil.getData(
themeDisplay.getCompanyId(),
LiferayExpandoAPIAction. class.getName(), LiferayExpandoAPIAction.expandoTableName,
LiferayExpandoAPIAction.columnNames[3], row.getClassPK(), StringPool.BLANK) %></td>
<td><%=ExpandoValueLocalServiceUtil.getData(
themeDisplay.getCompanyId(),
LiferayExpandoAPIAction. class.getName(), LiferayExpandoAPIAction.expandoTableName,
LiferayExpandoAPIAction.columnNames[4], row.getClassPK(), 0) %></td>
<td><%=ExpandoValueLocalServiceUtil.getData(
themeDisplay.getCompanyId(),
LiferayExpandoAPIAction. class.getName(), LiferayExpandoAPIAction.expandoTableName,
LiferayExpandoAPIAction.columnNames[5], row.getClassPK(), StringPool.BLANK) %></td>
</tr>
<%}}%>
</table>
<h1><%=message!= null?message:StringPool.BLANK%></h1>
|
|
package com.meera.liferay.expandoapi;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portlet.expando.NoSuchTableException;
import com.liferay.portlet.expando.model.ExpandoColumnConstants;
import com.liferay.portlet.expando.model.ExpandoTable;
import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class LiferayExpandoAPIAction extends MVCPortlet {
public static String[] columnNames={"FirstName","LastName","Designation","EmailAddress","Age","PhoneNumber"};
public static String expandoTableName="Employee";
private static Log _log = LogFactoryUtil. getLog(LiferayExpandoAPIAction. class);
public void addEmployee(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
try {
boolean dataAdedSuccess= false;
String firstNameValue = ParamUtil. getString(actionRequest, "employeeFirstName");
String lastNameValue = ParamUtil. getString(actionRequest, "employeeLastName");
String employeeDesignation = ParamUtil. getString(actionRequest, "employeeDesignation");
String emailAddressValue = ParamUtil. getString(actionRequest, "emailAddress");
int employeeAgeValue = ParamUtil. getInteger(actionRequest, "employeeAge");
String phoneNumberValue = ParamUtil. getString(actionRequest, "phoneNumber");
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys. THEME_DISPLAY);
long companyId=themeDisplay.getCompanyId();
ExpandoTable expandoTable=checkTable(companyId, expandoTableName);
_log.info("expandoTable"+expandoTable.getTableId());
if(expandoTable!= null){
long classPK = CounterLocalServiceUtil. increment(LiferayExpandoAPIAction. class.getName());
ExpandoValueLocalServiceUtil. addValue(companyId, LiferayExpandoAPIAction. class.getName(), expandoTableName,
columnNames[0],classPK, firstNameValue);
ExpandoValueLocalServiceUtil. addValue(companyId, LiferayExpandoAPIAction. class.getName(), expandoTableName,
columnNames[1],classPK, lastNameValue);
ExpandoValueLocalServiceUtil. addValue(companyId, LiferayExpandoAPIAction. class.getName(), expandoTableName,
columnNames[2],classPK, employeeDesignation);
ExpandoValueLocalServiceUtil. addValue(companyId, LiferayExpandoAPIAction. class.getName(), expandoTableName,
columnNames[3],classPK, emailAddressValue);
ExpandoValueLocalServiceUtil. addValue(companyId, LiferayExpandoAPIAction. class.getName(), expandoTableName,
columnNames[4],classPK, employeeAgeValue);
ExpandoValueLocalServiceUtil. addValue(companyId, LiferayExpandoAPIAction. class.getName(), expandoTableName,
columnNames[5],classPK, phoneNumberValue);
dataAdedSuccess= true;
}
if (dataAdedSuccess) {
// adding success message
SessionMessages. add(actionRequest.getPortletSession(),
"employee-add-success");
_log.info("Employee have been added successfylly");
}
// navigate to add student jsp page
actionResponse.setRenderParameter("mvcPath",
"/html/jsps/add_employee.jsp");
} catch (Exception e) {
SessionErrors. add(actionRequest.getPortletSession(),
"employee-add-error");
e.printStackTrace();
}
}
public ExpandoTable addTable( long companyId, String tableName)
throws PortalException, SystemException {
ExpandoTable expandoTable=ExpandoTableLocalServiceUtil. addTable(
companyId, LiferayExpandoAPIAction. class.getName(), tableName);
_log.error("Expando Table Created Successfully.");
return expandoTable;
}
public ExpandoTable checkTable( long companyId, String tableName)
throws Exception {
ExpandoTable expandoTable = null;
try {
expandoTable = ExpandoTableLocalServiceUtil. getTable(
companyId, LiferayExpandoAPIAction. class.getName(), tableName);
}
catch (NoSuchTableException nste) {
expandoTable = addTable(companyId, tableName);
for(String columnName: columnNames){
if(columnName.equals("Age")){
ExpandoColumnLocalServiceUtil. addColumn(
expandoTable.getTableId(), columnName,
ExpandoColumnConstants. INTEGER);
} else{
ExpandoColumnLocalServiceUtil. addColumn(
expandoTable.getTableId(), columnName,
ExpandoColumnConstants. STRING);
}
_log.error("Expando Column"+columnName+"Created Successfully.");
}
}
return expandoTable;
}
}
|




