001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.workflowdefinitions.action;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.workflow.WorkflowDefinition;
020    import com.liferay.portal.kernel.workflow.WorkflowDefinitionManagerUtil;
021    import com.liferay.portal.theme.ThemeDisplay;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portal.util.WebKeys;
024    
025    import java.util.List;
026    
027    import javax.portlet.PortletRequest;
028    
029    import javax.servlet.http.HttpServletRequest;
030    
031    /**
032     * @author Bruno Farache
033     */
034    public class ActionUtil {
035    
036            public static void getWorkflowDefinition(HttpServletRequest request)
037                    throws Exception {
038    
039                    if (request.getAttribute(WebKeys.WORKFLOW_DEFINITION) != null) {
040                            return;
041                    }
042    
043                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
044                            WebKeys.THEME_DISPLAY);
045    
046                    String name = ParamUtil.getString(request, "name");
047                    int version = ParamUtil.getInteger(request, "version");
048    
049                    List<WorkflowDefinition> workflowDefinitions =
050                            WorkflowDefinitionManagerUtil.getWorkflowDefinitions(
051                                    themeDisplay.getCompanyId(), name, QueryUtil.ALL_POS,
052                                    QueryUtil.ALL_POS, null);
053    
054                    for (WorkflowDefinition workflowDefinition : workflowDefinitions) {
055                            if (version == workflowDefinition.getVersion()) {
056                                    request.setAttribute(
057                                            WebKeys.WORKFLOW_DEFINITION, workflowDefinition);
058    
059                                    break;
060                            }
061                    }
062            }
063    
064            public static void getWorkflowDefinition(PortletRequest portletRequest)
065                    throws Exception {
066    
067                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
068                            portletRequest);
069    
070                    getWorkflowDefinition(request);
071            }
072    
073    }