1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.expando.util;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.search.Document;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.UnicodeProperties;
24  import com.liferay.portlet.expando.model.ExpandoBridge;
25  import com.liferay.portlet.expando.model.ExpandoColumn;
26  import com.liferay.portlet.expando.model.ExpandoTableConstants;
27  import com.liferay.portlet.expando.model.ExpandoValue;
28  import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
29  import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * <a href="ExpandoBridgeIndexerImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Raymond Augé
38   */
39  public class ExpandoBridgeIndexerImpl implements ExpandoBridgeIndexer {
40  
41      public void addAttributes(Document doc, ExpandoBridge expandoBridge) {
42          if (expandoBridge == null) {
43              return;
44          }
45  
46          try {
47              doAddAttributes(doc, expandoBridge);
48          }
49          catch (SystemException se) {
50              _log.error(se, se);
51          }
52      }
53  
54      protected void doAddAttributes(Document doc, ExpandoBridge expandoBridge)
55          throws SystemException {
56  
57          List<ExpandoColumn> expandoColumns =
58              ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
59                  expandoBridge.getClassName());
60  
61          if ((expandoColumns == null) || expandoColumns.isEmpty()) {
62              return;
63          }
64  
65          List<ExpandoColumn> indexedColumns = new ArrayList<ExpandoColumn>();
66  
67          for (ExpandoColumn expandoColumn : expandoColumns) {
68              UnicodeProperties properties =
69                  expandoColumn.getTypeSettingsProperties();
70  
71              boolean indexable = GetterUtil.getBoolean(
72                  properties.get(ExpandoBridgeIndexer.INDEXABLE));
73  
74              if (indexable) {
75                  indexedColumns.add(expandoColumn);
76              }
77          }
78  
79          if (indexedColumns.isEmpty()) {
80              return;
81          }
82  
83          List<ExpandoValue> expandoValues =
84              ExpandoValueLocalServiceUtil.getRowValues(
85                  expandoBridge.getClassName(),
86                  ExpandoTableConstants.DEFAULT_TABLE_NAME,
87                  expandoBridge.getClassPK(), QueryUtil.ALL_POS,
88                  QueryUtil.ALL_POS);
89  
90          for (ExpandoColumn expandoColumn : indexedColumns) {
91              try {
92                  String value = expandoColumn.getDefaultData();
93  
94                  for (ExpandoValue expandoValue : expandoValues) {
95                      if (expandoValue.getColumnId() ==
96                              expandoColumn.getColumnId()) {
97  
98                          value = expandoValue.getData();
99  
100                         break;
101                     }
102                 }
103 
104                 doc.addText(expandoColumn.getName(), value);
105             }
106             catch (Exception e) {
107                 _log.error("Indexing " + expandoColumn.getName(), e);
108             }
109         }
110     }
111 
112     private static Log _log = LogFactoryUtil.getLog(
113         ExpandoBridgeIndexerImpl.class);
114 
115 }