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