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.imagegallery.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.StringUtil;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
34  
35  import java.util.Date;
36  
37  import javax.portlet.PortletURL;
38  
39  /**
40   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Bruno Farache
44   */
45  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
46  
47      public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
48  
49      public static void addImage(
50              long companyId, long groupId, long folderId, long imageId,
51              String name, String description,  Date modifiedDate,
52              String[] tagsEntries)
53          throws SearchException {
54  
55          Document doc = getImageDocument(
56              companyId, groupId, folderId, imageId, name, description,
57              modifiedDate, tagsEntries);
58  
59          SearchEngineUtil.addDocument(companyId, doc);
60      }
61  
62      public static void deleteImage(long companyId, long imageId)
63          throws SearchException {
64  
65          SearchEngineUtil.deleteDocument(companyId, getImageUID(imageId));
66      }
67  
68      public static Document getImageDocument(
69          long companyId, long groupId, long folderId, long imageId,
70          String name, String description, Date modifiedDate,
71          String[] tagsEntries) {
72  
73          Document doc = new DocumentImpl();
74  
75          doc.addUID(PORTLET_ID, imageId);
76  
77          doc.addModifiedDate(modifiedDate);
78  
79          doc.addKeyword(Field.COMPANY_ID, companyId);
80          doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
81          doc.addKeyword(Field.GROUP_ID, groupId);
82  
83          doc.addText(Field.TITLE, name);
84          doc.addText(Field.DESCRIPTION, description);
85  
86          doc.addKeyword("folderId", folderId);
87          doc.addKeyword(Field.ENTRY_CLASS_PK, imageId);
88  
89          doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
90  
91          return doc;
92      }
93  
94      public static String getImageUID(long imageId) {
95          Document doc = new DocumentImpl();
96  
97          doc.addUID(PORTLET_ID, imageId);
98  
99          return doc.get(Field.UID);
100     }
101 
102     public static void updateImage(
103             long companyId, long groupId, long folderId, long imageId,
104             String name, String description, Date modifiedDate,
105             String[] tagsEntries)
106         throws SearchException {
107 
108         Document doc = getImageDocument(
109             companyId, groupId, folderId, imageId, name, description,
110             modifiedDate, tagsEntries);
111 
112         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
113     }
114 
115     public DocumentSummary getDocumentSummary(
116         com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
117 
118         // Title
119 
120         String title = doc.get(Field.TITLE);
121 
122         // Content
123 
124         String content = doc.get(Field.CONTENT);
125 
126         content = StringUtil.shorten(content, 200);
127 
128         // Portlet URL
129 
130         String imageId = doc.get(Field.ENTRY_CLASS_PK);
131 
132         portletURL.setParameter("struts_action", "/image_gallery/edit_image");
133         portletURL.setParameter("imageId", imageId);
134 
135         return new DocumentSummary(title, content, portletURL);
136     }
137 
138     public void reIndex(String[] ids) throws SearchException {
139         try {
140             IGFolderLocalServiceUtil.reIndex(ids);
141         }
142         catch (Exception e) {
143             throw new SearchException(e);
144         }
145     }
146 
147 }