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.portal.plugin;
022:
023: import com.liferay.portal.kernel.search.DocumentSummary;
024: import com.liferay.portal.kernel.search.Indexer;
025: import com.liferay.portal.kernel.search.SearchException;
026: import com.liferay.portal.kernel.util.StringMaker;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.StringUtil;
029: import com.liferay.portal.lucene.LuceneFields;
030: import com.liferay.portal.lucene.LuceneUtil;
031: import com.liferay.portal.model.impl.CompanyImpl;
032: import com.liferay.util.Html;
033: import com.liferay.util.License;
034:
035: import java.io.IOException;
036:
037: import java.util.Date;
038: import java.util.Iterator;
039: import java.util.List;
040:
041: import javax.portlet.PortletURL;
042:
043: import org.apache.lucene.document.Document;
044: import org.apache.lucene.index.IndexWriter;
045: import org.apache.lucene.index.Term;
046:
047: /**
048: * <a href="PluginPackageIndexer.java.html"><b><i>View Source</i></b></a>
049: *
050: * @author Jorge Ferrer
051: * @author Brian Wing Shun Chan
052: *
053: */
054: public class PluginPackageIndexer implements Indexer {
055:
056: public static final String PORTLET_ID = "PluginPackageIndexer";
057:
058: public static void addPluginPackage(String moduleId, String name,
059: String version, Date modifiedDate, String author,
060: List types, List tags, List licenses, List liferayVersions,
061: String shortDescription, String longDescription,
062: String changeLog, String pageURL, String repositoryURL,
063: String status, String installedVersion) throws IOException {
064:
065: Document doc = getAddPluginPackageDocument(moduleId, name,
066: version, modifiedDate, author, types, tags, licenses,
067: liferayVersions, shortDescription, longDescription,
068: changeLog, pageURL, repositoryURL, status,
069: installedVersion);
070:
071: IndexWriter writer = null;
072:
073: try {
074: writer = LuceneUtil.getWriter(CompanyImpl.SYSTEM);
075:
076: writer.addDocument(doc);
077: } finally {
078: if (writer != null) {
079: LuceneUtil.write(CompanyImpl.SYSTEM);
080: }
081: }
082: }
083:
084: public static void cleanIndex() throws IOException {
085: LuceneUtil.deleteDocuments(CompanyImpl.SYSTEM, new Term(
086: LuceneFields.PORTLET_ID, PORTLET_ID));
087: }
088:
089: public static Document getAddPluginPackageDocument(String moduleId,
090: String name, String version, Date modifiedDate,
091: String author, List types, List tags, List licenses,
092: List liferayVersions, String shortDescription,
093: String longDescription, String changeLog, String pageURL,
094: String repositoryURL, String status, String installedVersion) {
095:
096: ModuleId moduleIdObj = ModuleId.getInstance(moduleId);
097:
098: shortDescription = Html.stripHtml(shortDescription);
099: longDescription = Html.stripHtml(longDescription);
100:
101: String content = name + " " + author + " " + shortDescription
102: + " " + longDescription;
103:
104: Document doc = new Document();
105:
106: doc.add(LuceneFields.getKeyword(LuceneFields.UID, LuceneFields
107: .getUID(PORTLET_ID, moduleId)));
108:
109: doc.add(LuceneFields.getKeyword(LuceneFields.PORTLET_ID,
110: PORTLET_ID));
111:
112: doc.add(LuceneFields.getText(LuceneFields.TITLE, name));
113: doc.add(LuceneFields.getText(LuceneFields.CONTENT, content));
114:
115: doc.add(LuceneFields.getDate(LuceneFields.MODIFIED));
116:
117: doc.add(LuceneFields.getKeyword("moduleId", moduleId));
118: doc.add(LuceneFields.getKeyword("groupId", moduleIdObj
119: .getGroupId()));
120: doc.add(LuceneFields.getKeyword("artifactId", moduleIdObj
121: .getArtifactId()));
122: doc.add(LuceneFields.getKeyword("version", version));
123: doc.add(LuceneFields.getDate("modified-date", modifiedDate));
124: doc.add(LuceneFields.getKeyword("shortDescription",
125: shortDescription));
126: doc.add(LuceneFields.getKeyword("changeLog", changeLog));
127: doc
128: .add(LuceneFields.getKeyword("repositoryURL",
129: repositoryURL));
130:
131: StringMaker sm = new StringMaker();
132:
133: Iterator itr = types.iterator();
134:
135: while (itr.hasNext()) {
136: String type = (String) itr.next();
137:
138: doc.add(LuceneFields.getKeyword("type", type));
139:
140: sm.append(type);
141:
142: if (itr.hasNext()) {
143: sm.append(StringPool.COMMA);
144: sm.append(StringPool.SPACE);
145: }
146: }
147:
148: doc.add(LuceneFields.getKeyword("types", sm.toString()));
149:
150: sm = new StringMaker();
151:
152: itr = tags.iterator();
153:
154: while (itr.hasNext()) {
155: String tag = (String) itr.next();
156:
157: doc.add(LuceneFields.getKeyword("tag", tag));
158:
159: sm.append(tag);
160:
161: if (itr.hasNext()) {
162: sm.append(StringPool.COMMA);
163: sm.append(StringPool.SPACE);
164: }
165: }
166:
167: doc.add(LuceneFields.getKeyword("tags", sm.toString()));
168:
169: boolean osiLicense = false;
170:
171: itr = licenses.iterator();
172:
173: while (itr.hasNext()) {
174: License license = (License) itr.next();
175:
176: doc.add(LuceneFields.getKeyword("license", license
177: .getName()));
178:
179: if (license.isOsiApproved()) {
180: osiLicense = true;
181: }
182: }
183:
184: doc.add(LuceneFields.getKeyword("osi-approved-license", String
185: .valueOf(osiLicense)));
186:
187: doc.add(LuceneFields.getKeyword("status", status));
188:
189: if (installedVersion != null) {
190: doc.add(LuceneFields.getKeyword("installedVersion",
191: installedVersion));
192: }
193:
194: return doc;
195: }
196:
197: public static void removePluginPackage(String moduleId)
198: throws IOException {
199:
200: LuceneUtil.deleteDocuments(CompanyImpl.SYSTEM, new Term(
201: LuceneFields.UID, LuceneFields.getUID(PORTLET_ID,
202: moduleId)));
203: }
204:
205: public static void updatePluginPackage(String moduleId,
206: String name, String version, Date modifiedDate,
207: String author, List types, List tags, List licenses,
208: List liferayVersions, String shortDescription,
209: String longDescription, String changeLog, String pageURL,
210: String repositoryURL, String status, String installedVersion)
211: throws IOException {
212:
213: try {
214: removePluginPackage(moduleId);
215: } catch (IOException ioe) {
216: }
217:
218: addPluginPackage(moduleId, name, version, modifiedDate, author,
219: types, tags, licenses, liferayVersions,
220: shortDescription, longDescription, changeLog, pageURL,
221: repositoryURL, status, installedVersion);
222: }
223:
224: public DocumentSummary getDocumentSummary(
225: com.liferay.portal.kernel.search.Document doc,
226: PortletURL portletURL) {
227:
228: // Title
229:
230: String title = doc.get(LuceneFields.TITLE);
231:
232: // Content
233:
234: String content = doc.get(LuceneFields.CONTENT);
235:
236: content = StringUtil.shorten(content, 200);
237:
238: // URL
239:
240: String moduleId = doc.get("moduleId");
241: String repositoryURL = doc.get("repositoryURL");
242:
243: portletURL.setParameter("struts_action", "/admin/view");
244: portletURL.setParameter("tabs2", "repositories");
245: portletURL.setParameter("moduleId", moduleId);
246: portletURL.setParameter("repositoryURL", repositoryURL);
247:
248: return new DocumentSummary(title, content, portletURL);
249: }
250:
251: public void reIndex(String[] ids) throws SearchException {
252: try {
253: PluginPackageUtil.reIndex();
254: } catch (Exception e) {
255: throw new SearchException(e);
256: }
257: }
258:
259: }
|