1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.expando.model.impl;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryUtil;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.search.Document;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.UnicodeProperties;
29  import com.liferay.portlet.expando.model.ExpandoBridge;
30  import com.liferay.portlet.expando.model.ExpandoColumn;
31  import com.liferay.portlet.expando.model.ExpandoTableConstants;
32  import com.liferay.portlet.expando.model.ExpandoValue;
33  import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
34  import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
35  import com.liferay.portlet.expando.util.ExpandoBridgeIndexer;
36  
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  /**
41   * <a href="ExpandoBridgeIndexerImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Raymond Augé
44   *
45   */
46  public class ExpandoBridgeIndexerImpl implements ExpandoBridgeIndexer {
47  
48      public void addAttributes(Document doc, ExpandoBridge expandoBridge) {
49          if (expandoBridge == null) {
50              return;
51          }
52  
53          try {
54              doAddAttributes(doc, expandoBridge);
55          }
56          catch (SystemException se) {
57              _log.error(se, se);
58          }
59      }
60  
61      protected void doAddAttributes(Document doc, ExpandoBridge expandoBridge)
62          throws SystemException {
63  
64          List<ExpandoColumn> expandoColumns =
65              ExpandoColumnLocalServiceUtil.getDefaultTableColumns(
66                  expandoBridge.getClassName());
67  
68          if ((expandoColumns == null) || expandoColumns.isEmpty()) {
69              return;
70          }
71  
72          List<ExpandoColumn> indexedColumns = new ArrayList<ExpandoColumn>();
73  
74          for (ExpandoColumn expandoColumn : expandoColumns) {
75              UnicodeProperties properties =
76                  expandoColumn.getTypeSettingsProperties();
77  
78              boolean indexable = GetterUtil.getBoolean(
79                  properties.get(ExpandoBridgeIndexer.INDEXABLE));
80  
81              if (indexable) {
82                  indexedColumns.add(expandoColumn);
83              }
84          }
85  
86          if (indexedColumns.isEmpty()) {
87              return;
88          }
89  
90          List<ExpandoValue> expandoValues =
91              ExpandoValueLocalServiceUtil.getRowValues(
92                  expandoBridge.getClassName(),
93                  ExpandoTableConstants.DEFAULT_TABLE_NAME,
94                  expandoBridge.getClassPK(), QueryUtil.ALL_POS,
95                  QueryUtil.ALL_POS);
96  
97          for (ExpandoColumn expandoColumn : indexedColumns) {
98              try {
99                  String value = expandoColumn.getDefaultData();
100 
101                 for (ExpandoValue expandoValue : expandoValues) {
102                     if (expandoValue.getColumnId() ==
103                             expandoColumn.getColumnId()) {
104 
105                         value = expandoValue.getData();
106 
107                         break;
108                     }
109                 }
110 
111                 doc.addText(expandoColumn.getName(), value);
112             }
113             catch (Exception e) {
114                 _log.error("Indexing " + expandoColumn.getName(), e);
115             }
116         }
117     }
118 
119     private static Log _log =
120         LogFactoryUtil.getLog(ExpandoBridgeIndexerImpl.class);
121 
122 }