1
22
23 package com.liferay.portal.plugin;
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.Indexer;
30 import com.liferay.portal.kernel.search.SearchEngineUtil;
31 import com.liferay.portal.kernel.search.SearchException;
32 import com.liferay.portal.kernel.util.HtmlUtil;
33 import com.liferay.portal.kernel.util.StringUtil;
34 import com.liferay.portal.model.CompanyConstants;
35 import com.liferay.util.License;
36
37 import java.util.Date;
38 import java.util.List;
39
40 import javax.portlet.PortletURL;
41
42
50 public class PluginPackageIndexer implements Indexer {
51
52 public static final String PORTLET_ID = "PluginPackageIndexer";
53
54 public static void addPluginPackage(
55 String moduleId, String name, String version, Date modifiedDate,
56 String author, List<String> types, List<String> tags, List licenses,
57 List liferayVersions, String shortDescription,
58 String longDescription, String changeLog, String pageURL,
59 String repositoryURL, String status, String installedVersion)
60 throws SearchException {
61
62 Document doc = getPluginPackageDocument(
63 moduleId, name, version, modifiedDate, author, types, tags,
64 licenses, liferayVersions, shortDescription, longDescription,
65 changeLog, pageURL, repositoryURL, status, installedVersion);
66
67 SearchEngineUtil.addDocument(CompanyConstants.SYSTEM, doc);
68 }
69
70 public static void cleanIndex() throws SearchException {
71 SearchEngineUtil.deletePortletDocuments(
72 CompanyConstants.SYSTEM, PORTLET_ID);
73 }
74
75 public static Document getPluginPackageDocument(
76 String moduleId, String name, String version, Date modifiedDate,
77 String author, List<String> types, List<String> tags, List licenses,
78 List liferayVersions, String shortDescription,
79 String longDescription, String changeLog, String pageURL,
80 String repositoryURL, String status, String installedVersion) {
81
82 ModuleId moduleIdObj = ModuleId.getInstance(moduleId);
83
84 shortDescription = HtmlUtil.extractText(shortDescription);
85 longDescription = HtmlUtil.extractText(longDescription);
86
87 String content =
88 name + " " + author + " " + shortDescription + " " +
89 longDescription;
90
91 Document doc = new DocumentImpl();
92
93 doc.addUID(PORTLET_ID, moduleId);
94
95 doc.addModifiedDate(modifiedDate);
96
97 doc.addKeyword(Field.PORTLET_ID, PORTLET_ID);
98 doc.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
99
100 doc.addText(Field.TITLE, name);
101 doc.addText(Field.CONTENT, content);
102
103 doc.addKeyword("moduleId", moduleId);
104 doc.addKeyword("artifactId", moduleIdObj.getArtifactId());
105 doc.addKeyword("version", version);
106 doc.addText("author", author);
107 doc.addKeyword("type", types.toArray(new String[0]));
108 doc.addKeyword("tag", tags.toArray(new String[0]));
109
110 String[] licenseNames = new String[licenses.size()];
111
112 boolean osiLicense = false;
113
114 for (int i = 0; i < licenses.size(); i++) {
115 License license = (License)licenses.get(i);
116
117 licenseNames[i] = license.getName();
118
119 if (license.isOsiApproved()) {
120 osiLicense = true;
121 }
122 }
123
124 doc.addKeyword("license", licenseNames);
125 doc.addKeyword("osi-approved-license", String.valueOf(osiLicense));
126 doc.addText("shortDescription", shortDescription);
127 doc.addText("longDescription", longDescription);
128 doc.addText("changeLog", changeLog);
129 doc.addText("pageURL", pageURL);
130 doc.addKeyword("repositoryURL", repositoryURL);
131 doc.addKeyword("status", status);
132 doc.addKeyword("installedVersion", installedVersion);
133
134 return doc;
135 }
136
137 public static String getPluginPackagerUID(String moduleId) {
138 Document doc = new DocumentImpl();
139
140 doc.addUID(PORTLET_ID, moduleId);
141
142 return doc.get(Field.UID);
143 }
144
145 public static void removePluginPackage(String moduleId)
146 throws SearchException {
147
148 SearchEngineUtil.deleteDocument(
149 CompanyConstants.SYSTEM, getPluginPackagerUID(moduleId));
150 }
151
152 public static void updatePluginPackage(
153 String moduleId, String name, String version, Date modifiedDate,
154 String author, List<String> types, List<String> tags, List licenses,
155 List liferayVersions, String shortDescription,
156 String longDescription, String changeLog, String pageURL,
157 String repositoryURL, String status, String installedVersion)
158 throws SearchException {
159
160 Document doc = getPluginPackageDocument(
161 moduleId, name, version, modifiedDate, author, types, tags,
162 licenses, liferayVersions, shortDescription, longDescription,
163 changeLog, pageURL, repositoryURL, status, installedVersion);
164
165 SearchEngineUtil.updateDocument(
166 CompanyConstants.SYSTEM, doc.get(Field.UID), doc);
167 }
168
169 public DocumentSummary getDocumentSummary(
170 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
171
172
174 String title = doc.get(Field.TITLE);
175
176
178 String content = doc.get(Field.CONTENT);
179
180 content = StringUtil.shorten(content, 200);
181
182
184 String moduleId = doc.get("moduleId");
185 String repositoryURL = doc.get("repositoryURL");
186
187 portletURL.setParameter(
188 "struts_action", "/admin/view");
189 portletURL.setParameter("tabs2", "repositories");
190 portletURL.setParameter("moduleId", moduleId);
191 portletURL.setParameter("repositoryURL", repositoryURL);
192
193 return new DocumentSummary(title, content, portletURL);
194 }
195
196 public void reIndex(String[] ids) throws SearchException {
197 try {
198 PluginPackageUtil.reIndex();
199 }
200 catch (Exception e) {
201 throw new SearchException(e);
202 }
203 }
204
205 }