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