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