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