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 getFolder(long folderId)
078                    throws PortalException, SystemException {
079    
080                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
081    
082                    JournalFolderPermission.check(
083                            getPermissionChecker(), folder, ActionKeys.VIEW);
084    
085                    return folder;
086            }
087    
088            @Override
089            public List<Long> getFolderIds(long groupId, long folderId)
090                            throws PortalException, SystemException {
091    
092                    JournalFolderPermission.check(
093                            getPermissionChecker(), groupId, folderId, ActionKeys.VIEW);
094    
095                    List<Long> folderIds = getSubfolderIds(groupId, folderId, true);
096    
097                    folderIds.add(0, folderId);
098    
099                    return folderIds;
100            }
101    
102            @Override
103            public List<JournalFolder> getFolders(long groupId) throws SystemException {
104                    return journalFolderPersistence.filterFindByGroupId(groupId);
105            }
106    
107            @Override
108            public List<JournalFolder> getFolders(long groupId, long parentFolderId)
109                    throws SystemException {
110    
111                    return getFolders(
112                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
113            }
114    
115            @Override
116            public List<JournalFolder> getFolders(
117                            long groupId, long parentFolderId, int status)
118                    throws SystemException {
119    
120                    return journalFolderPersistence.filterFindByG_P_S(
121                            groupId, parentFolderId, status);
122            }
123    
124            @Override
125            public List<JournalFolder> getFolders(
126                            long groupId, long parentFolderId, int start, int end)
127                    throws SystemException {
128    
129                    return getFolders(
130                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED, start,
131                            end);
132            }
133    
134            @Override
135            public List<JournalFolder> getFolders(
136                            long groupId, long parentFolderId, int status, int start, int end)
137                    throws SystemException {
138    
139                    return journalFolderPersistence.filterFindByG_P_S(
140                            groupId, parentFolderId, status, start, end);
141            }
142    
143            @Override
144            public List<Object> getFoldersAndArticles(
145                            long groupId, long folderId, int status, int start, int end,
146                            OrderByComparator obc)
147                    throws SystemException {
148    
149                    QueryDefinition queryDefinition = new QueryDefinition(
150                            status, start, end, obc);
151    
152                    return journalFolderFinder.filterFindF_A_ByG_F(
153                            groupId, folderId, queryDefinition);
154            }
155    
156            @Override
157            public List<Object> getFoldersAndArticles(
158                            long groupId, long folderId, int start, int end,
159                            OrderByComparator obc)
160                    throws SystemException {
161    
162                    return getFoldersAndArticles(
163                            groupId, folderId, WorkflowConstants.STATUS_ANY, start, end, obc);
164            }
165    
166            @Override
167            public int getFoldersAndArticlesCount(
168                            long groupId, List<Long> folderIds, int status)
169                    throws SystemException {
170    
171                    QueryDefinition queryDefinition = new QueryDefinition(status);
172    
173                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
174                            return journalArticleFinder.filterCountByG_F(
175                                    groupId, folderIds, queryDefinition);
176                    }
177                    else {
178                            int start = 0;
179                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
180    
181                            int articlesCount = journalArticleFinder.filterCountByG_F(
182                                    groupId, folderIds.subList(start, end), queryDefinition);
183    
184                            folderIds.subList(start, end).clear();
185    
186                            articlesCount += getFoldersAndArticlesCount(
187                                    groupId, folderIds, status);
188    
189                            return articlesCount;
190                    }
191            }
192    
193            @Override
194            public int getFoldersAndArticlesCount(long groupId, long folderId)
195                    throws SystemException {
196    
197                    return getFoldersAndArticlesCount(
198                            groupId, folderId, WorkflowConstants.STATUS_ANY);
199            }
200    
201            @Override
202            public int getFoldersAndArticlesCount(
203                            long groupId, long folderId, int status)
204                    throws SystemException {
205    
206                    return journalFolderFinder.filterCountF_A_ByG_F(
207                            groupId, folderId, new QueryDefinition(status));
208            }
209    
210            @Override
211            public int getFoldersCount(long groupId, long parentFolderId)
212                    throws SystemException {
213    
214                    return getFoldersCount(
215                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
216            }
217    
218            @Override
219            public int getFoldersCount(long groupId, long parentFolderId, int status)
220                    throws SystemException {
221    
222                    if (status == WorkflowConstants.STATUS_ANY) {
223                            return journalFolderPersistence.filterCountByG_P_NotS(
224                                    groupId, parentFolderId, WorkflowConstants.STATUS_IN_TRASH);
225                    }
226                    else {
227                            return journalFolderPersistence.filterCountByG_P_S(
228                                    groupId, parentFolderId, status);
229                    }
230            }
231    
232            @Override
233            public void getSubfolderIds(
234                            List<Long> folderIds, long groupId, long folderId)
235                    throws SystemException {
236    
237                    List<JournalFolder> folders =
238                            journalFolderPersistence.filterFindByG_P_NotS(
239                                    groupId, folderId, WorkflowConstants.STATUS_IN_TRASH);
240    
241                    for (JournalFolder folder : folders) {
242                            folderIds.add(folder.getFolderId());
243    
244                            getSubfolderIds(
245                                    folderIds, folder.getGroupId(), folder.getFolderId());
246                    }
247            }
248    
249            @Override
250            public List<Long> getSubfolderIds(
251                            long groupId, long folderId, boolean recurse)
252                    throws SystemException {
253    
254                    List<Long> folderIds = new ArrayList<Long>();
255    
256                    getSubfolderIds(folderIds, groupId, folderId);
257    
258                    return folderIds;
259            }
260    
261            @Override
262            public JournalFolder moveFolder(
263                            long folderId, long parentFolderId, ServiceContext serviceContext)
264                    throws PortalException, SystemException {
265    
266                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
267    
268                    JournalFolderPermission.check(
269                            getPermissionChecker(), folder, ActionKeys.UPDATE);
270    
271                    return journalFolderLocalService.moveFolder(
272                            folderId, parentFolderId, serviceContext);
273            }
274    
275            @Override
276            public JournalFolder moveFolderFromTrash(
277                            long folderId, long parentFolderId, ServiceContext serviceContext)
278                    throws PortalException, SystemException {
279    
280                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
281    
282                    JournalFolderPermission.check(
283                            getPermissionChecker(), folder, ActionKeys.UPDATE);
284    
285                    return journalFolderLocalService.moveFolderFromTrash(
286                            getUserId(), folderId, parentFolderId, serviceContext);
287            }
288    
289            @Override
290            public JournalFolder moveFolderToTrash(long folderId)
291                    throws PortalException, SystemException {
292    
293                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
294    
295                    JournalFolderPermission.check(
296                            getPermissionChecker(), folder, ActionKeys.DELETE);
297    
298                    return journalFolderLocalService.moveFolderToTrash(
299                            getUserId(), folderId);
300            }
301    
302            @Override
303            public void restoreFolderFromTrash(long folderId)
304                            throws PortalException, SystemException {
305    
306                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
307    
308                    JournalFolderPermission.check(
309                            getPermissionChecker(), folder, ActionKeys.UPDATE);
310    
311                    journalFolderLocalService.restoreFolderFromTrash(getUserId(), folderId);
312            }
313    
314            @Override
315            public JournalFolder updateFolder(
316                            long folderId, long parentFolderId, String name, String description,
317                            boolean mergeWithParentFolder, ServiceContext serviceContext)
318                    throws PortalException, SystemException {
319    
320                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
321    
322                    JournalFolderPermission.check(
323                            getPermissionChecker(), folder, ActionKeys.UPDATE);
324    
325                    return journalFolderLocalService.updateFolder(
326                            getUserId(), folderId, parentFolderId, name, description,
327                            mergeWithParentFolder, serviceContext);
328            }
329    
330    }