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