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.portal.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.Indexable;
020    import com.liferay.portal.kernel.search.IndexableType;
021    import com.liferay.portal.kernel.systemevent.SystemEvent;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.model.ExportImportConfiguration;
025    import com.liferay.portal.model.SystemEventConstants;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.base.ExportImportConfigurationLocalServiceBaseImpl;
029    import com.liferay.portlet.trash.model.TrashEntry;
030    
031    import java.io.Serializable;
032    
033    import java.util.Date;
034    import java.util.List;
035    import java.util.Map;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Daniel Kocsis
040     */
041    public class ExportImportConfigurationLocalServiceImpl
042            extends ExportImportConfigurationLocalServiceBaseImpl {
043    
044            @Indexable(type = IndexableType.REINDEX)
045            @Override
046            public ExportImportConfiguration addExportImportConfiguration(
047                            long userId, long groupId, String name, String description,
048                            int type, Map<String, Serializable> settingsMap, int status,
049                            ServiceContext serviceContext)
050                    throws PortalException {
051    
052                    User user = userPersistence.findByPrimaryKey(userId);
053                    Date now = new Date();
054    
055                    long exportImportConfigurationId = counterLocalService.increment();
056    
057                    ExportImportConfiguration exportImportConfiguration =
058                            exportImportConfigurationPersistence.create(
059                                    exportImportConfigurationId);
060    
061                    exportImportConfiguration.setGroupId(groupId);
062                    exportImportConfiguration.setCompanyId(user.getCompanyId());
063                    exportImportConfiguration.setUserId(userId);
064                    exportImportConfiguration.setUserName(user.getFullName());
065                    exportImportConfiguration.setCreateDate(
066                            serviceContext.getCreateDate(now));
067                    exportImportConfiguration.setModifiedDate(
068                            serviceContext.getModifiedDate(now));
069                    exportImportConfiguration.setName(name);
070                    exportImportConfiguration.setDescription(description);
071                    exportImportConfiguration.setType(type);
072    
073                    if (settingsMap != null) {
074                            String settings = JSONFactoryUtil.serialize(settingsMap);
075    
076                            exportImportConfiguration.setSettings(settings);
077                    }
078    
079                    exportImportConfiguration.setStatus(status);
080                    exportImportConfiguration.setStatusByUserId(userId);
081                    exportImportConfiguration.setStatusByUserName(user.getScreenName());
082                    exportImportConfiguration.setStatusDate(now);
083    
084                    return exportImportConfigurationPersistence.update(
085                            exportImportConfiguration);
086            }
087    
088            @Override
089            public ExportImportConfiguration addExportImportConfiguration(
090                            long userId, long groupId, String name, String description,
091                            int type, Map<String, Serializable> settingsMap,
092                            ServiceContext serviceContext)
093                    throws PortalException {
094    
095                    return exportImportConfigurationLocalService.
096                            addExportImportConfiguration(
097                                    userId, groupId, name, description, type, settingsMap,
098                                    WorkflowConstants.STATUS_APPROVED, serviceContext);
099            }
100    
101            @Indexable(type = IndexableType.DELETE)
102            @Override
103            @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
104            public ExportImportConfiguration deleteExportImportConfiguration(
105                            ExportImportConfiguration exportImportConfiguration)
106                    throws PortalException {
107    
108                    exportImportConfigurationPersistence.remove(exportImportConfiguration);
109    
110                    trashEntryLocalService.deleteEntry(
111                            ExportImportConfiguration.class.getName(),
112                            exportImportConfiguration.getExportImportConfigurationId());
113    
114                    return exportImportConfiguration;
115            }
116    
117            @Override
118            public ExportImportConfiguration deleteExportImportConfiguration(
119                            long exportImportConfigurationId)
120                    throws PortalException {
121    
122                    ExportImportConfiguration exportImportConfiguration =
123                            exportImportConfigurationPersistence.findByPrimaryKey(
124                                    exportImportConfigurationId);
125    
126                    return exportImportConfigurationLocalService.
127                            deleteExportImportConfiguration(exportImportConfiguration);
128            }
129    
130            @Override
131            public void deleteExportImportConfigurations(long groupId)
132                    throws PortalException {
133    
134                    List<ExportImportConfiguration> exportImportConfigurations =
135                            exportImportConfigurationPersistence.findByGroupId(groupId);
136    
137                    for (ExportImportConfiguration exportImportConfiguration :
138                                    exportImportConfigurations) {
139    
140                            exportImportConfigurationLocalService.
141                                    deleteExportImportConfiguration(exportImportConfiguration);
142                    }
143            }
144    
145            @Override
146            public List<ExportImportConfiguration> getExportImportConfigurations(
147                    long groupId, int type) {
148    
149                    return exportImportConfigurationPersistence.findByG_T_S(
150                            groupId, type, WorkflowConstants.STATUS_APPROVED);
151            }
152    
153            @Override
154            public List<ExportImportConfiguration> getExportImportConfigurations(
155                    long groupId, int type, int start, int end,
156                    OrderByComparator<ExportImportConfiguration> orderByComparator) {
157    
158                    return exportImportConfigurationPersistence.findByG_T_S(
159                            groupId, type, WorkflowConstants.STATUS_APPROVED, start, end,
160                            orderByComparator);
161            }
162    
163            @Override
164            public int getExportImportConfigurationsCount(long groupId) {
165                    return exportImportConfigurationPersistence.countByG_S(
166                            groupId, WorkflowConstants.STATUS_APPROVED);
167            }
168    
169            @Override
170            public int getExportImportConfigurationsCount(long groupId, int type) {
171                    return exportImportConfigurationPersistence.countByG_T_S(
172                            groupId, type, WorkflowConstants.STATUS_APPROVED);
173            }
174    
175            @Indexable(type = IndexableType.REINDEX)
176            @Override
177            public ExportImportConfiguration moveExportImportConfigurationToTrash(
178                            long userId, long exportImportConfigurationId)
179                    throws PortalException {
180    
181                    ExportImportConfiguration exportImportConfiguration =
182                            exportImportConfigurationPersistence.findByPrimaryKey(
183                                    exportImportConfigurationId);
184    
185                    int oldStatus = exportImportConfiguration.getStatus();
186    
187                    exportImportConfiguration = updateStatus(
188                            userId, exportImportConfiguration.getExportImportConfigurationId(),
189                            WorkflowConstants.STATUS_IN_TRASH);
190    
191                    trashEntryLocalService.addTrashEntry(
192                            userId, exportImportConfiguration.getGroupId(),
193                            ExportImportConfiguration.class.getName(),
194                            exportImportConfiguration.getExportImportConfigurationId(), null,
195                            null, oldStatus, null, null);
196    
197                    return exportImportConfiguration;
198            }
199    
200            @Indexable(type = IndexableType.REINDEX)
201            @Override
202            public ExportImportConfiguration restoreExportImportConfigurationFromTrash(
203                            long userId, long exportImportConfigurationId)
204                    throws PortalException {
205    
206                    ExportImportConfiguration exportImportConfiguration =
207                            exportImportConfigurationPersistence.findByPrimaryKey(
208                                    exportImportConfigurationId);
209    
210                    TrashEntry trashEntry = trashEntryLocalService.getEntry(
211                            ExportImportConfiguration.class.getName(),
212                            exportImportConfigurationId);
213    
214                    exportImportConfiguration = updateStatus(
215                            userId, exportImportConfiguration.getExportImportConfigurationId(),
216                            trashEntry.getStatus());
217    
218                    trashEntryLocalService.deleteEntry(
219                            ExportImportConfiguration.class.getName(),
220                            exportImportConfiguration.getExportImportConfigurationId());
221    
222                    return exportImportConfiguration;
223            }
224    
225            @Indexable(type = IndexableType.REINDEX)
226            @Override
227            public ExportImportConfiguration updateExportImportConfiguration(
228                            long userId, long exportImportConfigurationId, String name,
229                            String description, Map<String, Serializable> settingsMap,
230                            ServiceContext serviceContext)
231                    throws PortalException {
232    
233                    User user = userPersistence.findByPrimaryKey(userId);
234    
235                    ExportImportConfiguration exportImportConfiguration =
236                            exportImportConfigurationPersistence.findByPrimaryKey(
237                                    exportImportConfigurationId);
238    
239                    exportImportConfiguration.setUserId(userId);
240                    exportImportConfiguration.setUserName(user.getFullName());
241                    exportImportConfiguration.setModifiedDate(
242                            serviceContext.getModifiedDate(new Date()));
243                    exportImportConfiguration.setName(name);
244                    exportImportConfiguration.setDescription(description);
245    
246                    if (settingsMap != null) {
247                            String settings = JSONFactoryUtil.serialize(settingsMap);
248    
249                            exportImportConfiguration.setSettings(settings);
250                    }
251    
252                    return exportImportConfigurationPersistence.update(
253                            exportImportConfiguration);
254            }
255    
256            @Indexable(type = IndexableType.REINDEX)
257            @Override
258            public ExportImportConfiguration updateStatus(
259                            long userId, long exportImportConfigurationId, int status)
260                    throws PortalException {
261    
262                    User user = userPersistence.findByPrimaryKey(userId);
263    
264                    ExportImportConfiguration exportImportConfiguration =
265                            exportImportConfigurationPersistence.findByPrimaryKey(
266                                    exportImportConfigurationId);
267    
268                    exportImportConfiguration.setStatus(status);
269                    exportImportConfiguration.setStatusByUserId(userId);
270                    exportImportConfiguration.setStatusByUserName(user.getScreenName());
271                    exportImportConfiguration.setStatusDate(new Date());
272    
273                    exportImportConfigurationPersistence.update(exportImportConfiguration);
274    
275                    return exportImportConfiguration;
276            }
277    
278    }