1   /**
2    * Copyright (c) 2000-2008 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.kernel.util.StringUtil;
37  import com.liferay.portal.security.auth.PrincipalException;
38  import com.liferay.portal.security.permission.ActionKeys;
39  import com.liferay.portal.struts.PortletAction;
40  import com.liferay.portal.theme.ThemeDisplay;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portal.util.WebKeys;
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.taggedcontent.util.AssetPublisherUtil;
53  import com.liferay.portlet.tags.TagsEntryException;
54  
55  import java.io.File;
56  
57  import javax.portlet.ActionRequest;
58  import javax.portlet.ActionResponse;
59  import javax.portlet.PortletConfig;
60  import javax.portlet.RenderRequest;
61  import javax.portlet.RenderResponse;
62  
63  import org.apache.struts.action.ActionForm;
64  import org.apache.struts.action.ActionForward;
65  import org.apache.struts.action.ActionMapping;
66  
67  /**
68   * <a href="EditFileEntryAction.java.html"><b><i>View Source</i></b></a>
69   *
70   * @author Brian Wing Shun Chan
71   * @author Alexander Chow
72   *
73   */
74  public class EditFileEntryAction extends PortletAction {
75  
76      public void processAction(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              ActionRequest actionRequest, ActionResponse actionResponse)
79          throws Exception {
80  
81          FileEntryForm fileEntryForm = (FileEntryForm)form;
82  
83          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
84  
85          try {
86              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
87                  updateFileEntry(fileEntryForm, actionRequest, actionResponse);
88              }
89              else if (cmd.equals(Constants.DELETE)) {
90                  deleteFileEntry(actionRequest);
91  
92                  sendRedirect(actionRequest, actionResponse);
93              }
94              else if (cmd.equals(Constants.LOCK)) {
95                  lockFileEntry(actionRequest);
96              }
97              else if (cmd.equals(Constants.UNLOCK)) {
98                  unlockFileEntry(actionRequest);
99              }
100         }
101         catch (Exception e) {
102             if (e instanceof DuplicateLockException ||
103                 e instanceof NoSuchFileEntryException ||
104                 e instanceof PrincipalException) {
105 
106                 if (e instanceof DuplicateLockException) {
107                     DuplicateLockException dle = (DuplicateLockException)e;
108 
109                     SessionErrors.add(
110                         actionRequest, dle.getClass().getName(), dle.getLock());
111                 }
112                 else {
113                     SessionErrors.add(actionRequest, e.getClass().getName());
114                 }
115 
116                 setForward(actionRequest, "portlet.document_library.error");
117             }
118             else if (e instanceof DuplicateFileException ||
119                      e instanceof DuplicateFolderNameException ||
120                      e instanceof FileNameException ||
121                      e instanceof FileSizeException ||
122                      e instanceof NoSuchFolderException ||
123                      e instanceof SourceFileNameException) {
124 
125                 SessionErrors.add(actionRequest, e.getClass().getName());
126             }
127             else if (e instanceof TagsEntryException) {
128                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
129             }
130             else {
131                 throw e;
132             }
133         }
134     }
135 
136     public ActionForward render(
137             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
138             RenderRequest renderRequest, RenderResponse renderResponse)
139         throws Exception {
140 
141         try {
142             ActionUtil.getFileEntry(renderRequest);
143         }
144         catch (Exception e) {
145             if (e instanceof NoSuchFileEntryException ||
146                 e instanceof PrincipalException) {
147 
148                 SessionErrors.add(renderRequest, e.getClass().getName());
149 
150                 return mapping.findForward("portlet.document_library.error");
151             }
152             else {
153                 throw e;
154             }
155         }
156 
157         String forward = "portlet.document_library.edit_file_entry";
158 
159         if (renderRequest.getWindowState().equals(LiferayWindowState.POP_UP)) {
160             forward = "portlet.document_library.edit_file_entry_form";
161         }
162 
163         return mapping.findForward(getForward(renderRequest, forward));
164     }
165 
166     protected void deleteFileEntry(ActionRequest actionRequest)
167         throws Exception {
168 
169         long folderId = ParamUtil.getLong(actionRequest, "folderId");
170         String name = ParamUtil.getString(actionRequest, "name");
171         double version = ParamUtil.getDouble(actionRequest, "version");
172 
173         DLFileEntryServiceUtil.deleteFileEntry(folderId, name, version);
174     }
175 
176     protected void lockFileEntry(ActionRequest actionRequest) throws Exception {
177         long folderId = ParamUtil.getLong(actionRequest, "folderId");
178         String name = ParamUtil.getString(actionRequest, "name");
179 
180         DLFileEntryServiceUtil.lockFileEntry(folderId, name);
181     }
182 
183     protected void unlockFileEntry(ActionRequest actionRequest)
184         throws Exception {
185 
186         long folderId = ParamUtil.getLong(actionRequest, "folderId");
187         String name = ParamUtil.getString(actionRequest, "name");
188 
189         DLFileEntryServiceUtil.unlockFileEntry(folderId, name);
190     }
191 
192     protected void updateFileEntry(
193             FileEntryForm fileEntryForm, ActionRequest actionRequest,
194             ActionResponse actionResponse)
195         throws Exception {
196 
197         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
198             actionRequest);
199 
200         String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
201 
202         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
203             WebKeys.THEME_DISPLAY);
204 
205         long folderId = ParamUtil.getLong(uploadRequest, "folderId");
206         long newFolderId = ParamUtil.getLong(uploadRequest, "newFolderId");
207         String name = ParamUtil.getString(uploadRequest, "name");
208         String sourceFileName = uploadRequest.getFileName("file");
209 
210         String title = ParamUtil.getString(uploadRequest, "title");
211         String description = ParamUtil.getString(uploadRequest, "description");
212 
213         String[] tagsEntries = StringUtil.split(
214             ParamUtil.getString(uploadRequest, "tagsEntries"));
215 
216         String extraSettings = PropertiesUtil.toString(
217             fileEntryForm.getExtraSettingsProperties());
218 
219         File file = uploadRequest.getFile("file");
220 
221         String[] communityPermissions = actionRequest.getParameterValues(
222             "communityPermissions");
223         String[] guestPermissions = actionRequest.getParameterValues(
224             "guestPermissions");
225 
226         if (cmd.equals(Constants.ADD)) {
227 
228             // Add file entry
229 
230             DLFolderPermission.check(
231                 themeDisplay.getPermissionChecker(), folderId,
232                 ActionKeys.ADD_DOCUMENT);
233 
234             DLFileEntry entry = DLFileEntryLocalServiceUtil.addFileEntry(
235                 themeDisplay.getUserId(), folderId, sourceFileName, title,
236                 description, tagsEntries, extraSettings, file,
237                 communityPermissions, guestPermissions);
238 
239             AssetPublisherUtil.addAndStoreSelection(
240                 actionRequest, DLFileEntry.class.getName(),
241                 entry.getFileEntryId(), -1);
242         }
243         else {
244 
245             // Update file entry
246 
247             DLFileEntryPermission.check(
248                 themeDisplay.getPermissionChecker(), folderId, name,
249                 ActionKeys.UPDATE);
250 
251             DLFileEntryLocalServiceUtil.updateFileEntry(
252                 themeDisplay.getUserId(), folderId, newFolderId, name,
253                 sourceFileName, title, description, tagsEntries, extraSettings,
254                 file);
255         }
256 
257         AssetPublisherUtil.addRecentFolderId(
258             actionRequest, DLFileEntry.class.getName(), folderId);
259     }
260 
261 }