/** * 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.dynamic.data.mapping.web.internal.portlet.action; import com.liferay.dynamic.data.mapping.constants.DDMPortletKeys; import com.liferay.dynamic.data.mapping.exception.TemplateScriptException; import com.liferay.dynamic.data.mapping.model.DDMTemplate; import com.liferay.dynamic.data.mapping.service.DDMTemplateService; import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand; import com.liferay.portal.kernel.service.ServiceContext; import com.liferay.portal.kernel.service.ServiceContextFactory; import com.liferay.portal.kernel.template.TemplateConstants; import com.liferay.portal.kernel.upload.UploadPortletRequest; import com.liferay.portal.kernel.util.ContentTypes; import com.liferay.portal.kernel.util.FileUtil; import com.liferay.portal.kernel.util.LocalizationUtil; import com.liferay.portal.kernel.util.MimeTypesUtil; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.Portal; import com.liferay.portal.kernel.util.PortletKeys; import com.liferay.portal.kernel.util.Validator; import java.io.File; import java.util.Locale; import java.util.Map; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; /** * @author Leonardo Barros */ @Component( immediate = true, property = { "javax.portlet.name=" + DDMPortletKeys.DYNAMIC_DATA_MAPPING, "javax.portlet.name=" + PortletKeys.PORTLET_DISPLAY_TEMPLATE, "mvc.command.name=addTemplate" }, service = MVCActionCommand.class ) public class AddTemplateMVCActionCommand extends DDMBaseMVCActionCommand { protected DDMTemplate addTemplate(ActionRequest actionRequest) throws Exception { UploadPortletRequest uploadPortletRequest = portal.getUploadPortletRequest(actionRequest); long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId"); long classNameId = ParamUtil.getLong( uploadPortletRequest, "classNameId"); long classPK = ParamUtil.getLong(uploadPortletRequest, "classPK"); long resourceClassNameId = ParamUtil.getLong( uploadPortletRequest, "resourceClassNameId"); String templateKey = ParamUtil.getString( uploadPortletRequest, "templateKey"); Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap( uploadPortletRequest, "name"); Map<Locale, String> descriptionMap = LocalizationUtil.getLocalizationMap( uploadPortletRequest, "description"); String type = ParamUtil.getString(uploadPortletRequest, "type"); String mode = ParamUtil.getString(uploadPortletRequest, "mode"); String language = ParamUtil.getString( uploadPortletRequest, "language", TemplateConstants.LANG_TYPE_VM); String script = getScript(uploadPortletRequest); boolean cacheable = ParamUtil.getBoolean( uploadPortletRequest, "cacheable"); boolean smallImage = ParamUtil.getBoolean( uploadPortletRequest, "smallImage"); String smallImageURL = ParamUtil.getString( uploadPortletRequest, "smallImageURL"); File smallImageFile = uploadPortletRequest.getFile("smallImageFile"); ServiceContext serviceContext = ServiceContextFactory.getInstance( DDMTemplate.class.getName(), uploadPortletRequest); return ddmTemplateService.addTemplate( groupId, classNameId, classPK, resourceClassNameId, templateKey, nameMap, descriptionMap, type, mode, language, script, cacheable, smallImage, smallImageURL, smallImageFile, serviceContext); } @Override protected void doProcessAction( ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { DDMTemplate template = addTemplate(actionRequest); updatePortletPreferences(actionRequest, template); addSuccessMessage(actionRequest, actionResponse); setRedirectAttribute(actionRequest, template); } protected String getFileScriptContent( UploadPortletRequest uploadPortletRequest) throws Exception { File file = uploadPortletRequest.getFile("script"); if (file == null) { return null; } String fileScriptContent = FileUtil.read(file); String contentType = MimeTypesUtil.getContentType(file); if (Validator.isNotNull(fileScriptContent) && !isValidContentType(contentType)) { throw new TemplateScriptException( "Invalid contentType " + contentType); } return fileScriptContent; } protected String getScript(UploadPortletRequest uploadPortletRequest) throws Exception { String fileScriptContent = getFileScriptContent(uploadPortletRequest); if (Validator.isNotNull(fileScriptContent)) { return fileScriptContent; } return ParamUtil.getString(uploadPortletRequest, "scriptContent"); } protected boolean isValidContentType(String contentType) { if (contentType.equals(ContentTypes.APPLICATION_XSLT_XML) || contentType.startsWith(ContentTypes.TEXT)) { return true; } return false; } @Reference(unbind = "-") protected void setDDMTemplateService( DDMTemplateService ddmTemplateService) { this.ddmTemplateService = ddmTemplateService; } protected DDMTemplateService ddmTemplateService; @Reference protected Portal portal; }