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                            FileVersion fileVersion = null;
101    
102                            if (Validator.isNotNull(version)) {
103                                    fileVersion = fileEntry.getFileVersion(version);
104    
105                                    request.setAttribute(
106                                            WebKeys.DOCUMENT_LIBRARY_FILE_VERSION, fileVersion);
107                            }
108                            else {
109                                    fileVersion = fileEntry.getFileVersion();
110                            }
111    
112                            RawMetadataProcessorUtil.generateMetadata(fileVersion);
113    
114                            String cmd = ParamUtil.getString(request, Constants.CMD);
115    
116                            if ((fileVersion.isInTrash() || fileVersion.isInTrashContainer()) &&
117                                    !cmd.equals(Constants.MOVE_FROM_TRASH)) {
118    
119                                    throw new NoSuchFileEntryException();
120                            }
121                    }
122            }
123    
124            public static void getFileEntry(PortletRequest portletRequest)
125                    throws Exception {
126    
127                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
128                            portletRequest);
129    
130                    getFileEntry(request);
131            }
132    
133            public static void getFileShortcut(HttpServletRequest request)
134                    throws Exception {
135    
136                    long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
137    
138                    DLFileShortcut fileShortcut = null;
139    
140                    if (fileShortcutId > 0) {
141                            fileShortcut = DLAppServiceUtil.getFileShortcut(fileShortcutId);
142                    }
143    
144                    request.setAttribute(
145                            WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUT, fileShortcut);
146            }
147    
148            public static void getFileShortcut(PortletRequest portletRequest)
149                    throws Exception {
150    
151                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
152                            portletRequest);
153    
154                    getFileShortcut(request);
155            }
156    
157            public static void getFileShortcuts(HttpServletRequest request)
158                    throws Exception {
159    
160                    long[] fileShortcutIds = StringUtil.split(
161                            ParamUtil.getString(request, "fileShortcutIds"), 0L);
162    
163                    List<DLFileShortcut> fileShortcuts = new ArrayList<DLFileShortcut>();
164    
165                    for (long fileShortcutId : fileShortcutIds) {
166                            if (fileShortcutId > 0) {
167                                    fileShortcuts.add(
168                                            DLAppServiceUtil.getFileShortcut(fileShortcutId));
169                            }
170                    }
171    
172                    request.setAttribute(
173                            WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUTS, fileShortcuts);
174            }
175    
176            public static void getFileShortcuts(PortletRequest portletRequest)
177                    throws Exception {
178    
179                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
180                            portletRequest);
181    
182                    getFileShortcuts(request);
183            }
184    
185            public static void getFolder(HttpServletRequest request) throws Exception {
186                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
187                            WebKeys.THEME_DISPLAY);
188    
189                    long folderId = ParamUtil.getLong(request, "folderId");
190    
191                    Folder folder = null;
192    
193                    if ((folderId > 0) &&
194                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
195    
196                            folder = DLAppServiceUtil.getFolder(folderId);
197    
198                            if (folder.getModel() instanceof DLFolder) {
199                                    DLFolder dlFolder = (DLFolder)folder.getModel();
200    
201                                    if (dlFolder.isInTrash() || dlFolder.isInTrashContainer()) {
202                                            throw new NoSuchFolderException();
203                                    }
204                            }
205                    }
206                    else {
207                            DLPermission.check(
208                                    themeDisplay.getPermissionChecker(),
209                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
210                    }
211    
212                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDER, folder);
213            }
214    
215            public static void getFolder(PortletRequest portletRequest)
216                    throws Exception {
217    
218                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
219                            portletRequest);
220    
221                    getFolder(request);
222            }
223    
224            public static void getFolders(HttpServletRequest request) throws Exception {
225                    long[] folderIds = StringUtil.split(
226                            ParamUtil.getString(request, "folderIds"), 0L);
227    
228                    List<Folder> folders = new ArrayList<Folder>();
229    
230                    for (long folderId : folderIds) {
231                            try {
232                                    Folder folder = DLAppServiceUtil.getFolder(folderId);
233    
234                                    folders.add(folder);
235                            }
236                            catch (NoSuchFolderException nsfee) {
237                            }
238                    }
239    
240                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDERS, folders);
241            }
242    
243            public static void getFolders(PortletRequest portletRequest)
244                    throws Exception {
245    
246                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
247                            portletRequest);
248    
249                    getFolders(request);
250            }
251    
252            public static void getRepository(HttpServletRequest request)
253                    throws Exception {
254    
255                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
256                            WebKeys.THEME_DISPLAY);
257    
258                    long repositoryId = ParamUtil.getLong(request, "repositoryId");
259    
260                    Repository repository = null;
261    
262                    if (repositoryId > 0) {
263                            repository = RepositoryServiceUtil.getRepository(repositoryId);
264                    }
265                    else {
266                            DLPermission.check(
267                                    themeDisplay.getPermissionChecker(),
268                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
269                    }
270    
271                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_REPOSITORY, repository);
272            }
273    
274            public static void getRepository(PortletRequest portletRequest)
275                    throws Exception {
276    
277                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
278                            portletRequest);
279    
280                    getRepository(request);
281            }
282    
283    }