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.addKeyword(Field.PORTLET_ID, PORTLET_ID);
96 doc.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
97
98 doc.addText(Field.TITLE, name);
99 doc.addText(Field.CONTENT, content);
100
101 doc.addModifiedDate();
102
103 doc.addKeyword("moduleId", moduleId);
104 doc.addKeyword("artifactId", moduleIdObj.getArtifactId());
105 doc.addKeyword("version", version);
106 doc.addDate("modified-date", modifiedDate);
107 doc.addKeyword("shortDescription", shortDescription);
108 doc.addKeyword("changeLog", changeLog);
109 doc.addKeyword("repositoryURL", repositoryURL);
110 doc.addKeyword("type", types.toArray(new String[0]));
111 doc.addKeyword("tag", tags.toArray(new String[0]));
112
113 String[] licenseNames = new String[licenses.size()];
114
115 boolean osiLicense = false;
116
117 for (int i = 0; i < licenses.size(); i++) {
118 License license = (License)licenses.get(i);
119
120 licenseNames[i] = license.getName();
121
122 if (license.isOsiApproved()) {
123 osiLicense = true;
124 }
125 }
126
127 doc.addKeyword("license", licenseNames);
128 doc.addKeyword("osi-approved-license", String.valueOf(osiLicense));
129 doc.addKeyword("status", status);
130
131 if (installedVersion != null) {
132 doc.addKeyword("installedVersion", installedVersion);
133 }
134
135 return doc;
136 }
137
138 public static String getPluginPackagerUID(String moduleId) {
139 Document doc = new DocumentImpl();
140
141 doc.addUID(PORTLET_ID, moduleId);
142
143 return doc.get(Field.UID);
144 }
145
146 public static void removePluginPackage(String moduleId)
147 throws SearchException {
148
149 SearchEngineUtil.deleteDocument(
150 CompanyConstants.SYSTEM, getPluginPackagerUID(moduleId));
151 }
152
153 public static void updatePluginPackage(
154 String moduleId, String name, String version, Date modifiedDate,
155 String author, List<String> types, List<String> tags, List licenses,
156 List liferayVersions, String shortDescription,
157 String longDescription, String changeLog, String pageURL,
158 String repositoryURL, String status, String installedVersion)
159 throws SearchException {
160
161 Document doc = getPluginPackageDocument(
162 moduleId, name, version, modifiedDate, author, types, tags,
163 licenses, liferayVersions, shortDescription, longDescription,
164 changeLog, pageURL, repositoryURL, status, installedVersion);
165
166 SearchEngineUtil.updateDocument(
167 CompanyConstants.SYSTEM, doc.get(Field.UID), doc);
168 }
169
170 public DocumentSummary getDocumentSummary(
171 com.liferay.portal.kernel.search.Document doc, PortletURL portletURL) {
172
173
175 String title = doc.get(Field.TITLE);
176
177
179 String content = doc.get(Field.CONTENT);
180
181 content = StringUtil.shorten(content, 200);
182
183
185 String moduleId = doc.get("moduleId");
186 String repositoryURL = doc.get("repositoryURL");
187
188 portletURL.setParameter(
189 "struts_action", "/admin/view");
190 portletURL.setParameter("tabs2", "repositories");
191 portletURL.setParameter("moduleId", moduleId);
192 portletURL.setParameter("repositoryURL", repositoryURL);
193
194 return new DocumentSummary(title, content, portletURL);
195 }
196
197 public void reIndex(String[] ids) throws SearchException {
198 try {
199 PluginPackageUtil.reIndex();
200 }
201 catch (Exception e) {
202 throw new SearchException(e);
203 }
204 }
205
206 }