001    /**
002     * Copyright (c) 2000-2012 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.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.spring.aop.AnnotationChainableMethodAdvice;
025    import com.liferay.portal.spring.aop.ServiceBeanAopProxy;
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 indexer = IndexerRegistryUtil.getIndexer(returnType.getName());
066    
067                    if (indexer != null) {
068                            if (indexable.type() == IndexableType.DELETE) {
069                                    indexer.delete(result);
070                            }
071                            else {
072                                    indexer.reindex(result);
073                            }
074                    }
075                    else {
076                            ServiceBeanAopProxy.removeMethodInterceptor(methodInvocation, this);
077                    }
078            }
079    
080            @Override
081            public Indexable getNullAnnotation() {
082                    return _nullIndexable;
083            }
084    
085            private static Log _log = LogFactoryUtil.getLog(IndexableAdvice.class);
086    
087            private static Indexable _nullIndexable =
088                    new Indexable() {
089    
090                            public Class<? extends Annotation> annotationType() {
091                                    return Indexable.class;
092                            }
093    
094                            public IndexableType type() {
095                                    return null;
096                            }
097    
098                    };
099    
100    }