com.liferay.layout.admin.web.internal.portlet.action.EditLayoutSetMVCActionCommand.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.layout.admin.web.internal.portlet.action.EditLayoutSetMVCActionCommand.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.layout.admin.web.internal.portlet.action;

import com.liferay.document.library.kernel.service.DLAppLocalService;
import com.liferay.layout.admin.constants.LayoutAdminPortletKeys;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.model.LayoutSet;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.service.GroupLocalService;
import com.liferay.portal.kernel.service.GroupService;
import com.liferay.portal.kernel.service.LayoutSetLocalService;
import com.liferay.portal.kernel.service.LayoutSetService;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PropertiesParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnicodeProperties;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Eudaldo Alonso
 */
@Component(immediate = true, property = { "javax.portlet.name=" + LayoutAdminPortletKeys.GROUP_PAGES,
        "mvc.command.name=/layout/edit_layout_set" }, service = MVCActionCommand.class)
public class EditLayoutSetMVCActionCommand extends BaseMVCActionCommand {

    @Override
    protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        long layoutSetId = ParamUtil.getLong(actionRequest, "layoutSetId");

        long liveGroupId = ParamUtil.getLong(actionRequest, "liveGroupId");
        long stagingGroupId = ParamUtil.getLong(actionRequest, "stagingGroupId");
        boolean privateLayout = ParamUtil.getBoolean(actionRequest, "privateLayout");

        LayoutSet layoutSet = _layoutSetLocalService.getLayoutSet(layoutSetId);

        updateLogo(actionRequest, liveGroupId, stagingGroupId, privateLayout);

        updateLookAndFeel(actionRequest, themeDisplay.getCompanyId(), liveGroupId, stagingGroupId, privateLayout,
                layoutSet.getSettingsProperties());

        updateMergePages(actionRequest, liveGroupId);

        updateRobots(actionRequest, liveGroupId, privateLayout);

        updateSettings(actionRequest, liveGroupId, stagingGroupId, privateLayout,
                layoutSet.getSettingsProperties());
    }

    protected void updateLogo(ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
            boolean privateLayout) throws Exception {

        boolean deleteLogo = ParamUtil.getBoolean(actionRequest, "deleteLogo");

        byte[] logoBytes = null;

        long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");

        if (fileEntryId > 0) {
            FileEntry fileEntry = _dlAppLocalService.getFileEntry(fileEntryId);

            logoBytes = FileUtil.getBytes(fileEntry.getContentStream());
        }

        long groupId = liveGroupId;

        if (stagingGroupId > 0) {
            groupId = stagingGroupId;
        }

        _layoutSetService.updateLogo(groupId, privateLayout, !deleteLogo, logoBytes);
    }

    protected void updateLookAndFeel(ActionRequest actionRequest, long companyId, long liveGroupId,
            long stagingGroupId, boolean privateLayout, UnicodeProperties typeSettingsProperties) throws Exception {

        String[] devices = StringUtil.split(ParamUtil.getString(actionRequest, "devices"));

        for (String device : devices) {
            String deviceThemeId = ParamUtil.getString(actionRequest, device + "ThemeId");
            String deviceColorSchemeId = ParamUtil.getString(actionRequest, device + "ColorSchemeId");
            String deviceCss = ParamUtil.getString(actionRequest, device + "Css");

            if (Validator.isNotNull(deviceThemeId)) {
                deviceColorSchemeId = _actionUtil.getColorSchemeId(companyId, deviceThemeId, deviceColorSchemeId);

                _actionUtil.updateThemeSettingsProperties(actionRequest, companyId, typeSettingsProperties, device,
                        deviceThemeId, false);
            }

            long groupId = liveGroupId;

            if (stagingGroupId > 0) {
                groupId = stagingGroupId;
            }

            _layoutSetService.updateLookAndFeel(groupId, privateLayout, deviceThemeId, deviceColorSchemeId,
                    deviceCss);
        }
    }

    protected void updateMergePages(ActionRequest actionRequest, long liveGroupId) throws Exception {

        boolean mergeGuestPublicPages = ParamUtil.getBoolean(actionRequest, "mergeGuestPublicPages");

        Group liveGroup = _groupLocalService.getGroup(liveGroupId);

        UnicodeProperties typeSettingsProperties = liveGroup.getTypeSettingsProperties();

        typeSettingsProperties.setProperty("mergeGuestPublicPages", String.valueOf(mergeGuestPublicPages));

        _groupService.updateGroup(liveGroupId, liveGroup.getTypeSettings());
    }

    protected void updateRobots(ActionRequest actionRequest, long liveGroupId, boolean privateLayout)
            throws Exception {

        Group liveGroup = _groupLocalService.getGroup(liveGroupId);

        UnicodeProperties typeSettingsProperties = liveGroup.getTypeSettingsProperties();

        String propertyName = "false-robots.txt";

        if (privateLayout) {
            propertyName = "true-robots.txt";
        }

        String robots = ParamUtil.getString(actionRequest, "robots",
                liveGroup.getTypeSettingsProperty(propertyName));

        typeSettingsProperties.setProperty(propertyName, robots);

        _groupService.updateGroup(liveGroup.getGroupId(), typeSettingsProperties.toString());
    }

    protected void updateSettings(ActionRequest actionRequest, long liveGroupId, long stagingGroupId,
            boolean privateLayout, UnicodeProperties settingsProperties) throws Exception {

        UnicodeProperties typeSettingsProperties = PropertiesParamUtil.getProperties(actionRequest,
                "TypeSettingsProperties--");

        settingsProperties.putAll(typeSettingsProperties);

        long groupId = liveGroupId;

        if (stagingGroupId > 0) {
            groupId = stagingGroupId;
        }

        _layoutSetService.updateSettings(groupId, privateLayout, settingsProperties.toString());
    }

    @Reference
    private ActionUtil _actionUtil;

    @Reference
    private DLAppLocalService _dlAppLocalService;

    @Reference
    private GroupLocalService _groupLocalService;

    @Reference
    private GroupService _groupService;

    @Reference
    private LayoutSetLocalService _layoutSetLocalService;

    @Reference
    private LayoutSetService _layoutSetService;

}