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