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 int getExportImportConfigurationsCount(long groupId) {
244                    return exportImportConfigurationPersistence.countByG_S(
245                            groupId, WorkflowConstants.STATUS_APPROVED);
246            }
247    
248            @Override
249            public int getExportImportConfigurationsCount(long groupId, int type) {
250                    return exportImportConfigurationPersistence.countByG_T_S(
251                            groupId, type, WorkflowConstants.STATUS_APPROVED);
252            }
253    
254            @Indexable(type = IndexableType.REINDEX)
255            @Override
256            public ExportImportConfiguration moveExportImportConfigurationToTrash(
257                            long userId, long exportImportConfigurationId)
258                    throws PortalException {
259    
260                    ExportImportConfiguration exportImportConfiguration =
261                            exportImportConfigurationPersistence.findByPrimaryKey(
262                                    exportImportConfigurationId);
263    
264                    int oldStatus = exportImportConfiguration.getStatus();
265    
266                    exportImportConfiguration = updateStatus(
267                            userId, exportImportConfiguration.getExportImportConfigurationId(),
268                            WorkflowConstants.STATUS_IN_TRASH);
269    
270                    trashEntryLocalService.addTrashEntry(
271                            userId, exportImportConfiguration.getGroupId(),
272                            ExportImportConfiguration.class.getName(),
273                            exportImportConfiguration.getExportImportConfigurationId(), null,
274                            null, oldStatus, null, null);
275    
276                    return exportImportConfiguration;
277            }
278    
279            @Indexable(type = IndexableType.REINDEX)
280            @Override
281            public ExportImportConfiguration restoreExportImportConfigurationFromTrash(
282                            long userId, long exportImportConfigurationId)
283                    throws PortalException {
284    
285                    ExportImportConfiguration exportImportConfiguration =
286                            exportImportConfigurationPersistence.findByPrimaryKey(
287                                    exportImportConfigurationId);
288    
289                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
290                            ExportImportConfiguration.class.getName(),
291                            exportImportConfigurationId);
292    
293                    exportImportConfiguration = updateStatus(
294                            userId, exportImportConfiguration.getExportImportConfigurationId(),
295                            trashEntry.getStatus());
296    
297                    trashEntryLocalService.deleteEntry(
298                            ExportImportConfiguration.class.getName(),
299                            exportImportConfiguration.getExportImportConfigurationId());
300    
301                    return exportImportConfiguration;
302            }
303    
304            @Override
305            public BaseModelSearchResult<ExportImportConfiguration>
306                            searchExportImportConfigurations(
307                                    long companyId, long groupId, int type, String keywords,
308                                    int start, int end, Sort sort)
309                    throws PortalException {
310    
311                    String description = null;
312                    String name = null;
313                    boolean andOperator = false;
314    
315                    if (Validator.isNotNull(keywords)) {
316                            description = keywords;
317                            name = keywords;
318                    }
319                    else {
320                            andOperator = true;
321                    }
322    
323                    return searchExportImportConfigurations(
324                            companyId, groupId, type, name, description, andOperator, start,
325                            end, sort);
326            }
327    
328            @Override
329            public BaseModelSearchResult<ExportImportConfiguration>
330                            searchExportImportConfigurations(
331                                    long companyId, long groupId, int type, String name,
332                                    String description, boolean andSearch, int start, int end,
333                                    Sort sort)
334                    throws PortalException {
335    
336                    Indexer<ExportImportConfiguration> indexer =
337                            IndexerRegistryUtil.nullSafeGetIndexer(
338                                    ExportImportConfiguration.class);
339    
340                    SearchContext searchContext = buildSearchContext(
341                            companyId, groupId, type, name, description, andSearch, start, end,
342                            sort);
343    
344                    for (int i = 0; i < 10; i++) {
345                            Hits hits = indexer.search(searchContext);
346    
347                            List<ExportImportConfiguration> exportImportConfigurations =
348                                    exportImportConfigurationLocalService.
349                                            getExportImportConfigurations(hits);
350    
351                            if (exportImportConfigurations != null) {
352                                    return new BaseModelSearchResult<>(
353                                            exportImportConfigurations, hits.getLength());
354                            }
355                    }
356    
357                    throw new SearchException(
358                            "Unable to fix the search index after 10 attempts");
359            }
360    
361            @Indexable(type = IndexableType.REINDEX)
362            @Override
363            public ExportImportConfiguration updateExportImportConfiguration(
364                            long userId, long exportImportConfigurationId, String name,
365                            String description, Map<String, Serializable> settingsMap,
366                            ServiceContext serviceContext)
367                    throws PortalException {
368    
369                    User user = userPersistence.findByPrimaryKey(userId);
370    
371                    ExportImportConfiguration exportImportConfiguration =
372                            exportImportConfigurationPersistence.findByPrimaryKey(
373                                    exportImportConfigurationId);
374    
375                    exportImportConfiguration.setUserId(userId);
376                    exportImportConfiguration.setUserName(user.getFullName());
377                    exportImportConfiguration.setName(name);
378                    exportImportConfiguration.setDescription(description);
379    
380                    if (settingsMap != null) {
381                            String settings = JSONFactoryUtil.serialize(settingsMap);
382    
383                            exportImportConfiguration.setSettings(settings);
384                    }
385    
386                    return exportImportConfigurationPersistence.update(
387                            exportImportConfiguration);
388            }
389    
390            @Indexable(type = IndexableType.REINDEX)
391            @Override
392            public ExportImportConfiguration updateStatus(
393                            long userId, long exportImportConfigurationId, int status)
394                    throws PortalException {
395    
396                    User user = userPersistence.findByPrimaryKey(userId);
397    
398                    ExportImportConfiguration exportImportConfiguration =
399                            exportImportConfigurationPersistence.findByPrimaryKey(
400                                    exportImportConfigurationId);
401    
402                    exportImportConfiguration.setStatus(status);
403                    exportImportConfiguration.setStatusByUserId(userId);
404                    exportImportConfiguration.setStatusByUserName(user.getScreenName());
405                    exportImportConfiguration.setStatusDate(new Date());
406    
407                    exportImportConfigurationPersistence.update(exportImportConfiguration);
408    
409                    return exportImportConfiguration;
410            }
411    
412            protected SearchContext buildSearchContext(
413                    long companyId, long groupId, int type, String name, String description,
414                    boolean andSearch, int start, int end, Sort sort) {
415    
416                    SearchContext searchContext = new SearchContext();
417    
418                    searchContext.setAndSearch(andSearch);
419    
420                    Map<String, Serializable> attributes = new HashMap<>();
421    
422                    attributes.put(Field.STATUS, WorkflowConstants.STATUS_APPROVED);
423                    attributes.put("description", description);
424                    attributes.put("groupId", groupId);
425                    attributes.put("name", name);
426                    attributes.put("type", type);
427    
428                    searchContext.setAttributes(attributes);
429    
430                    searchContext.setCompanyId(companyId);
431                    searchContext.setEnd(end);
432    
433                    if (sort != null) {
434                            searchContext.setSorts(sort);
435                    }
436    
437                    searchContext.setStart(start);
438    
439                    QueryConfig queryConfig = searchContext.getQueryConfig();
440    
441                    queryConfig.setHighlightEnabled(false);
442                    queryConfig.setScoreEnabled(false);
443    
444                    return searchContext;
445            }
446    
447    }