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