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.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                            @Override
091                            public Class<? extends Annotation> annotationType() {
092                                    return Indexable.class;
093                            }
094    
095                            @Override
096                            public IndexableType type() {
097                                    return null;
098                            }
099    
100                    };
101    
102    }