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