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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.search.BaseModelSearchResult;
020    import com.liferay.portal.kernel.search.Document;
021    import com.liferay.portal.kernel.search.Field;
022    import com.liferay.portal.kernel.search.Hits;
023    import com.liferay.portal.kernel.search.Indexable;
024    import com.liferay.portal.kernel.search.IndexableType;
025    import com.liferay.portal.kernel.search.Indexer;
026    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027    import com.liferay.portal.kernel.search.QueryConfig;
028    import com.liferay.portal.kernel.search.SearchContext;
029    import com.liferay.portal.kernel.search.SearchException;
030    import com.liferay.portal.kernel.search.Sort;
031    import com.liferay.portal.kernel.systemevent.SystemEvent;
032    import com.liferay.portal.kernel.util.GetterUtil;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.kernel.workflow.WorkflowConstants;
037    import com.liferay.portal.model.SystemEventConstants;
038    import com.liferay.portal.model.User;
039    import com.liferay.portal.service.ServiceContext;
040    import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationConstants;
041    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
042    import com.liferay.portlet.exportimport.service.base.ExportImportConfigurationLocalServiceBaseImpl;
043    import com.liferay.portlet.trash.model.TrashEntry;
044    
045    import java.io.Serializable;
046    
047    import java.util.ArrayList;
048    import java.util.Date;
049    import java.util.HashMap;
050    import java.util.List;
051    import java.util.Map;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     * @author Daniel Kocsis
056     * @author Akos Thurzo
057     */
058    public class ExportImportConfigurationLocalServiceImpl
059            extends ExportImportConfigurationLocalServiceBaseImpl {
060    
061            @Override
062            public ExportImportConfiguration addDraftExportImportConfiguration(
063                            long userId, int type, Map<String, Serializable> settingsMap)
064                    throws PortalException {
065    
066                    return exportImportConfigurationLocalService.
067                            addDraftExportImportConfiguration(
068                                    userId, GetterUtil.getString(settingsMap.get("portletId")),
069                                    type, settingsMap);
070            }
071    
072            @Override
073            public ExportImportConfiguration addDraftExportImportConfiguration(
074                            long userId, String name, int type,
075                            Map<String, Serializable> settingsMap)
076                    throws PortalException {
077    
078                    long groupId = GetterUtil.getLong(settingsMap.get("sourceGroupId"));
079    
080                    if ((type == ExportImportConfigurationConstants.TYPE_IMPORT_LAYOUT) ||
081                            (type == ExportImportConfigurationConstants.TYPE_IMPORT_PORTLET)) {
082    
083                            groupId = GetterUtil.getLong(settingsMap.get("targetGroupId"));
084                    }
085    
086                    return exportImportConfigurationLocalService.
087                            addExportImportConfiguration(
088                                    userId, groupId, name, StringPool.BLANK, type, settingsMap,
089                                    WorkflowConstants.STATUS_DRAFT, new ServiceContext());
090            }
091    
092            @Indexable(type = IndexableType.REINDEX)
093            @Override
094            public ExportImportConfiguration addExportImportConfiguration(
095                            long userId, long groupId, String name, String description,
096                            int type, Map<String, Serializable> settingsMap, int status,
097                            ServiceContext serviceContext)
098                    throws PortalException {
099    
100                    User user = userPersistence.findByPrimaryKey(userId);
101                    Date now = new Date();
102    
103                    long exportImportConfigurationId = counterLocalService.increment();
104    
105                    ExportImportConfiguration exportImportConfiguration =
106                            exportImportConfigurationPersistence.create(
107                                    exportImportConfigurationId);
108    
109                    exportImportConfiguration.setGroupId(groupId);
110                    exportImportConfiguration.setCompanyId(user.getCompanyId());
111                    exportImportConfiguration.setUserId(userId);
112                    exportImportConfiguration.setUserName(user.getFullName());
113                    exportImportConfiguration.setName(name);
114                    exportImportConfiguration.setDescription(description);
115                    exportImportConfiguration.setType(type);
116    
117                    if (settingsMap != null) {
118                            String settings = JSONFactoryUtil.serialize(settingsMap);
119    
120                            exportImportConfiguration.setSettings(settings);
121                    }
122    
123                    exportImportConfiguration.setStatus(status);
124                    exportImportConfiguration.setStatusByUserId(userId);
125                    exportImportConfiguration.setStatusByUserName(user.getScreenName());
126                    exportImportConfiguration.setStatusDate(now);
127    
128                    return exportImportConfigurationPersistence.update(
129                            exportImportConfiguration);
130            }
131    
132            @Override
133            public ExportImportConfiguration addExportImportConfiguration(
134                            long userId, long groupId, String name, String description,
135                            int type, Map<String, Serializable> settingsMap,
136                            ServiceContext serviceContext)
137                    throws PortalException {
138    
139                    return exportImportConfigurationLocalService.
140                            addExportImportConfiguration(
141                                    userId, groupId, name, description, type, settingsMap,
142                                    WorkflowConstants.STATUS_APPROVED, serviceContext);
143            }
144    
145            @Indexable(type = IndexableType.DELETE)
146            @Override
147            @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
148            public ExportImportConfiguration deleteExportImportConfiguration(
149                            ExportImportConfiguration exportImportConfiguration)
150                    throws PortalException {
151    
152                    exportImportConfigurationPersistence.remove(exportImportConfiguration);
153    
154                    trashEntryLocalService.deleteEntry(
155                            ExportImportConfiguration.class.getName(),
156                            exportImportConfiguration.getExportImportConfigurationId());
157    
158                    return exportImportConfiguration;
159            }
160    
161            @Override
162            public ExportImportConfiguration deleteExportImportConfiguration(
163                            long exportImportConfigurationId)
164                    throws PortalException {
165    
166                    ExportImportConfiguration exportImportConfiguration =
167                            exportImportConfigurationPersistence.findByPrimaryKey(
168                                    exportImportConfigurationId);
169    
170                    return exportImportConfigurationLocalService.
171                            deleteExportImportConfiguration(exportImportConfiguration);
172            }
173    
174            @Override
175            public void deleteExportImportConfigurations(long groupId)
176                    throws PortalException {
177    
178                    List<ExportImportConfiguration> exportImportConfigurations =
179                            exportImportConfigurationPersistence.findByGroupId(groupId);
180    
181                    for (ExportImportConfiguration exportImportConfiguration :
182                                    exportImportConfigurations) {
183    
184                            exportImportConfigurationLocalService.
185                                    deleteExportImportConfiguration(exportImportConfiguration);
186                    }
187            }
188    
189            @Override
190            public List<ExportImportConfiguration> getExportImportConfigurations(
191                            Hits hits)
192                    throws PortalException {
193    
194                    List<Document> documents = hits.toList();
195    
196                    List<ExportImportConfiguration> exportImportConfigurations =
197                            new ArrayList<>(documents.size());
198    
199                    for (Document document : documents) {
200                            long exportImportConfigurationId = GetterUtil.getLong(
201                                    document.get("exportImportConfigurationId"));
202    
203                            ExportImportConfiguration exportImportConfiguration =
204                                    exportImportConfigurationLocalService.
205                                            getExportImportConfiguration(exportImportConfigurationId);
206    
207                            if (exportImportConfiguration == null) {
208                                    exportImportConfigurations = null;
209    
210                                    Indexer<ExportImportConfiguration> indexer =
211                                            IndexerRegistryUtil.getIndexer(
212                                                    ExportImportConfiguration.class);
213    
214                                    long companyId = GetterUtil.getLong(
215                                            document.get(Field.COMPANY_ID));
216    
217                                    indexer.delete(companyId, document.getUID());
218                            }
219                            else if (exportImportConfigurations != null) {
220                                    exportImportConfigurations.add(exportImportConfiguration);
221                            }
222                    }
223    
224                    return exportImportConfigurations;
225            }
226    
227            @Override
228            public List<ExportImportConfiguration> getExportImportConfigurations(
229                    long groupId, int type) {
230    
231                    return exportImportConfigurationPersistence.findByG_T_S(
232                            groupId, type, WorkflowConstants.STATUS_APPROVED);
233            }
234    
235            @Override
236            public List<ExportImportConfiguration> getExportImportConfigurations(
237                    long groupId, int type, int start, int end,
238                    OrderByComparator<ExportImportConfiguration> orderByComparator) {
239    
240                    return exportImportConfigurationPersistence.findByG_T_S(
241                            groupId, type, WorkflowConstants.STATUS_APPROVED, start, end,
242                            orderByComparator);
243            }
244    
245            @Override
246            public int getExportImportConfigurationsCount(long groupId) {
247                    return exportImportConfigurationPersistence.countByG_S(
248                            groupId, WorkflowConstants.STATUS_APPROVED);
249            }
250    
251            @Override
252            public int getExportImportConfigurationsCount(long groupId, int type) {
253                    return exportImportConfigurationPersistence.countByG_T_S(
254                            groupId, type, WorkflowConstants.STATUS_APPROVED);
255            }
256    
257            @Indexable(type = IndexableType.REINDEX)
258            @Override
259            public ExportImportConfiguration moveExportImportConfigurationToTrash(
260                            long userId, long exportImportConfigurationId)
261                    throws PortalException {
262    
263                    ExportImportConfiguration exportImportConfiguration =
264                            exportImportConfigurationPersistence.findByPrimaryKey(
265                                    exportImportConfigurationId);
266    
267                    int oldStatus = exportImportConfiguration.getStatus();
268    
269                    exportImportConfiguration = updateStatus(
270                            userId, exportImportConfiguration.getExportImportConfigurationId(),
271                            WorkflowConstants.STATUS_IN_TRASH);
272    
273                    trashEntryLocalService.addTrashEntry(
274                            userId, exportImportConfiguration.getGroupId(),
275                            ExportImportConfiguration.class.getName(),
276                            exportImportConfiguration.getExportImportConfigurationId(), null,
277                            null, oldStatus, null, null);
278    
279                    return exportImportConfiguration;
280            }
281    
282            @Indexable(type = IndexableType.REINDEX)
283            @Override
284            public ExportImportConfiguration restoreExportImportConfigurationFromTrash(
285                            long userId, long exportImportConfigurationId)
286                    throws PortalException {
287    
288                    ExportImportConfiguration exportImportConfiguration =
289                            exportImportConfigurationPersistence.findByPrimaryKey(
290                                    exportImportConfigurationId);
291    
292                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
293                            ExportImportConfiguration.class.getName(),
294                            exportImportConfigurationId);
295    
296                    exportImportConfiguration = updateStatus(
297                            userId, exportImportConfiguration.getExportImportConfigurationId(),
298                            trashEntry.getStatus());
299    
300                    trashEntryLocalService.deleteEntry(
301                            ExportImportConfiguration.class.getName(),
302                            exportImportConfiguration.getExportImportConfigurationId());
303    
304                    return exportImportConfiguration;
305            }
306    
307            @Override
308            public BaseModelSearchResult<ExportImportConfiguration>
309                    searchExportImportConfigurations(
310                            long companyId, long groupId, int type, String keywords, int start,
311                            int end, Sort sort)
312                    throws PortalException {
313    
314                    String description = null;
315                    String name = null;
316                    boolean andOperator = false;
317    
318                    if (Validator.isNotNull(keywords)) {
319                            description = keywords;
320                            name = keywords;
321                    }
322                    else {
323                            andOperator = true;
324                    }
325    
326                    return searchExportImportConfigurations(
327                            companyId, groupId, type, name, description, andOperator, start,
328                            end, sort);
329            }
330    
331            @Override
332            public BaseModelSearchResult<ExportImportConfiguration>
333                    searchExportImportConfigurations(
334                            long companyId, long groupId, int type, String name,
335                            String description, boolean andSearch, int start, int end,
336                            Sort sort)
337                    throws PortalException {
338    
339                    Indexer<ExportImportConfiguration> indexer =
340                            IndexerRegistryUtil.nullSafeGetIndexer(
341                                    ExportImportConfiguration.class);
342    
343                    SearchContext searchContext = buildSearchContext(
344                            companyId, groupId, type, name, description, andSearch, start, end,
345                            sort);
346    
347                    for (int i = 0; i < 10; i++) {
348                            Hits hits = indexer.search(searchContext);
349    
350                            List<ExportImportConfiguration> exportImportConfigurations =
351                                    exportImportConfigurationLocalService.
352                                            getExportImportConfigurations(hits);
353    
354                            if (exportImportConfigurations != null) {
355                                    return new BaseModelSearchResult<>(
356                                            exportImportConfigurations, hits.getLength());
357                            }
358                    }
359    
360                    throw new SearchException(
361                            "Unable to fix the search index after 10 attempts");
362            }
363    
364            @Indexable(type = IndexableType.REINDEX)
365            @Override
366            public ExportImportConfiguration updateExportImportConfiguration(
367                            long userId, long exportImportConfigurationId, String name,
368                            String description, Map<String, Serializable> settingsMap,
369                            ServiceContext serviceContext)
370                    throws PortalException {
371    
372                    User user = userPersistence.findByPrimaryKey(userId);
373    
374                    ExportImportConfiguration exportImportConfiguration =
375                            exportImportConfigurationPersistence.findByPrimaryKey(
376                                    exportImportConfigurationId);
377    
378                    exportImportConfiguration.setUserId(userId);
379                    exportImportConfiguration.setUserName(user.getFullName());
380                    exportImportConfiguration.setName(name);
381                    exportImportConfiguration.setDescription(description);
382    
383                    if (settingsMap != null) {
384                            String settings = JSONFactoryUtil.serialize(settingsMap);
385    
386                            exportImportConfiguration.setSettings(settings);
387                    }
388    
389                    return exportImportConfigurationPersistence.update(
390                            exportImportConfiguration);
391            }
392    
393            @Indexable(type = IndexableType.REINDEX)
394            @Override
395            public ExportImportConfiguration updateStatus(
396                            long userId, long exportImportConfigurationId, int status)
397                    throws PortalException {
398    
399                    User user = userPersistence.findByPrimaryKey(userId);
400    
401                    ExportImportConfiguration exportImportConfiguration =
402                            exportImportConfigurationPersistence.findByPrimaryKey(
403                                    exportImportConfigurationId);
404    
405                    exportImportConfiguration.setStatus(status);
406                    exportImportConfiguration.setStatusByUserId(userId);
407                    exportImportConfiguration.setStatusByUserName(user.getScreenName());
408                    exportImportConfiguration.setStatusDate(new Date());
409    
410                    exportImportConfigurationPersistence.update(exportImportConfiguration);
411    
412                    return exportImportConfiguration;
413            }
414    
415            protected SearchContext buildSearchContext(
416                    long companyId, long groupId, int type, String name, String description,
417                    boolean andSearch, int start, int end, Sort sort) {
418    
419                    SearchContext searchContext = new SearchContext();
420    
421                    searchContext.setAndSearch(andSearch);
422    
423                    Map<String, Serializable> attributes = new HashMap<>();
424    
425                    attributes.put(Field.STATUS, WorkflowConstants.STATUS_APPROVED);
426                    attributes.put("description", description);
427                    attributes.put("groupId", groupId);
428                    attributes.put("name", name);
429                    attributes.put("type", type);
430    
431                    searchContext.setAttributes(attributes);
432    
433                    searchContext.setCompanyId(companyId);
434                    searchContext.setEnd(end);
435    
436                    if (sort != null) {
437                            searchContext.setSorts(sort);
438                    }
439    
440                    searchContext.setStart(start);
441    
442                    QueryConfig queryConfig = searchContext.getQueryConfig();
443    
444                    queryConfig.setHighlightEnabled(false);
445                    queryConfig.setScoreEnabled(false);
446    
447                    return searchContext;
448            }
449    
450    }