1   /**
2    * Copyright (c) 2000-2008 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.imagegallery.util;
24  
25  import com.liferay.portal.kernel.search.DocumentSummary;
26  import com.liferay.portal.kernel.search.SearchException;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.lucene.LuceneFields;
29  import com.liferay.portal.lucene.LuceneUtil;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
32  
33  import java.io.IOException;
34  
35  import javax.portlet.PortletURL;
36  
37  import org.apache.lucene.document.Document;
38  import org.apache.lucene.index.IndexWriter;
39  import org.apache.lucene.index.Term;
40  
41  /**
42   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
48  
49      public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
50  
51      public static void addImage(
52              long companyId, long groupId, long folderId, long imageId,
53              String name, String description, String[] tagsEntries)
54          throws IOException {
55  
56          Document doc = getAddImageDocument(
57              companyId, groupId, folderId, imageId, name, description,
58              tagsEntries);
59  
60          IndexWriter writer = null;
61  
62          try {
63              writer = LuceneUtil.getWriter(companyId);
64  
65              writer.addDocument(doc);
66          }
67          finally {
68              if (writer != null) {
69                  LuceneUtil.write(companyId);
70              }
71          }
72      }
73  
74      public static void deleteImage(long companyId, long imageId)
75          throws IOException {
76  
77          LuceneUtil.deleteDocuments(
78              companyId,
79              new Term(
80                  LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, imageId)));
81      }
82  
83      public static Document getAddImageDocument(
84          long companyId, long groupId, long folderId, long imageId,
85          String name, String description, String[] tagsEntries) {
86  
87          Document doc = new Document();
88  
89          LuceneUtil.addKeyword(
90              doc, LuceneFields.UID, LuceneFields.getUID(PORTLET_ID, imageId));
91  
92          LuceneUtil.addKeyword(doc, LuceneFields.COMPANY_ID, companyId);
93          LuceneUtil.addKeyword(doc, LuceneFields.PORTLET_ID, PORTLET_ID);
94          LuceneUtil.addKeyword(doc, LuceneFields.GROUP_ID, groupId);
95  
96          LuceneUtil.addText(doc, LuceneFields.NAME, name);
97          LuceneUtil.addText(doc, LuceneFields.DESCRIPTION, description);
98  
99          LuceneUtil.addModifiedDate(doc);
100 
101         LuceneUtil.addKeyword(doc, "folderId", folderId);
102         LuceneUtil.addKeyword(doc, "imageId", imageId);
103 
104         LuceneUtil.addKeyword(doc, LuceneFields.TAG_ENTRY, tagsEntries);
105 
106         return doc;
107     }
108 
109     public static void updateImage(
110             long companyId, long groupId, long folderId, long imageId,
111             String name, String description, String[] tagsEntries)
112         throws IOException {
113 
114         try {
115             deleteImage(companyId, imageId);
116         }
117         catch (IOException ioe) {
118         }
119 
120         addImage(
121             companyId, groupId, folderId, imageId, name, description,
122             tagsEntries);
123     }
124 
125     public DocumentSummary getDocumentSummary(
126         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
127 
128         // Title
129 
130         String title = doc.get(LuceneFields.TITLE);
131 
132         // Content
133 
134         String content = doc.get(LuceneFields.CONTENT);
135 
136         content = StringUtil.shorten(content, 200);
137 
138         // URL
139 
140         String imageId = doc.get("imageId");
141 
142         portletURL.setParameter("struts_action", "/image_gallery/edit_image");
143         portletURL.setParameter("imageId", imageId);
144 
145         return new DocumentSummary(title, content, portletURL);
146     }
147 
148     public void reIndex(String[] ids) throws SearchException {
149         try {
150             IGFolderLocalServiceUtil.reIndex(ids);
151         }
152         catch (Exception e) {
153             throw new SearchException(e);
154         }
155     }
156 
157 }