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