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.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.ResourceBundleLoader; import com.liferay.portal.kernel.util.UnicodeProperties; import com.liferay.portal.kernel.util.WebKeys; 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_content_layout" }, service = MVCActionCommand.class) public class AddContentLayoutMVCActionCommand 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"); boolean privateLayout = ParamUtil.getBoolean(actionRequest, "privateLayout"); long parentLayoutId = ParamUtil.getLong(actionRequest, "parentLayoutId"); String name = ParamUtil.getString(actionRequest, "name"); Map<Locale, String> nameMap = new HashMap<>(); nameMap.put(themeDisplay.getLocale(), name); long layoutPageTemplateEntryId = ParamUtil.getLong(actionRequest, "layoutPageTemplateEntryId"); UnicodeProperties typeSettingsProperties = new UnicodeProperties(); typeSettingsProperties.setProperty("layoutPageTemplateEntryId", String.valueOf(layoutPageTemplateEntryId)); ServiceContext serviceContext = ServiceContextFactory.getInstance(Layout.class.getName(), actionRequest); JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); try { _layoutService.addLayout(groupId, privateLayout, parentLayoutId, nameMap, new HashMap<Locale, String>(), new HashMap<Locale, String>(), new HashMap<Locale, String>(), new HashMap<Locale, String>(), "content", typeSettingsProperties.toString(), false, new HashMap<Locale, String>(), serviceContext); 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(AddContentLayoutMVCActionCommand.class); @Reference private LayoutService _layoutService; @Reference private Portal _portal; @Reference(target = "(bundle.symbolic.name=com.liferay.layout.admin.web)", unbind = "-") private ResourceBundleLoader _resourceBundleLoader; }