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