001
014
015 package com.liferay.portlet.dynamicdatamapping.util;
016
017 import com.liferay.portal.kernel.json.JSONArray;
018 import com.liferay.portal.kernel.json.JSONFactoryUtil;
019 import com.liferay.portal.kernel.language.LanguageUtil;
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.ArrayUtil;
024 import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
025 import com.liferay.portal.kernel.util.HtmlUtil;
026 import com.liferay.portal.kernel.util.LocaleUtil;
027 import com.liferay.portal.kernel.util.PropsKeys;
028 import com.liferay.portal.kernel.util.PropsUtil;
029 import com.liferay.portal.kernel.util.StringBundler;
030 import com.liferay.portal.kernel.util.StringPool;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
033 import com.liferay.portlet.dynamicdatamapping.storage.Field;
034 import com.liferay.portlet.dynamicdatamapping.storage.Fields;
035
036 import java.io.Serializable;
037
038 import java.text.Format;
039
040 import java.util.Date;
041 import java.util.Locale;
042
043
046 public class DDMIndexerImpl implements DDMIndexer {
047
048 @Override
049 public void addAttributes(
050 Document document, DDMStructure ddmStructure, Fields fields) {
051
052 for (Field field : fields) {
053 try {
054 String indexType = ddmStructure.getFieldProperty(
055 field.getName(), "indexType");
056
057 String structureKey = ddmStructure.getStructureKey();
058
059 if (structureKey.equals("TIKARAWMETADATA")) {
060 indexType = "text";
061 }
062
063 if (Validator.isNull(indexType)) {
064 continue;
065 }
066
067 for (Locale locale : LanguageUtil.getAvailableLocales()) {
068 String name = encodeName(
069 ddmStructure.getStructureId(), field.getName(), locale);
070
071 Serializable value = field.getValue(locale);
072
073 if (value instanceof Boolean) {
074 document.addKeyword(name, (Boolean)value);
075 }
076 else if (value instanceof Boolean[]) {
077 document.addKeyword(name, (Boolean[])value);
078 }
079 else if (value instanceof Date) {
080 document.addDate(name, (Date)value);
081 }
082 else if (value instanceof Date[]) {
083 document.addDate(name, (Date[])value);
084 }
085 else if (value instanceof Double) {
086 document.addNumber(name, (Double)value);
087 }
088 else if (value instanceof Double[]) {
089 document.addNumber(name, (Double[])value);
090 }
091 else if (value instanceof Integer) {
092 document.addNumber(name, (Integer)value);
093 }
094 else if (value instanceof Integer[]) {
095 document.addNumber(name, (Integer[])value);
096 }
097 else if (value instanceof Long) {
098 document.addNumber(name, (Long)value);
099 }
100 else if (value instanceof Long[]) {
101 document.addNumber(name, (Long[])value);
102 }
103 else if (value instanceof Float) {
104 document.addNumber(name, (Float)value);
105 }
106 else if (value instanceof Float[]) {
107 document.addNumber(name, (Float[])value);
108 }
109 else if (value instanceof Object[]) {
110 String[] valuesString = ArrayUtil.toStringArray(
111 (Object[])value);
112
113 if (indexType.equals("keyword")) {
114 document.addKeyword(name, valuesString);
115 }
116 else {
117 document.addText(name, valuesString);
118 }
119 }
120 else {
121 String valueString = String.valueOf(value);
122
123 String type = field.getType();
124
125 if (type.equals(DDMImpl.TYPE_RADIO) ||
126 type.equals(DDMImpl.TYPE_SELECT)) {
127
128 JSONArray jsonArray =
129 JSONFactoryUtil.createJSONArray(valueString);
130
131 String[] stringArray = ArrayUtil.toStringArray(
132 jsonArray);
133
134 if (indexType.equals("keyword")) {
135 document.addKeyword(name, stringArray);
136 }
137 else {
138 document.addText(name, stringArray);
139 }
140 }
141 else {
142 if (type.equals(DDMImpl.TYPE_DDM_TEXT_HTML)) {
143 valueString = HtmlUtil.extractText(valueString);
144 }
145
146 if (indexType.equals("keyword")) {
147 document.addKeyword(name, valueString);
148 }
149 else {
150 document.addText(name, valueString);
151 }
152 }
153 }
154 }
155 }
156 catch (Exception e) {
157 if (_log.isWarnEnabled()) {
158 _log.warn(e, e);
159 }
160 }
161 }
162 }
163
164 @Override
165 public String encodeName(long ddmStructureId, String fieldName) {
166 return encodeName(ddmStructureId, fieldName, null);
167 }
168
169 @Override
170 public String encodeName(
171 long ddmStructureId, String fieldName, Locale locale) {
172
173 StringBundler sb = new StringBundler(7);
174
175 sb.append(DDM_FIELD_NAMESPACE);
176 sb.append(StringPool.FORWARD_SLASH);
177 sb.append(ddmStructureId);
178 sb.append(StringPool.FORWARD_SLASH);
179 sb.append(fieldName);
180
181 if (Validator.isNotNull(locale)) {
182 sb.append(StringPool.UNDERLINE);
183 sb.append(LocaleUtil.toLanguageId(locale));
184 }
185
186 return sb.toString();
187 }
188
189 @Override
190 public String extractIndexableAttributes(
191 DDMStructure ddmStructure, Fields fields, Locale locale) {
192
193 Format dateFormat = FastDateFormatFactoryUtil.getSimpleDateFormat(
194 PropsUtil.get(PropsKeys.INDEX_DATE_FORMAT_PATTERN));
195
196 StringBundler sb = new StringBundler();
197
198 for (Field field : fields) {
199 try {
200 String indexType = ddmStructure.getFieldProperty(
201 field.getName(), "indexType");
202
203 String structureKey = ddmStructure.getStructureKey();
204
205 if (structureKey.equals("TIKARAWMETADATA")) {
206 indexType = "text";
207 }
208
209 if (Validator.isNull(indexType)) {
210 continue;
211 }
212
213 Serializable value = field.getValue(locale);
214
215 if ((value instanceof Boolean) || (value instanceof Double) ||
216 (value instanceof Integer) || (value instanceof Long) ||
217 (value instanceof Float)) {
218
219 sb.append(value);
220 sb.append(StringPool.SPACE);
221 }
222 else if (value instanceof Date) {
223 sb.append(dateFormat.format(value));
224 sb.append(StringPool.SPACE);
225 }
226 else if (value instanceof Date[]) {
227 Date[] dates = (Date[])value;
228
229 for (Date date : dates) {
230 sb.append(dateFormat.format(date));
231 sb.append(StringPool.SPACE);
232 }
233 }
234 else if (value instanceof Object[]) {
235 Object[] values = (Object[])value;
236
237 for (Object object : values) {
238 sb.append(object);
239 sb.append(StringPool.SPACE);
240 }
241 }
242 else {
243 String valueString = String.valueOf(value);
244
245 String type = field.getType();
246
247 if (type.equals(DDMImpl.TYPE_RADIO) ||
248 type.equals(DDMImpl.TYPE_SELECT)) {
249
250 JSONArray jsonArray = JSONFactoryUtil.createJSONArray(
251 valueString);
252
253 String[] stringArray = ArrayUtil.toStringArray(
254 jsonArray);
255
256 sb.append(stringArray);
257 sb.append(StringPool.SPACE);
258 }
259 else {
260 if (type.equals(DDMImpl.TYPE_DDM_TEXT_HTML)) {
261 valueString = HtmlUtil.extractText(valueString);
262 }
263
264 sb.append(valueString);
265 sb.append(StringPool.SPACE);
266 }
267 }
268 }
269 catch (Exception e) {
270 if (_log.isWarnEnabled()) {
271 _log.warn(e, e);
272 }
273 }
274 }
275
276 return sb.toString();
277 }
278
279 private static Log _log = LogFactoryUtil.getLog(DDMIndexerImpl.class);
280
281 }