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