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.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    }