001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.LocaleUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
029    import com.liferay.portlet.dynamicdatamapping.storage.Field;
030    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
031    
032    import java.io.Serializable;
033    
034    import java.util.Date;
035    import java.util.Iterator;
036    import java.util.Locale;
037    
038    /**
039     * @author Alexander Chow
040     */
041    public class DDMIndexerImpl implements DDMIndexer {
042    
043            public void addAttributes(
044                    Document document, DDMStructure ddmStructure, Fields fields) {
045    
046                    Iterator<Field> itr = fields.iterator();
047    
048                    while (itr.hasNext()) {
049                            Field field = itr.next();
050    
051                            try {
052                                    String indexType = ddmStructure.getFieldProperty(
053                                            field.getName(), "indexType");
054    
055                                    if (Validator.isNull(indexType)) {
056                                            continue;
057                                    }
058    
059                                    for (Locale locale : LanguageUtil.getAvailableLocales()) {
060                                            String name = encodeName(
061                                                    ddmStructure.getStructureId(), field.getName(), locale);
062    
063                                            Serializable value = field.getValue(locale);
064    
065                                            if (value instanceof Boolean) {
066                                                    document.addKeyword(name, (Boolean)value);
067                                            }
068                                            else if (value instanceof Boolean[]) {
069                                                    document.addKeyword(name, (Boolean[])value);
070                                            }
071                                            else if (value instanceof Date) {
072                                                    document.addDate(name, (Date)value);
073                                            }
074                                            else if (value instanceof Date[]) {
075                                                    document.addDate(name, (Date[])value);
076                                            }
077                                            else if (value instanceof Double) {
078                                                    document.addKeyword(name, (Double)value);
079                                            }
080                                            else if (value instanceof Double[]) {
081                                                    document.addKeyword(name, (Double[])value);
082                                            }
083                                            else if (value instanceof Integer) {
084                                                    document.addKeyword(name, (Integer)value);
085                                            }
086                                            else if (value instanceof Integer[]) {
087                                                    document.addKeyword(name, (Integer[])value);
088                                            }
089                                            else if (value instanceof Object[]) {
090                                                    String[] valuesString = ArrayUtil.toStringArray(
091                                                            (Object[])value);
092    
093                                                    if (indexType.equals("keyword")) {
094                                                            document.addKeyword(name, valuesString);
095                                                    }
096                                                    else {
097                                                            document.addText(name, valuesString);
098                                                    }
099                                            }
100                                            else {
101                                                    String valueString = String.valueOf(value);
102    
103                                                    String type = field.getType();
104    
105                                                    if (type.equals(DDMImpl.TYPE_RADIO) ||
106                                                            type.equals(DDMImpl.TYPE_SELECT)) {
107    
108                                                            JSONArray jsonArray =
109                                                                    JSONFactoryUtil.createJSONArray(valueString);
110    
111                                                            String[] stringArray = ArrayUtil.toStringArray(
112                                                                    jsonArray);
113    
114                                                            if (indexType.equals("keyword")) {
115                                                                    document.addKeyword(name, stringArray);
116                                                            }
117                                                            else {
118                                                                    document.addText(name, stringArray);
119                                                            }
120                                                    }
121                                                    else {
122                                                            if (indexType.equals("keyword")) {
123                                                                    document.addKeyword(name, valueString);
124                                                            }
125                                                            else {
126                                                                    document.addText(name, valueString);
127                                                            }
128                                                    }
129                                            }
130                                    }
131                            }
132                            catch (Exception e) {
133                                    if (_log.isWarnEnabled()) {
134                                            _log.warn(e, e);
135                                    }
136                            }
137                    }
138            }
139    
140            public String encodeName(long ddmStructureId, String fieldName) {
141                    return encodeName(ddmStructureId, fieldName, null);
142            }
143    
144            public String encodeName(
145                    long ddmStructureId, String fieldName, Locale locale) {
146    
147                    StringBundler sb = new StringBundler(7);
148    
149                    sb.append(DDM_FIELD_NAMESPACE);
150                    sb.append(StringPool.FORWARD_SLASH);
151                    sb.append(ddmStructureId);
152                    sb.append(StringPool.FORWARD_SLASH);
153                    sb.append(fieldName);
154    
155                    if (Validator.isNotNull(locale)) {
156                            sb.append(StringPool.UNDERLINE);
157                            sb.append(LocaleUtil.toLanguageId(locale));
158                    }
159    
160                    return sb.toString();
161            }
162    
163            private static Log _log = LogFactoryUtil.getLog(DDMIndexerImpl.class);
164    
165    }