001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.workflowdefinitionlinks.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.workflow.WorkflowException;
024    import com.liferay.portal.service.WorkflowDefinitionLinkLocalServiceUtil;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.WebKeys;
028    
029    import java.util.Enumeration;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Jorge Ferrer
043     * @author Bruno Farache
044     * @author Juan Fernández
045     */
046    public class EditWorkflowDefinitionLinkAction extends PortletAction {
047    
048            public void processAction(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            ActionRequest actionRequest, ActionResponse actionResponse)
051                    throws Exception {
052    
053                    try {
054                            updateWorkflowDefinitionLinks(actionRequest);
055    
056                            sendRedirect(actionRequest, actionResponse);
057                    }
058                    catch (Exception e) {
059                            if (e instanceof WorkflowException) {
060                                    SessionErrors.add(actionRequest, e.getClass().getName());
061    
062                                    setForward(
063                                            actionRequest, "portlet.workflow_definition_links.error");
064                            }
065                            else {
066                                    throw e;
067                            }
068                    }
069            }
070    
071            public ActionForward render(
072                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
073                            RenderRequest renderRequest, RenderResponse renderResponse)
074                    throws Exception {
075    
076                    return mapping.findForward(getForward(
077                            renderRequest, "portlet.workflow_definition_links.view"));
078            }
079    
080            protected void updateWorkflowDefinitionLink(
081                            ActionRequest actionRequest, String className, String value)
082                    throws Exception {
083    
084                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
085    
086                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
087                            WebKeys.THEME_DISPLAY);
088    
089                    if (Validator.isNull(value)) {
090                            WorkflowDefinitionLinkLocalServiceUtil.deleteWorkflowDefinitionLink(
091                                    themeDisplay.getCompanyId(), groupId, className);
092                    }
093                    else {
094                            String[] values = StringUtil.split(value, StringPool.AT);
095    
096                            String workflowDefinitionName = values[0];
097                            int workflowDefinitionVersion = GetterUtil.getInteger(
098                                    values[1]);
099    
100                            WorkflowDefinitionLinkLocalServiceUtil.updateWorkflowDefinitionLink(
101                                    themeDisplay.getUserId(), themeDisplay.getCompanyId(),
102                                    groupId, className, workflowDefinitionName,
103                                    workflowDefinitionVersion);
104                    }
105            }
106    
107            protected void updateWorkflowDefinitionLinks(ActionRequest actionRequest)
108                    throws Exception {
109    
110                    Enumeration<String> enu = actionRequest.getParameterNames();
111    
112                    while (enu.hasMoreElements()) {
113                            String name = enu.nextElement();
114    
115                            if (!name.startsWith(_PREFIX)) {
116                                    continue;
117                            }
118    
119                            String className = name.substring(_PREFIX.length(), name.length());
120                            String value = ParamUtil.getString(actionRequest, name);
121    
122                            updateWorkflowDefinitionLink(actionRequest, className, value);
123                    }
124            }
125    
126            private static final String _PREFIX = "workflowDefinitionName@";
127    
128    }