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                    Indexer<Object> indexer = IndexerRegistryUtil.getIndexer(
066                            returnType.getName());
067    
068                    if (indexer == null) {
069                            serviceBeanAopCacheManager.removeMethodInterceptor(
070                                    methodInvocation, this);
071    
072                            return;
073                    }
074    
075                    Object[] arguments = methodInvocation.getArguments();
076    
077                    for (int i = arguments.length - 1; i >= 0; i--) {
078                            if (arguments[i] instanceof ServiceContext) {
079                                    ServiceContext serviceContext = (ServiceContext)arguments[i];
080    
081                                    if (serviceContext.isIndexingEnabled()) {
082                                            break;
083                                    }
084    
085                                    return;
086                            }
087                    }
088    
089                    if (indexable.type() == IndexableType.DELETE) {
090                            indexer.delete(result);
091                    }
092                    else {
093                            indexer.reindex(result);
094                    }
095            }
096    
097            @Override
098            public Indexable getNullAnnotation() {
099                    return _nullIndexable;
100            }
101    
102            private static final Log _log = LogFactoryUtil.getLog(
103                    IndexableAdvice.class);
104    
105            private static final Indexable _nullIndexable = new Indexable() {
106    
107                    @Override
108                    public Class<? extends Annotation> annotationType() {
109                            return Indexable.class;
110                    }
111    
112                    @Override
113                    public IndexableType type() {
114                            return null;
115                    }
116    
117            };
118    
119    }