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