001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.RawMetadataProcessor;
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                    try {
090                            if (fileEntryId > 0) {
091                                    fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId);
092                            }
093    
094                            request.setAttribute(
095                                    WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, fileEntry);
096                    }
097                    catch (NoSuchFileEntryException nsfee) {
098                    }
099    
100                    String version = ParamUtil.getString(request, "version");
101    
102                    if (fileEntry != null) {
103                            if (Validator.isNotNull(version)) {
104                                    FileVersion fileVersion = fileEntry.getFileVersion(version);
105    
106                                    request.setAttribute(
107                                            WebKeys.DOCUMENT_LIBRARY_FILE_VERSION, fileVersion);
108    
109                                    RawMetadataProcessor.generateMetadata(fileVersion);
110                            }
111                            else {
112                                    RawMetadataProcessor.generateMetadata(
113                                            fileEntry.getFileVersion());
114                            }
115                    }
116            }
117    
118            public static void getFileEntry(PortletRequest portletRequest)
119                    throws Exception {
120    
121                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
122                            portletRequest);
123    
124                    getFileEntry(request);
125            }
126    
127            public static void getFileShortcut(HttpServletRequest request)
128                    throws Exception {
129    
130                    long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
131    
132                    DLFileShortcut fileShortcut = null;
133    
134                    if (fileShortcutId > 0) {
135                            fileShortcut = DLAppServiceUtil.getFileShortcut(fileShortcutId);
136                    }
137    
138                    request.setAttribute(
139                            WebKeys.DOCUMENT_LIBRARY_FILE_SHORTCUT, fileShortcut);
140            }
141    
142            public static void getFileShortcut(PortletRequest portletRequest)
143                    throws Exception {
144    
145                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
146                            portletRequest);
147    
148                    getFileShortcut(request);
149            }
150    
151            public static void getFolder(HttpServletRequest request) throws Exception {
152                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
153                            WebKeys.THEME_DISPLAY);
154    
155                    long folderId = ParamUtil.getLong(request, "folderId");
156    
157                    Folder folder = null;
158    
159                    if ((folderId > 0) &&
160                            (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
161    
162                            folder = DLAppServiceUtil.getFolder(folderId);
163                    }
164                    else {
165                            DLPermission.check(
166                                    themeDisplay.getPermissionChecker(),
167                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
168                    }
169    
170                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDER, folder);
171            }
172    
173            public static void getFolder(PortletRequest portletRequest)
174                    throws Exception {
175    
176                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
177                            portletRequest);
178    
179                    getFolder(request);
180            }
181    
182            public static void getFolders(HttpServletRequest request) throws Exception {
183                    long[] folderIds = StringUtil.split(
184                            ParamUtil.getString(request, "folderIds"), 0L);
185    
186                    List<Folder> folders = new ArrayList<Folder>();
187    
188                    for (long folderId : folderIds) {
189                            try {
190                                    Folder folder = DLAppServiceUtil.getFolder(folderId);
191    
192                                    folders.add(folder);
193                            }
194                            catch (NoSuchFolderException nsfee) {
195                            }
196                    }
197    
198                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_FOLDERS, folders);
199            }
200    
201            public static void getFolders(PortletRequest portletRequest)
202                    throws Exception {
203    
204                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
205                            portletRequest);
206    
207                    getFolders(request);
208            }
209    
210            public static void getRepository(HttpServletRequest request)
211                    throws Exception {
212    
213                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
214                            WebKeys.THEME_DISPLAY);
215    
216                    long repositoryId = ParamUtil.getLong(request, "repositoryId");
217    
218                    Repository repository = null;
219    
220                    if (repositoryId > 0) {
221                            repository = RepositoryServiceUtil.getRepository(repositoryId);
222                    }
223                    else {
224                            DLPermission.check(
225                                    themeDisplay.getPermissionChecker(),
226                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
227                    }
228    
229                    request.setAttribute(WebKeys.DOCUMENT_LIBRARY_REPOSITORY, repository);
230            }
231    
232            public static void getRepository(PortletRequest portletRequest)
233                    throws Exception {
234    
235                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
236                            portletRequest);
237    
238                    getRepository(request);
239            }
240    
241    }