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