001    /**
002     * Copyright (c) 2000-2013 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.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    }