1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.action;
24  
25  import com.liferay.documentlibrary.DuplicateFileException;
26  import com.liferay.documentlibrary.FileNameException;
27  import com.liferay.documentlibrary.FileSizeException;
28  import com.liferay.documentlibrary.SourceFileNameException;
29  import com.liferay.lock.DuplicateLockException;
30  import com.liferay.portal.kernel.portlet.LiferayWindowState;
31  import com.liferay.portal.kernel.servlet.SessionErrors;
32  import com.liferay.portal.kernel.upload.UploadPortletRequest;
33  import com.liferay.portal.kernel.util.Constants;
34  import com.liferay.portal.kernel.util.ParamUtil;
35  import com.liferay.portal.kernel.util.PropertiesUtil;
36  import com.liferay.portal.security.auth.PrincipalException;
37  import com.liferay.portal.security.permission.ActionKeys;
38  import com.liferay.portal.struts.PortletAction;
39  import com.liferay.portal.theme.ThemeDisplay;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portal.util.WebKeys;
42  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
43  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
44  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
45  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
46  import com.liferay.portlet.documentlibrary.form.FileEntryForm;
47  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
48  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
49  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
50  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
51  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
52  import com.liferay.portlet.tags.TagsEntryException;
53  
54  import java.io.File;
55  
56  import javax.portlet.ActionRequest;
57  import javax.portlet.ActionResponse;
58  import javax.portlet.PortletConfig;
59  import javax.portlet.RenderRequest;
60  import javax.portlet.RenderResponse;
61  
62  import org.apache.struts.action.ActionForm;
63  import org.apache.struts.action.ActionForward;
64  import org.apache.struts.action.ActionMapping;
65  
66  /**
67   * <a href="EditFileEntryAction.java.html"><b><i>View Source</i></b></a>
68   *
69   * @author Brian Wing Shun Chan
70   * @author Alexander Chow
71   *
72   */
73  public class EditFileEntryAction extends PortletAction {
74  
75      public void processAction(
76              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
77              ActionRequest actionRequest, ActionResponse actionResponse)
78          throws Exception {
79  
80          FileEntryForm fileEntryForm = (FileEntryForm)form;
81  
82          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
83  
84          try {
85              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
86                  updateFileEntry(fileEntryForm, actionRequest, actionResponse);
87              }
88              else if (cmd.equals(Constants.DELETE)) {
89                  deleteFileEntry(actionRequest);
90  
91                  sendRedirect(actionRequest, actionResponse);
92              }
93              else if (cmd.equals(Constants.LOCK)) {
94                  lockFileEntry(actionRequest);
95              }
96              else if (cmd.equals(Constants.UNLOCK)) {
97                  unlockFileEntry(actionRequest);
98              }
99          }
100         catch (Exception e) {
101             if (e instanceof DuplicateLockException ||
102                 e instanceof NoSuchFileEntryException ||
103                 e instanceof PrincipalException) {
104 
105                 if (e instanceof DuplicateLockException) {
106                     DuplicateLockException dle = (DuplicateLockException)e;
107 
108                     SessionErrors.add(
109                         actionRequest, dle.getClass().getName(), dle.getLock());
110                 }
111                 else {
112                     SessionErrors.add(actionRequest, e.getClass().getName());
113                 }
114 
115                 setForward(actionRequest, "portlet.document_library.error");
116             }
117             else if (e instanceof DuplicateFileException ||
118                      e instanceof DuplicateFolderNameException ||
119                      e instanceof FileNameException ||
120                      e instanceof FileSizeException ||
121                      e instanceof NoSuchFolderException ||
122                      e instanceof SourceFileNameException) {
123 
124                 SessionErrors.add(actionRequest, e.getClass().getName());
125             }
126             else if (e instanceof TagsEntryException) {
127                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
128             }
129             else {
130                 throw e;
131             }
132         }
133     }
134 
135     public ActionForward render(
136             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
137             RenderRequest renderRequest, RenderResponse renderResponse)
138         throws Exception {
139 
140         try {
141             ActionUtil.getFileEntry(renderRequest);
142         }
143         catch (Exception e) {
144             if (e instanceof NoSuchFileEntryException ||
145                 e instanceof PrincipalException) {
146 
147                 SessionErrors.add(renderRequest, e.getClass().getName());
148 
149                 return mapping.findForward("portlet.document_library.error");
150             }
151             else {
152                 throw e;
153             }
154         }
155 
156         String forward = "portlet.document_library.edit_file_entry";
157 
158         if (renderRequest.getWindowState().equals(LiferayWindowState.POP_UP)) {
159             forward = "portlet.document_library.edit_file_entry_form";
160         }
161 
162         return mapping.findForward(getForward(renderRequest, forward));
163     }
164 
165     protected void deleteFileEntry(ActionRequest actionRequest)
166         throws Exception {
167 
168         long folderId = ParamUtil.getLong(actionRequest, "folderId");
169         String name = ParamUtil.getString(actionRequest, "name");
170         double version = ParamUtil.getDouble(actionRequest, "version");
171 
172         DLFileEntryServiceUtil.deleteFileEntry(folderId, name, version);
173     }
174 
175     protected void lockFileEntry(ActionRequest actionRequest) throws Exception {
176         long folderId = ParamUtil.getLong(actionRequest, "folderId");
177         String name = ParamUtil.getString(actionRequest, "name");
178 
179         DLFileEntryServiceUtil.lockFileEntry(folderId, name);
180     }
181 
182     protected void unlockFileEntry(ActionRequest actionRequest)
183         throws Exception {
184 
185         long folderId = ParamUtil.getLong(actionRequest, "folderId");
186         String name = ParamUtil.getString(actionRequest, "name");
187 
188         DLFileEntryServiceUtil.unlockFileEntry(folderId, name);
189     }
190 
191     protected void updateFileEntry(
192             FileEntryForm fileEntryForm, ActionRequest actionRequest,
193             ActionResponse actionResponse)
194         throws Exception {
195 
196         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
197             actionRequest);
198 
199         String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
200 
201         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
202             WebKeys.THEME_DISPLAY);
203 
204         long folderId = ParamUtil.getLong(uploadRequest, "folderId");
205         long newFolderId = ParamUtil.getLong(uploadRequest, "newFolderId");
206         String name = ParamUtil.getString(uploadRequest, "name");
207         String sourceFileName = uploadRequest.getFileName("file");
208 
209         String title = ParamUtil.getString(uploadRequest, "title");
210         String description = ParamUtil.getString(uploadRequest, "description");
211 
212         String[] tagsEntries = PortalUtil.getTagsEntries(actionRequest);
213 
214         String extraSettings = PropertiesUtil.toString(
215             fileEntryForm.getExtraSettingsProperties());
216 
217         File file = uploadRequest.getFile("file");
218 
219         String[] communityPermissions = actionRequest.getParameterValues(
220             "communityPermissions");
221         String[] guestPermissions = actionRequest.getParameterValues(
222             "guestPermissions");
223 
224         if (cmd.equals(Constants.ADD)) {
225 
226             // Add file entry
227 
228             DLFolderPermission.check(
229                 themeDisplay.getPermissionChecker(), folderId,
230                 ActionKeys.ADD_DOCUMENT);
231 
232             DLFileEntry entry = DLFileEntryLocalServiceUtil.addFileEntry(
233                 themeDisplay.getUserId(), folderId, sourceFileName, title,
234                 description, tagsEntries, extraSettings, file,
235                 communityPermissions, guestPermissions);
236 
237             AssetPublisherUtil.addAndStoreSelection(
238                 actionRequest, DLFileEntry.class.getName(),
239                 entry.getFileEntryId(), -1);
240         }
241         else {
242 
243             // Update file entry
244 
245             DLFileEntryPermission.check(
246                 themeDisplay.getPermissionChecker(), folderId, name,
247                 ActionKeys.UPDATE);
248 
249             DLFileEntryLocalServiceUtil.updateFileEntry(
250                 themeDisplay.getUserId(), folderId, newFolderId, name,
251                 sourceFileName, title, description, tagsEntries, extraSettings,
252                 file);
253         }
254 
255         AssetPublisherUtil.addRecentFolderId(
256             actionRequest, DLFileEntry.class.getName(), folderId);
257     }
258 
259 }