1
14
15 package com.liferay.portlet.expando.util;
16
17 import com.liferay.portal.kernel.dao.orm.QueryUtil;
18 import com.liferay.portal.kernel.exception.PortalException;
19 import com.liferay.portal.kernel.exception.SystemException;
20 import com.liferay.portal.kernel.log.Log;
21 import com.liferay.portal.kernel.log.LogFactoryUtil;
22 import com.liferay.portal.kernel.search.Document;
23 import com.liferay.portal.kernel.util.GetterUtil;
24 import com.liferay.portal.kernel.util.StringBundler;
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portal.kernel.util.UnicodeProperties;
27 import com.liferay.portlet.expando.model.ExpandoBridge;
28 import com.liferay.portlet.expando.model.ExpandoColumn;
29 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
30 import com.liferay.portlet.expando.model.ExpandoTableConstants;
31 import com.liferay.portlet.expando.model.ExpandoValue;
32 import com.liferay.portlet.expando.model.impl.ExpandoValueImpl;
33 import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
34 import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39
44 public class ExpandoBridgeIndexerImpl implements ExpandoBridgeIndexer {
45
46 public void addAttributes(Document document, ExpandoBridge expandoBridge) {
47 if (expandoBridge == null) {
48 return;
49 }
50
51 try {
52 doAddAttributes(document, expandoBridge);
53 }
54 catch (SystemException se) {
55 _log.error(se, se);
56 }
57 }
58
59 public String encodeFieldName(String columnName) {
60 StringBundler sb = new StringBundler(3);
61
62 sb.append(_FIELD_NAMESPACE);
63 sb.append(StringPool.FORWARD_SLASH);
64 sb.append(ExpandoTableConstants.DEFAULT_TABLE_NAME.toLowerCase());
65 sb.append(StringPool.FORWARD_SLASH);
66 sb.append(columnName);
67
68 return sb.toString();
69 }
70
71 protected void doAddAttributes(
72 Document document, ExpandoBridge expandoBridge)
73 throws SystemException {
74
75 List<ExpandoColumn> expandoColumns =
76 ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
77 expandoBridge.getCompanyId(), expandoBridge.getClassName());
78
79 if ((expandoColumns == null) || expandoColumns.isEmpty()) {
80 return;
81 }
82
83 List<ExpandoColumn> indexedColumns = new ArrayList<ExpandoColumn>();
84
85 for (ExpandoColumn expandoColumn : expandoColumns) {
86 UnicodeProperties properties =
87 expandoColumn.getTypeSettingsProperties();
88
89 boolean indexable = GetterUtil.getBoolean(
90 properties.get(ExpandoBridgeIndexer.INDEXABLE));
91
92 if (indexable) {
93 indexedColumns.add(expandoColumn);
94 }
95 }
96
97 if (indexedColumns.isEmpty()) {
98 return;
99 }
100
101 List<ExpandoValue> expandoValues =
102 ExpandoValueLocalServiceUtil.getRowValues(
103 expandoBridge.getCompanyId(), expandoBridge.getClassName(),
104 ExpandoTableConstants.DEFAULT_TABLE_NAME,
105 expandoBridge.getClassPK(), QueryUtil.ALL_POS,
106 QueryUtil.ALL_POS);
107
108 for (ExpandoColumn expandoColumn : indexedColumns) {
109 try {
110 addAttribute(document, expandoColumn, expandoValues);
111 }
112 catch (Exception e) {
113 _log.error("Indexing " + expandoColumn.getName(), e);
114 }
115 }
116 }
117
118 protected void addAttribute(
119 Document document, ExpandoColumn expandoColumn,
120 List<ExpandoValue> expandoValues)
121 throws PortalException, SystemException {
122
123 String fieldName = encodeFieldName(expandoColumn.getName());
124
125 ExpandoValue expandoValue = new ExpandoValueImpl();
126
127 expandoValue.setColumnId(expandoColumn.getColumnId());
128 expandoValue.setData(expandoColumn.getDefaultData());
129
130 boolean defaultValue = true;
131
132 for (ExpandoValue curExpandoValue : expandoValues) {
133 if (curExpandoValue.getColumnId() == expandoColumn.getColumnId()) {
134 expandoValue = curExpandoValue;
135
136 defaultValue = false;
137
138 break;
139 }
140 }
141
142 int type = expandoColumn.getType();
143
144 if (type == ExpandoColumnConstants.BOOLEAN) {
145 document.addKeyword(fieldName, expandoValue.getBoolean());
146 }
147 else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
148 if (!defaultValue) {
149 document.addKeyword(fieldName, expandoValue.getBooleanArray());
150 }
151 else {
152 document.addKeyword(fieldName, new boolean[0]);
153 }
154 }
155 else if (type == ExpandoColumnConstants.DATE) {
156 document.addDate(fieldName, expandoValue.getDate());
157 }
158 else if (type == ExpandoColumnConstants.DOUBLE) {
159 document.addKeyword(fieldName, expandoValue.getDouble());
160 }
161 else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
162 if (!defaultValue) {
163 document.addKeyword(fieldName, expandoValue.getDoubleArray());
164 }
165 else {
166 document.addKeyword(fieldName, new double[0]);
167 }
168 }
169 else if (type == ExpandoColumnConstants.FLOAT) {
170 document.addKeyword(fieldName, expandoValue.getFloat());
171 }
172 else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
173 if (!defaultValue) {
174 document.addKeyword(fieldName, expandoValue.getFloatArray());
175 }
176 else {
177 document.addKeyword(fieldName, new float[0]);
178 }
179 }
180 else if (type == ExpandoColumnConstants.INTEGER) {
181 document.addKeyword(fieldName, expandoValue.getInteger());
182 }
183 else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
184 if (!defaultValue) {
185 document.addKeyword(fieldName, expandoValue.getIntegerArray());
186 }
187 else {
188 document.addKeyword(fieldName, new int[0]);
189 }
190 }
191 else if (type == ExpandoColumnConstants.LONG) {
192 document.addKeyword(fieldName, expandoValue.getLong());
193 }
194 else if (type == ExpandoColumnConstants.LONG_ARRAY) {
195 if (!defaultValue) {
196 document.addKeyword(fieldName, expandoValue.getLongArray());
197 }
198 else {
199 document.addKeyword(fieldName, new long[0]);
200 }
201 }
202 else if (type == ExpandoColumnConstants.SHORT) {
203 document.addKeyword(fieldName, expandoValue.getShort());
204 }
205 else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
206 if (!defaultValue) {
207 document.addKeyword(fieldName, expandoValue.getShortArray());
208 }
209 else {
210 document.addKeyword(fieldName, new short[0]);
211 }
212 }
213 else if (type == ExpandoColumnConstants.STRING) {
214 document.addText(fieldName, expandoValue.getString());
215 }
216 else if (type == ExpandoColumnConstants.STRING_ARRAY) {
217 if (!defaultValue) {
218 document.addKeyword(fieldName, expandoValue.getStringArray());
219 }
220 else {
221 document.addKeyword(fieldName, new String[0]);
222 }
223 }
224 }
225
226 protected static final String _FIELD_NAMESPACE = "expando";
227
228 private static Log _log = LogFactoryUtil.getLog(
229 ExpandoBridgeIndexerImpl.class);
230
231 }