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.servlet.SessionErrors;
31  import com.liferay.portal.kernel.upload.UploadPortletRequest;
32  import com.liferay.portal.kernel.util.Constants;
33  import com.liferay.portal.kernel.util.ParamUtil;
34  import com.liferay.portal.kernel.util.PropertiesUtil;
35  import com.liferay.portal.kernel.util.StringUtil;
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.documentlibrary.DuplicateFolderNameException;
43  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
44  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
45  import com.liferay.portlet.documentlibrary.form.FileEntryForm;
46  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
47  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
48  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
49  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
50  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
51  import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
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              else if (cmd.equals(Constants.LOCK)) {
92                  lockFileEntry(actionRequest);
93              }
94              else if (cmd.equals(Constants.UNLOCK)) {
95                  unlockFileEntry(actionRequest);
96              }
97  
98              sendRedirect(actionRequest, actionResponse);
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         return mapping.findForward(getForward(renderRequest, forward));
159     }
160 
161     protected void deleteFileEntry(ActionRequest actionRequest)
162         throws Exception {
163 
164         long folderId = ParamUtil.getLong(actionRequest, "folderId");
165         String name = ParamUtil.getString(actionRequest, "name");
166         double version = ParamUtil.getDouble(actionRequest, "version");
167 
168         DLFileEntryServiceUtil.deleteFileEntry(folderId, name, version);
169     }
170 
171     protected void lockFileEntry(ActionRequest actionRequest) throws Exception {
172         long folderId = ParamUtil.getLong(actionRequest, "folderId");
173         String name = ParamUtil.getString(actionRequest, "name");
174 
175         DLFileEntryServiceUtil.lockFileEntry(folderId, name);
176     }
177 
178     protected void unlockFileEntry(ActionRequest actionRequest)
179         throws Exception {
180 
181         long folderId = ParamUtil.getLong(actionRequest, "folderId");
182         String name = ParamUtil.getString(actionRequest, "name");
183 
184         DLFileEntryServiceUtil.unlockFileEntry(folderId, name);
185     }
186 
187     protected void updateFileEntry(
188             FileEntryForm fileEntryForm, ActionRequest actionRequest,
189             ActionResponse actionResponse)
190         throws Exception {
191 
192         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
193             actionRequest);
194 
195         String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
196 
197         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
198             WebKeys.THEME_DISPLAY);
199 
200         long folderId = ParamUtil.getLong(uploadRequest, "folderId");
201         long newFolderId = ParamUtil.getLong(uploadRequest, "newFolderId");
202         String name = ParamUtil.getString(uploadRequest, "name");
203         String sourceFileName = uploadRequest.getFileName("file");
204 
205         String title = ParamUtil.getString(uploadRequest, "title");
206         String description = ParamUtil.getString(uploadRequest, "description");
207 
208         String[] tagsEntries = StringUtil.split(
209             ParamUtil.getString(uploadRequest, "tagsEntries"));
210 
211         String extraSettings = PropertiesUtil.toString(
212             fileEntryForm.getExtraSettingsProperties());
213 
214         File file = uploadRequest.getFile("file");
215 
216         String[] communityPermissions = PortalUtil.getCommunityPermissions(
217             actionRequest);
218         String[] guestPermissions = PortalUtil.getGuestPermissions(
219             actionRequest);
220 
221         if (cmd.equals(Constants.ADD)) {
222 
223             // Add file entry
224 
225             DLFolderPermission.check(
226                 themeDisplay.getPermissionChecker(), folderId,
227                 ActionKeys.ADD_DOCUMENT);
228 
229             DLFileEntry entry = DLFileEntryLocalServiceUtil.addFileEntry(
230                 themeDisplay.getUserId(), folderId, sourceFileName, title,
231                 description, tagsEntries, extraSettings, file,
232                 communityPermissions, guestPermissions);
233 
234             AssetPublisherUtil.addAndStoreSelection(
235                 actionRequest, DLFileEntry.class.getName(),
236                 entry.getFileEntryId(), -1);
237         }
238         else {
239 
240             // Update file entry
241 
242             DLFileEntryPermission.check(
243                 themeDisplay.getPermissionChecker(), folderId, name,
244                 ActionKeys.UPDATE);
245 
246             DLFileEntryLocalServiceUtil.updateFileEntry(
247                 themeDisplay.getUserId(), folderId, newFolderId, name,
248                 sourceFileName, title, description, tagsEntries, extraSettings,
249                 file);
250         }
251 
252         AssetPublisherUtil.addRecentFolderId(
253             actionRequest, DLFileEntry.class.getName(), folderId);
254     }
255 
256 }