001
014
015 package com.liferay.portlet.expando.util;
016
017 import com.liferay.expando.kernel.model.ExpandoBridge;
018 import com.liferay.expando.kernel.model.ExpandoColumn;
019 import com.liferay.expando.kernel.model.ExpandoColumnConstants;
020 import com.liferay.expando.kernel.model.ExpandoTableConstants;
021 import com.liferay.expando.kernel.model.ExpandoValue;
022 import com.liferay.expando.kernel.service.ExpandoColumnLocalServiceUtil;
023 import com.liferay.expando.kernel.service.ExpandoValueLocalServiceUtil;
024 import com.liferay.expando.kernel.util.ExpandoBridgeIndexer;
025 import com.liferay.portal.kernel.dao.orm.QueryUtil;
026 import com.liferay.portal.kernel.exception.PortalException;
027 import com.liferay.portal.kernel.exception.SystemException;
028 import com.liferay.portal.kernel.log.Log;
029 import com.liferay.portal.kernel.log.LogFactoryUtil;
030 import com.liferay.portal.kernel.search.Document;
031 import com.liferay.portal.kernel.security.pacl.DoPrivileged;
032 import com.liferay.portal.kernel.util.ArrayUtil;
033 import com.liferay.portal.kernel.util.GetterUtil;
034 import com.liferay.portal.kernel.util.StringBundler;
035 import com.liferay.portal.kernel.util.StringPool;
036 import com.liferay.portal.kernel.util.StringUtil;
037 import com.liferay.portal.kernel.util.UnicodeProperties;
038 import com.liferay.portlet.expando.model.impl.ExpandoValueImpl;
039
040 import java.util.ArrayList;
041 import java.util.List;
042
043
046 @DoPrivileged
047 public class ExpandoBridgeIndexerImpl implements ExpandoBridgeIndexer {
048
049 @Override
050 public void addAttributes(Document document, ExpandoBridge expandoBridge) {
051 if (expandoBridge == null) {
052 return;
053 }
054
055 try {
056 doAddAttributes(document, expandoBridge);
057 }
058 catch (SystemException se) {
059 _log.error(se, se);
060 }
061 }
062
063 @Override
064 public String encodeFieldName(String columnName) {
065 StringBundler sb = new StringBundler(5);
066
067 sb.append(FIELD_NAMESPACE);
068 sb.append(StringPool.DOUBLE_UNDERLINE);
069 sb.append(
070 StringUtil.toLowerCase(ExpandoTableConstants.DEFAULT_TABLE_NAME));
071 sb.append(StringPool.DOUBLE_UNDERLINE);
072 sb.append(columnName);
073
074 return sb.toString();
075 }
076
077 protected void addAttribute(
078 Document document, ExpandoColumn expandoColumn,
079 List<ExpandoValue> expandoValues)
080 throws PortalException {
081
082 String fieldName = encodeFieldName(expandoColumn.getName());
083
084 ExpandoValue expandoValue = new ExpandoValueImpl();
085
086 expandoValue.setColumnId(expandoColumn.getColumnId());
087 expandoValue.setData(expandoColumn.getDefaultData());
088
089 boolean defaultValue = true;
090
091 for (ExpandoValue curExpandoValue : expandoValues) {
092 if (curExpandoValue.getColumnId() == expandoColumn.getColumnId()) {
093 expandoValue = curExpandoValue;
094
095 defaultValue = false;
096
097 break;
098 }
099 }
100
101 UnicodeProperties typeSettingsProperties =
102 expandoColumn.getTypeSettingsProperties();
103
104 int indexType = GetterUtil.getInteger(
105 typeSettingsProperties.getProperty(
106 ExpandoColumnConstants.INDEX_TYPE));
107
108 int type = expandoColumn.getType();
109
110 if (type == ExpandoColumnConstants.BOOLEAN) {
111 document.addKeyword(fieldName, expandoValue.getBoolean());
112 }
113 else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
114 if (!defaultValue) {
115 document.addKeyword(fieldName, expandoValue.getBooleanArray());
116 }
117 else {
118 document.addKeyword(fieldName, new boolean[0]);
119 }
120 }
121 else if (type == ExpandoColumnConstants.DATE) {
122 document.addDate(fieldName, expandoValue.getDate());
123 }
124 else if (type == ExpandoColumnConstants.DOUBLE) {
125 document.addKeyword(fieldName, expandoValue.getDouble());
126 }
127 else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
128 if (!defaultValue) {
129 document.addKeyword(fieldName, expandoValue.getDoubleArray());
130 }
131 else {
132 document.addKeyword(fieldName, new double[0]);
133 }
134 }
135 else if (type == ExpandoColumnConstants.FLOAT) {
136 document.addKeyword(fieldName, expandoValue.getFloat());
137 }
138 else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
139 if (!defaultValue) {
140 document.addKeyword(fieldName, expandoValue.getFloatArray());
141 }
142 else {
143 document.addKeyword(fieldName, new float[0]);
144 }
145 }
146 else if (type == ExpandoColumnConstants.INTEGER) {
147 document.addKeyword(fieldName, expandoValue.getInteger());
148 }
149 else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
150 if (!defaultValue) {
151 document.addKeyword(fieldName, expandoValue.getIntegerArray());
152 }
153 else {
154 document.addKeyword(fieldName, new int[0]);
155 }
156 }
157 else if (type == ExpandoColumnConstants.LONG) {
158 document.addKeyword(fieldName, expandoValue.getLong());
159 }
160 else if (type == ExpandoColumnConstants.LONG_ARRAY) {
161 if (!defaultValue) {
162 document.addKeyword(fieldName, expandoValue.getLongArray());
163 }
164 else {
165 document.addKeyword(fieldName, new long[0]);
166 }
167 }
168 else if (type == ExpandoColumnConstants.NUMBER) {
169 document.addKeyword(fieldName, expandoValue.getNumber().toString());
170 }
171 else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
172 if (!defaultValue) {
173 document.addKeyword(
174 fieldName,
175 ArrayUtil.toStringArray(expandoValue.getNumberArray()));
176 }
177 else {
178 document.addKeyword(fieldName, new long[0]);
179 }
180 }
181 else if (type == ExpandoColumnConstants.SHORT) {
182 document.addKeyword(fieldName, expandoValue.getShort());
183 }
184 else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
185 if (!defaultValue) {
186 document.addKeyword(fieldName, expandoValue.getShortArray());
187 }
188 else {
189 document.addKeyword(fieldName, new short[0]);
190 }
191 }
192 else if (type == ExpandoColumnConstants.STRING) {
193 if (indexType == ExpandoColumnConstants.INDEX_TYPE_KEYWORD) {
194 document.addKeyword(fieldName, expandoValue.getString());
195 }
196 else {
197 document.addText(fieldName, expandoValue.getString());
198 }
199 }
200 else if (type == ExpandoColumnConstants.STRING_ARRAY) {
201 if (!defaultValue) {
202 if (indexType == ExpandoColumnConstants.INDEX_TYPE_KEYWORD) {
203 document.addKeyword(
204 fieldName, expandoValue.getStringArray());
205 }
206 else {
207 document.addText(
208 fieldName,
209 StringUtil.merge(
210 expandoValue.getStringArray(), StringPool.SPACE));
211 }
212 }
213 else {
214 if (indexType == ExpandoColumnConstants.INDEX_TYPE_KEYWORD) {
215 document.addKeyword(fieldName, StringPool.BLANK);
216 }
217 else {
218 document.addText(fieldName, StringPool.BLANK);
219 }
220 }
221 }
222 else if (type == ExpandoColumnConstants.STRING_LOCALIZED) {
223 if (!defaultValue) {
224 if (indexType == ExpandoColumnConstants.INDEX_TYPE_KEYWORD) {
225 document.addLocalizedKeyword(
226 fieldName, expandoValue.getStringMap());
227 }
228 else {
229 document.addLocalizedText(
230 fieldName, expandoValue.getStringMap());
231 }
232 }
233 }
234 }
235
236 protected void doAddAttributes(
237 Document document, ExpandoBridge expandoBridge) {
238
239 List<ExpandoColumn> expandoColumns =
240 ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
241 expandoBridge.getCompanyId(), expandoBridge.getClassName());
242
243 if ((expandoColumns == null) || expandoColumns.isEmpty()) {
244 return;
245 }
246
247 List<ExpandoColumn> indexedColumns = new ArrayList<>();
248
249 for (ExpandoColumn expandoColumn : expandoColumns) {
250 UnicodeProperties properties =
251 expandoColumn.getTypeSettingsProperties();
252
253 int indexType = GetterUtil.getInteger(
254 properties.get(ExpandoColumnConstants.INDEX_TYPE));
255
256 if (indexType != ExpandoColumnConstants.INDEX_TYPE_NONE) {
257 indexedColumns.add(expandoColumn);
258 }
259 }
260
261 if (indexedColumns.isEmpty()) {
262 return;
263 }
264
265 List<ExpandoValue> expandoValues =
266 ExpandoValueLocalServiceUtil.getRowValues(
267 expandoBridge.getCompanyId(), expandoBridge.getClassName(),
268 ExpandoTableConstants.DEFAULT_TABLE_NAME,
269 expandoBridge.getClassPK(), QueryUtil.ALL_POS,
270 QueryUtil.ALL_POS);
271
272 for (ExpandoColumn expandoColumn : indexedColumns) {
273 try {
274 addAttribute(document, expandoColumn, expandoValues);
275 }
276 catch (Exception e) {
277 _log.error("Indexing " + expandoColumn.getName(), e);
278 }
279 }
280 }
281
282 protected static final String FIELD_NAMESPACE = "expando";
283
284 private static final Log _log = LogFactoryUtil.getLog(
285 ExpandoBridgeIndexerImpl.class);
286
287 }