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 start, int end,
146                            OrderByComparator obc)
147                    throws SystemException {
148    
149                    QueryDefinition queryDefinition = new QueryDefinition(
150                            WorkflowConstants.STATUS_ANY, start, end, obc);
151    
152                    return journalFolderFinder.filterFindF_A_ByG_F(
153                            groupId, folderId, queryDefinition);
154            }
155    
156            @Override
157            public int getFoldersAndArticlesCount(
158                            long groupId, List<Long> folderIds, int status)
159                    throws SystemException {
160    
161                    QueryDefinition queryDefinition = new QueryDefinition(status);
162    
163                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
164                            return journalArticleFinder.filterCountByG_F(
165                                    groupId, folderIds, queryDefinition);
166                    }
167                    else {
168                            int start = 0;
169                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
170    
171                            int articlesCount = journalArticleFinder.filterCountByG_F(
172                                    groupId, folderIds.subList(start, end), queryDefinition);
173    
174                            folderIds.subList(start, end).clear();
175    
176                            articlesCount += getFoldersAndArticlesCount(
177                                    groupId, folderIds, status);
178    
179                            return articlesCount;
180                    }
181            }
182    
183            @Override
184            public int getFoldersAndArticlesCount(long groupId, long folderId)
185                    throws SystemException {
186    
187                    return getFoldersAndArticlesCount(
188                            groupId, folderId, WorkflowConstants.STATUS_ANY);
189            }
190    
191            @Override
192            public int getFoldersAndArticlesCount(
193                            long groupId, long folderId, int status)
194                    throws SystemException {
195    
196                    return journalFolderFinder.filterCountF_A_ByG_F(
197                            groupId, folderId, new QueryDefinition(status));
198            }
199    
200            @Override
201            public int getFoldersCount(long groupId, long parentFolderId)
202                    throws SystemException {
203    
204                    return getFoldersCount(
205                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
206            }
207    
208            @Override
209            public int getFoldersCount(long groupId, long parentFolderId, int status)
210                    throws SystemException {
211    
212                    if (status == WorkflowConstants.STATUS_ANY) {
213                            return journalFolderPersistence.filterCountByG_P_NotS(
214                                    groupId, parentFolderId, WorkflowConstants.STATUS_IN_TRASH);
215                    }
216                    else {
217                            return journalFolderPersistence.filterCountByG_P_S(
218                                    groupId, parentFolderId, status);
219                    }
220            }
221    
222            @Override
223            public void getSubfolderIds(
224                            List<Long> folderIds, long groupId, long folderId)
225                    throws SystemException {
226    
227                    List<JournalFolder> folders = journalFolderPersistence.filterFindByG_P(
228                            groupId, folderId);
229    
230                    for (JournalFolder folder : folders) {
231                            if (folder.isInTrash() || folder.isInTrashContainer()) {
232                                    continue;
233                            }
234    
235                            folderIds.add(folder.getFolderId());
236    
237                            getSubfolderIds(
238                                    folderIds, folder.getGroupId(), folder.getFolderId());
239                    }
240            }
241    
242            @Override
243            public List<Long> getSubfolderIds(
244                            long groupId, long folderId, boolean recurse)
245                    throws SystemException {
246    
247                    List<Long> folderIds = new ArrayList<Long>();
248    
249                    getSubfolderIds(folderIds, groupId, folderId);
250    
251                    return folderIds;
252            }
253    
254            @Override
255            public JournalFolder moveFolder(
256                            long folderId, long parentFolderId, ServiceContext serviceContext)
257                    throws PortalException, SystemException {
258    
259                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
260    
261                    JournalFolderPermission.check(
262                            getPermissionChecker(), folder, ActionKeys.UPDATE);
263    
264                    return journalFolderLocalService.moveFolder(
265                            folderId, parentFolderId, serviceContext);
266            }
267    
268            @Override
269            public JournalFolder moveFolderFromTrash(
270                            long folderId, long parentFolderId, ServiceContext serviceContext)
271                    throws PortalException, SystemException {
272    
273                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
274    
275                    JournalFolderPermission.check(
276                            getPermissionChecker(), folder, ActionKeys.UPDATE);
277    
278                    return journalFolderLocalService.moveFolderFromTrash(
279                            getUserId(), folderId, parentFolderId, serviceContext);
280            }
281    
282            @Override
283            public JournalFolder moveFolderToTrash(long folderId)
284                    throws PortalException, SystemException {
285    
286                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
287    
288                    JournalFolderPermission.check(
289                            getPermissionChecker(), folder, ActionKeys.DELETE);
290    
291                    return journalFolderLocalService.moveFolderToTrash(
292                            getUserId(), folderId);
293            }
294    
295            @Override
296            public void restoreFolderFromTrash(long folderId)
297                            throws PortalException, SystemException {
298    
299                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
300    
301                    JournalFolderPermission.check(
302                            getPermissionChecker(), folder, ActionKeys.UPDATE);
303    
304                    journalFolderLocalService.restoreFolderFromTrash(getUserId(), folderId);
305            }
306    
307            @Override
308            public JournalFolder updateFolder(
309                            long folderId, long parentFolderId, String name, String description,
310                            boolean mergeWithParentFolder, ServiceContext serviceContext)
311                    throws PortalException, SystemException {
312    
313                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
314    
315                    JournalFolderPermission.check(
316                            getPermissionChecker(), folder, ActionKeys.UPDATE);
317    
318                    return journalFolderLocalService.updateFolder(
319                            getUserId(), folderId, parentFolderId, name, description,
320                            mergeWithParentFolder, serviceContext);
321            }
322    
323    }