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.model.impl;
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  import com.liferay.portlet.expando.util.ExpandoBridgeIndexer;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * <a href="ExpandoBridgeIndexerImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Raymond Augé
47   *
48   */
49  public class ExpandoBridgeIndexerImpl implements ExpandoBridgeIndexer {
50  
51      public void addAttributes(Document doc, ExpandoBridge expandoBridge) {
52          if (expandoBridge == null) {
53              return;
54          }
55  
56          try {
57              doAddAttributes(doc, expandoBridge);
58          }
59          catch (SystemException se) {
60              _log.error(se, se);
61          }
62      }
63  
64      protected void doAddAttributes(Document doc, ExpandoBridge expandoBridge)
65          throws SystemException {
66  
67          List<ExpandoColumn> expandoColumns =
68              ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
69                  expandoBridge.getClassName());
70  
71          if ((expandoColumns == null) || expandoColumns.isEmpty()) {
72              return;
73          }
74  
75          List<ExpandoColumn> indexedColumns = new ArrayList<ExpandoColumn>();
76  
77          for (ExpandoColumn expandoColumn : expandoColumns) {
78              UnicodeProperties properties =
79                  expandoColumn.getTypeSettingsProperties();
80  
81              boolean indexable = GetterUtil.getBoolean(
82                  properties.get(ExpandoBridgeIndexer.INDEXABLE));
83  
84              if (indexable) {
85                  indexedColumns.add(expandoColumn);
86              }
87          }
88  
89          if (indexedColumns.isEmpty()) {
90              return;
91          }
92  
93          List<ExpandoValue> expandoValues =
94              ExpandoValueLocalServiceUtil.getRowValues(
95                  expandoBridge.getClassName(),
96                  ExpandoTableConstants.DEFAULT_TABLE_NAME,
97                  expandoBridge.getClassPK(), QueryUtil.ALL_POS,
98                  QueryUtil.ALL_POS);
99  
100         for (ExpandoColumn expandoColumn : indexedColumns) {
101             try {
102                 String value = expandoColumn.getDefaultData();
103 
104                 for (ExpandoValue expandoValue : expandoValues) {
105                     if (expandoValue.getColumnId() ==
106                             expandoColumn.getColumnId()) {
107 
108                         value = expandoValue.getData();
109 
110                         break;
111                     }
112                 }
113 
114                 doc.addText(expandoColumn.getName(), value);
115             }
116             catch (Exception e) {
117                 _log.error("Indexing " + expandoColumn.getName(), e);
118             }
119         }
120     }
121 
122     private static Log _log =
123         LogFactoryUtil.getLog(ExpandoBridgeIndexerImpl.class);
124 
125 }