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    
151                    exportImportConfigurationPersistence.remove(exportImportConfiguration);
152    
153                    trashEntryLocalService.deleteEntry(
154                            ExportImportConfiguration.class.getName(),
155                            exportImportConfiguration.getExportImportConfigurationId());
156    
157                    return exportImportConfiguration;
158            }
159    
160            @Override
161            public ExportImportConfiguration deleteExportImportConfiguration(
162                            long exportImportConfigurationId)
163                    throws PortalException {
164    
165                    ExportImportConfiguration exportImportConfiguration =
166                            exportImportConfigurationPersistence.findByPrimaryKey(
167                                    exportImportConfigurationId);
168    
169                    return exportImportConfigurationLocalService.
170                            deleteExportImportConfiguration(exportImportConfiguration);
171            }
172    
173            @Override
174            public void deleteExportImportConfigurations(long groupId) {
175                    List<ExportImportConfiguration> exportImportConfigurations =
176                            exportImportConfigurationPersistence.findByGroupId(groupId);
177    
178                    for (ExportImportConfiguration exportImportConfiguration :
179                                    exportImportConfigurations) {
180    
181                            exportImportConfigurationLocalService.
182                                    deleteExportImportConfiguration(exportImportConfiguration);
183                    }
184            }
185    
186            @Override
187            public List<ExportImportConfiguration> getExportImportConfigurations(
188                            Hits hits)
189                    throws PortalException {
190    
191                    List<Document> documents = hits.toList();
192    
193                    List<ExportImportConfiguration> exportImportConfigurations =
194                            new ArrayList<>(documents.size());
195    
196                    for (Document document : documents) {
197                            long exportImportConfigurationId = GetterUtil.getLong(
198                                    document.get("exportImportConfigurationId"));
199    
200                            ExportImportConfiguration exportImportConfiguration =
201                                    exportImportConfigurationLocalService.
202                                            getExportImportConfiguration(exportImportConfigurationId);
203    
204                            if (exportImportConfiguration == null) {
205                                    exportImportConfigurations = null;
206    
207                                    Indexer<ExportImportConfiguration> indexer =
208                                            IndexerRegistryUtil.getIndexer(
209                                                    ExportImportConfiguration.class);
210    
211                                    long companyId = GetterUtil.getLong(
212                                            document.get(Field.COMPANY_ID));
213    
214                                    indexer.delete(companyId, document.getUID());
215                            }
216                            else if (exportImportConfigurations != null) {
217                                    exportImportConfigurations.add(exportImportConfiguration);
218                            }
219                    }
220    
221                    return exportImportConfigurations;
222            }
223    
224            @Override
225            public List<ExportImportConfiguration> getExportImportConfigurations(
226                    long groupId, int type) {
227    
228                    return exportImportConfigurationPersistence.findByG_T_S(
229                            groupId, type, WorkflowConstants.STATUS_APPROVED);
230            }
231    
232            @Override
233            public List<ExportImportConfiguration> getExportImportConfigurations(
234                    long groupId, int type, int start, int end,
235                    OrderByComparator<ExportImportConfiguration> orderByComparator) {
236    
237                    return exportImportConfigurationPersistence.findByG_T_S(
238                            groupId, type, WorkflowConstants.STATUS_APPROVED, start, end,
239                            orderByComparator);
240            }
241    
242            @Override
243            public List<ExportImportConfiguration> getExportImportConfigurations(
244                    long companyId, long groupId, String keywords, int type, int start,
245                    int end,
246                    OrderByComparator<ExportImportConfiguration> orderByComparator) {
247    
248                    return exportImportConfigurationFinder.findByKeywords(
249                            companyId, groupId, keywords, type,
250                            WorkflowConstants.STATUS_APPROVED, start, end, orderByComparator);
251            }
252    
253            @Override
254            public List<ExportImportConfiguration> getExportImportConfigurations(
255                    long companyId, long groupId, String name, String description, int type,
256                    boolean andSearch, int start, int end,
257                    OrderByComparator<ExportImportConfiguration> orderByComparator) {
258    
259                    return exportImportConfigurationFinder.findByC_G_N_D_T(
260                            companyId, groupId, name, description, type,
261                            WorkflowConstants.STATUS_APPROVED, andSearch, start, end,
262                            orderByComparator);
263            }
264    
265            @Override
266            public int getExportImportConfigurationsCount(long groupId) {
267                    return exportImportConfigurationPersistence.countByG_S(
268                            groupId, WorkflowConstants.STATUS_APPROVED);
269            }
270    
271            @Override
272            public int getExportImportConfigurationsCount(long groupId, int type) {
273                    return exportImportConfigurationPersistence.countByG_T_S(
274                            groupId, type, WorkflowConstants.STATUS_APPROVED);
275            }
276    
277            @Override
278            public int getExportImportConfigurationsCount(
279                    long companyId, long groupId, String keywords, int type) {
280    
281                    return exportImportConfigurationFinder.countByKeywords(
282                            companyId, groupId, keywords, type,
283                            WorkflowConstants.STATUS_APPROVED);
284            }
285    
286            @Override
287            public int getExportImportConfigurationsCount(
288                    long companyId, long groupId, String name, String description, int type,
289                    boolean andSearch) {
290    
291                    return exportImportConfigurationFinder.countByC_G_N_D_T(
292                            companyId, groupId, name, description, type,
293                            WorkflowConstants.STATUS_APPROVED, andSearch);
294            }
295    
296            @Indexable(type = IndexableType.REINDEX)
297            @Override
298            public ExportImportConfiguration moveExportImportConfigurationToTrash(
299                            long userId, long exportImportConfigurationId)
300                    throws PortalException {
301    
302                    ExportImportConfiguration exportImportConfiguration =
303                            exportImportConfigurationPersistence.findByPrimaryKey(
304                                    exportImportConfigurationId);
305    
306                    int oldStatus = exportImportConfiguration.getStatus();
307    
308                    exportImportConfiguration = updateStatus(
309                            userId, exportImportConfiguration.getExportImportConfigurationId(),
310                            WorkflowConstants.STATUS_IN_TRASH);
311    
312                    trashEntryLocalService.addTrashEntry(
313                            userId, exportImportConfiguration.getGroupId(),
314                            ExportImportConfiguration.class.getName(),
315                            exportImportConfiguration.getExportImportConfigurationId(), null,
316                            null, oldStatus, null, null);
317    
318                    return exportImportConfiguration;
319            }
320    
321            @Indexable(type = IndexableType.REINDEX)
322            @Override
323            public ExportImportConfiguration restoreExportImportConfigurationFromTrash(
324                            long userId, long exportImportConfigurationId)
325                    throws PortalException {
326    
327                    ExportImportConfiguration exportImportConfiguration =
328                            exportImportConfigurationPersistence.findByPrimaryKey(
329                                    exportImportConfigurationId);
330    
331                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
332                            ExportImportConfiguration.class.getName(),
333                            exportImportConfigurationId);
334    
335                    exportImportConfiguration = updateStatus(
336                            userId, exportImportConfiguration.getExportImportConfigurationId(),
337                            trashEntry.getStatus());
338    
339                    trashEntryLocalService.deleteEntry(
340                            ExportImportConfiguration.class.getName(),
341                            exportImportConfiguration.getExportImportConfigurationId());
342    
343                    return exportImportConfiguration;
344            }
345    
346            @Override
347            public BaseModelSearchResult<ExportImportConfiguration>
348                            searchExportImportConfigurations(
349                                    long companyId, long groupId, int type, String keywords,
350                                    int start, int end, Sort sort)
351                    throws PortalException {
352    
353                    String description = null;
354                    String name = null;
355                    boolean andOperator = false;
356    
357                    if (Validator.isNotNull(keywords)) {
358                            description = keywords;
359                            name = keywords;
360                    }
361                    else {
362                            andOperator = true;
363                    }
364    
365                    return searchExportImportConfigurations(
366                            companyId, groupId, type, name, description, andOperator, start,
367                            end, sort);
368            }
369    
370            @Override
371            public BaseModelSearchResult<ExportImportConfiguration>
372                            searchExportImportConfigurations(
373                                    long companyId, long groupId, int type, String name,
374                                    String description, boolean andSearch, int start, int end,
375                                    Sort sort)
376                    throws PortalException {
377    
378                    Indexer<ExportImportConfiguration> indexer =
379                            IndexerRegistryUtil.nullSafeGetIndexer(
380                                    ExportImportConfiguration.class);
381    
382                    SearchContext searchContext = buildSearchContext(
383                            companyId, groupId, type, name, description, andSearch, start, end,
384                            sort);
385    
386                    for (int i = 0; i < 10; i++) {
387                            Hits hits = indexer.search(searchContext);
388    
389                            List<ExportImportConfiguration> exportImportConfigurations =
390                                    exportImportConfigurationLocalService.
391                                            getExportImportConfigurations(hits);
392    
393                            if (exportImportConfigurations != null) {
394                                    return new BaseModelSearchResult<>(
395                                            exportImportConfigurations, hits.getLength());
396                            }
397                    }
398    
399                    throw new SearchException(
400                            "Unable to fix the search index after 10 attempts");
401            }
402    
403            @Indexable(type = IndexableType.REINDEX)
404            @Override
405            public ExportImportConfiguration updateExportImportConfiguration(
406                            long userId, long exportImportConfigurationId, String name,
407                            String description, Map<String, Serializable> settingsMap,
408                            ServiceContext serviceContext)
409                    throws PortalException {
410    
411                    User user = userPersistence.findByPrimaryKey(userId);
412    
413                    ExportImportConfiguration exportImportConfiguration =
414                            exportImportConfigurationPersistence.findByPrimaryKey(
415                                    exportImportConfigurationId);
416    
417                    exportImportConfiguration.setUserId(userId);
418                    exportImportConfiguration.setUserName(user.getFullName());
419                    exportImportConfiguration.setName(name);
420                    exportImportConfiguration.setDescription(description);
421    
422                    if (settingsMap != null) {
423                            String settings = JSONFactoryUtil.serialize(settingsMap);
424    
425                            exportImportConfiguration.setSettings(settings);
426                    }
427    
428                    return exportImportConfigurationPersistence.update(
429                            exportImportConfiguration);
430            }
431    
432            @Indexable(type = IndexableType.REINDEX)
433            @Override
434            public ExportImportConfiguration updateStatus(
435                            long userId, long exportImportConfigurationId, int status)
436                    throws PortalException {
437    
438                    User user = userPersistence.findByPrimaryKey(userId);
439    
440                    ExportImportConfiguration exportImportConfiguration =
441                            exportImportConfigurationPersistence.findByPrimaryKey(
442                                    exportImportConfigurationId);
443    
444                    exportImportConfiguration.setStatus(status);
445                    exportImportConfiguration.setStatusByUserId(userId);
446                    exportImportConfiguration.setStatusByUserName(user.getScreenName());
447                    exportImportConfiguration.setStatusDate(new Date());
448    
449                    exportImportConfigurationPersistence.update(exportImportConfiguration);
450    
451                    return exportImportConfiguration;
452            }
453    
454            protected SearchContext buildSearchContext(
455                    long companyId, long groupId, int type, String name, String description,
456                    boolean andSearch, int start, int end, Sort sort) {
457    
458                    SearchContext searchContext = new SearchContext();
459    
460                    searchContext.setAndSearch(andSearch);
461    
462                    Map<String, Serializable> attributes = new HashMap<>();
463    
464                    attributes.put(Field.STATUS, WorkflowConstants.STATUS_APPROVED);
465                    attributes.put("description", description);
466                    attributes.put("groupId", groupId);
467                    attributes.put("name", name);
468                    attributes.put("type", type);
469    
470                    searchContext.setAttributes(attributes);
471    
472                    searchContext.setCompanyId(companyId);
473                    searchContext.setEnd(end);
474    
475                    if (sort != null) {
476                            searchContext.setSorts(sort);
477                    }
478    
479                    searchContext.setStart(start);
480    
481                    QueryConfig queryConfig = searchContext.getQueryConfig();
482    
483                    queryConfig.setHighlightEnabled(false);
484                    queryConfig.setScoreEnabled(false);
485    
486                    return searchContext;
487            }
488    
489    }