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.PropsKeys;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.CacheField;
026    import com.liferay.portal.model.ColorScheme;
027    import com.liferay.portal.model.Company;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.model.LayoutSet;
030    import com.liferay.portal.model.LayoutSetPrototype;
031    import com.liferay.portal.model.Theme;
032    import com.liferay.portal.model.VirtualHost;
033    import com.liferay.portal.service.CompanyLocalServiceUtil;
034    import com.liferay.portal.service.GroupLocalServiceUtil;
035    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
036    import com.liferay.portal.service.ThemeLocalServiceUtil;
037    import com.liferay.portal.service.VirtualHostLocalServiceUtil;
038    import com.liferay.portal.util.PrefsPropsUtil;
039    import com.liferay.portal.util.PropsValues;
040    
041    import java.io.IOException;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Jorge Ferrer
046     */
047    public class LayoutSetImpl extends LayoutSetBaseImpl {
048    
049            public LayoutSetImpl() {
050            }
051    
052            @Override
053            public ColorScheme getColorScheme() throws SystemException {
054                    return ThemeLocalServiceUtil.getColorScheme(
055                            getCompanyId(), getTheme().getThemeId(), getColorSchemeId(), false);
056            }
057    
058            @Override
059            public String getCompanyFallbackVirtualHostname() {
060                    if (_companyFallbackVirtualHostname != null) {
061                            return _companyFallbackVirtualHostname;
062                    }
063    
064                    _companyFallbackVirtualHostname = StringPool.BLANK;
065    
066                    if (Validator.isNotNull(
067                                    PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME) &&
068                            !isPrivateLayout()) {
069    
070                            try {
071                                    Group group = GroupLocalServiceUtil.fetchGroup(
072                                            getCompanyId(),
073                                            PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);
074    
075                                    if ((group != null) && (getGroupId() == group.getGroupId())) {
076                                            Company company = CompanyLocalServiceUtil.fetchCompany(
077                                                    getCompanyId());
078    
079                                            if (company != null) {
080                                                    _companyFallbackVirtualHostname =
081                                                            company.getVirtualHostname();
082                                            }
083                                    }
084                            }
085                            catch (SystemException se) {
086                            }
087                    }
088    
089                    return _companyFallbackVirtualHostname;
090            }
091    
092            @Override
093            public Group getGroup() throws PortalException, SystemException {
094                    return GroupLocalServiceUtil.getGroup(getGroupId());
095            }
096    
097            @Override
098            public long getLayoutSetPrototypeId()
099                    throws PortalException, SystemException {
100    
101                    String layoutSetPrototypeUuid = getLayoutSetPrototypeUuid();
102    
103                    if (Validator.isNull(layoutSetPrototypeUuid)) {
104                            return 0;
105                    }
106    
107                    LayoutSetPrototype layoutSetPrototype =
108                            LayoutSetPrototypeLocalServiceUtil.
109                                    getLayoutSetPrototypeByUuidAndCompanyId(
110                                            layoutSetPrototypeUuid, getCompanyId());
111    
112                    return layoutSetPrototype.getLayoutSetPrototypeId();
113            }
114    
115            @Override
116            public long getLiveLogoId() {
117                    long logoId = 0;
118    
119                    Group group = null;
120    
121                    try {
122                            group = getGroup();
123    
124                            if (!group.isStagingGroup()) {
125                                    return logoId;
126                            }
127                    }
128                    catch (Exception e) {
129                            return logoId;
130                    }
131    
132                    Group liveGroup = group.getLiveGroup();
133    
134                    LayoutSet liveLayoutSet = null;
135    
136                    if (isPrivateLayout()) {
137                            liveLayoutSet = liveGroup.getPrivateLayoutSet();
138                    }
139                    else {
140                            liveLayoutSet = liveGroup.getPublicLayoutSet();
141                    }
142    
143                    return liveLayoutSet.getLogoId();
144            }
145    
146            @Override
147            public String getSettings() {
148                    if (_settingsProperties == null) {
149                            return super.getSettings();
150                    }
151                    else {
152                            return _settingsProperties.toString();
153                    }
154            }
155    
156            @Override
157            public UnicodeProperties getSettingsProperties() {
158                    if (_settingsProperties == null) {
159                            _settingsProperties = new UnicodeProperties(true);
160    
161                            try {
162                                    _settingsProperties.load(super.getSettings());
163                            }
164                            catch (IOException ioe) {
165                                    _log.error(ioe, ioe);
166                            }
167                    }
168    
169                    return _settingsProperties;
170            }
171    
172            @Override
173            public String getSettingsProperty(String key) {
174                    UnicodeProperties settingsProperties = getSettingsProperties();
175    
176                    return settingsProperties.getProperty(key);
177            }
178    
179            @Override
180            public Theme getTheme() throws SystemException {
181                    return ThemeLocalServiceUtil.getTheme(
182                            getCompanyId(), getThemeId(), false);
183            }
184    
185            @Override
186            public String getThemeSetting(String key, String device)
187                    throws SystemException {
188    
189                    UnicodeProperties settingsProperties = getSettingsProperties();
190    
191                    String value = settingsProperties.getProperty(
192                            ThemeSettingImpl.namespaceProperty(device, key));
193    
194                    if (value != null) {
195                            return value;
196                    }
197    
198                    Theme theme = getTheme(device);
199    
200                    value = theme.getSetting(key);
201    
202                    return value;
203            }
204    
205            @Override
206            public String getVirtualHostname() {
207                    if (_virtualHostname != null) {
208                            return _virtualHostname;
209                    }
210    
211                    try {
212                            VirtualHost virtualHost =
213                                    VirtualHostLocalServiceUtil.fetchVirtualHost(
214                                            getCompanyId(), getLayoutSetId());
215    
216                            if (virtualHost == null) {
217                                    _virtualHostname = StringPool.BLANK;
218                            }
219                            else {
220                                    _virtualHostname = virtualHost.getHostname();
221                            }
222                    }
223                    catch (Exception e) {
224                            _virtualHostname = StringPool.BLANK;
225                    }
226    
227                    return _virtualHostname;
228            }
229    
230            @Override
231            public ColorScheme getWapColorScheme() throws SystemException {
232                    return ThemeLocalServiceUtil.getColorScheme(
233                            getCompanyId(), getWapTheme().getThemeId(), getWapColorSchemeId(),
234                            true);
235            }
236    
237            @Override
238            public Theme getWapTheme() throws SystemException {
239                    return ThemeLocalServiceUtil.getTheme(
240                            getCompanyId(), getWapThemeId(), true);
241            }
242    
243            @Override
244            public boolean isLayoutSetPrototypeLinkActive() {
245                    if (isLayoutSetPrototypeLinkEnabled() &&
246                            Validator.isNotNull(getLayoutSetPrototypeUuid())) {
247    
248                            return true;
249                    }
250    
251                    return false;
252            }
253    
254            @Override
255            public void setCompanyFallbackVirtualHostname(
256                    String companyFallbackVirtualHostname) {
257    
258                    _companyFallbackVirtualHostname = companyFallbackVirtualHostname;
259            }
260    
261            @Override
262            public void setSettings(String settings) {
263                    _settingsProperties = null;
264    
265                    super.setSettings(settings);
266            }
267    
268            @Override
269            public void setSettingsProperties(UnicodeProperties settingsProperties) {
270                    _settingsProperties = settingsProperties;
271    
272                    super.setSettings(_settingsProperties.toString());
273            }
274    
275            @Override
276            public void setVirtualHostname(String virtualHostname) {
277                    _virtualHostname = virtualHostname;
278            }
279    
280            protected Theme getTheme(String device) throws SystemException {
281                    boolean controlPanel = false;
282    
283                    try {
284                            Group group = getGroup();
285    
286                            controlPanel = group.isControlPanel();
287                    }
288                    catch (Exception e) {
289                    }
290    
291                    if (controlPanel) {
292                            String themeId = PrefsPropsUtil.getString(
293                                    getCompanyId(),
294                                    PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
295    
296                            return ThemeLocalServiceUtil.getTheme(
297                                    getCompanyId(), themeId, !device.equals("regular"));
298                    }
299                    else if (device.equals("regular")) {
300                            return getTheme();
301                    }
302                    else {
303                            return getWapTheme();
304                    }
305            }
306    
307            private static Log _log = LogFactoryUtil.getLog(LayoutSetImpl.class);
308    
309            @CacheField
310            private String _companyFallbackVirtualHostname;
311    
312            private UnicodeProperties _settingsProperties;
313    
314            @CacheField
315            private String _virtualHostname;
316    
317    }