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.FORWARD_SLASH);
067 sb.append(ExpandoTableConstants.DEFAULT_TABLE_NAME.toLowerCase());
068 sb.append(StringPool.FORWARD_SLASH);
069 sb.append(columnName);
070
071 return sb.toString();
072 }
073
074 protected void addAttribute(
075 Document document, ExpandoColumn expandoColumn,
076 List<ExpandoValue> expandoValues)
077 throws PortalException, SystemException {
078
079 String fieldName = encodeFieldName(expandoColumn.getName());
080
081 ExpandoValue expandoValue = new ExpandoValueImpl();
082
083 expandoValue.setColumnId(expandoColumn.getColumnId());
084 expandoValue.setData(expandoColumn.getDefaultData());
085
086 boolean defaultValue = true;
087
088 for (ExpandoValue curExpandoValue : expandoValues) {
089 if (curExpandoValue.getColumnId() == expandoColumn.getColumnId()) {
090 expandoValue = curExpandoValue;
091
092 defaultValue = false;
093
094 break;
095 }
096 }
097
098 UnicodeProperties typeSettingsProperties =
099 expandoColumn.getTypeSettingsProperties();
100
101 int indexType = GetterUtil.getInteger(
102 typeSettingsProperties.getProperty(
103 ExpandoColumnConstants.INDEX_TYPE));
104
105 int type = expandoColumn.getType();
106
107 if (type == ExpandoColumnConstants.BOOLEAN) {
108 document.addKeyword(fieldName, expandoValue.getBoolean());
109 }
110 else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
111 if (!defaultValue) {
112 document.addKeyword(fieldName, expandoValue.getBooleanArray());
113 }
114 else {
115 document.addKeyword(fieldName, new boolean[0]);
116 }
117 }
118 else if (type == ExpandoColumnConstants.DATE) {
119 document.addDate(fieldName, expandoValue.getDate());
120 }
121 else if (type == ExpandoColumnConstants.DOUBLE) {
122 document.addKeyword(fieldName, expandoValue.getDouble());
123 }
124 else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
125 if (!defaultValue) {
126 document.addKeyword(fieldName, expandoValue.getDoubleArray());
127 }
128 else {
129 document.addKeyword(fieldName, new double[0]);
130 }
131 }
132 else if (type == ExpandoColumnConstants.FLOAT) {
133 document.addKeyword(fieldName, expandoValue.getFloat());
134 }
135 else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
136 if (!defaultValue) {
137 document.addKeyword(fieldName, expandoValue.getFloatArray());
138 }
139 else {
140 document.addKeyword(fieldName, new float[0]);
141 }
142 }
143 else if (type == ExpandoColumnConstants.INTEGER) {
144 document.addKeyword(fieldName, expandoValue.getInteger());
145 }
146 else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
147 if (!defaultValue) {
148 document.addKeyword(fieldName, expandoValue.getIntegerArray());
149 }
150 else {
151 document.addKeyword(fieldName, new int[0]);
152 }
153 }
154 else if (type == ExpandoColumnConstants.LONG) {
155 document.addKeyword(fieldName, expandoValue.getLong());
156 }
157 else if (type == ExpandoColumnConstants.LONG_ARRAY) {
158 if (!defaultValue) {
159 document.addKeyword(fieldName, expandoValue.getLongArray());
160 }
161 else {
162 document.addKeyword(fieldName, new long[0]);
163 }
164 }
165 else if (type == ExpandoColumnConstants.SHORT) {
166 document.addKeyword(fieldName, expandoValue.getShort());
167 }
168 else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
169 if (!defaultValue) {
170 document.addKeyword(fieldName, expandoValue.getShortArray());
171 }
172 else {
173 document.addKeyword(fieldName, new short[0]);
174 }
175 }
176 else if (type == ExpandoColumnConstants.STRING) {
177 if (indexType == ExpandoColumnConstants.INDEX_TYPE_KEYWORD) {
178 document.addKeyword(fieldName, expandoValue.getString());
179 }
180 else {
181 document.addText(fieldName, expandoValue.getString());
182 }
183 }
184 else if (type == ExpandoColumnConstants.STRING_ARRAY) {
185 if (!defaultValue) {
186 if (indexType == ExpandoColumnConstants.INDEX_TYPE_KEYWORD) {
187 document.addKeyword(
188 fieldName, expandoValue.getStringArray());
189 }
190 else {
191 document.addText(
192 fieldName,
193 StringUtil.merge(
194 expandoValue.getStringArray(), StringPool.SPACE));
195 }
196 }
197 else {
198 if (indexType == ExpandoColumnConstants.INDEX_TYPE_KEYWORD) {
199 document.addKeyword(fieldName, StringPool.BLANK);
200 }
201 else {
202 document.addText(fieldName, StringPool.BLANK);
203 }
204 }
205 }
206 }
207
208 protected void doAddAttributes(
209 Document document, ExpandoBridge expandoBridge)
210 throws SystemException {
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 Log _log = LogFactoryUtil.getLog(
258 ExpandoBridgeIndexerImpl.class);
259
260 }