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