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