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