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.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.search.Document;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.UnicodeProperties;
31  import com.liferay.portlet.expando.model.ExpandoBridge;
32  import com.liferay.portlet.expando.model.ExpandoColumnConstants;
33  import com.liferay.portlet.expando.model.ExpandoTableConstants;
34  import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;
35  import com.liferay.portlet.expando.util.ExpandoBridgeIndexer;
36  
37  import java.util.Enumeration;
38  
39  /**
40   * <a href="ExpandoBridgeIndexerImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Raymond Augé
43   *
44   */
45  public class ExpandoBridgeIndexerImpl implements ExpandoBridgeIndexer {
46  
47      public void addAttributes(Document doc, ExpandoBridge expandoBridge) {
48          if (expandoBridge == null) {
49              return;
50          }
51  
52          Enumeration<String> enu = expandoBridge.getAttributeNames();
53  
54          while (enu.hasMoreElements()) {
55              String name = enu.nextElement();
56  
57              int type = expandoBridge.getAttributeType(name);
58  
59              UnicodeProperties properties = expandoBridge.getAttributeProperties(
60                  name);
61  
62              boolean indexable = GetterUtil.getBoolean(
63                  properties.get(ExpandoBridgeIndexer.INDEXABLE));
64  
65              if (!indexable || (type != ExpandoColumnConstants.STRING)) {
66                  continue;
67              }
68  
69              try {
70                  String value = ExpandoValueLocalServiceUtil.getData(
71                      expandoBridge.getClassName(),
72                      ExpandoTableConstants.DEFAULT_TABLE_NAME, name,
73                      expandoBridge.getClassPK(), StringPool.BLANK);
74  
75                  doc.addText(name, value);
76              }
77              catch (Exception e) {
78                  _log.error("Indexing " + name, e);
79              }
80          }
81      }
82  
83      private static Log _log =
84           LogFactoryUtil.getLog(ExpandoBridgeIndexerImpl.class);
85  
86  }