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