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