Java tutorial
/** * 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.layout.admin.constants.LayoutAdminPortletKeys; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.json.JSONFactoryUtil; import com.liferay.portal.kernel.json.JSONObject; import com.liferay.portal.kernel.language.LanguageUtil; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.model.Layout; import com.liferay.portal.kernel.model.LayoutTypePortlet; import com.liferay.portal.kernel.portlet.JSONPortletResponseUtil; import com.liferay.portal.kernel.portlet.LiferayPortletResponse; import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand; import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand; import com.liferay.portal.kernel.service.LayoutService; import com.liferay.portal.kernel.service.ServiceContext; import com.liferay.portal.kernel.service.ServiceContextFactory; import com.liferay.portal.kernel.theme.ThemeDisplay; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.Portal; import com.liferay.portal.kernel.util.PropertiesParamUtil; import com.liferay.portal.kernel.util.ResourceBundleLoader; import com.liferay.portal.kernel.util.UnicodeProperties; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.util.PropsValues; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.ResourceBundle; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletURL; 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/add_simple_layout" }, service = MVCActionCommand.class) public class AddSimpleLayoutMVCActionCommand extends BaseMVCActionCommand { @Override protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); long groupId = ParamUtil.getLong(actionRequest, "groupId"); long liveGroupId = ParamUtil.getLong(actionRequest, "liveGroupId"); long stagingGroupId = ParamUtil.getLong(actionRequest, "stagingGroupId"); boolean privateLayout = ParamUtil.getBoolean(actionRequest, "privateLayout"); long parentLayoutId = ParamUtil.getLong(actionRequest, "parentLayoutId"); String name = ParamUtil.getString(actionRequest, "name"); String type = ParamUtil.getString(actionRequest, "type"); Map<Locale, String> nameMap = new HashMap<>(); nameMap.put(themeDisplay.getLocale(), name); ServiceContext serviceContext = ServiceContextFactory.getInstance(Layout.class.getName(), actionRequest); UnicodeProperties typeSettingsProperties = PropertiesParamUtil.getProperties(actionRequest, "TypeSettingsProperties--"); JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); try { Layout layout = _layoutService.addLayout(groupId, privateLayout, parentLayoutId, nameMap, new HashMap<Locale, String>(), new HashMap<Locale, String>(), new HashMap<Locale, String>(), new HashMap<Locale, String>(), type, typeSettingsProperties.toString(), false, new HashMap<Locale, String>(), serviceContext); LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType(); layoutTypePortlet.setLayoutTemplateId(themeDisplay.getUserId(), PropsValues.DEFAULT_LAYOUT_TEMPLATE_ID); _layoutService.updateLayout(groupId, privateLayout, layout.getLayoutId(), layout.getTypeSettings()); _actionUtil.updateLookAndFeel(actionRequest, themeDisplay.getCompanyId(), liveGroupId, stagingGroupId, privateLayout, layout.getLayoutId(), layout.getTypeSettingsProperties()); jsonObject.put("redirectURL", getRedirectURL(actionResponse)); JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject); } catch (PortalException pe) { if (_log.isDebugEnabled()) { _log.debug(pe, pe); } ResourceBundle resourceBundle = _resourceBundleLoader.loadResourceBundle(themeDisplay.getLocale()); jsonObject.put("error", LanguageUtil.get(resourceBundle, "an-unexpected-error-occurred")); JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject); } } protected String getRedirectURL(ActionResponse actionResponse) { LiferayPortletResponse liferayPortletResponse = _portal.getLiferayPortletResponse(actionResponse); PortletURL portletURL = liferayPortletResponse.createRenderURL(); portletURL.setParameter("mvcPath", "/view.jsp"); return portletURL.toString(); } private static final Log _log = LogFactoryUtil.getLog(AddSimpleLayoutMVCActionCommand.class); @Reference private ActionUtil _actionUtil; @Reference private LayoutService _layoutService; @Reference private Portal _portal; @Reference(target = "(bundle.symbolic.name=com.liferay.layout.admin.web)", unbind = "-") private ResourceBundleLoader _resourceBundleLoader; }