001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    QueryDefinition queryDefinition = new QueryDefinition(
164                            status, start, end, obc);
165    
166                    return journalFolderFinder.filterFindF_A_ByG_F(
167                            groupId, folderId, queryDefinition);
168            }
169    
170            @Override
171            public List<Object> getFoldersAndArticles(
172                            long groupId, long folderId, int start, int end,
173                            OrderByComparator obc)
174                    throws SystemException {
175    
176                    return getFoldersAndArticles(
177                            groupId, folderId, WorkflowConstants.STATUS_ANY, start, end, obc);
178            }
179    
180            @Override
181            public int getFoldersAndArticlesCount(
182                            long groupId, List<Long> folderIds, int status)
183                    throws SystemException {
184    
185                    QueryDefinition queryDefinition = new QueryDefinition(status);
186    
187                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
188                            return journalArticleFinder.filterCountByG_F(
189                                    groupId, folderIds, queryDefinition);
190                    }
191                    else {
192                            int start = 0;
193                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
194    
195                            int articlesCount = journalArticleFinder.filterCountByG_F(
196                                    groupId, folderIds.subList(start, end), queryDefinition);
197    
198                            folderIds.subList(start, end).clear();
199    
200                            articlesCount += getFoldersAndArticlesCount(
201                                    groupId, folderIds, status);
202    
203                            return articlesCount;
204                    }
205            }
206    
207            @Override
208            public int getFoldersAndArticlesCount(long groupId, long folderId)
209                    throws SystemException {
210    
211                    return getFoldersAndArticlesCount(
212                            groupId, folderId, WorkflowConstants.STATUS_ANY);
213            }
214    
215            @Override
216            public int getFoldersAndArticlesCount(
217                            long groupId, long folderId, int status)
218                    throws SystemException {
219    
220                    return journalFolderFinder.filterCountF_A_ByG_F(
221                            groupId, folderId, new QueryDefinition(status));
222            }
223    
224            @Override
225            public int getFoldersCount(long groupId, long parentFolderId)
226                    throws SystemException {
227    
228                    return getFoldersCount(
229                            groupId, parentFolderId, WorkflowConstants.STATUS_APPROVED);
230            }
231    
232            @Override
233            public int getFoldersCount(long groupId, long parentFolderId, int status)
234                    throws SystemException {
235    
236                    if (status == WorkflowConstants.STATUS_ANY) {
237                            return journalFolderPersistence.filterCountByG_P_NotS(
238                                    groupId, parentFolderId, WorkflowConstants.STATUS_IN_TRASH);
239                    }
240                    else {
241                            return journalFolderPersistence.filterCountByG_P_S(
242                                    groupId, parentFolderId, status);
243                    }
244            }
245    
246            /**
247             * @deprecated As of 7.0.0, replaced by {@link #getSubfolderIds(List, long,
248             *             long, boolean)}
249             */
250            @Deprecated
251            @Override
252            public void getSubfolderIds(
253                            List<Long> folderIds, long groupId, long folderId)
254                    throws SystemException {
255    
256                    getSubfolderIds(folderIds, groupId, folderId, true);
257            }
258    
259            @Override
260            public void getSubfolderIds(
261                            List<Long> folderIds, long groupId, long folderId, boolean recurse)
262                    throws SystemException {
263    
264                    List<JournalFolder> folders =
265                            journalFolderPersistence.filterFindByG_P_NotS(
266                                    groupId, folderId, WorkflowConstants.STATUS_IN_TRASH);
267    
268                    for (JournalFolder folder : folders) {
269                            folderIds.add(folder.getFolderId());
270    
271                            if (recurse) {
272                                    getSubfolderIds(
273                                            folderIds, folder.getGroupId(), folder.getFolderId(),
274                                            recurse);
275                            }
276                    }
277            }
278    
279            @Override
280            public List<Long> getSubfolderIds(
281                            long groupId, long folderId, boolean recurse)
282                    throws SystemException {
283    
284                    List<Long> folderIds = new ArrayList<Long>();
285    
286                    getSubfolderIds(folderIds, groupId, folderId, recurse);
287    
288                    return folderIds;
289            }
290    
291            @Override
292            public JournalFolder moveFolder(
293                            long folderId, long parentFolderId, ServiceContext serviceContext)
294                    throws PortalException, SystemException {
295    
296                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
297    
298                    JournalFolderPermission.check(
299                            getPermissionChecker(), folder, ActionKeys.UPDATE);
300    
301                    return journalFolderLocalService.moveFolder(
302                            folderId, parentFolderId, serviceContext);
303            }
304    
305            @Override
306            public JournalFolder moveFolderFromTrash(
307                            long folderId, long parentFolderId, ServiceContext serviceContext)
308                    throws PortalException, SystemException {
309    
310                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
311    
312                    JournalFolderPermission.check(
313                            getPermissionChecker(), folder, ActionKeys.UPDATE);
314    
315                    return journalFolderLocalService.moveFolderFromTrash(
316                            getUserId(), folderId, parentFolderId, serviceContext);
317            }
318    
319            @Override
320            public JournalFolder moveFolderToTrash(long folderId)
321                    throws PortalException, SystemException {
322    
323                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
324    
325                    JournalFolderPermission.check(
326                            getPermissionChecker(), folder, ActionKeys.DELETE);
327    
328                    return journalFolderLocalService.moveFolderToTrash(
329                            getUserId(), folderId);
330            }
331    
332            @Override
333            public void restoreFolderFromTrash(long folderId)
334                    throws PortalException, SystemException {
335    
336                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
337    
338                    JournalFolderPermission.check(
339                            getPermissionChecker(), folder, ActionKeys.UPDATE);
340    
341                    journalFolderLocalService.restoreFolderFromTrash(getUserId(), folderId);
342            }
343    
344            @Override
345            public JournalFolder updateFolder(
346                            long folderId, long parentFolderId, String name, String description,
347                            boolean mergeWithParentFolder, ServiceContext serviceContext)
348                    throws PortalException, SystemException {
349    
350                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
351    
352                    JournalFolderPermission.check(
353                            getPermissionChecker(), folder, ActionKeys.UPDATE);
354    
355                    return journalFolderLocalService.updateFolder(
356                            getUserId(), folderId, parentFolderId, name, description,
357                            mergeWithParentFolder, serviceContext);
358            }
359    
360    }