/**
* 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.portal.workflow.definition.web.internal.portlet.action;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.LocalizationUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortletKeys;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.kernel.workflow.WorkflowDefinition;
import com.liferay.portal.kernel.workflow.WorkflowDefinitionFileException;
import com.liferay.portal.kernel.workflow.WorkflowDefinitionManager;
import com.liferay.portal.kernel.workflow.WorkflowException;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Leonardo Barros
*/
@Component(
immediate = true,
property = {
"javax.portlet.name=" + PortletKeys.WORKFLOW_DEFINITION,
"mvc.command.name=updateWorkflowDefinition"
},
service = MVCActionCommand.class
)
public class UpdateWorkflowDefinitionMVCActionCommand
extends BaseMVCActionCommand {
@Override
public boolean processAction(
ActionRequest actionRequest, ActionResponse actionResponse)
throws PortletException {
try {
doProcessAction(actionRequest, actionResponse);
return SessionErrors.isEmpty(actionRequest);
}
catch (WorkflowException we) {
hideDefaultErrorMessage(actionRequest);
SessionErrors.add(actionRequest, we.getClass());
return false;
}
catch (PortletException pe) {
throw pe;
}
catch (Exception e) {
throw new PortletException(e);
}
}
@Override
protected void doProcessAction(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
WebKeys.THEME_DISPLAY);
String name = ParamUtil.getString(actionRequest, "name");
int version = ParamUtil.getInteger(actionRequest, "version");
Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
actionRequest, "title");
String content = ParamUtil.getString(actionRequest, "content");
if (Validator.isNull(content)) {
throw new WorkflowDefinitionFileException();
}
WorkflowDefinition workflowDefinition =
workflowDefinitionManager.getWorkflowDefinition(
themeDisplay.getCompanyId(), name, version);
if (Objects.equals(workflowDefinition.getContent(), content)) {
workflowDefinitionManager.updateTitle(
themeDisplay.getCompanyId(), themeDisplay.getUserId(), name,
version, getTitle(titleMap));
}
else {
workflowDefinitionManager.deployWorkflowDefinition(
themeDisplay.getCompanyId(), themeDisplay.getUserId(),
getTitle(titleMap), content.getBytes());
}
sendRedirect(actionRequest, actionResponse);
}
protected String getTitle(Map<Locale, String> titleMap) {
if (titleMap == null) {
return null;
}
String value = StringPool.BLANK;
for (Locale locale : LanguageUtil.getAvailableLocales()) {
String languageId = LocaleUtil.toLanguageId(locale);
String title = titleMap.get(locale);
if (Validator.isNotNull(title)) {
value = LocalizationUtil.updateLocalization(
value, "Title", title, languageId);
}
else {
value = LocalizationUtil.removeLocalization(
value, "Title", languageId);
}
}
return value;
}
@Reference(target = "(proxy.bean=false)")
protected WorkflowDefinitionManager workflowDefinitionManager;
}