001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.softwarecatalog.util;
022:
023: import com.liferay.portal.kernel.search.DocumentSummary;
024: import com.liferay.portal.kernel.search.SearchException;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.lucene.LuceneFields;
027: import com.liferay.portal.lucene.LuceneUtil;
028: import com.liferay.portal.util.PortletKeys;
029: import com.liferay.portlet.softwarecatalog.service.SCProductEntryLocalServiceUtil;
030: import com.liferay.util.Html;
031:
032: import java.io.IOException;
033:
034: import java.util.Date;
035:
036: import javax.portlet.PortletURL;
037:
038: import org.apache.lucene.document.Document;
039: import org.apache.lucene.index.IndexWriter;
040: import org.apache.lucene.index.Term;
041:
042: /**
043: * <a href="Indexer.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Jorge Ferrer
046: * @author Brian Wing Shun Chan
047: * @author Harry Mark
048: *
049: */
050: public class Indexer implements
051: com.liferay.portal.kernel.search.Indexer {
052:
053: public static final String PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
054:
055: public static void addProductEntry(long companyId, long groupId,
056: long userId, String userName, long productEntryId,
057: String name, Date modifiedDate, String version,
058: String type, String shortDescription,
059: String longDescription, String pageURL, String repoGroupId,
060: String repoArtifactId) throws IOException {
061:
062: Document doc = getAddProductEntryDocument(companyId, groupId,
063: userId, userName, productEntryId, name, modifiedDate,
064: version, type, shortDescription, longDescription,
065: pageURL, repoGroupId, repoArtifactId);
066:
067: IndexWriter writer = null;
068:
069: try {
070: writer = LuceneUtil.getWriter(companyId);
071:
072: writer.addDocument(doc);
073: } finally {
074: if (writer != null) {
075: LuceneUtil.write(companyId);
076: }
077: }
078: }
079:
080: public static void deleteProductEntry(long companyId,
081: long productEntryId) throws IOException {
082:
083: LuceneUtil.deleteDocuments(companyId, new Term(
084: LuceneFields.UID, LuceneFields.getUID(PORTLET_ID,
085: productEntryId)));
086: }
087:
088: public static Document getAddProductEntryDocument(long companyId,
089: long groupId, long userId, String userName,
090: long productEntryId, String name, Date modifiedDate,
091: String version, String type, String shortDescription,
092: String longDescription, String pageURL, String repoGroupId,
093: String repoArtifactId) {
094:
095: shortDescription = Html.stripHtml(shortDescription);
096: longDescription = Html.stripHtml(longDescription);
097:
098: String content = userId + " " + userName + " " + type + " "
099: + shortDescription + " " + longDescription + " "
100: + pageURL + repoGroupId + " " + repoArtifactId;
101:
102: Document doc = new Document();
103:
104: doc.add(LuceneFields.getKeyword(LuceneFields.UID, LuceneFields
105: .getUID(PORTLET_ID, productEntryId)));
106:
107: doc.add(LuceneFields.getKeyword(LuceneFields.COMPANY_ID,
108: companyId));
109: doc.add(LuceneFields.getKeyword(LuceneFields.PORTLET_ID,
110: PORTLET_ID));
111: doc
112: .add(LuceneFields.getKeyword(LuceneFields.GROUP_ID,
113: groupId));
114: doc.add(LuceneFields.getKeyword(LuceneFields.USER_ID, userId));
115:
116: doc.add(LuceneFields.getText(LuceneFields.TITLE, name));
117: doc.add(LuceneFields.getText(LuceneFields.CONTENT, content));
118:
119: doc.add(LuceneFields.getDate(LuceneFields.MODIFIED));
120:
121: doc.add(LuceneFields.getKeyword("productEntryId",
122: productEntryId));
123: doc.add(LuceneFields.getDate("modified-date", modifiedDate));
124: doc.add(LuceneFields.getText("version", version));
125: doc.add(LuceneFields.getKeyword("type", type));
126: doc.add(LuceneFields.getKeyword("repoGroupId", repoGroupId));
127: doc.add(LuceneFields.getKeyword("repoArtifactId",
128: repoArtifactId));
129:
130: return doc;
131: }
132:
133: public static void updateProductEntry(long companyId, long groupId,
134: long userId, String userName, long productEntryId,
135: String name, Date modifiedDate, String version,
136: String type, String shortDescription,
137: String longDescription, String pageURL, String repoGroupId,
138: String repoArtifactId) throws IOException {
139:
140: try {
141: deleteProductEntry(companyId, productEntryId);
142: } catch (IOException ioe) {
143: }
144:
145: addProductEntry(companyId, groupId, userId, userName,
146: productEntryId, name, modifiedDate, version, type,
147: shortDescription, longDescription, pageURL,
148: repoGroupId, repoArtifactId);
149: }
150:
151: public DocumentSummary getDocumentSummary(
152: com.liferay.portal.kernel.search.Document doc,
153: PortletURL portletURL) {
154:
155: // Title
156:
157: String title = doc.get(LuceneFields.TITLE);
158:
159: // Content
160:
161: String content = doc.get(LuceneFields.CONTENT);
162:
163: content = StringUtil.shorten(content, 200);
164:
165: // URL
166:
167: String productEntryId = doc.get("productEntryId");
168:
169: portletURL.setParameter("struts_action",
170: "/software_catalog/view_product_entry");
171: portletURL.setParameter("productEntryId", productEntryId);
172:
173: return new DocumentSummary(title, content, portletURL);
174: }
175:
176: public void reIndex(String[] ids) throws SearchException {
177: try {
178: SCProductEntryLocalServiceUtil.reIndex(ids);
179: } catch (Exception e) {
180: throw new SearchException(e);
181: }
182: }
183:
184: }
|