1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.journal.util;
24  
25  import com.liferay.portal.kernel.search.Document;
26  import com.liferay.portal.kernel.search.DocumentImpl;
27  import com.liferay.portal.kernel.search.DocumentSummary;
28  import com.liferay.portal.kernel.search.Field;
29  import com.liferay.portal.kernel.search.SearchEngineUtil;
30  import com.liferay.portal.kernel.search.SearchException;
31  import com.liferay.portal.kernel.util.HtmlUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.kernel.xml.Element;
36  import com.liferay.portal.kernel.xml.SAXReaderUtil;
37  import com.liferay.portal.util.PortletKeys;
38  import com.liferay.portlet.expando.model.ExpandoBridge;
39  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
40  import com.liferay.portlet.journal.model.JournalArticle;
41  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
42  
43  import java.util.Date;
44  import java.util.List;
45  
46  import javax.portlet.PortletURL;
47  
48  /**
49   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Harry Mark
53   * @author Bruno Farache
54   * @author Raymond Augé
55   */
56  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
57  
58      public static final String PORTLET_ID = PortletKeys.JOURNAL;
59  
60      public static void addArticle(
61              long companyId, long groupId, long resourcePrimKey,
62              String articleId, double version, String title, String description,
63              String content, String type, Date displayDate,
64              String[] tagsCategories, String[] tagsEntries,
65              ExpandoBridge expandoBridge)
66          throws SearchException {
67  
68          Document doc = getArticleDocument(
69              companyId, groupId, resourcePrimKey, articleId, version, title,
70              description, content, type, displayDate, tagsCategories,
71              tagsEntries, expandoBridge);
72  
73          SearchEngineUtil.addDocument(companyId, doc);
74      }
75  
76      public static void deleteArticle(
77              long companyId, long groupId, String articleId)
78          throws SearchException {
79  
80          SearchEngineUtil.deleteDocument(
81              companyId, getArticleUID(groupId, articleId));
82      }
83  
84      public static Document getArticleDocument(
85          long companyId, long groupId, long resourcePrimKey, String articleId,
86          double version, String title, String description, String content,
87          String type, Date displayDate, String[] tagsCategories,
88          String[] tagsEntries, ExpandoBridge expandoBridge) {
89  
90          Document doc = new DocumentImpl();
91  
92          if ((content != null) &&
93              ((content.indexOf("<dynamic-content") != -1) ||
94               (content.indexOf("<static-content") != -1))) {
95  
96              content = _getIndexableContent(doc, content);
97  
98              content = StringUtil.replace(
99                  content, "<![CDATA[", StringPool.BLANK);
100             content = StringUtil.replace(content, "]]>", StringPool.BLANK);
101         }
102 
103         content = StringUtil.replace(content, "&amp;", "&");
104         content = StringUtil.replace(content, "&lt;", "<");
105         content = StringUtil.replace(content, "&gt;", ">");
106 
107         content = HtmlUtil.extractText(content);
108 
109         doc.addUID(PORTLET_ID, groupId, articleId);
110 
111         doc.addModifiedDate(displayDate);
112 
113         doc.addKeyword(Field.COMPANY_ID, companyId);
114         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
115         doc.addKeyword(Field.GROUP_ID, groupId);
116 
117         doc.addText(Field.TITLE, title);
118         doc.addText(Field.CONTENT, content);
119         doc.addText(Field.DESCRIPTION, description);
120         doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
121         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
122 
123         doc.addKeyword(Field.ENTRY_CLASS_NAME, JournalArticle.class.getName());
124         doc.addKeyword(Field.ENTRY_CLASS_PK, articleId);
125         doc.addKeyword(Field.ROOT_ENTRY_CLASS_PK, resourcePrimKey);
126         doc.addKeyword(Field.VERSION, version);
127         doc.addKeyword(Field.TYPE, type);
128 
129         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
130 
131         return doc;
132     }
133 
134     public static String getArticleUID(long groupId, String articleId) {
135         Document doc = new DocumentImpl();
136 
137         doc.addUID(PORTLET_ID, groupId, articleId);
138 
139         return doc.get(Field.UID);
140     }
141 
142     public static void updateArticle(
143             long companyId, long groupId, long resourcePrimKey,
144             String articleId, double version, String title, String description,
145             String content, String type, Date displayDate,
146             String[] tagsCategories, String[] tagsEntries,
147             ExpandoBridge expandoBridge)
148         throws SearchException {
149 
150         Document doc = getArticleDocument(
151             companyId, groupId, resourcePrimKey, articleId, version, title,
152             description, content, type, displayDate, tagsCategories,
153             tagsEntries, expandoBridge);
154 
155         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
156     }
157 
158     public String[] getClassNames() {
159         return _CLASS_NAMES;
160     }
161 
162     public DocumentSummary getDocumentSummary(
163         Document doc, String snippet, PortletURL portletURL) {
164 
165         // Title
166 
167         String title = doc.get(Field.TITLE);
168 
169         // Content
170 
171         String content = snippet;
172 
173         if (Validator.isNull(snippet)) {
174             content = StringUtil.shorten(doc.get(Field.CONTENT), 200);
175         }
176 
177         // Portlet URL
178 
179         String groupId = doc.get("groupId");
180         String articleId = doc.get(Field.ENTRY_CLASS_PK);
181         String version = doc.get("version");
182 
183         portletURL.setParameter("struts_action", "/journal/edit_article");
184         portletURL.setParameter("groupId", groupId);
185         portletURL.setParameter("articleId", articleId);
186         portletURL.setParameter("version", version);
187 
188         return new DocumentSummary(title, content, portletURL);
189     }
190 
191     public void reIndex(String className, long classPK) throws SearchException {
192         try {
193             JournalArticleLocalServiceUtil.reIndex(classPK);
194         }
195         catch (Exception e) {
196             throw new SearchException(e);
197         }
198     }
199 
200     public void reIndex(String[] ids) throws SearchException {
201         try {
202             JournalArticleLocalServiceUtil.reIndex(ids);
203         }
204         catch (Exception e) {
205             throw new SearchException(e);
206         }
207     }
208 
209     private static String _getIndexableContent(Document doc, String content) {
210         try {
211             StringBuilder sb = new StringBuilder();
212 
213             com.liferay.portal.kernel.xml.Document contentDoc =
214                 SAXReaderUtil.read(content);
215 
216             Element root = contentDoc.getRootElement();
217 
218             _getIndexableContent(sb, doc, root);
219 
220             return sb.toString();
221         }
222         catch (Exception e) {
223             e.printStackTrace();
224 
225             return content;
226         }
227     }
228 
229     private static void _getIndexableContent(
230             StringBuilder sb, Document doc, Element root)
231         throws Exception {
232 
233         for (Element el : root.elements()) {
234             String elType = el.attributeValue("type", StringPool.BLANK);
235             String elIndexType = el.attributeValue(
236                 "index-type", StringPool.BLANK);
237 
238             _indexField(doc, el, elType, elIndexType);
239 
240             if (elType.equals("text") || elType.equals("text_box") ||
241                 elType.equals("text_area")) {
242 
243                 for (Element dynamicContent : el.elements("dynamic-content")) {
244                     String text = dynamicContent.getText();
245 
246                     sb.append(text);
247                     sb.append(StringPool.SPACE);
248                 }
249             }
250             else if (el.getName().equals("static-content")) {
251                 String text = el.getText();
252 
253                 sb.append(text);
254                 sb.append(StringPool.SPACE);
255             }
256 
257             _getIndexableContent(sb, doc, el);
258         }
259     }
260 
261     private static void _indexField(
262         Document doc, Element el, String elType, String elIndexType) {
263 
264         if (Validator.isNull(elIndexType)) {
265             return;
266         }
267 
268         Element dynamicContent = el.element("dynamic-content");
269 
270         String name = el.attributeValue("name", StringPool.BLANK);
271         String[] value = new String[] {dynamicContent.getText()};
272 
273         if (elType.equals("multi-list")) {
274             List<Element> options = dynamicContent.elements();
275 
276             value = new String[options.size()];
277 
278             for (int i = 0; i < options.size(); i++) {
279                 value[i] = options.get(i).getText();
280             }
281         }
282 
283         if (elIndexType.equals("keyword")) {
284             doc.addKeyword(name, value);
285         }
286         else if (elIndexType.equals("text")) {
287             doc.addText(name, StringUtil.merge(value, StringPool.SPACE));
288         }
289     }
290 
291     private static final String[] _CLASS_NAMES = new String[] {
292         JournalArticle.class.getName()
293     };
294 
295 }