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