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.portal.workflow;
016    
017    import com.liferay.portal.kernel.workflow.RequiredWorkflowDefinitionException;
018    import com.liferay.portal.service.WorkflowDefinitionLinkLocalServiceUtil;
019    
020    import org.aspectj.lang.ProceedingJoinPoint;
021    
022    /**
023     * @author Brian Wing Shun Chan
024     */
025    public class WorkflowLinkAdvice {
026    
027            public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
028                    throws Throwable {
029    
030                    String methodName = proceedingJoinPoint.getSignature().getName();
031                    Object[] arguments = proceedingJoinPoint.getArgs();
032    
033                    if (methodName.equals(_UPDATE_ACTIVE)) {
034                            long companyId = (Long)arguments[0];
035                            String name = (String)arguments[2];
036                            int version = (Integer)arguments[3];
037                            boolean active = (Boolean)arguments[4];
038    
039                            if (!active) {
040                                    int workflowDefinitionLinksCount =
041                                            WorkflowDefinitionLinkLocalServiceUtil.
042                                                    getWorkflowDefinitionLinksCount(
043                                                            companyId, name, version);
044    
045                                    if (workflowDefinitionLinksCount >= 1) {
046                                            throw new RequiredWorkflowDefinitionException();
047                                    }
048                            }
049                    }
050    
051                    return proceedingJoinPoint.proceed();
052            }
053    
054            private static final String _UPDATE_ACTIVE = "updateActive";
055    
056    }