001    /**
002     * Copyright (c) 2000-2012 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            public JournalFolder addFolder(
038                            long groupId, long parentFolderId, String name, String description,
039                            ServiceContext serviceContext)
040                    throws PortalException, SystemException {
041    
042                    JournalFolderPermission.check(
043                            getPermissionChecker(), serviceContext.getScopeGroupId(),
044                            parentFolderId, ActionKeys.ADD_FOLDER);
045    
046                    return journalFolderLocalService.addFolder(
047                            getUserId(), groupId, parentFolderId, name, description,
048                            serviceContext);
049            }
050    
051            public void deleteFolder(long folderId)
052                    throws PortalException, SystemException {
053    
054                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
055    
056                    JournalFolderPermission.check(
057                            getPermissionChecker(), folder, ActionKeys.DELETE);
058    
059                    journalFolderLocalService.deleteFolder(folderId);
060            }
061    
062            public JournalFolder getFolder(long folderId)
063                    throws PortalException, SystemException {
064    
065                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
066    
067                    JournalFolderPermission.check(
068                            getPermissionChecker(), folder, ActionKeys.VIEW);
069    
070                    return folder;
071            }
072    
073            public List<JournalFolder> getFolders(long groupId) throws SystemException {
074                    return journalFolderPersistence.filterFindByGroupId(groupId);
075            }
076    
077            public List<JournalFolder> getFolders(long groupId, long parentFolderId)
078                    throws SystemException {
079    
080                    return journalFolderPersistence.filterFindByG_P(
081                            groupId, parentFolderId);
082            }
083    
084            public List<JournalFolder> getFolders(
085                            long groupId, long parentFolderId, int start, int end)
086                    throws SystemException {
087    
088                    return journalFolderPersistence.filterFindByG_P(
089                            groupId, parentFolderId, start, end);
090            }
091    
092            public List<Object> getFoldersAndArticles(
093                            long groupId, long folderId, int start, int end,
094                            OrderByComparator obc)
095                    throws SystemException {
096    
097                    QueryDefinition queryDefinition = new QueryDefinition(
098                            WorkflowConstants.STATUS_ANY, start, end, obc);
099    
100                    return journalFolderFinder.filterFindF_A_ByG_F(
101                            groupId, folderId, queryDefinition);
102            }
103    
104            public int getFoldersAndArticlesCount(
105                            long groupId, List<Long> folderIds, int status)
106                    throws SystemException {
107    
108                    QueryDefinition queryDefinition = new QueryDefinition(status);
109    
110                    if (folderIds.size() <= PropsValues.SQL_DATA_MAX_PARAMETERS) {
111                            return journalArticleFinder.filterCountByG_F(
112                                    groupId, folderIds, queryDefinition);
113                    }
114                    else {
115                            int start = 0;
116                            int end = PropsValues.SQL_DATA_MAX_PARAMETERS;
117    
118                            int articlesCount = journalArticleFinder.filterCountByG_F(
119                                    groupId, folderIds.subList(start, end), queryDefinition);
120    
121                            folderIds.subList(start, end).clear();
122    
123                            articlesCount += getFoldersAndArticlesCount(
124                                    groupId, folderIds, status);
125    
126                            return articlesCount;
127                    }
128            }
129    
130            public int getFoldersAndArticlesCount(long groupId, long folderId)
131                    throws SystemException {
132    
133                    return getFoldersAndArticlesCount(
134                            groupId, folderId, WorkflowConstants.STATUS_ANY);
135            }
136    
137            public int getFoldersAndArticlesCount(
138                            long groupId, long folderId, int status)
139                    throws SystemException {
140    
141                    return journalFolderFinder.filterCountF_A_ByG_F(
142                            groupId, folderId, new QueryDefinition(status));
143            }
144    
145            public int getFoldersCount(long groupId, long parentFolderId)
146                    throws SystemException {
147    
148                    return journalFolderPersistence.filterCountByG_P(
149                            groupId, parentFolderId);
150            }
151    
152            public void getSubfolderIds(
153                            List<Long> folderIds, long groupId, long folderId)
154                    throws SystemException {
155    
156                    List<JournalFolder> folders = journalFolderPersistence.filterFindByG_P(
157                            groupId, folderId);
158    
159                    for (JournalFolder folder : folders) {
160                            folderIds.add(folder.getFolderId());
161    
162                            getSubfolderIds(
163                                    folderIds, folder.getGroupId(), folder.getFolderId());
164                    }
165            }
166    
167            public List<Long> getSubfolderIds(
168                            long groupId, long folderId, boolean recurse)
169                    throws SystemException {
170    
171                    List<Long> folderIds = new ArrayList<Long>();
172    
173                    getSubfolderIds(folderIds, groupId, folderId);
174    
175                    return folderIds;
176            }
177    
178            public JournalFolder moveFolder(
179                            long folderId, long parentFolderId, ServiceContext serviceContext)
180                    throws PortalException, SystemException {
181    
182                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
183    
184                    JournalFolderPermission.check(
185                            getPermissionChecker(), folder, ActionKeys.UPDATE);
186    
187                    return journalFolderLocalService.moveFolder(
188                            folderId, parentFolderId, serviceContext);
189            }
190    
191            public JournalFolder updateFolder(
192                            long folderId, long parentFolderId, String name, String description,
193                            boolean mergeWithParentFolder, ServiceContext serviceContext)
194                    throws PortalException, SystemException {
195    
196                    JournalFolder folder = journalFolderLocalService.getFolder(folderId);
197    
198                    JournalFolderPermission.check(
199                            getPermissionChecker(), folder, ActionKeys.UPDATE);
200    
201                    return journalFolderLocalService.updateFolder(
202                            getUserId(), folderId, parentFolderId, name, description,
203                            mergeWithParentFolder, serviceContext);
204            }
205    
206    }