001    /**
002     * Copyright (c) 2000-2013 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.journal.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portlet.journal.model.JournalFolder;
026    import com.liferay.portlet.journal.service.base.JournalFolderServiceBaseImpl;
027    import com.liferay.portlet.journal.service.permission.JournalFolderPermission;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * @author Juan Fern??ndez
034     */
035    public class JournalFolderServiceImpl extends JournalFolderServiceBaseImpl {
036    
037            @Override
038            public JournalFolder addFolder(
039                            long groupId, long parentFolderId, String name, String description,
040                            ServiceContext serviceContext)
041                    throws PortalException, SystemException {
042    
043                    JournalFolderPermission.check(
044                            getPermissionChecker(), serviceContext.getScopeGroupId(),
045                            parentFolderId, ActionKeys.ADD_FOLDER);
046    
047                    return journalFolderLocalService.addFolder(
048                            getUserId(), groupId, parentFolderId, name, description,
049                            serviceContext);
050            }
051    
052            @Override
053            public void deleteFolder(long folderId)
054                    throws PortalException, SystemException {
055    
056                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
057    
058                    JournalFolderPermission.check(
059                            getPermissionChecker(), folder, ActionKeys.DELETE);
060    
061                    journalFolderLocalService.deleteFolder(folderId);
062            }
063    
064            @Override
065            public void deleteFolder(long folderId, boolean includeTrashedEntries)
066                    throws PortalException, SystemException {
067    
068                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
069    
070                    JournalFolderPermission.check(
071                            getPermissionChecker(), folder, ActionKeys.DELETE);
072    
073                    journalFolderLocalService.deleteFolder(folderId, includeTrashedEntries);
074            }
075    
076            @Override
077            public JournalFolder fetchFolder(long folderId)
078                    throws PortalException, SystemException {
079    
080                    JournalFolder folder = journalFolderLocalService.fetchFolder(folderId);
081    
082                    if (folder != null) {
083                            JournalFolderPermission.check(
084                                    getPermissionChecker(), folder, ActionKeys.VIEW);
085                    }
086    
087                    return folder;
088            }
089    
090            @Override
091            public JournalFolder getFolder(long folderId)
092                    throws PortalException, SystemException {
093    
094                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
095    
096                    JournalFolderPermission.check(
097                            getPermissionChecker(), folder, ActionKeys.VIEW);
098    
099                    return folder;
100            }
101    
102            @Override
103            public List<Long> getFolderIds(long groupId, long folderId)
104                    throws PortalException, SystemException {
105    
106                    JournalFolderPermission.check(
107                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
108    
109                    List<Long> folderIds = getSubfolderIds(groupId, folderId, true);
110    
111                    folderIds.add(0, folderId);
112    
113                    return folderIds;
114            }
115    
116            @Override
117            public List<JournalFolder> getFolders(long groupId) throws SystemException {
118                    return journalFolderPersistence.filterFindByGroupId(groupId);
119            }
120    
121            @Override
122            public List<JournalFolder> getFolders(long groupId, long parentFolderId)
123                    throws SystemException {
124    
125                    return getFolders(
126                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
127            }
128    
129            @Override
130            public List<JournalFolder> getFolders(
131                            long groupId, long parentFolderId, int status)
132                    throws SystemException {
133    
134                    return journalFolderPersistence.filterFindByG_P_S(
135                            groupId, parentFolderId, status);
136            }
137    
138            @Override
139            public List<JournalFolder> getFolders(
140                            long groupId, long parentFolderId, int start, int end)
141                    throws SystemException {
142    
143                    return getFolders(
144                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, start,
145                            end);
146            }
147    
148            @Override
149            public List<JournalFolder> getFolders(
150                            long groupId, long parentFolderId, int status, int start, int end)
151                    throws SystemException {
152    
153                    return journalFolderPersistence.filterFindByG_P_S(
154                            groupId, parentFolderId, status, start, end);
155            }
156    
157            @Override
158            public List<Object> getFoldersAndArticles(
159                            long groupId, long folderId, int status, int start, int end,
160                            OrderByComparator obc)
161                    throws SystemException {
162    
163                    return getFoldersAndArticles(
164                            groupId, 0, folderId, status, start, end, obc);
165            }
166    
167            @Override
168            public List<Object> getFoldersAndArticles(
169                            long groupId, long folderId, int start, int end,
170                            OrderByComparator obc)
171                    throws SystemException {
172    
173                    return getFoldersAndArticles(
174                            groupId, folderId, WorkflowConstants.STATUS_ANY, start, end, obc);
175            }
176    
177            @Override
178            public List<Object> getFoldersAndArticles(
179                            long groupId, long userId, long folderId, int status, int start,
180                            int end, OrderByComparator obc)
181                    throws SystemException {
182    
183                    QueryDefinition queryDefinition = new QueryDefinition(
184                            status, userId, true, start, end, obc);
185    
186                    return journalFolderFinder.filterFindF_A_ByG_F(
187                            groupId, folderId, queryDefinition);
188            }
189    
190            @Override
191            public int getFoldersAndArticlesCount(
192                            long groupId, List<Long> folderIds, int status)
193                    throws SystemException {
194    
195                    QueryDefinition queryDefinition = new QueryDefinition(status);
196    
197                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
198                            return journalArticleFinder.filterCountByG_F(
199                                    groupId, folderIds, queryDefinition);
200                    }
201                    else {
202                            int start = 0;
203                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
204    
205                            int articlesCount = journalArticleFinder.filterCountByG_F(
206                                    groupId, folderIds.subList(start, end), queryDefinition);
207    
208                            folderIds.subList(start, end).clear();
209    
210                            articlesCount += getFoldersAndArticlesCount(
211                                    groupId, folderIds, status);
212    
213                            return articlesCount;
214                    }
215            }
216    
217            @Override
218            public int getFoldersAndArticlesCount(long groupId, long folderId)
219                    throws SystemException {
220    
221                    return getFoldersAndArticlesCount(
222                            groupId, folderId, WorkflowConstants.STATUS_ANY);
223            }
224    
225            @Override
226            public int getFoldersAndArticlesCount(
227                            long groupId, long folderId, int status)
228                    throws SystemException {
229    
230                    return getFoldersAndArticlesCount(groupId, 0, folderId, status);
231            }
232    
233            @Override
234            public int getFoldersAndArticlesCount(
235                            long groupId, long userId, long folderId, int status)
236                    throws SystemException {
237    
238                    QueryDefinition queryDefinition = new QueryDefinition(
239                            status, userId, true);
240    
241                    return journalFolderFinder.filterCountF_A_ByG_F(
242                            groupId, folderId, queryDefinition);
243            }
244    
245            @Override
246            public int getFoldersCount(long groupId, long parentFolderId)
247                    throws SystemException {
248    
249                    return getFoldersCount(
250                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
251            }
252    
253            @Override
254            public int getFoldersCount(long groupId, long parentFolderId, int status)
255                    throws SystemException {
256    
257                    if (status == WorkflowConstants.STATUS_ANY) {
258                            return journalFolderPersistence.filterCountByG_P_NotS(
259                                    groupId, parentFolderId, WorkflowConstants.STATUS_IN_TRASH);
260                    }
261                    else {
262                            return journalFolderPersistence.filterCountByG_P_S(
263                                    groupId, parentFolderId, status);
264                    }
265            }
266    
267            /**
268             * @deprecated As of 7.0.0, replaced by {@link #getSubfolderIds(List, long,
269             *             long, boolean)}
270             */
271            @Deprecated
272            @Override
273            public void getSubfolderIds(
274                            List<Long> folderIds, long groupId, long folderId)
275                    throws SystemException {
276    
277                    getSubfolderIds(folderIds, groupId, folderId, true);
278            }
279    
280            @Override
281            public void getSubfolderIds(
282                            List<Long> folderIds, long groupId, long folderId, boolean recurse)
283                    throws SystemException {
284    
285                    List<JournalFolder> folders =
286                            journalFolderPersistence.filterFindByG_P_NotS(
287                                    groupId, folderId, WorkflowConstants.STATUS_IN_TRASH);
288    
289                    for (JournalFolder folder : folders) {
290                            folderIds.add(folder.getFolderId());
291    
292                            if (recurse) {
293                                    getSubfolderIds(
294                                            folderIds, folder.getGroupId(), folder.getFolderId(),
295                                            recurse);
296                            }
297                    }
298            }
299    
300            @Override
301            public List<Long> getSubfolderIds(
302                            long groupId, long folderId, boolean recurse)
303                    throws SystemException {
304    
305                    List<Long> folderIds = new ArrayList<Long>();
306    
307                    getSubfolderIds(folderIds, groupId, folderId, recurse);
308    
309                    return folderIds;
310            }
311    
312            @Override
313            public JournalFolder moveFolder(
314                            long folderId, long parentFolderId, ServiceContext serviceContext)
315                    throws PortalException, SystemException {
316    
317                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
318    
319                    JournalFolderPermission.check(
320                            getPermissionChecker(), folder, ActionKeys.UPDATE);
321    
322                    return journalFolderLocalService.moveFolder(
323                            folderId, parentFolderId, serviceContext);
324            }
325    
326            @Override
327            public JournalFolder moveFolderFromTrash(
328                            long folderId, long parentFolderId, ServiceContext serviceContext)
329                    throws PortalException, SystemException {
330    
331                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
332    
333                    JournalFolderPermission.check(
334                            getPermissionChecker(), folder, ActionKeys.UPDATE);
335    
336                    return journalFolderLocalService.moveFolderFromTrash(
337                            getUserId(), folderId, parentFolderId, serviceContext);
338            }
339    
340            @Override
341            public JournalFolder moveFolderToTrash(long folderId)
342                    throws PortalException, SystemException {
343    
344                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
345    
346                    JournalFolderPermission.check(
347                            getPermissionChecker(), folder, ActionKeys.DELETE);
348    
349                    return journalFolderLocalService.moveFolderToTrash(
350                            getUserId(), folderId);
351            }
352    
353            @Override
354            public void restoreFolderFromTrash(long folderId)
355                    throws PortalException, SystemException {
356    
357                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
358    
359                    JournalFolderPermission.check(
360                            getPermissionChecker(), folder, ActionKeys.UPDATE);
361    
362                    journalFolderLocalService.restoreFolderFromTrash(getUserId(), folderId);
363            }
364    
365            @Override
366            public JournalFolder updateFolder(
367                            long folderId, long parentFolderId, String name, String description,
368                            boolean mergeWithParentFolder, ServiceContext serviceContext)
369                    throws PortalException, SystemException {
370    
371                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
372    
373                    JournalFolderPermission.check(
374                            getPermissionChecker(), folder, ActionKeys.UPDATE);
375    
376                    return journalFolderLocalService.updateFolder(
377                            getUserId(), folderId, parentFolderId, name, description,
378                            mergeWithParentFolder, serviceContext);
379            }
380    
381    }