2 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
\r
4 * This library is free software; you can redistribute it and/or modify it under
\r
5 * the terms of the GNU Lesser General Public License as published by the Free
\r
6 * Software Foundation; either version 2.1 of the License, or (at your option)
\r
9 * This library is distributed in the hope that it will be useful, but WITHOUT
\r
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
\r
15 package com.liferay.portal.model.impl;
\r
17 import com.liferay.portal.LayoutFriendlyURLException;
\r
18 import com.liferay.portal.NoSuchGroupException;
\r
19 import com.liferay.portal.kernel.exception.PortalException;
\r
20 import com.liferay.portal.kernel.exception.SystemException;
\r
21 import com.liferay.portal.kernel.log.Log;
\r
22 import com.liferay.portal.kernel.log.LogFactoryUtil;
\r
23 import com.liferay.portal.kernel.util.CharPool;
\r
24 import com.liferay.portal.kernel.util.HttpUtil;
\r
25 import com.liferay.portal.kernel.util.ListUtil;
\r
26 import com.liferay.portal.kernel.util.LocaleUtil;
\r
27 import com.liferay.portal.kernel.util.StringPool;
\r
28 import com.liferay.portal.kernel.util.StringUtil;
\r
29 import com.liferay.portal.kernel.util.UnicodeProperties;
\r
30 import com.liferay.portal.kernel.util.Validator;
\r
31 import com.liferay.portal.model.ColorScheme;
\r
32 import com.liferay.portal.model.Group;
\r
33 import com.liferay.portal.model.Layout;
\r
34 import com.liferay.portal.model.LayoutConstants;
\r
35 import com.liferay.portal.model.LayoutOverride;
\r
36 import com.liferay.portal.model.LayoutSet;
\r
37 import com.liferay.portal.model.LayoutType;
\r
38 import com.liferay.portal.model.LayoutTypePortlet;
\r
39 import com.liferay.portal.model.LayoutTypePortletConstants;
\r
40 import com.liferay.portal.model.Portlet;
\r
41 import com.liferay.portal.model.Theme;
\r
42 import com.liferay.portal.security.permission.ActionKeys;
\r
43 import com.liferay.portal.security.permission.PermissionChecker;
\r
44 import com.liferay.portal.service.GroupLocalServiceUtil;
\r
45 import com.liferay.portal.service.LayoutLocalServiceUtil;
\r
46 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
\r
47 import com.liferay.portal.service.PortletLocalServiceUtil;
\r
48 import com.liferay.portal.service.ThemeLocalServiceUtil;
\r
49 import com.liferay.portal.service.permission.LayoutPermissionUtil;
\r
50 import com.liferay.portal.theme.ThemeDisplay;
\r
51 import com.liferay.portal.util.CookieKeys;
\r
52 import com.liferay.portal.util.LayoutClone;
\r
53 import com.liferay.portal.util.LayoutCloneFactory;
\r
54 import com.liferay.portal.util.PortalUtil;
\r
55 import com.liferay.portal.util.PropsValues;
\r
56 import com.liferay.portal.util.WebKeys;
\r
57 import com.liferay.portlet.PortletURLImpl;
\r
58 import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
\r
60 import java.io.IOException;
\r
62 import java.util.ArrayList;
\r
63 import java.util.Iterator;
\r
64 import java.util.List;
\r
65 import java.util.Locale;
\r
66 import java.util.Map;
\r
67 import java.util.Set;
\r
69 import javax.portlet.PortletException;
\r
70 import javax.portlet.PortletMode;
\r
71 import javax.portlet.PortletRequest;
\r
72 import javax.portlet.WindowState;
\r
74 import javax.servlet.http.HttpServletRequest;
\r
77 * @author Brian Wing Shun Chan
\r
79 public class LayoutImpl extends LayoutBaseImpl implements LayoutOverride {
\r
81 public static int validateFriendlyURL(String friendlyURL) {
\r
82 if (friendlyURL.length() < 2) {
\r
83 return LayoutFriendlyURLException.TOO_SHORT;
\r
86 if (friendlyURL.length() > LayoutConstants.FRIENDLY_URL_MAX_LENGTH) {
\r
87 return LayoutFriendlyURLException.TOO_LONG;
\r
90 if (!friendlyURL.startsWith(StringPool.SLASH)) {
\r
91 return LayoutFriendlyURLException.DOES_NOT_START_WITH_SLASH;
\r
94 if (friendlyURL.endsWith(StringPool.SLASH)) {
\r
95 return LayoutFriendlyURLException.ENDS_WITH_SLASH;
\r
98 if (friendlyURL.indexOf(StringPool.DOUBLE_SLASH) != -1) {
\r
99 return LayoutFriendlyURLException.ADJACENT_SLASHES;
\r
102 for (char c : friendlyURL.toCharArray()) {
\r
103 if (!Validator.isChar(c) && !Validator.isDigit(c) &&
\r
104 (c != CharPool.DASH) && (c != CharPool.PERCENT) &&
\r
105 (c != CharPool.PERIOD) && (c != CharPool.PLUS) &&
\r
106 (c != CharPool.SLASH) && (c != CharPool.STAR) &&
\r
107 (c != CharPool.UNDERLINE)) {
\r
109 return LayoutFriendlyURLException.INVALID_CHARACTERS;
\r
116 public static void validateFriendlyURLKeyword(String friendlyURL)
\r
117 throws LayoutFriendlyURLException {
\r
119 for (String keyword : PropsValues.LAYOUT_FRIENDLY_URL_KEYWORDS) {
\r
120 if (StringUtil.endsWith(
\r
121 friendlyURL, StringUtil.quote(keyword, StringPool.SLASH)) ||
\r
122 StringUtil.endsWith(
\r
123 friendlyURL, StringPool.SLASH + keyword)) {
\r
125 LayoutFriendlyURLException lfurle =
\r
126 new LayoutFriendlyURLException(
\r
127 LayoutFriendlyURLException.KEYWORD_CONFLICT);
\r
129 lfurle.setKeywordConflict(keyword);
\r
136 public LayoutImpl() {
\r
139 public List<Layout> getAllChildren() throws SystemException {
\r
140 List<Layout> layouts = new ArrayList<Layout>();
\r
142 Iterator<Layout> itr = getChildren().iterator();
\r
144 while (itr.hasNext()) {
\r
145 Layout layout = itr.next();
\r
147 layouts.add(layout);
\r
148 layouts.addAll(layout.getAllChildren());
\r
154 public long getAncestorLayoutId() throws PortalException, SystemException {
\r
157 Layout layout = this;
\r
160 if (!layout.isRootLayout()) {
\r
161 layout = LayoutLocalServiceUtil.getLayout(
\r
162 layout.getGroupId(), layout.isPrivateLayout(),
\r
163 layout.getParentLayoutId());
\r
166 layoutId = layout.getLayoutId();
\r
175 public long getAncestorPlid() throws PortalException, SystemException {
\r
178 Layout layout = this;
\r
181 if (!layout.isRootLayout()) {
\r
182 layout = LayoutLocalServiceUtil.getLayout(
\r
183 layout.getGroupId(), layout.isPrivateLayout(),
\r
184 layout.getParentLayoutId());
\r
187 plid = layout.getPlid();
\r
196 public List<Layout> getAncestors() throws PortalException, SystemException {
\r
197 List<Layout> layouts = new ArrayList<Layout>();
\r
199 Layout layout = this;
\r
202 if (!layout.isRootLayout()) {
\r
203 layout = LayoutLocalServiceUtil.getLayout(
\r
204 layout.getGroupId(), layout.isPrivateLayout(),
\r
205 layout.getParentLayoutId());
\r
207 layouts.add(layout);
\r
217 public List<Layout> getChildren() throws SystemException {
\r
218 return LayoutLocalServiceUtil.getLayouts(
\r
219 getGroupId(), isPrivateLayout(), getLayoutId());
\r
222 public List<Layout> getChildren(PermissionChecker permissionChecker)
\r
223 throws PortalException, SystemException {
\r
225 List<Layout> layouts = ListUtil.copy(getChildren());
\r
227 Iterator<Layout> itr = layouts.iterator();
\r
229 while (itr.hasNext()) {
\r
230 Layout layout = itr.next();
\r
232 if (layout.isHidden() ||
\r
233 !LayoutPermissionUtil.contains(
\r
234 permissionChecker, layout, ActionKeys.VIEW)) {
\r
243 public ColorScheme getColorScheme()
\r
244 throws PortalException, SystemException {
\r
246 if (isInheritLookAndFeel()) {
\r
247 return getLayoutSet().getColorScheme();
\r
250 return ThemeLocalServiceUtil.getColorScheme(
\r
251 getCompanyId(), getTheme().getThemeId(), getColorSchemeId(),
\r
256 public String getCssText() throws PortalException, SystemException {
\r
257 if (isInheritLookAndFeel()) {
\r
258 return getLayoutSet().getCss();
\r
265 public Group getGroup() throws PortalException, SystemException {
\r
266 return GroupLocalServiceUtil.getGroup(getGroupId());
\r
269 public String getHTMLTitle(Locale locale) {
\r
270 String localeLanguageId = LocaleUtil.toLanguageId(locale);
\r
272 return getHTMLTitle(localeLanguageId);
\r
275 public String getHTMLTitle(String localeLanguageId) {
\r
276 String htmlTitle = getTitle(localeLanguageId);
\r
278 if (Validator.isNull(htmlTitle)) {
\r
279 htmlTitle = getName(localeLanguageId);
\r
285 public LayoutSet getLayoutSet() throws PortalException, SystemException {
\r
286 if (_layoutSet == null) {
\r
287 _layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
\r
288 getGroupId(), isPrivateLayout());
\r
294 public LayoutType getLayoutType() {
\r
295 if (_layoutType == null) {
\r
296 _layoutType = new LayoutTypePortletImpl(this);
\r
299 return _layoutType;
\r
302 public long getParentPlid() throws PortalException, SystemException {
\r
303 if (getParentLayoutId() == LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
\r
307 Layout layout = LayoutLocalServiceUtil.getLayout(
\r
308 getGroupId(), isPrivateLayout(), getParentLayoutId());
\r
310 return layout.getPlid();
\r
313 public String getRegularURL(HttpServletRequest request)
\r
314 throws PortalException, SystemException {
\r
316 return _getURL(request, false, false, false);
\r
319 public String getAjaxURL(HttpServletRequest request)
\r
320 throws SystemException, PortalException {
\r
322 return _getURL(request, false, false, true);
\r
325 public String getResetLayoutURL(HttpServletRequest request)
\r
326 throws PortalException, SystemException {
\r
328 return _getURL(request, true, true, false);
\r
331 public String getResetMaxStateURL(HttpServletRequest request)
\r
332 throws PortalException, SystemException {
\r
334 return _getURL(request, true, false, false);
\r
337 public Group getScopeGroup() throws PortalException, SystemException {
\r
338 Group group = null;
\r
341 group = GroupLocalServiceUtil.getLayoutGroup(
\r
342 getCompanyId(), getPlid());
\r
344 catch (NoSuchGroupException nsge) {
\r
350 public String getTarget() {
\r
351 return PortalUtil.getLayoutTarget(this);
\r
354 public Theme getTheme() throws PortalException, SystemException {
\r
355 if (isInheritLookAndFeel()) {
\r
356 return getLayoutSet().getTheme();
\r
359 return ThemeLocalServiceUtil.getTheme(
\r
360 getCompanyId(), getThemeId(), false);
\r
364 public String getThemeSetting(String key, String device) {
\r
365 UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
\r
367 String value = typeSettingsProperties.getProperty(
\r
368 ThemeSettingImpl.namespaceProperty(device, key));
\r
370 if (value != null) {
\r
375 LayoutSet layoutSet = getLayoutSet();
\r
377 value = layoutSet.getThemeSetting(key, device);
\r
379 catch (Exception e) {
\r
386 public String getTypeSettings() {
\r
387 if (_typeSettingsProperties == null) {
\r
388 return super.getTypeSettings();
\r
391 return _typeSettingsProperties.toString();
\r
395 public UnicodeProperties getTypeSettingsProperties() {
\r
396 if (_typeSettingsProperties == null) {
\r
397 _typeSettingsProperties = new UnicodeProperties(true);
\r
399 _typeSettingsProperties.fastLoad(super.getTypeSettings());
\r
402 return _typeSettingsProperties;
\r
405 public String getTypeSettingsProperty(String key) {
\r
406 UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
\r
408 return typeSettingsProperties.getProperty(key);
\r
411 public String getTypeSettingsProperty(String key, String defaultValue) {
\r
412 UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
\r
414 return typeSettingsProperties.getProperty(key, defaultValue);
\r
417 public ColorScheme getWapColorScheme()
\r
418 throws PortalException, SystemException {
\r
420 if (isInheritLookAndFeel()) {
\r
421 return getLayoutSet().getWapColorScheme();
\r
424 return ThemeLocalServiceUtil.getColorScheme(
\r
425 getCompanyId(), getWapTheme().getThemeId(),
\r
426 getWapColorSchemeId(), true);
\r
430 public Theme getWapTheme() throws PortalException, SystemException {
\r
431 if (isInheritWapLookAndFeel()) {
\r
432 return getLayoutSet().getWapTheme();
\r
435 return ThemeLocalServiceUtil.getTheme(
\r
436 getCompanyId(), getWapThemeId(), true);
\r
440 public boolean hasAncestor(long layoutId)
\r
441 throws PortalException, SystemException {
\r
443 long parentLayoutId = getParentLayoutId();
\r
445 while (parentLayoutId != LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
\r
446 if (parentLayoutId == layoutId) {
\r
450 Layout parentLayout = LayoutLocalServiceUtil.getLayout(
\r
451 getGroupId(), isPrivateLayout(), parentLayoutId);
\r
453 parentLayoutId = parentLayout.getParentLayoutId();
\r
460 public boolean hasChildren() throws SystemException {
\r
461 return LayoutLocalServiceUtil.hasLayouts(
\r
462 getGroupId(), isPrivateLayout(), getLayoutId());
\r
465 public boolean hasScopeGroup() throws PortalException, SystemException {
\r
466 Group group = getScopeGroup();
\r
468 if (group != null) {
\r
476 public boolean isChildSelected(boolean selectable, Layout layout)
\r
477 throws PortalException, SystemException {
\r
480 long plid = getPlid();
\r
482 List<Layout> ancestors = layout.getAncestors();
\r
484 for (Layout curLayout : ancestors) {
\r
485 if (plid == curLayout.getPlid()) {
\r
494 public boolean isContentDisplayPage() {
\r
495 UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
\r
497 String defaultAssetPublisherPortletId =
\r
498 typeSettingsProperties.getProperty(
\r
499 LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID);
\r
501 if (Validator.isNotNull(defaultAssetPublisherPortletId)) {
\r
509 public boolean isFirstChild() {
\r
510 if (getPriority() == 0) {
\r
518 public boolean isFirstParent() {
\r
519 if (isFirstChild() && isRootLayout()) {
\r
527 public boolean isInheritLookAndFeel() {
\r
528 if (Validator.isNull(getThemeId()) ||
\r
529 Validator.isNull(getColorSchemeId())) {
\r
538 public boolean isInheritWapLookAndFeel() {
\r
539 if (Validator.isNull(getWapThemeId()) ||
\r
540 Validator.isNull(getWapColorSchemeId())) {
\r
549 public boolean isLayoutPrototypeLinkActive() {
\r
550 if (isLayoutPrototypeLinkEnabled() &&
\r
551 Validator.isNotNull(getLayoutPrototypeUuid())) {
\r
559 public boolean isPublicLayout() {
\r
560 return !isPrivateLayout();
\r
563 public boolean isRootLayout() {
\r
564 if (getParentLayoutId() == LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
\r
572 public boolean isSelected(
\r
573 boolean selectable, Layout layout, long ancestorPlid) {
\r
576 long plid = getPlid();
\r
578 if ((plid == layout.getPlid()) || (plid == ancestorPlid)) {
\r
586 public boolean isTypeArticle() {
\r
587 if (getType().equals(LayoutConstants.TYPE_ARTICLE)) {
\r
595 public boolean isTypeControlPanel() {
\r
596 if (getType().equals(LayoutConstants.TYPE_CONTROL_PANEL)) {
\r
604 public boolean isTypeEmbedded() {
\r
605 if (getType().equals(LayoutConstants.TYPE_EMBEDDED)) {
\r
613 public boolean isTypeLinkToLayout() {
\r
614 if (getType().equals(LayoutConstants.TYPE_LINK_TO_LAYOUT)) {
\r
622 public boolean isTypePanel() {
\r
623 if (getType().equals(LayoutConstants.TYPE_PANEL)) {
\r
631 public boolean isTypePortlet() {
\r
632 if (getType().equals(LayoutConstants.TYPE_PORTLET)) {
\r
640 public boolean isTypeURL() {
\r
641 if (getType().equals(LayoutConstants.TYPE_URL)) {
\r
650 public void setGroupId(long groupId) {
\r
651 super.setGroupId(groupId);
\r
656 public void setLayoutSet(LayoutSet layoutSet) {
\r
657 _layoutSet = layoutSet;
\r
661 public void setPrivateLayout(boolean privateLayout) {
\r
662 super.setPrivateLayout(privateLayout);
\r
668 public void setTypeSettings(String typeSettings) {
\r
669 _typeSettingsProperties = null;
\r
671 super.setTypeSettings(typeSettings);
\r
674 public void setTypeSettingsProperties(
\r
675 UnicodeProperties typeSettingsProperties) {
\r
677 _typeSettingsProperties = typeSettingsProperties;
\r
679 super.setTypeSettings(_typeSettingsProperties.toString());
\r
682 private LayoutTypePortlet _getLayoutTypePortletClone(
\r
683 HttpServletRequest request)
\r
684 throws IOException {
\r
686 LayoutTypePortlet layoutTypePortlet = null;
\r
688 LayoutClone layoutClone = LayoutCloneFactory.getInstance();
\r
690 if (layoutClone != null) {
\r
691 String typeSettings = layoutClone.get(request, getPlid());
\r
693 if (typeSettings != null) {
\r
694 UnicodeProperties typeSettingsProperties =
\r
695 new UnicodeProperties(true);
\r
697 typeSettingsProperties.load(typeSettings);
\r
699 String stateMax = typeSettingsProperties.getProperty(
\r
700 LayoutTypePortletConstants.STATE_MAX);
\r
701 String stateMin = typeSettingsProperties.getProperty(
\r
702 LayoutTypePortletConstants.STATE_MIN);
\r
704 Layout layout = (Layout)this.clone();
\r
706 layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
\r
708 layoutTypePortlet.setStateMax(stateMax);
\r
709 layoutTypePortlet.setStateMin(stateMin);
\r
713 if (layoutTypePortlet == null) {
\r
714 layoutTypePortlet = (LayoutTypePortlet)getLayoutType();
\r
717 return layoutTypePortlet;
\r
720 private String _getURL(
\r
721 HttpServletRequest request, boolean resetMaxState,
\r
722 boolean resetRenderParameters, boolean ajaxUrl)
\r
723 throws PortalException, SystemException {
\r
725 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
\r
726 WebKeys.THEME_DISPLAY);
\r
728 if (resetMaxState) {
\r
729 Layout layout = themeDisplay.getLayout();
\r
731 LayoutTypePortlet layoutTypePortlet = null;
\r
733 if (layout.equals(this)) {
\r
734 layoutTypePortlet = themeDisplay.getLayoutTypePortlet();
\r
738 layoutTypePortlet = _getLayoutTypePortletClone(request);
\r
740 catch (IOException ioe) {
\r
741 _log.error("Unable to clone layout settings", ioe);
\r
743 layoutTypePortlet = (LayoutTypePortlet)getLayoutType();
\r
747 if (layoutTypePortlet.hasStateMax()) {
\r
748 String portletId = StringUtil.split(
\r
749 layoutTypePortlet.getStateMax())[0];
\r
751 PortletURLImpl portletURLImpl = new PortletURLImpl(
\r
752 request, portletId, getPlid(), PortletRequest.ACTION_PHASE);
\r
755 portletURLImpl.setWindowState(WindowState.NORMAL);
\r
756 portletURLImpl.setPortletMode(PortletMode.VIEW);
\r
758 catch (PortletException pe) {
\r
759 throw new SystemException(pe);
\r
762 portletURLImpl.setAnchor(false);
\r
764 if (PropsValues.LAYOUT_DEFAULT_P_L_RESET &&
\r
765 !resetRenderParameters) {
\r
767 portletURLImpl.setParameter("p_l_reset", "0");
\r
769 else if (!PropsValues.LAYOUT_DEFAULT_P_L_RESET &&
\r
770 resetRenderParameters) {
\r
772 portletURLImpl.setParameter("p_l_reset", "1");
\r
775 return portletURLImpl.toString();
\r
779 String portalURL = PortalUtil.getPortalURL(request);
\r
781 String url = PortalUtil.getLayoutURL(this, themeDisplay);
\r
783 if (!CookieKeys.hasSessionId(request) &&
\r
784 (url.startsWith(portalURL) || url.startsWith(StringPool.SLASH))) {
\r
786 url = PortalUtil.getURLWithSessionId(
\r
787 url, request.getSession().getId());
\r
790 if (!CookieKeys.hasSessionId(request)) {
\r
791 url = PortalUtil.getURLWithSessionId(
\r
792 url, request.getSession().getId());
\r
796 LayoutTypePortlet ltp = (LayoutTypePortlet) this.getLayoutType();
\r
797 List<Portlet> portlets = ltp.getAllPortlets();
\r
798 if(portlets.size() > 0){
\r
800 String viewAjaxJSPKey = ENTMainUtilsLocalServiceUtil.getViewAjaxJSPKey();
\r
802 Portlet portlet = null;
\r
803 Map<String, String> map = null;
\r
804 Set<String> keys = null;
\r
805 for(Portlet p: portlets){
\r
806 map = p.getInitParams();
\r
807 keys = map.keySet();
\r
808 if(keys.contains(viewAjaxJSPKey)){
\r
814 if(Validator.isNotNull(portlet)){
\r
815 url = HttpUtil.addParameter(url, "p_p_id", portlet.getPortletId());
\r
816 url = HttpUtil.addParameter(url, "p_p_lifecycle", 0);
\r
817 url = HttpUtil.addParameter(url, "p_p_state", "exclusive");
\r
818 url = HttpUtil.addParameter(url, "p_p_mode", "view");
\r
819 url = HttpUtil.addParameter(url, "p_p_col_id", "column-1");
\r
820 url = HttpUtil.addParameter(url, "p_p_col_count", 1);
\r
821 url = HttpUtil.addParameter(url, "jspPage", map.get(viewAjaxJSPKey));
\r
826 if (!resetMaxState) {
\r
830 if (PropsValues.LAYOUT_DEFAULT_P_L_RESET && !resetRenderParameters) {
\r
831 url = HttpUtil.addParameter(url, "p_l_reset", 0);
\r
833 else if (!PropsValues.LAYOUT_DEFAULT_P_L_RESET &&
\r
834 resetRenderParameters) {
\r
836 url = HttpUtil.addParameter(url, "p_l_reset", 1);
\r
842 private static Log _log = LogFactoryUtil.getLog(LayoutImpl.class);
\r
844 private LayoutSet _layoutSet;
\r
845 private LayoutType _layoutType;
\r
846 private UnicodeProperties _typeSettingsProperties;
\r