package com.ebizwindow.crm.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.ebizwindow.crm.constants.PortletIDs; 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.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.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(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; } public static PortletURL getPortletURL(PortletRequest 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; } public static PortletURL getPortletURLInLayout(ActionRequest actionRequest) { String portletId = PortalUtil.getPortletId(actionRequest); ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest .getAttribute(WebKeys.THEME_DISPLAY); Layout layout = themeDisplay.getLayout(); long plid = layout.getPlid(); 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); } _log.debug("redirectURL : " + redirectURL.toString()); return redirectURL; } public static String getObjDetailURL(PortletRequest portletRequest, String portletId, long entityId) throws SystemException, PortalException { long groupId = PortalUtil.getScopeGroupId(portletRequest); int lifeCycle = 1; String windowState = WindowState.NORMAL.toString(); String portletMode = PortletMode.VIEW.toString(); String detailURL = PortletURLUtil.getPortletURLByGroupId( portletRequest, groupId, portletId, lifeCycle, windowState, portletMode); if (portletId.equals(PortletIDs.CUSTOMER_DETAIL)) { detailURL = detailURL + "&customerId=" + entityId; } else if (portletId.equals(PortletIDs.CONTACT_DETAIL)) { detailURL = detailURL + "&contactId=" + entityId; } else if (portletId.equals(PortletIDs.MARKET_DETAIL)) { detailURL = detailURL + "&marketId=" + entityId; } else if (portletId.equals(PortletIDs.CLUE_DETAIL)) { detailURL = detailURL + "&clueId=" + entityId; } else if (portletId.equals(PortletIDs.OPPORTUNITY_DETAIL)) { detailURL = detailURL + "&opportunityId=" + entityId; } else if (portletId.equals(PortletIDs.QUOTATION_DETAIL)) { detailURL = detailURL + ""ationId=" + entityId; } else if (portletId.equals(PortletIDs.CONTRACT_DETAIL)) { detailURL = detailURL + "&contractId=" + entityId; } else if (portletId.equals(PortletIDs.ACTIVITY_DETAIL)) { detailURL = detailURL + "&activityId=" + entityId; } else if (portletId.equals(PortletIDs.PRODUCT)) { detailURL = detailURL + "&productIds" + entityId; } else if (portletId.equals(PortletIDs.ORDER_DETAIL)) { detailURL = detailURL + "&orderId=" + entityId; } detailURL = detailURL + "&operation=load"; return detailURL; } @SuppressWarnings("unused") 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; } } } public static String getReturnUrl(ActionRequest actionRequest, String portletId, String layoutId) throws PortalException, SystemException { long groupId = PortalUtil.getScopeGroupId(actionRequest); int lifeCycle = 0; String portletMode = PortletMode.VIEW.toString(); String windowState = WindowState.NORMAL.toString(); String url = getPortletURLByGroupId(actionRequest, groupId, portletId, lifeCycle, windowState, portletMode); StringBuffer sb = new StringBuffer(url); sb.append("&layoutId=").append(layoutId) .append("&fileLarge=fileLarge"); return sb.toString(); } }