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.expando.util;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
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.security.pacl.DoPrivileged;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.UnicodeProperties;
029    import com.liferay.portlet.expando.model.ExpandoBridge;
030    import com.liferay.portlet.expando.model.ExpandoColumn;
031    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
032    import com.liferay.portlet.expando.model.ExpandoTableConstants;
033    import com.liferay.portlet.expando.model.ExpandoValue;
034    import com.liferay.portlet.expando.model.impl.ExpandoValueImpl;
035    import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
036    import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
037    
038    import java.util.ArrayList;
039    import java.util.List;
040    
041    /**
042     * @author Raymond Aug??
043     */
044    @DoPrivileged
045    public class ExpandoBridgeIndexerImpl implements ExpandoBridgeIndexer {
046    
047            @Override
048            public void addAttributes(Document document, ExpandoBridge expandoBridge) {
049                    if (expandoBridge == null) {
050                            return;
051                    }
052    
053                    try {
054                            doAddAttributes(document, expandoBridge);
055                    }
056                    catch (SystemException se) {
057                            _log.error(se, se);
058                    }
059            }
060    
061            @Override
062            public String encodeFieldName(String columnName) {
063                    StringBundler sb = new StringBundler(3);
064    
065                    sb.append(FIELD_NAMESPACE);
066                    sb.append(StringPool.FORWARD_SLASH);
067                    sb.append(
068                            StringUtil.toLowerCase(ExpandoTableConstants.DEFAULT_TABLE_NAME));
069                    sb.append(StringPool.FORWARD_SLASH);
070                    sb.append(columnName);
071    
072                    return sb.toString();
073            }
074    
075            protected void addAttribute(
076                            Document document, ExpandoColumn expandoColumn,
077                            List<ExpandoValue> expandoValues)
078                    throws PortalException, SystemException {
079    
080                    String fieldName = encodeFieldName(expandoColumn.getName());
081    
082                    ExpandoValue expandoValue = new ExpandoValueImpl();
083    
084                    expandoValue.setColumnId(expandoColumn.getColumnId());
085                    expandoValue.setData(expandoColumn.getDefaultData());
086    
087                    boolean defaultValue = true;
088    
089                    for (ExpandoValue curExpandoValue : expandoValues) {
090                            if (curExpandoValue.getColumnId() == expandoColumn.getColumnId()) {
091                                    expandoValue = curExpandoValue;
092    
093                                    defaultValue = false;
094    
095                                    break;
096                            }
097                    }
098    
099                    UnicodeProperties typeSettingsProperties =
100                            expandoColumn.getTypeSettingsProperties();
101    
102                    int indexType = GetterUtil.getInteger(
103                            typeSettingsProperties.getProperty(
104                                    ExpandoColumnConstants.INDEX_TYPE));
105    
106                    int type = expandoColumn.getType();
107    
108                    if (type == ExpandoColumnConstants.BOOLEAN) {
109                            document.addKeyword(fieldName, expandoValue.getBoolean());
110                    }
111                    else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
112                            if (!defaultValue) {
113                                    document.addKeyword(fieldName, expandoValue.getBooleanArray());
114                            }
115                            else {
116                                    document.addKeyword(fieldName, new boolean[0]);
117                            }
118                    }
119                    else if (type == ExpandoColumnConstants.DATE) {
120                            document.addDate(fieldName, expandoValue.getDate());
121                    }
122                    else if (type == ExpandoColumnConstants.DOUBLE) {
123                            document.addKeyword(fieldName, expandoValue.getDouble());
124                    }
125                    else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
126                            if (!defaultValue) {
127                                    document.addKeyword(fieldName, expandoValue.getDoubleArray());
128                            }
129                            else {
130                                    document.addKeyword(fieldName, new double[0]);
131                            }
132                    }
133                    else if (type == ExpandoColumnConstants.FLOAT) {
134                            document.addKeyword(fieldName, expandoValue.getFloat());
135                    }
136                    else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
137                            if (!defaultValue) {
138                                    document.addKeyword(fieldName, expandoValue.getFloatArray());
139                            }
140                            else {
141                                    document.addKeyword(fieldName, new float[0]);
142                            }
143                    }
144                    else if (type == ExpandoColumnConstants.INTEGER) {
145                            document.addKeyword(fieldName, expandoValue.getInteger());
146                    }
147                    else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
148                            if (!defaultValue) {
149                                    document.addKeyword(fieldName, expandoValue.getIntegerArray());
150                            }
151                            else {
152                                    document.addKeyword(fieldName, new int[0]);
153                            }
154                    }
155                    else if (type == ExpandoColumnConstants.LONG) {
156                            document.addKeyword(fieldName, expandoValue.getLong());
157                    }
158                    else if (type == ExpandoColumnConstants.LONG_ARRAY) {
159                            if (!defaultValue) {
160                                    document.addKeyword(fieldName, expandoValue.getLongArray());
161                            }
162                            else {
163                                    document.addKeyword(fieldName, new long[0]);
164                            }
165                    }
166                    else if (type == ExpandoColumnConstants.SHORT) {
167                            document.addKeyword(fieldName, expandoValue.getShort());
168                    }
169                    else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
170                            if (!defaultValue) {
171                                    document.addKeyword(fieldName, expandoValue.getShortArray());
172                            }
173                            else {
174                                    document.addKeyword(fieldName, new short[0]);
175                            }
176                    }
177                    else if (type == ExpandoColumnConstants.STRING) {
178                            if (indexType == ExpandoColumnConstants.INDEX_TYPE_KEYWORD) {
179                                    document.addKeyword(fieldName, expandoValue.getString());
180                            }
181                            else {
182                                    document.addText(fieldName, expandoValue.getString());
183                            }
184                    }
185                    else if (type == ExpandoColumnConstants.STRING_ARRAY) {
186                            if (!defaultValue) {
187                                    if (indexType == ExpandoColumnConstants.INDEX_TYPE_KEYWORD) {
188                                            document.addKeyword(
189                                                    fieldName, expandoValue.getStringArray());
190                                    }
191                                    else {
192                                            document.addText(
193                                                    fieldName,
194                                                    StringUtil.merge(
195                                                            expandoValue.getStringArray(), StringPool.SPACE));
196                                    }
197                            }
198                            else {
199                                    if (indexType == ExpandoColumnConstants.INDEX_TYPE_KEYWORD) {
200                                            document.addKeyword(fieldName, StringPool.BLANK);
201                                    }
202                                    else {
203                                            document.addText(fieldName, StringPool.BLANK);
204                                    }
205                            }
206                    }
207            }
208    
209            protected void doAddAttributes(
210                            Document document, ExpandoBridge expandoBridge)
211                    throws SystemException {
212    
213                    List<ExpandoColumn> expandoColumns =
214                            ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
215                                    expandoBridge.getCompanyId(), expandoBridge.getClassName());
216    
217                    if ((expandoColumns == null) || expandoColumns.isEmpty()) {
218                            return;
219                    }
220    
221                    List<ExpandoColumn> indexedColumns = new ArrayList<ExpandoColumn>();
222    
223                    for (ExpandoColumn expandoColumn : expandoColumns) {
224                            UnicodeProperties properties =
225                                    expandoColumn.getTypeSettingsProperties();
226    
227                            int indexType = GetterUtil.getInteger(
228                                    properties.get(ExpandoColumnConstants.INDEX_TYPE));
229    
230                            if (indexType != ExpandoColumnConstants.INDEX_TYPE_NONE) {
231                                    indexedColumns.add(expandoColumn);
232                            }
233                    }
234    
235                    if (indexedColumns.isEmpty()) {
236                            return;
237                    }
238    
239                    List<ExpandoValue> expandoValues =
240                            ExpandoValueLocalServiceUtil.getRowValues(
241                                    expandoBridge.getCompanyId(), expandoBridge.getClassName(),
242                                    ExpandoTableConstants.DEFAULT_TABLE_NAME,
243                                    expandoBridge.getClassPK(), QueryUtil.ALL_POS,
244                                    QueryUtil.ALL_POS);
245    
246                    for (ExpandoColumn expandoColumn : indexedColumns) {
247                            try {
248                                    addAttribute(document, expandoColumn, expandoValues);
249                            }
250                            catch (Exception e) {
251                                    _log.error("Indexing " + expandoColumn.getName(), e);
252                            }
253                    }
254            }
255    
256            protected static final String FIELD_NAMESPACE = "expando";
257    
258            private static Log _log = LogFactoryUtil.getLog(
259                    ExpandoBridgeIndexerImpl.class);
260    
261    }