001    /**
002     * Copyright (c) 2000-2012 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.util.bridges.alloy;
016    
017    import com.liferay.portal.kernel.search.BaseIndexer;
018    import com.liferay.portal.kernel.search.BooleanQuery;
019    import com.liferay.portal.kernel.search.Document;
020    import com.liferay.portal.kernel.search.DocumentImpl;
021    import com.liferay.portal.kernel.search.Field;
022    import com.liferay.portal.kernel.search.Indexer;
023    import com.liferay.portal.kernel.search.SearchContext;
024    import com.liferay.portal.kernel.search.SearchEngineUtil;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.MethodHandler;
027    import com.liferay.portal.kernel.util.MethodKey;
028    import com.liferay.portal.kernel.workflow.WorkflowConstants;
029    import com.liferay.portal.model.AuditedModel;
030    import com.liferay.portal.model.BaseModel;
031    
032    import java.util.ArrayList;
033    import java.util.Collection;
034    import java.util.List;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public abstract class BaseAlloyIndexer extends BaseIndexer {
040    
041            public BaseAlloyIndexer(String portletId, String className) {
042                    this.portletId = portletId;
043                    classNames = new String[] {className};
044    
045                    getModelMethodKey = new MethodKey(
046                            "XxxLocalServiceUtil", "getXxx", long.class);
047                    getModelsCountMethodKey = new MethodKey(
048                            "XxxLocalServiceUtil", "getXxxsCount");
049                    getModelsMethodKey = new MethodKey(
050                            "XxxLocalServiceUtil", "getXxxs", int.class, int.class);
051            }
052    
053            public String[] getClassNames() {
054                    return classNames;
055            }
056    
057            @Override
058            public void postProcessContextQuery(
059                            BooleanQuery contextQuery, SearchContext searchContext)
060                    throws Exception {
061    
062                    int status = GetterUtil.getInteger(
063                            searchContext.getAttribute(Field.STATUS),
064                            WorkflowConstants.STATUS_ANY);
065    
066                    if (status != WorkflowConstants.STATUS_ANY) {
067                            contextQuery.addRequiredTerm(Field.STATUS, status);
068                    }
069            }
070    
071            @Override
072            protected void doDelete(Object obj) throws Exception {
073                    BaseModel<?> baseModel = (BaseModel<?>)obj;
074    
075                    Document document = new DocumentImpl();
076    
077                    document.addUID(
078                            portletId, String.valueOf(baseModel.getPrimaryKeyObj()));
079    
080                    AuditedModel auditedModel = (AuditedModel)obj;
081    
082                    SearchEngineUtil.deleteDocument(
083                            auditedModel.getCompanyId(), document.get(Field.UID));
084            }
085    
086            @Override
087            protected void doReindex(Object obj) throws Exception {
088                    Document document = getDocument(obj);
089    
090                    AuditedModel auditedModel = (AuditedModel)obj;
091    
092                    SearchEngineUtil.updateDocument(auditedModel.getCompanyId(), document);
093            }
094    
095            @Override
096            protected void doReindex(String className, long classPK) throws Exception {
097                    MethodHandler methodHandler = new MethodHandler(
098                            getModelMethodKey, classPK);
099    
100                    Object model = methodHandler.invoke(false);
101    
102                    doReindex(model);
103            }
104    
105            @Override
106            protected void doReindex(String[] ids) throws Exception {
107                    long companyId = GetterUtil.getLong(ids[0]);
108    
109                    reindexModels(companyId);
110            }
111    
112            @Override
113            protected String getPortletId(SearchContext searchContext) {
114                    return portletId;
115            }
116    
117            protected void reindexModels(long companyId) throws Exception {
118                    MethodHandler methodHandler = new MethodHandler(
119                            getModelsCountMethodKey);
120    
121                    int count = (Integer)methodHandler.invoke(false);
122    
123                    int pages = count / Indexer.DEFAULT_INTERVAL;
124    
125                    for (int i = 0; i <= pages; i++) {
126                            int start = (i * Indexer.DEFAULT_INTERVAL);
127                            int end = start + Indexer.DEFAULT_INTERVAL;
128    
129                            reindexModels(companyId, start, end);
130                    }
131            }
132    
133            protected void reindexModels(long companyId, int start, int end)
134                    throws Exception {
135    
136                    MethodHandler methodHandler = new MethodHandler(
137                            getModelsMethodKey, start, end);
138    
139                    List<Object> models = (List<Object>)methodHandler.invoke(false);
140    
141                    if (models.isEmpty()) {
142                            return;
143                    }
144    
145                    Collection<Document> documents = new ArrayList<Document>();
146    
147                    for (Object model : models) {
148                            Document document = getDocument(model);
149    
150                            documents.add(document);
151                    }
152    
153                    SearchEngineUtil.updateDocuments(companyId, documents);
154            }
155    
156            protected String[] classNames;
157            protected MethodKey getModelMethodKey;
158            protected MethodKey getModelsCountMethodKey;
159            protected MethodKey getModelsMethodKey;
160            protected String portletId;
161    
162    }