001    /**
002     * Copyright (c) 2000-2013 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.portlet.journal.util;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
023    import com.liferay.portal.kernel.portlet.LiferayWindowState;
024    import com.liferay.portal.kernel.search.BaseIndexer;
025    import com.liferay.portal.kernel.search.BooleanQuery;
026    import com.liferay.portal.kernel.search.Document;
027    import com.liferay.portal.kernel.search.DocumentImpl;
028    import com.liferay.portal.kernel.search.Field;
029    import com.liferay.portal.kernel.search.SearchContext;
030    import com.liferay.portal.kernel.search.SearchEngineUtil;
031    import com.liferay.portal.kernel.search.Summary;
032    import com.liferay.portal.kernel.util.CharPool;
033    import com.liferay.portal.kernel.util.GetterUtil;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.security.permission.ActionKeys;
036    import com.liferay.portal.security.permission.PermissionChecker;
037    import com.liferay.portal.util.PortletKeys;
038    import com.liferay.portlet.journal.model.JournalFolder;
039    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
040    import com.liferay.portlet.journal.service.permission.JournalFolderPermission;
041    import com.liferay.portlet.journal.service.persistence.JournalFolderActionableDynamicQuery;
042    import com.liferay.portlet.trash.util.TrashUtil;
043    
044    import java.util.Locale;
045    
046    import javax.portlet.PortletRequest;
047    import javax.portlet.PortletURL;
048    import javax.portlet.WindowStateException;
049    
050    /**
051     * @author Eduardo Garcia
052     */
053    public class JournalFolderIndexer extends BaseIndexer {
054    
055            public static final String[] CLASS_NAMES = {JournalFolder.class.getName()};
056    
057            public static final String PORTLET_ID = PortletKeys.JOURNAL;
058    
059            public JournalFolderIndexer() {
060                    setFilterSearch(true);
061                    setPermissionAware(true);
062            }
063    
064            @Override
065            public String[] getClassNames() {
066                    return CLASS_NAMES;
067            }
068    
069            @Override
070            public String getPortletId() {
071                    return PORTLET_ID;
072            }
073    
074            @Override
075            public boolean hasPermission(
076                            PermissionChecker permissionChecker, String entryClassName,
077                            long entryClassPK, String actionId)
078                    throws Exception {
079    
080                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(
081                            entryClassPK);
082    
083                    return JournalFolderPermission.contains(
084                            permissionChecker, folder, ActionKeys.VIEW);
085            }
086    
087            @Override
088            public void postProcessContextQuery(
089                            BooleanQuery contextQuery, SearchContext searchContext)
090                    throws Exception {
091    
092                    addStatus(contextQuery, searchContext);
093            }
094    
095            @Override
096            protected void doDelete(Object obj) throws Exception {
097                    JournalFolder folder = (JournalFolder)obj;
098    
099                    Document document = new DocumentImpl();
100    
101                    document.addUID(PORTLET_ID, folder.getFolderId());
102    
103                    SearchEngineUtil.deleteDocument(
104                            getSearchEngineId(), folder.getCompanyId(),
105                            document.get(Field.UID));
106            }
107    
108            @Override
109            protected Document doGetDocument(Object obj) throws Exception {
110                    JournalFolder folder = (JournalFolder)obj;
111    
112                    if (_log.isDebugEnabled()) {
113                            _log.debug("Indexing folder " + folder);
114                    }
115    
116                    Document document = getBaseModelDocument(PORTLET_ID, folder);
117    
118                    document.addText(Field.DESCRIPTION, folder.getDescription());
119                    document.addKeyword(Field.FOLDER_ID, folder.getParentFolderId());
120    
121                    String title = folder.getName();
122    
123                    if (folder.isInTrash()) {
124                            title = TrashUtil.getOriginalTitle(title);
125                    }
126    
127                    document.addText(Field.TITLE, title);
128    
129                    document.addKeyword(
130                            Field.TREE_PATH,
131                            StringUtil.split(folder.getTreePath(), CharPool.SLASH));
132    
133                    if (_log.isDebugEnabled()) {
134                            _log.debug("Document " + folder + " indexed successfully");
135                    }
136    
137                    return document;
138            }
139    
140            @Override
141            protected Summary doGetSummary(
142                    Document document, Locale locale, String snippet,
143                    PortletURL portletURL) {
144    
145                    LiferayPortletURL liferayPortletURL = (LiferayPortletURL)portletURL;
146    
147                    liferayPortletURL.setLifecycle(PortletRequest.ACTION_PHASE);
148    
149                    try {
150                            liferayPortletURL.setWindowState(LiferayWindowState.EXCLUSIVE);
151                    }
152                    catch (WindowStateException wse) {
153                    }
154    
155                    String folderId = document.get(Field.ENTRY_CLASS_PK);
156    
157                    portletURL.setParameter("struts_action", "/journal/view");
158                    portletURL.setParameter("folderId", folderId);
159    
160                    Summary summary = createSummary(
161                            document, Field.TITLE, Field.DESCRIPTION);
162    
163                    summary.setMaxContentLength(200);
164                    summary.setPortletURL(portletURL);
165    
166                    return summary;
167            }
168    
169            @Override
170            protected void doReindex(Object obj) throws Exception {
171                    JournalFolder folder = (JournalFolder)obj;
172    
173                    Document document = getDocument(folder);
174    
175                    SearchEngineUtil.updateDocument(
176                            getSearchEngineId(), folder.getCompanyId(), document);
177            }
178    
179            @Override
180            protected void doReindex(String className, long classPK) throws Exception {
181                    JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(classPK);
182    
183                    doReindex(folder);
184            }
185    
186            @Override
187            protected void doReindex(String[] ids) throws Exception {
188                    long companyId = GetterUtil.getLong(ids[0]);
189    
190                    reindexFolders(companyId);
191            }
192    
193            @Override
194            protected String getPortletId(SearchContext searchContext) {
195                    return PORTLET_ID;
196            }
197    
198            protected void reindexFolders(long companyId)
199                    throws PortalException, SystemException {
200    
201                    ActionableDynamicQuery actionableDynamicQuery =
202                            new JournalFolderActionableDynamicQuery() {
203    
204                            @Override
205                            protected void performAction(Object object) {
206                                    JournalFolder folder = (JournalFolder)object;
207    
208                                    try {
209                                            Document document = getDocument(folder);
210    
211                                            if (document != null) {
212                                                    addDocument(document);
213                                            }
214                                    }
215                                    catch (PortalException pe) {
216                                            if (_log.isWarnEnabled()) {
217                                                    _log.warn(
218                                                            "Unable to index journal folder " +
219                                                                    folder.getFolderId(),
220                                                            pe);
221                                            }
222                                    }
223                            }
224    
225                    };
226    
227                    actionableDynamicQuery.setCompanyId(companyId);
228                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
229    
230                    actionableDynamicQuery.performActions();
231            }
232    
233            private static Log _log = LogFactoryUtil.getLog(JournalFolderIndexer.class);
234    
235    }