001    /**
002     * Copyright (c) 2000-present 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.portlet.exportimport.lifecycle;
016    
017    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_LAYOUT_IMPORT_SUCCEEDED;
018    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.EVENT_PORTLET_IMPORT_SUCCEEDED;
019    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.PROCESS_FLAG_LAYOUT_IMPORT_IN_PROCESS;
020    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS;
021    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.PROCESS_FLAG_PORTLET_IMPORT_IN_PROCESS;
022    import static com.liferay.portlet.exportimport.lifecycle.ExportImportLifecycleConstants.PROCESS_FLAG_PORTLET_STAGING_IN_PROCESS;
023    
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.search.Indexer;
027    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
028    import com.liferay.portal.kernel.search.SearchException;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.ListUtil;
031    import com.liferay.portal.kernel.util.MapUtil;
032    import com.liferay.portal.model.User;
033    import com.liferay.portlet.exportimport.lar.PortletDataContext;
034    import com.liferay.portlet.exportimport.lar.PortletDataHandlerKeys;
035    
036    import java.io.Serializable;
037    
038    import java.util.List;
039    
040    /**
041     * @author Mate Thurzo
042     */
043    public class IndexingExportImportLifecycleListener
044            implements ExportImportLifecycleListener {
045    
046            @Override
047            public boolean isParallel() {
048                    return true;
049            }
050    
051            @Override
052            public void onExportImportLifecycleEvent(
053                            ExportImportLifecycleEvent exportImportLifecycleEvent)
054                    throws Exception {
055    
056                    if (((exportImportLifecycleEvent.getCode() !=
057                                    EVENT_LAYOUT_IMPORT_SUCCEEDED) ||
058                             ((exportImportLifecycleEvent.getProcessFlag() !=
059                                     PROCESS_FLAG_LAYOUT_IMPORT_IN_PROCESS) &&
060                              (exportImportLifecycleEvent.getProcessFlag() !=
061                                      PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS))) &&
062                            ((exportImportLifecycleEvent.getCode() !=
063                                    EVENT_PORTLET_IMPORT_SUCCEEDED) ||
064                             ((exportImportLifecycleEvent.getProcessFlag() !=
065                                     PROCESS_FLAG_PORTLET_IMPORT_IN_PROCESS) &&
066                              (exportImportLifecycleEvent.getProcessFlag() !=
067                                      PROCESS_FLAG_PORTLET_STAGING_IN_PROCESS)))) {
068    
069                            return;
070                    }
071    
072                    List<Serializable> attributes =
073                            exportImportLifecycleEvent.getAttributes();
074    
075                    if (ListUtil.isEmpty(attributes)) {
076                            return;
077                    }
078    
079                    PortletDataContext portletDataContext =
080                            (PortletDataContext)attributes.get(0);
081    
082                    if (portletDataContext == null) {
083                            return;
084                    }
085    
086                    long userId = 0;
087    
088                    if (attributes.size() == 2) {
089                            userId = GetterUtil.getLong(attributes.get(1));
090                    }
091    
092                    reindex(portletDataContext, userId);
093            }
094    
095            protected void reindex(PortletDataContext portletDataContext, long userId) {
096                    boolean importPermissions = MapUtil.getBoolean(
097                            portletDataContext.getParameterMap(),
098                            PortletDataHandlerKeys.PERMISSIONS);
099    
100                    if (importPermissions) {
101                            if (userId > 0) {
102                                    Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
103                                            User.class);
104    
105                                    try {
106                                            indexer.reindex(userId);
107                                    }
108                                    catch (SearchException se) {
109                                            if (_log.isDebugEnabled()) {
110                                                    _log.debug("Unable to reindex user " + userId, se);
111                                            }
112                                    }
113                            }
114                    }
115    
116                    Indexer portletDataContextIndexer = IndexerRegistryUtil.getIndexer(
117                            PortletDataContext.class);
118    
119                    try {
120                            portletDataContextIndexer.reindex(portletDataContext);
121                    }
122                    catch (SearchException se) {
123                            if (_log.isDebugEnabled()) {
124                                    _log.debug("Unable to reindex portlet data context", se);
125                            }
126                    }
127            }
128    
129            private static final Log _log = LogFactoryUtil.getLog(
130                    IndexingExportImportLifecycleListener.class);
131    
132    }