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.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 getFolder(HttpServletRequest request) throws Exception {
147                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
148                            WebKeys.THEME_DISPLAY);
149    
150                    long folderId = ParamUtil.getLong(request, "folderId");
151    
152                    Folder folder = null;
153    
154                    if ((folderId > 0) &&
155                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
156    
157                            folder = DLAppServiceUtil.getFolder(folderId);
158                    }
159                    else {
160                            DLPermission.check(
161                                    themeDisplay.getPermissionChecker(),
162                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
163                    }
164    
165                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDER, folder);
166            }
167    
168            public static void getFolder(PortletRequest portletRequest)
169                    throws Exception {
170    
171                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
172                            portletRequest);
173    
174                    getFolder(request);
175            }
176    
177            public static void getFolders(HttpServletRequest request) throws Exception {
178                    long[] folderIds = StringUtil.split(
179                            ParamUtil.getString(request, "folderIds"), 0L);
180    
181                    List<Folder> folders = new ArrayList<Folder>();
182    
183                    for (long folderId : folderIds) {
184                            try {
185                                    Folder folder = DLAppServiceUtil.getFolder(folderId);
186    
187                                    folders.add(folder);
188                            }
189                            catch (NoSuchFolderException nsfee) {
190                            }
191                    }
192    
193                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDERS, folders);
194            }
195    
196            public static void getFolders(PortletRequest portletRequest)
197                    throws Exception {
198    
199                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
200                            portletRequest);
201    
202                    getFolders(request);
203            }
204    
205            public static void getRepository(HttpServletRequest request)
206                    throws Exception {
207    
208                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
209                            WebKeys.THEME_DISPLAY);
210    
211                    long repositoryId = ParamUtil.getLong(request, "repositoryId");
212    
213                    Repository repository = null;
214    
215                    if (repositoryId > 0) {
216                            repository = RepositoryServiceUtil.getRepository(repositoryId);
217                    }
218                    else {
219                            DLPermission.check(
220                                    themeDisplay.getPermissionChecker(),
221                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
222                    }
223    
224                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_REPOSITORY, repository);
225            }
226    
227            public static void getRepository(PortletRequest portletRequest)
228                    throws Exception {
229    
230                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
231                            portletRequest);
232    
233                    getRepository(request);
234            }
235    
236    }