001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.LayoutSet;
026    import com.liferay.portal.model.LayoutWrapper;
027    import com.liferay.portal.model.VirtualLayoutConstants;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.WebKeys;
030    
031    import java.util.Locale;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    /**
036     * @author Raymond Aug??
037     */
038    public class VirtualLayout extends LayoutWrapper {
039    
040            public VirtualLayout(Layout sourceLayout, Group targetGroup) {
041                    super(sourceLayout);
042    
043                    _sourceLayout = sourceLayout;
044                    _targetGroup = targetGroup;
045            }
046    
047            @Override
048            public Object clone() {
049                    return new VirtualLayout((Layout)_sourceLayout.clone(), _targetGroup);
050            }
051    
052            @Override
053            public String getFriendlyURL() {
054                    return getFriendlyURL(null);
055            }
056    
057            @Override
058            public String getFriendlyURL(Locale locale) {
059                    StringBundler sb = new StringBundler(4);
060    
061                    sb.append(VirtualLayoutConstants.CANONICAL_URL_SEPARATOR);
062    
063                    try {
064                            Group group = _sourceLayout.getGroup();
065    
066                            sb.append(group.getFriendlyURL());
067                    }
068                    catch (Exception e) {
069                            _log.error(e, e);
070                    }
071    
072                    if (locale == null) {
073                            sb.append(_sourceLayout.getFriendlyURL());
074                    }
075                    else {
076                            sb.append(_sourceLayout.getFriendlyURL(locale));
077                    }
078    
079                    return sb.toString();
080            }
081    
082            @Override
083            public Group getGroup() {
084                    return getHostGroup();
085            }
086    
087            @Override
088            public long getGroupId() {
089                    return getVirtualGroupId();
090            }
091    
092            public Group getHostGroup() {
093                    return _targetGroup;
094            }
095    
096            @Override
097            public LayoutSet getLayoutSet() {
098                    if (isPrivateLayout()) {
099                            return _targetGroup.getPrivateLayoutSet();
100                    }
101    
102                    return _targetGroup.getPublicLayoutSet();
103            }
104    
105            @Override
106            public String getRegularURL(HttpServletRequest request)
107                    throws PortalException, SystemException {
108    
109                    String layoutURL = _sourceLayout.getRegularURL(request);
110    
111                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
112                            WebKeys.THEME_DISPLAY);
113    
114                    return injectVirtualGroupURL(layoutURL, themeDisplay.getLocale());
115            }
116    
117            @Override
118            public String getResetLayoutURL(HttpServletRequest request)
119                    throws PortalException, SystemException {
120    
121                    String layoutURL = _sourceLayout.getResetLayoutURL(request);
122    
123                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
124                            WebKeys.THEME_DISPLAY);
125    
126                    return injectVirtualGroupURL(layoutURL, themeDisplay.getLocale());
127            }
128    
129            @Override
130            public String getResetMaxStateURL(HttpServletRequest request)
131                    throws PortalException, SystemException {
132    
133                    String layoutURL = _sourceLayout.getResetMaxStateURL(request);
134    
135                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
136                            WebKeys.THEME_DISPLAY);
137    
138                    return injectVirtualGroupURL(layoutURL, themeDisplay.getLocale());
139            }
140    
141            public long getSourceGroupId() {
142                    return _sourceLayout.getGroupId();
143            }
144    
145            public Layout getSourceLayout() {
146                    return _sourceLayout;
147            }
148    
149            public long getVirtualGroupId() {
150                    return _targetGroup.getGroupId();
151            }
152    
153            protected String injectVirtualGroupURL(String layoutURL, Locale locale) {
154                    if (_sourceLayout.isTypeURL()) {
155                            return layoutURL;
156                    }
157    
158                    try {
159                            Group group = _sourceLayout.getGroup();
160    
161                            StringBundler sb = new StringBundler(4);
162    
163                            int pos = layoutURL.indexOf(group.getFriendlyURL());
164    
165                            sb.append(layoutURL.substring(0, pos));
166                            sb.append(_targetGroup.getFriendlyURL());
167                            sb.append(getFriendlyURL(locale));
168    
169                            pos = layoutURL.indexOf(StringPool.QUESTION);
170    
171                            if (pos > 0) {
172                                    sb.append(layoutURL.substring(pos));
173                            }
174    
175                            return sb.toString();
176                    }
177                    catch (Exception e) {
178                            throw new IllegalStateException(e);
179                    }
180            }
181    
182            private static Log _log = LogFactoryUtil.getLog(VirtualLayout.class);
183    
184            private Layout _sourceLayout;
185            private Group _targetGroup;
186    
187    }