001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.plugin;
016    
017    import com.liferay.portal.kernel.plugin.License;
018    import com.liferay.portal.kernel.plugin.PluginPackage;
019    import com.liferay.portal.kernel.search.BaseIndexer;
020    import com.liferay.portal.kernel.search.BooleanClauseOccur;
021    import com.liferay.portal.kernel.search.BooleanQuery;
022    import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
023    import com.liferay.portal.kernel.search.Document;
024    import com.liferay.portal.kernel.search.DocumentImpl;
025    import com.liferay.portal.kernel.search.Field;
026    import com.liferay.portal.kernel.search.Query;
027    import com.liferay.portal.kernel.search.SearchContext;
028    import com.liferay.portal.kernel.search.SearchEngineUtil;
029    import com.liferay.portal.kernel.search.Summary;
030    import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
031    import com.liferay.portal.kernel.util.HtmlUtil;
032    import com.liferay.portal.kernel.util.StringBundler;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.model.CompanyConstants;
037    
038    import java.util.ArrayList;
039    import java.util.Collection;
040    import java.util.Date;
041    import java.util.List;
042    
043    import javax.portlet.PortletURL;
044    
045    /**
046     * @author Jorge Ferrer
047     * @author Brian Wing Shun Chan
048     * @author Bruno Farache
049     * @author Raymond Augé
050     */
051    public class PluginPackageIndexer extends BaseIndexer {
052    
053            public static final String[] CLASS_NAMES = {PluginPackage.class.getName()};
054    
055            public static final String PORTLET_ID = "PluginPackageIndexer";
056    
057            public String[] getClassNames() {
058                    return CLASS_NAMES;
059            }
060    
061            public Summary getSummary(
062                    Document document, String snippet, PortletURL portletURL) {
063    
064                    String title = document.get(Field.TITLE);
065    
066                    String content = snippet;
067    
068                    if (Validator.isNull(snippet)) {
069                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
070                    }
071    
072                    String moduleId = document.get("moduleId");
073                    String repositoryURL = document.get("repositoryURL");
074    
075                    portletURL.setParameter(
076                            "struts_action", "/admin/view");
077                    portletURL.setParameter("tabs2", "repositories");
078                    portletURL.setParameter("moduleId", moduleId);
079                    portletURL.setParameter("repositoryURL", repositoryURL);
080    
081                    return new Summary(title, content, portletURL);
082            }
083    
084            protected void doDelete(Object obj) throws Exception {
085                    PluginPackage pluginPackage = (PluginPackage)obj;
086    
087                    Document document = new DocumentImpl();
088    
089                    document.addUID(PORTLET_ID, pluginPackage.getModuleId());
090    
091                    SearchEngineUtil.deleteDocument(
092                            CompanyConstants.SYSTEM, document.get(Field.UID));
093            }
094    
095            protected Document doGetDocument(Object obj) throws Exception {
096                    PluginPackage pluginPackage = (PluginPackage)obj;
097    
098                    String moduleId = pluginPackage.getModuleId();
099                    String name = pluginPackage.getName();
100                    String version = pluginPackage.getVersion();
101                    Date modifiedDate = pluginPackage.getModifiedDate();
102                    String author = pluginPackage.getAuthor();
103                    List<String> types = pluginPackage.getTypes();
104                    List<String> tags = pluginPackage.getTags();
105                    List<License> licenses = pluginPackage.getLicenses();
106                    //List<String> liferayVersions = pluginPackage.getLiferayVersions();
107                    String shortDescription = HtmlUtil.extractText(
108                            pluginPackage.getShortDescription());
109                    String longDescription = HtmlUtil.extractText(
110                            pluginPackage.getLongDescription());
111                    String changeLog = pluginPackage.getChangeLog();
112                    String pageURL = pluginPackage.getPageURL();
113                    String repositoryURL = pluginPackage.getRepositoryURL();
114    
115                    String[] statusAndInstalledVersion =
116                            PluginPackageUtil.getStatusAndInstalledVersion(pluginPackage);
117    
118                    String status = statusAndInstalledVersion[0];
119                    String installedVersion = statusAndInstalledVersion[1];
120    
121                    ModuleId moduleIdObj = ModuleId.getInstance(moduleId);
122    
123                    StringBundler sb = new StringBundler(7);
124    
125                    sb.append(name);
126                    sb.append(StringPool.SPACE);
127                    sb.append(author);
128                    sb.append(StringPool.SPACE);
129                    sb.append(shortDescription);
130                    sb.append(StringPool.SPACE);
131                    sb.append(longDescription);
132    
133                    String content = sb.toString();
134    
135                    Document document = new DocumentImpl();
136    
137                    document.addUID(PORTLET_ID, moduleId);
138    
139                    document.addModifiedDate(modifiedDate);
140    
141                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
142                    document.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
143    
144                    document.addText(Field.TITLE, name);
145                    document.addText(Field.CONTENT, content);
146    
147                    document.addKeyword("moduleId", moduleId);
148                    document.addKeyword("artifactId", moduleIdObj.getArtifactId());
149                    document.addKeyword("version", version);
150                    document.addText("author", author);
151                    document.addKeyword("type", types.toArray(new String[0]));
152                    document.addKeyword("tag", tags.toArray(new String[0]));
153    
154                    String[] licenseNames = new String[licenses.size()];
155    
156                    boolean osiLicense = false;
157    
158                    for (int i = 0; i < licenses.size(); i++) {
159                            License license = licenses.get(i);
160    
161                            licenseNames[i] = license.getName();
162    
163                            if (license.isOsiApproved()) {
164                                    osiLicense = true;
165                            }
166                    }
167    
168                    document.addKeyword("license", licenseNames);
169                    document.addKeyword("osi-approved-license", String.valueOf(osiLicense));
170                    document.addText("shortDescription", shortDescription);
171                    document.addText("longDescription", longDescription);
172                    document.addText("changeLog", changeLog);
173                    document.addText("pageURL", pageURL);
174                    document.addKeyword("repositoryURL", repositoryURL);
175                    document.addKeyword("status", status);
176                    document.addKeyword("installedVersion", installedVersion);
177    
178                    return document;
179            }
180    
181            protected void doReindex(Object obj) throws Exception {
182                    PluginPackage pluginPackage = (PluginPackage)obj;
183    
184                    Document document = getDocument(pluginPackage);
185    
186                    SearchEngineUtil.updateDocument(CompanyConstants.SYSTEM, document);
187            }
188    
189            protected void doReindex(String className, long classPK) throws Exception {
190            }
191    
192            protected void doReindex(String[] ids) throws Exception {
193                    SearchEngineUtil.deletePortletDocuments(
194                            CompanyConstants.SYSTEM, PORTLET_ID);
195    
196                    Collection<Document> documents = new ArrayList<Document>();
197    
198                    for (PluginPackage pluginPackage :
199                                    PluginPackageUtil.getAllAvailablePluginPackages()) {
200    
201                            Document document = getDocument(pluginPackage);
202    
203                            documents.add(document);
204                    }
205    
206                    SearchEngineUtil.updateDocuments(CompanyConstants.SYSTEM, documents);
207            }
208    
209            protected String getPortletId(SearchContext searchContext) {
210                    return PORTLET_ID;
211            }
212    
213            protected void postProcessFullQuery(
214                            BooleanQuery fullQuery, SearchContext searchContext)
215                    throws Exception {
216    
217                    String type = (String)searchContext.getAttribute("type");
218    
219                    if (Validator.isNotNull(type)) {
220                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
221    
222                            searchQuery.addRequiredTerm("type", type);
223    
224                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
225                    }
226    
227                    String tag = (String)searchContext.getAttribute("tag");
228    
229                    if (Validator.isNotNull(tag)) {
230                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
231    
232                            searchQuery.addExactTerm("tag", tag);
233    
234                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
235                    }
236    
237                    String repositoryURL = (String)searchContext.getAttribute(
238                            "repositoryURL");
239    
240                    if (Validator.isNotNull(repositoryURL)) {
241                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
242    
243                            Query query = TermQueryFactoryUtil.create(
244                                    "repositoryURL", repositoryURL);
245    
246                            searchQuery.add(query, BooleanClauseOccur.SHOULD);
247    
248                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
249                    }
250    
251                    String license = (String)searchContext.getAttribute("license");
252    
253                    if (Validator.isNotNull(license)) {
254                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
255    
256                            searchQuery.addExactTerm("license", license);
257    
258                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
259                    }
260    
261                    String status = (String)searchContext.getAttribute("status");
262    
263                    if (Validator.isNotNull(status) && !status.equals("all")) {
264                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
265    
266                            if (status.equals(
267                                            PluginPackageImpl.
268                                                    STATUS_NOT_INSTALLED_OR_OLDER_VERSION_INSTALLED)) {
269    
270                                    searchQuery.addExactTerm(
271                                            "status", PluginPackageImpl.STATUS_NOT_INSTALLED);
272                                    searchQuery.addExactTerm(
273                                            "status", PluginPackageImpl.STATUS_OLDER_VERSION_INSTALLED);
274                            }
275                            else {
276                                    searchQuery.addExactTerm("status", status);
277                            }
278    
279                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
280                    }
281            }
282    
283    }