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.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                            getSearchEngineId(), auditedModel.getCompanyId(),
084                            document.get(Field.UID));
085            }
086    
087            @Override
088            protected void doReindex(Object obj) throws Exception {
089                    Document document = getDocument(obj);
090    
091                    AuditedModel auditedModel = (AuditedModel)obj;
092    
093                    SearchEngineUtil.updateDocument(
094                            getSearchEngineId(), auditedModel.getCompanyId(), document);
095            }
096    
097            @Override
098            protected void doReindex(String className, long classPK) throws Exception {
099                    MethodHandler methodHandler = new MethodHandler(
100                            getModelMethodKey, classPK);
101    
102                    Object model = methodHandler.invoke(false);
103    
104                    doReindex(model);
105            }
106    
107            @Override
108            protected void doReindex(String[] ids) throws Exception {
109                    long companyId = GetterUtil.getLong(ids[0]);
110    
111                    reindexModels(companyId);
112            }
113    
114            @Override
115            protected String getPortletId(SearchContext searchContext) {
116                    return portletId;
117            }
118    
119            protected void reindexModels(long companyId) throws Exception {
120                    MethodHandler methodHandler = new MethodHandler(
121                            getModelsCountMethodKey);
122    
123                    int count = (Integer)methodHandler.invoke(false);
124    
125                    int pages = count / Indexer.DEFAULT_INTERVAL;
126    
127                    for (int i = 0; i <= pages; i++) {
128                            int start = (i * Indexer.DEFAULT_INTERVAL);
129                            int end = start + Indexer.DEFAULT_INTERVAL;
130    
131                            reindexModels(companyId, start, end);
132                    }
133            }
134    
135            protected void reindexModels(long companyId, int start, int end)
136                    throws Exception {
137    
138                    MethodHandler methodHandler = new MethodHandler(
139                            getModelsMethodKey, start, end);
140    
141                    List<Object> models = (List<Object>)methodHandler.invoke(false);
142    
143                    if (models.isEmpty()) {
144                            return;
145                    }
146    
147                    Collection<Document> documents = new ArrayList<Document>(models.size());
148    
149                    for (Object model : models) {
150                            Document document = getDocument(model);
151    
152                            documents.add(document);
153                    }
154    
155                    SearchEngineUtil.updateDocuments(
156                            getSearchEngineId(), companyId, documents);
157            }
158    
159            protected String[] classNames;
160            protected MethodKey getModelMethodKey;
161            protected MethodKey getModelsCountMethodKey;
162            protected MethodKey getModelsMethodKey;
163            protected String portletId;
164    
165    }