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.kernel.util.Validator;
33  import com.liferay.portal.model.Group;
34  import com.liferay.portal.service.GroupLocalServiceUtil;
35  import com.liferay.portal.util.PortletKeys;
36  import com.liferay.portlet.expando.model.ExpandoBridge;
37  import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;
38  import com.liferay.portlet.imagegallery.model.IGImage;
39  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
40  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
41  
42  import java.util.Date;
43  
44  import javax.portlet.PortletURL;
45  
46  /**
47   * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Bruno Farache
51   * @author Raymond Augé
52   */
53  public class Indexer implements com.liferay.portal.kernel.search.Indexer {
54  
55      public static final String PORTLET_ID = PortletKeys.IMAGE_GALLERY;
56  
57      public static void addImage(
58              long companyId, long groupId, long folderId, long imageId,
59              String name, String description, Date modifiedDate,
60              String[] tagsCategories, String[] tagsEntries,
61              ExpandoBridge expandoBridge)
62          throws SearchException {
63  
64          Document doc = getImageDocument(
65              companyId, groupId, folderId, imageId, name, description,
66              modifiedDate, tagsCategories, tagsEntries, expandoBridge);
67  
68          SearchEngineUtil.addDocument(companyId, doc);
69      }
70  
71      public static void deleteImage(long companyId, long imageId)
72          throws SearchException {
73  
74          SearchEngineUtil.deleteDocument(companyId, getImageUID(imageId));
75      }
76  
77      public static Document getImageDocument(
78          long companyId, long groupId, long folderId, long imageId,
79          String name, String description, Date modifiedDate,
80          String[] tagsCategories, String[] tagsEntries,
81          ExpandoBridge expandoBridge) {
82  
83          long scopeGroupId = groupId;
84  
85          try {
86              Group group = GroupLocalServiceUtil.getGroup(groupId);
87  
88              if (group.isLayout()) {
89                  groupId = group.getParentGroupId();
90              }
91          }
92          catch (Exception e) {
93          }
94  
95          Document doc = new DocumentImpl();
96  
97          doc.addUID(PORTLET_ID, imageId);
98  
99          doc.addModifiedDate(modifiedDate);
100 
101         doc.addKeyword(Field.COMPANY_ID, companyId);
102         doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
103         doc.addKeyword(Field.GROUP_ID, groupId);
104         doc.addKeyword(Field.SCOPE_GROUP_ID, scopeGroupId);
105 
106         doc.addText(Field.TITLE, name);
107         doc.addText(Field.DESCRIPTION, description);
108         doc.addKeyword(Field.TAGS_CATEGORIES, tagsCategories);
109         doc.addKeyword(Field.TAGS_ENTRIES, tagsEntries);
110 
111         doc.addKeyword("folderId", folderId);
112         doc.addKeyword(Field.ENTRY_CLASS_NAME, IGImage.class.getName());
113         doc.addKeyword(Field.ENTRY_CLASS_PK, imageId);
114 
115         ExpandoBridgeIndexerUtil.addAttributes(doc, expandoBridge);
116 
117         return doc;
118     }
119 
120     public static String getImageUID(long imageId) {
121         Document doc = new DocumentImpl();
122 
123         doc.addUID(PORTLET_ID, imageId);
124 
125         return doc.get(Field.UID);
126     }
127 
128     public static void updateImage(
129             long companyId, long groupId, long folderId, long imageId,
130             String name, String description, Date modifiedDate,
131             String[] tagsCategories, String[] tagsEntries,
132             ExpandoBridge expandoBridge)
133         throws SearchException {
134 
135         Document doc = getImageDocument(
136             companyId, groupId, folderId, imageId, name, description,
137             modifiedDate, tagsCategories, tagsEntries, expandoBridge);
138 
139         SearchEngineUtil.updateDocument(companyId, doc.get(Field.UID), doc);
140     }
141 
142     public String[] getClassNames() {
143         return _CLASS_NAMES;
144     }
145 
146     public DocumentSummary getDocumentSummary(
147         Document doc, String snippet, PortletURL portletURL) {
148 
149         // Title
150 
151         String title = doc.get(Field.TITLE);
152 
153         // Content
154 
155         String content = snippet;
156 
157         if (Validator.isNull(snippet)) {
158             content = StringUtil.shorten(doc.get(Field.DESCRIPTION), 200);
159         }
160 
161         // Portlet URL
162 
163         String imageId = doc.get(Field.ENTRY_CLASS_PK);
164 
165         portletURL.setParameter("struts_action", "/image_gallery/edit_image");
166         portletURL.setParameter("imageId", imageId);
167 
168         return new DocumentSummary(title, content, portletURL);
169     }
170 
171     public void reIndex(String className, long classPK) throws SearchException {
172         try {
173             IGImageLocalServiceUtil.reIndex(classPK);
174         }
175         catch (Exception e) {
176             throw new SearchException(e);
177         }
178     }
179 
180     public void reIndex(String[] ids) throws SearchException {
181         try {
182             IGFolderLocalServiceUtil.reIndex(ids);
183         }
184         catch (Exception e) {
185             throw new SearchException(e);
186         }
187     }
188 
189     private static final String[] _CLASS_NAMES = new String[] {
190         IGImage.class.getName()
191     };
192 
193 }