1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.expando.util;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.search.Document;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.UnicodeProperties;
32  import com.liferay.portlet.expando.model.ExpandoBridge;
33  import com.liferay.portlet.expando.model.ExpandoColumn;
34  import com.liferay.portlet.expando.model.ExpandoTableConstants;
35  import com.liferay.portlet.expando.model.ExpandoValue;
36  import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
37  import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * <a href="ExpandoBridgeIndexerImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Raymond Augé
46   */
47  public class ExpandoBridgeIndexerImpl implements ExpandoBridgeIndexer {
48  
49      public void addAttributes(Document doc, ExpandoBridge expandoBridge) {
50          if (expandoBridge == null) {
51              return;
52          }
53  
54          try {
55              doAddAttributes(doc, expandoBridge);
56          }
57          catch (SystemException se) {
58              _log.error(se, se);
59          }
60      }
61  
62      protected void doAddAttributes(Document doc, ExpandoBridge expandoBridge)
63          throws SystemException {
64  
65          List<ExpandoColumn> expandoColumns =
66              ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
67                  expandoBridge.getClassName());
68  
69          if ((expandoColumns == null) || expandoColumns.isEmpty()) {
70              return;
71          }
72  
73          List<ExpandoColumn> indexedColumns = new ArrayList<ExpandoColumn>();
74  
75          for (ExpandoColumn expandoColumn : expandoColumns) {
76              UnicodeProperties properties =
77                  expandoColumn.getTypeSettingsProperties();
78  
79              boolean indexable = GetterUtil.getBoolean(
80                  properties.get(ExpandoBridgeIndexer.INDEXABLE));
81  
82              if (indexable) {
83                  indexedColumns.add(expandoColumn);
84              }
85          }
86  
87          if (indexedColumns.isEmpty()) {
88              return;
89          }
90  
91          List<ExpandoValue> expandoValues =
92              ExpandoValueLocalServiceUtil.getRowValues(
93                  expandoBridge.getClassName(),
94                  ExpandoTableConstants.DEFAULT_TABLE_NAME,
95                  expandoBridge.getClassPK(), QueryUtil.ALL_POS,
96                  QueryUtil.ALL_POS);
97  
98          for (ExpandoColumn expandoColumn : indexedColumns) {
99              try {
100                 String value = expandoColumn.getDefaultData();
101 
102                 for (ExpandoValue expandoValue : expandoValues) {
103                     if (expandoValue.getColumnId() ==
104                             expandoColumn.getColumnId()) {
105 
106                         value = expandoValue.getData();
107 
108                         break;
109                     }
110                 }
111 
112                 doc.addText(expandoColumn.getName(), value);
113             }
114             catch (Exception e) {
115                 _log.error("Indexing " + expandoColumn.getName(), e);
116             }
117         }
118     }
119 
120     private static Log _log =
121         LogFactoryUtil.getLog(ExpandoBridgeIndexerImpl.class);
122 
123 }