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.documentlibrary.action;
016    
017    import com.liferay.portal.kernel.repository.model.FileEntry;
018    import com.liferay.portal.kernel.repository.model.FileVersion;
019    import com.liferay.portal.kernel.repository.model.Folder;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Repository;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.service.RepositoryServiceUtil;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
031    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
032    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
033    import com.liferay.portlet.documentlibrary.model.DLFolder;
034    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
035    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
036    import com.liferay.portlet.documentlibrary.service.permission.DLPermission;
037    import com.liferay.portlet.documentlibrary.util.RawMetadataProcessorUtil;
038    
039    import java.util.ArrayList;
040    import java.util.List;
041    
042    import javax.portlet.PortletRequest;
043    
044    import javax.servlet.http.HttpServletRequest;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     * @author Sergio Gonz??lez
049     */
050    public class ActionUtil {
051    
052            public static void getFileEntries(HttpServletRequest request)
053                    throws Exception {
054    
055                    List<FileEntry> fileEntries = new ArrayList<FileEntry>();
056    
057                    long[] fileEntryIds = StringUtil.split(
058                            ParamUtil.getString(request, "fileEntryIds"), 0L);
059    
060                    for (long fileEntryId : fileEntryIds) {
061                            try {
062                                    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(
063                                            fileEntryId);
064    
065                                    fileEntries.add(fileEntry);
066                            }
067                            catch (NoSuchFileEntryException nsfee) {
068                            }
069                    }
070    
071                    request.setAttribute(
072                            WebKeys.DOCUMENT_LIBRARY_FILE_ENTRIES, fileEntries);
073            }
074    
075            public static void getFileEntries(PortletRequest portletRequest)
076                    throws Exception {
077    
078                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
079                            portletRequest);
080    
081                    getFileEntries(request);
082            }
083    
084            public static void getFileEntry(HttpServletRequest request)
085                    throws Exception {
086    
087                    long fileEntryId = ParamUtil.getLong(request, "fileEntryId");
088    
089                    FileEntry fileEntry = null;
090    
091                    if (fileEntryId > 0) {
092                            fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId);
093                    }
094    
095                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, fileEntry);
096    
097                    String version = ParamUtil.getString(request, "version");
098    
099                    if (fileEntry == null) {
100                            return;
101                    }
102    
103                    FileVersion fileVersion = null;
104    
105                    if (Validator.isNotNull(version)) {
106                            fileVersion = fileEntry.getFileVersion(version);
107    
108                            request.setAttribute(
109                                    WebKeys.DOCUMENT_LIBRARY_FILE_VERSION, fileVersion);
110                    }
111                    else {
112                            fileVersion = fileEntry.getFileVersion();
113                    }
114    
115                    RawMetadataProcessorUtil.generateMetadata(fileVersion);
116    
117                    String cmd = ParamUtil.getString(request, Constants.CMD);
118    
119                    if ((fileEntry.isInTrash() || fileEntry.isInTrashContainer()) &&
120                            !cmd.equals(Constants.MOVE_FROM_TRASH)) {
121    
122                            throw new NoSuchFileEntryException();
123                    }
124            }
125    
126            public static void getFileEntry(PortletRequest portletRequest)
127                    throws Exception {
128    
129                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
130                            portletRequest);
131    
132                    getFileEntry(request);
133            }
134    
135            public static void getFileShortcut(HttpServletRequest request)
136                    throws Exception {
137    
138                    long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
139    
140                    DLFileShortcut fileShortcut = null;
141    
142                    if (fileShortcutId > 0) {
143                            fileShortcut = DLAppServiceUtil.getFileShortcut(fileShortcutId);
144                    }
145    
146                    request.setAttribute(
147                            WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUT, fileShortcut);
148            }
149    
150            public static void getFileShortcut(PortletRequest portletRequest)
151                    throws Exception {
152    
153                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
154                            portletRequest);
155    
156                    getFileShortcut(request);
157            }
158    
159            public static void getFileShortcuts(HttpServletRequest request)
160                    throws Exception {
161    
162                    long[] fileShortcutIds = StringUtil.split(
163                            ParamUtil.getString(request, "fileShortcutIds"), 0L);
164    
165                    List<DLFileShortcut> fileShortcuts = new ArrayList<DLFileShortcut>();
166    
167                    for (long fileShortcutId : fileShortcutIds) {
168                            if (fileShortcutId > 0) {
169                                    fileShortcuts.add(
170                                            DLAppServiceUtil.getFileShortcut(fileShortcutId));
171                            }
172                    }
173    
174                    request.setAttribute(
175                            WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUTS, fileShortcuts);
176            }
177    
178            public static void getFileShortcuts(PortletRequest portletRequest)
179                    throws Exception {
180    
181                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
182                            portletRequest);
183    
184                    getFileShortcuts(request);
185            }
186    
187            public static void getFolder(HttpServletRequest request) throws Exception {
188                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
189                            WebKeys.THEME_DISPLAY);
190    
191                    long folderId = ParamUtil.getLong(request, "folderId");
192    
193                    Folder folder = null;
194    
195                    if ((folderId > 0) &&
196                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
197    
198                            folder = DLAppServiceUtil.getFolder(folderId);
199    
200                            if (folder.getModel() instanceof DLFolder) {
201                                    DLFolder dlFolder = (DLFolder)folder.getModel();
202    
203                                    if (dlFolder.isInTrash() || dlFolder.isInTrashContainer()) {
204                                            throw new NoSuchFolderException();
205                                    }
206                            }
207                    }
208                    else {
209                            DLPermission.check(
210                                    themeDisplay.getPermissionChecker(),
211                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
212                    }
213    
214                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDER, folder);
215            }
216    
217            public static void getFolder(PortletRequest portletRequest)
218                    throws Exception {
219    
220                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
221                            portletRequest);
222    
223                    getFolder(request);
224            }
225    
226            public static void getFolders(HttpServletRequest request) throws Exception {
227                    long[] folderIds = StringUtil.split(
228                            ParamUtil.getString(request, "folderIds"), 0L);
229    
230                    List<Folder> folders = new ArrayList<Folder>();
231    
232                    for (long folderId : folderIds) {
233                            try {
234                                    Folder folder = DLAppServiceUtil.getFolder(folderId);
235    
236                                    folders.add(folder);
237                            }
238                            catch (NoSuchFolderException nsfee) {
239                            }
240                    }
241    
242                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDERS, folders);
243            }
244    
245            public static void getFolders(PortletRequest portletRequest)
246                    throws Exception {
247    
248                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
249                            portletRequest);
250    
251                    getFolders(request);
252            }
253    
254            public static void getRepository(HttpServletRequest request)
255                    throws Exception {
256    
257                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
258                            WebKeys.THEME_DISPLAY);
259    
260                    long repositoryId = ParamUtil.getLong(request, "repositoryId");
261    
262                    Repository repository = null;
263    
264                    if (repositoryId > 0) {
265                            repository = RepositoryServiceUtil.getRepository(repositoryId);
266                    }
267                    else {
268                            DLPermission.check(
269                                    themeDisplay.getPermissionChecker(),
270                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
271                    }
272    
273                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_REPOSITORY, repository);
274            }
275    
276            public static void getRepository(PortletRequest portletRequest)
277                    throws Exception {
278    
279                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
280                            portletRequest);
281    
282                    getRepository(request);
283            }
284    
285    }