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    
029    import javax.servlet.http.HttpServletRequest;
030    
031    /**
032     * @author Raymond Aug??
033     */
034    public class VirtualLayout extends LayoutWrapper {
035    
036            public VirtualLayout(Layout sourceLayout, Group targetGroup) {
037                    super(sourceLayout);
038    
039                    _sourceLayout = sourceLayout;
040                    _targetGroup = targetGroup;
041            }
042    
043            @Override
044            public Object clone() {
045                    return new VirtualLayout((Layout)_sourceLayout.clone(), _targetGroup);
046            }
047    
048            @Override
049            public String getFriendlyURL() {
050                    StringBundler sb = new StringBundler(4);
051    
052                    sb.append(VirtualLayoutConstants.CANONICAL_URL_SEPARATOR);
053    
054                    try {
055                            Group group = _sourceLayout.getGroup();
056    
057                            sb.append(group.getFriendlyURL());
058                    }
059                    catch (Exception e) {
060                            _log.error(e, e);
061                    }
062    
063                    sb.append(_sourceLayout.getFriendlyURL());
064    
065                    return sb.toString();
066            }
067    
068            @Override
069            public Group getGroup() {
070                    return getHostGroup();
071            }
072    
073            @Override
074            public long getGroupId() {
075                    return getVirtualGroupId();
076            }
077    
078            public Group getHostGroup() {
079                    return _targetGroup;
080            }
081    
082            @Override
083            public LayoutSet getLayoutSet() {
084                    if (isPrivateLayout()) {
085                            return _targetGroup.getPrivateLayoutSet();
086                    }
087    
088                    return _targetGroup.getPublicLayoutSet();
089            }
090    
091            @Override
092            public String getRegularURL(HttpServletRequest request)
093                    throws PortalException, SystemException {
094    
095                    String layoutURL = _sourceLayout.getRegularURL(request);
096    
097                    return injectVirtualGroupURL(layoutURL);
098            }
099    
100            @Override
101            public String getResetLayoutURL(HttpServletRequest request)
102                    throws PortalException, SystemException {
103    
104                    String layoutURL = _sourceLayout.getResetLayoutURL(request);
105    
106                    return injectVirtualGroupURL(layoutURL);
107            }
108    
109            @Override
110            public String getResetMaxStateURL(HttpServletRequest request)
111                    throws PortalException, SystemException {
112    
113                    String layoutURL = _sourceLayout.getResetMaxStateURL(request);
114    
115                    return injectVirtualGroupURL(layoutURL);
116            }
117    
118            public long getSourceGroupId() {
119                    return _sourceLayout.getGroupId();
120            }
121    
122            public Layout getSourceLayout() {
123                    return _sourceLayout;
124            }
125    
126            public long getVirtualGroupId() {
127                    return _targetGroup.getGroupId();
128            }
129    
130            protected String injectVirtualGroupURL(String layoutURL) {
131                    if (_sourceLayout.isTypeURL()) {
132                            return layoutURL;
133                    }
134    
135                    try {
136                            Group group = _sourceLayout.getGroup();
137    
138                            StringBundler sb = new StringBundler(4);
139    
140                            int pos = layoutURL.indexOf(group.getFriendlyURL());
141    
142                            sb.append(layoutURL.substring(0, pos));
143                            sb.append(_targetGroup.getFriendlyURL());
144                            sb.append(getFriendlyURL());
145    
146                            pos = layoutURL.indexOf(StringPool.QUESTION);
147    
148                            if (pos > 0) {
149                                    sb.append(layoutURL.substring(pos));
150                            }
151    
152                            return sb.toString();
153                    }
154                    catch (Exception e) {
155                            throw new IllegalStateException(e);
156                    }
157            }
158    
159            private static Log _log = LogFactoryUtil.getLog(VirtualLayout.class);
160    
161            private Layout _sourceLayout;
162            private Group _targetGroup;
163    
164    }