001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.ListUtil;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.CompanyConstants;
038    
039    import java.util.ArrayList;
040    import java.util.Collection;
041    import java.util.List;
042    import java.util.Locale;
043    
044    import javax.portlet.PortletURL;
045    
046    /**
047     * @author Jorge Ferrer
048     * @author Brian Wing Shun Chan
049     * @author Bruno Farache
050     * @author Raymond Augé
051     */
052    public class PluginPackageIndexer extends BaseIndexer {
053    
054            public static final String[] CLASS_NAMES = {PluginPackage.class.getName()};
055    
056            public static final String PORTLET_ID = "PluginPackageIndexer";
057    
058            public PluginPackageIndexer() {
059                    setStagingAware(false);
060            }
061    
062            public String[] getClassNames() {
063                    return CLASS_NAMES;
064            }
065    
066            public String getPortletId() {
067                    return PORTLET_ID;
068            }
069    
070            @Override
071            protected void doDelete(Object obj) throws Exception {
072                    PluginPackage pluginPackage = (PluginPackage)obj;
073    
074                    deleteDocument(CompanyConstants.SYSTEM, pluginPackage.getModuleId());
075            }
076    
077            @Override
078            protected Document doGetDocument(Object obj) throws Exception {
079                    PluginPackage pluginPackage = (PluginPackage)obj;
080    
081                    Document document = new DocumentImpl();
082    
083                    document.addUID(PORTLET_ID, pluginPackage.getModuleId());
084    
085                    document.addKeyword(Field.COMPANY_ID, CompanyConstants.SYSTEM);
086    
087                    StringBundler sb = new StringBundler(7);
088    
089                    sb.append(pluginPackage.getAuthor());
090                    sb.append(StringPool.SPACE);
091    
092                    String longDescription = HtmlUtil.extractText(
093                            pluginPackage.getLongDescription());
094    
095                    sb.append(longDescription);
096    
097                    sb.append(StringPool.SPACE);
098                    sb.append(pluginPackage.getName());
099                    sb.append(StringPool.SPACE);
100    
101                    String shortDescription = HtmlUtil.extractText(
102                            pluginPackage.getShortDescription());
103    
104                    sb.append(shortDescription);
105    
106                    document.addText(Field.CONTENT, sb.toString());
107    
108                    document.addKeyword(
109                            Field.ENTRY_CLASS_NAME, PluginPackage.class.getName());
110    
111                    ModuleId moduleIdObj = ModuleId.getInstance(
112                            pluginPackage.getModuleId());
113    
114                    document.addKeyword(Field.GROUP_ID, moduleIdObj.getGroupId());
115    
116                    document.addDate(Field.MODIFIED_DATE, pluginPackage.getModifiedDate());
117                    document.addKeyword(Field.PORTLET_ID, PORTLET_ID);
118    
119                    String[] statusAndInstalledVersion =
120                            PluginPackageUtil.getStatusAndInstalledVersion(pluginPackage);
121    
122                    document.addKeyword(Field.STATUS, statusAndInstalledVersion[0]);
123    
124                    document.addText(Field.TITLE, pluginPackage.getName());
125    
126                    document.addKeyword("artifactId", moduleIdObj.getArtifactId());
127                    document.addText("author", pluginPackage.getAuthor());
128                    document.addText("changeLog", pluginPackage.getChangeLog());
129                    document.addKeyword("installedVersion", statusAndInstalledVersion[1]);
130    
131                    List<License> licenses = pluginPackage.getLicenses();
132    
133                    document.addKeyword(
134                            "license",
135                            StringUtil.split(
136                                    ListUtil.toString(licenses, License.NAME_ACCESSOR)));
137    
138                    document.addText("longDescription", longDescription);
139                    document.addKeyword("moduleId", pluginPackage.getModuleId());
140    
141                    boolean osiLicense = false;
142    
143                    for (int i = 0; i < licenses.size(); i++) {
144                            License license = licenses.get(i);
145    
146                            if (license.isOsiApproved()) {
147                                    osiLicense = true;
148    
149                                    break;
150                            }
151                    }
152    
153                    document.addKeyword("osi-approved-license", osiLicense);
154                    document.addText("pageURL", pluginPackage.getPageURL());
155                    document.addKeyword("repositoryURL", pluginPackage.getRepositoryURL());
156                    document.addText("shortDescription", shortDescription);
157    
158                    List<String> tags = pluginPackage.getTags();
159    
160                    document.addKeyword("tag", tags.toArray(new String[0]));
161    
162                    List<String> types = pluginPackage.getTypes();
163    
164                    document.addKeyword("type", types.toArray(new String[0]));
165    
166                    document.addKeyword("version", pluginPackage.getVersion());
167    
168                    return document;
169            }
170    
171            @Override
172            protected Summary doGetSummary(
173                    Document document, Locale locale, String snippet,
174                    PortletURL portletURL) {
175    
176                    String title = document.get(Field.TITLE);
177    
178                    String content = snippet;
179    
180                    if (Validator.isNull(snippet)) {
181                            content = StringUtil.shorten(document.get(Field.CONTENT), 200);
182                    }
183    
184                    String moduleId = document.get("moduleId");
185                    String repositoryURL = document.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 Summary(title, content, portletURL);
194            }
195    
196            @Override
197            protected void doReindex(Object obj) throws Exception {
198                    PluginPackage pluginPackage = (PluginPackage)obj;
199    
200                    Document document = getDocument(pluginPackage);
201    
202                    SearchEngineUtil.updateDocument(CompanyConstants.SYSTEM, document);
203            }
204    
205            @Override
206            protected void doReindex(String className, long classPK) throws Exception {
207            }
208    
209            @Override
210            protected void doReindex(String[] ids) throws Exception {
211                    SearchEngineUtil.deletePortletDocuments(
212                            CompanyConstants.SYSTEM, PORTLET_ID);
213    
214                    Collection<Document> documents = new ArrayList<Document>();
215    
216                    for (PluginPackage pluginPackage :
217                                    PluginPackageUtil.getAllAvailablePluginPackages()) {
218    
219                            Document document = getDocument(pluginPackage);
220    
221                            documents.add(document);
222                    }
223    
224                    SearchEngineUtil.updateDocuments(CompanyConstants.SYSTEM, documents);
225            }
226    
227            @Override
228            protected String getPortletId(SearchContext searchContext) {
229                    return PORTLET_ID;
230            }
231    
232            @Override
233            protected void postProcessFullQuery(
234                            BooleanQuery fullQuery, SearchContext searchContext)
235                    throws Exception {
236    
237                    String type = (String)searchContext.getAttribute("type");
238    
239                    if (Validator.isNotNull(type)) {
240                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
241                                    searchContext);
242    
243                            searchQuery.addRequiredTerm("type", type);
244    
245                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
246                    }
247    
248                    String tag = (String)searchContext.getAttribute("tag");
249    
250                    if (Validator.isNotNull(tag)) {
251                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
252                                    searchContext);
253    
254                            searchQuery.addExactTerm("tag", tag);
255    
256                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
257                    }
258    
259                    String repositoryURL = (String)searchContext.getAttribute(
260                            "repositoryURL");
261    
262                    if (Validator.isNotNull(repositoryURL)) {
263                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
264                                    searchContext);
265    
266                            Query query = TermQueryFactoryUtil.create(
267                                    searchContext, "repositoryURL", repositoryURL);
268    
269                            searchQuery.add(query, BooleanClauseOccur.SHOULD);
270    
271                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
272                    }
273    
274                    String license = (String)searchContext.getAttribute("license");
275    
276                    if (Validator.isNotNull(license)) {
277                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
278                                    searchContext);
279    
280                            searchQuery.addExactTerm("license", license);
281    
282                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
283                    }
284    
285                    String status = (String)searchContext.getAttribute(Field.STATUS);
286    
287                    if (Validator.isNotNull(status) && !status.equals("all")) {
288                            BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(
289                                    searchContext);
290    
291                            if (status.equals(
292                                            PluginPackageImpl.
293                                                    STATUS_NOT_INSTALLED_OR_OLDER_VERSION_INSTALLED)) {
294    
295                                    searchQuery.addExactTerm(
296                                            Field.STATUS, PluginPackageImpl.STATUS_NOT_INSTALLED);
297                                    searchQuery.addExactTerm(
298                                            Field.STATUS,
299                                            PluginPackageImpl.STATUS_OLDER_VERSION_INSTALLED);
300                            }
301                            else {
302                                    searchQuery.addExactTerm(Field.STATUS, status);
303                            }
304    
305                            fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
306                    }
307            }
308    
309    }