001    /**
002     * Copyright (c) 2000-2012 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.mobile.device.rulegroup.action.impl;
016    
017    import com.liferay.portal.kernel.bean.BeanReference;
018    import com.liferay.portal.kernel.mobile.device.rulegroup.action.ActionHandler;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    import com.liferay.portal.model.ColorScheme;
022    import com.liferay.portal.model.Theme;
023    import com.liferay.portal.model.impl.ColorSchemeImpl;
024    import com.liferay.portal.service.ThemeLocalService;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
029    
030    import java.util.ArrayList;
031    import java.util.Collection;
032    import java.util.Collections;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    /**
038     * @author Edward Han
039     */
040    public class ThemeModificationActionHandler implements ActionHandler {
041    
042            public static String getHandlerType() {
043                    return ThemeModificationActionHandler.class.getName();
044            }
045    
046            public void applyAction(
047                    MDRAction mdrAction, HttpServletRequest request,
048                    HttpServletResponse response) {
049    
050                    long companyId = PortalUtil.getCompanyId(request);
051    
052                    UnicodeProperties typeSettingsProperties =
053                            mdrAction.getTypeSettingsProperties();
054    
055                    String themeId = GetterUtil.getString(
056                            typeSettingsProperties.getProperty("themeId"));
057    
058                    Theme theme = _themeLocalService.fetchTheme(companyId, themeId);
059    
060                    if (theme == null) {
061                            return;
062                    }
063    
064                    request.setAttribute(WebKeys.THEME, theme);
065    
066                    String colorSchemeId = GetterUtil.getString(
067                            typeSettingsProperties.getProperty("colorSchemeId"));
068    
069                    ColorScheme colorScheme = _themeLocalService.fetchColorScheme(
070                            companyId, themeId, colorSchemeId);
071    
072                    if (colorScheme == null) {
073                            colorScheme = ColorSchemeImpl.getNullColorScheme();
074                    }
075    
076                    request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
077    
078                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
079                            WebKeys.THEME_DISPLAY);
080    
081                    themeDisplay.setLookAndFeel(theme, colorScheme);
082            }
083    
084            public Collection<String> getPropertyNames() {
085                    return _propertyNames;
086            }
087    
088            public String getType() {
089                    return getHandlerType();
090            }
091    
092            public void setThemeLocalService(ThemeLocalService themeLocalService) {
093                    _themeLocalService = themeLocalService;
094            }
095    
096            private static Collection<String> _propertyNames;
097    
098            @BeanReference(type = ThemeLocalService.class)
099            private ThemeLocalService _themeLocalService;
100    
101            static {
102                    _propertyNames = new ArrayList<String>(2);
103    
104                    _propertyNames.add("colorSchemeId");
105                    _propertyNames.add("themeId");
106    
107                    _propertyNames = Collections.unmodifiableCollection(_propertyNames);
108            }
109    
110    }