001    /**
002     * Copyright (c) 2000-present 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.search;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.search.Indexable;
020    import com.liferay.portal.kernel.search.IndexableType;
021    import com.liferay.portal.kernel.search.Indexer;
022    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
023    import com.liferay.portal.model.BaseModel;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
026    
027    import java.lang.annotation.Annotation;
028    import java.lang.reflect.Method;
029    
030    import org.aopalliance.intercept.MethodInvocation;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class IndexableAdvice
036            extends AnnotationChainableMethodAdvice<Indexable> {
037    
038            @Override
039            public void afterReturning(MethodInvocation methodInvocation, Object result)
040                    throws Throwable {
041    
042                    if (result == null) {
043                            return;
044                    }
045    
046                    Indexable indexable = findAnnotation(methodInvocation);
047    
048                    if (indexable == _nullIndexable) {
049                            return;
050                    }
051    
052                    Method method = methodInvocation.getMethod();
053    
054                    Class<?> returnType = method.getReturnType();
055    
056                    if (!BaseModel.class.isAssignableFrom(returnType)) {
057                            if (_log.isWarnEnabled()) {
058                                    _log.warn(
059                                            methodInvocation + " does not have a valid return type");
060                            }
061    
062                            return;
063                    }
064    
065                    Object[] arguments = methodInvocation.getArguments();
066    
067                    ServiceContext serviceContext = null;
068    
069                    for (int i = arguments.length - 1; i >= 0; i--) {
070                            if (arguments[i] instanceof ServiceContext) {
071                                    serviceContext = (ServiceContext)arguments[i];
072    
073                                    break;
074                            }
075                    }
076    
077                    Indexer indexer = null;
078    
079                    if ((serviceContext == null) || serviceContext.isIndexingEnabled()) {
080                            indexer = IndexerRegistryUtil.getIndexer(returnType.getName());
081                    }
082    
083                    if (indexer != null) {
084                            if (indexable.type() == IndexableType.DELETE) {
085                                    indexer.delete(result);
086                            }
087                            else {
088                                    indexer.reindex(result);
089                            }
090                    }
091                    else {
092                            serviceBeanAopCacheManager.removeMethodInterceptor(
093                                    methodInvocation, this);
094                    }
095            }
096    
097            @Override
098            public Indexable getNullAnnotation() {
099                    return _nullIndexable;
100            }
101    
102            private static Log _log = LogFactoryUtil.getLog(IndexableAdvice.class);
103    
104            private static Indexable _nullIndexable = new Indexable() {
105    
106                    @Override
107                    public Class<? extends Annotation> annotationType() {
108                            return Indexable.class;
109                    }
110    
111                    @Override
112                    public IndexableType type() {
113                            return null;
114                    }
115    
116            };
117    
118    }