在写增删改查的时候,自己动手写了几个通用的工具类,这几个通用的工具类也是基于API写的

第一个是liferay中的分页。跟我们做普通的web开发,分页是一样的首先需要建立一个分页的实体的类

package com.ebizwindow.example.utils;

public class Page {

	private int pageCount;
private int pageNo;
private int pageSize; public Page() {
} public int getPageCount() {
return pageCount;
} public void setPageCount(int pageCount) {
this.pageCount = pageCount;
} public int getPageNo() {
return pageNo;
} public void setPageNo(int pageNo) {
this.pageNo = pageNo;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}

  第二个是实现分页的逻辑代码

	private void doList(PortletRequest portletRequest) throws SystemException {
long comapnyId = PortalUtil.getCompanyId(portletRequest);
List<Orenge> results = OrengeLocalServiceUtil.searchByCompanyId(
comapnyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
int pageCount = results.size();
Page page = new Page();
page.setPageSize(2);
int pageNo = ParamUtil.getInteger(portletRequest, "pageNo", 0);
if (pageNo == 0) {
if (portletRequest.getPortletSession().getAttribute("pageNo") != null) {
pageNo = (Integer) portletRequest.getPortletSession().getAttribute("pageNo");
} else {
pageNo = 1;
}
}
page.setPageNo(pageNo);
if (pageCount % page.getPageSize() == 0) {
page.setPageCount(pageCount / page.getPageSize());
} else {
page.setPageCount(pageCount / page.getPageSize() + 1);
}
int start = (page.getPageNo() - 1) * page.getPageSize() ;
int end = page.getPageSize() * page.getPageNo() ;
results = OrengeLocalServiceUtil.searchByCompanyId(comapnyId, start, end); portletRequest.setAttribute("results", results);
portletRequest.setAttribute("pageCount", page.getPageCount());
portletRequest.getPortletSession().setAttribute("pageNo", pageNo); }

  第二个通用的工具类是我们跳转的其他的portlet的Portlet路径的工具类

/**
*
*/
package com.ebizwindow.example.utils; import java.security.Key;
import java.util.List; import javax.portlet.ActionRequest;
import javax.portlet.PortletMode;
import javax.portlet.PortletModeException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;
import javax.portlet.WindowState;
import javax.portlet.WindowStateException;
import javax.servlet.http.HttpServletRequest; 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.util.HttpUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnicodeProperties;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Layout;
import com.liferay.portal.model.PortletConstants;
import com.liferay.portal.model.User;
import com.liferay.portal.security.auth.AuthTokenUtil;
import com.liferay.portal.service.LayoutLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.PortletURLFactoryUtil;
import com.liferay.util.Encryptor;
import com.liferay.util.EncryptorException; /**
* @author Ivan Li
*
*/
public class PortletURLUtil { private static Log _log = LogFactoryUtil.getLog(PortletURLUtil.class); public static String getPortletURLWithinCurrentPage(
PortletRequest portletRequest, String portletId, int lifeCycle,
String windowState, String portletMode) throws PortalException,
SystemException {
String portletURL = null; HttpServletRequest request = PortalUtil.getHttpServletRequest(portletRequest); ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY); String targetPortletId = null;
String rootPortletId = PortletConstants.getRootPortletId(portletId); Layout layout = themeDisplay.getLayout();
if (layout != null) { UnicodeProperties typeSettings = layout.getTypeSettingsProperties(); labelBreak:
for(String key : typeSettings.keySet()) {
String value = typeSettings.get(key);
int idx = value.indexOf(rootPortletId);
if (idx >= 0) {
String[] portletIds = StringUtil.split(value);
for(String id : portletIds) {
if (id.contains(rootPortletId)) {
targetPortletId = id;
break labelBreak;
}
}
}
} StringBuilder sb = new StringBuilder();
String layoutURL = PortalUtil.getLayoutFriendlyURL(layout, themeDisplay); sb.append(layoutURL);
sb.append(StringPool.QUESTION).append("p_p_id").append(StringPool.EQUAL).append(targetPortletId);
sb.append(StringPool.AMPERSAND).append("p_p_lifecycle").append(StringPool.EQUAL).append(lifeCycle);
sb.append(StringPool.AMPERSAND).append("p_p_state").append(StringPool.EQUAL).append(windowState);
sb.append(StringPool.AMPERSAND).append("p_p_mode").append(StringPool.EQUAL).append(portletMode); portletURL = sb.toString(); } else {
_log.debug("Get portlet URL by portletId within the current page!");
}
return portletURL;
} public static String getPortletURLByUserId(PortletRequest portletRequest, long userId, String portletId) throws PortalException, SystemException {
User user = UserLocalServiceUtil.getUserById(userId);
long groupId = user.getGroup().getGroupId(); return getPortletURLByGroupId(portletRequest, groupId, portletId, 1, WindowState.MAXIMIZED.toString(), PortletMode.VIEW.toString());
} public static String getPortletURLByGroupId(PortletRequest portletRequest,
long groupId, String portletId, int lifeCycle, String state,
String mode) throws SystemException, PortalException {
String portletURL = null;
Layout selectedLayout = null;
String targetPortletId = null;
String rootPortletId = PortletConstants.getRootPortletId(portletId);
List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(groupId, false);
if (layouts != null){
labelBreak:
for(Layout layout : layouts) {
UnicodeProperties typeSettings = layout.getTypeSettingsProperties();
for(String key : typeSettings.keySet()) {
String value = typeSettings.get(key);
int idx = value.indexOf(rootPortletId);
if (idx >= 0) {
String[] portletIds = StringUtil.split(value);
for(String id : portletIds) {
if (id.contains(rootPortletId)) {
selectedLayout = layout;
targetPortletId = id;
break labelBreak;
}
}
}
}
}
} if (selectedLayout != null) {
StringBuilder sb = new StringBuilder();
HttpServletRequest request = PortalUtil.getHttpServletRequest(portletRequest);
ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(WebKeys.THEME_DISPLAY);
String layoutURL = PortalUtil.getLayoutFriendlyURL(selectedLayout, themeDisplay); sb.append(layoutURL); sb.append(StringPool.QUESTION).append("p_auth").append(StringPool.EQUAL);
sb.append(processValue(null, AuthTokenUtil.getToken(request))); sb.append(StringPool.AMPERSAND).append("p_p_id").append(StringPool.EQUAL).append(targetPortletId);
sb.append(StringPool.AMPERSAND).append("p_p_lifecycle").append(StringPool.EQUAL).append(lifeCycle);
sb.append(StringPool.AMPERSAND).append("p_p_state").append(StringPool.EQUAL).append(state);
sb.append(StringPool.AMPERSAND).append("p_p_mode").append(StringPool.EQUAL).append(mode); portletURL = sb.toString();
} else {
_log.debug("Get Portlet URL By UserId and portletId with empty result!");
} return portletURL;
} public static PortletURL getPortletURL(ActionRequest actionRequest) throws PortalException, SystemException {
long groupId = PortalUtil.getScopeGroupId(actionRequest);
String portletId = PortalUtil.getPortletId(actionRequest);
long plid = PortalUtil.getPlidFromPortletId(groupId, portletId);
PortletURL redirectURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest),
portletId, plid, PortletRequest.ACTION_PHASE);
try {
redirectURL.setPortletMode(actionRequest.getPortletMode());
redirectURL.setWindowState(actionRequest.getWindowState());
} catch (PortletModeException e) {
_log.error(e);
} catch (WindowStateException e) {
_log.error(e);
}
return redirectURL;
} private static String processValue(Key key, String value) {
if (key == null) {
return HttpUtil.encodeURL(value);
} else {
try {
return HttpUtil.encodeURL(Encryptor.encrypt(key, value));
} catch (EncryptorException ee) {
return value;
}
}
} }

  

05-11 19:35