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.util.Constants;
32  import com.liferay.portal.kernel.util.ParamUtil;
33  import com.liferay.portal.kernel.util.PropertiesUtil;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.security.permission.ActionKeys;
37  import com.liferay.portal.struts.PortletAction;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.util.UploadRequestUtil;
40  import com.liferay.portal.util.WebKeys;
41  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
42  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
43  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
44  import com.liferay.portlet.documentlibrary.form.FileEntryForm;
45  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
46  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
47  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
48  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
49  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
50  import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
51  import com.liferay.portlet.tags.TagsEntryException;
52  import com.liferay.util.servlet.SessionErrors;
53  import com.liferay.util.servlet.UploadPortletRequest;
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 config,
78              ActionRequest req, ActionResponse res)
79          throws Exception {
80  
81          FileEntryForm fileEntryForm = (FileEntryForm)form;
82  
83          String cmd = ParamUtil.getString(req, Constants.CMD);
84  
85          try {
86              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
87                  updateFileEntry(fileEntryForm, req, res);
88              }
89              else if (cmd.equals(Constants.DELETE)) {
90                  deleteFileEntry(req);
91  
92                  sendRedirect(req, res);
93              }
94              else if (cmd.equals(Constants.LOCK)) {
95                  lockFileEntry(req);
96              }
97              else if (cmd.equals(Constants.UNLOCK)) {
98                  unlockFileEntry(req);
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                         req, dle.getClass().getName(), dle.getLock());
111                 }
112                 else {
113                     SessionErrors.add(req, e.getClass().getName());
114                 }
115 
116                 setForward(req, "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(req, e.getClass().getName());
126             }
127             else if (e instanceof TagsEntryException) {
128                 SessionErrors.add(req, e.getClass().getName(), e);
129             }
130             else {
131                 throw e;
132             }
133         }
134     }
135 
136     public ActionForward render(
137             ActionMapping mapping, ActionForm form, PortletConfig config,
138             RenderRequest req, RenderResponse res)
139         throws Exception {
140 
141         try {
142             ActionUtil.getFileEntry(req);
143         }
144         catch (Exception e) {
145             if (e instanceof NoSuchFileEntryException ||
146                 e instanceof PrincipalException) {
147 
148                 SessionErrors.add(req, 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 (req.getWindowState().equals(LiferayWindowState.POP_UP)) {
160             forward = "portlet.document_library.edit_file_entry_form";
161         }
162 
163         return mapping.findForward(getForward(req, forward));
164     }
165 
166     protected void deleteFileEntry(ActionRequest req) throws Exception {
167         long folderId = ParamUtil.getLong(req, "folderId");
168         String name = ParamUtil.getString(req, "name");
169         double version = ParamUtil.getDouble(req, "version");
170 
171         DLFileEntryServiceUtil.deleteFileEntry(folderId, name, version);
172     }
173 
174     protected void lockFileEntry(ActionRequest req) throws Exception {
175         long folderId = ParamUtil.getLong(req, "folderId");
176         String name = ParamUtil.getString(req, "name");
177 
178         DLFileEntryServiceUtil.lockFileEntry(folderId, name);
179     }
180 
181     protected void unlockFileEntry(ActionRequest req) throws Exception {
182         long folderId = ParamUtil.getLong(req, "folderId");
183         String name = ParamUtil.getString(req, "name");
184 
185         DLFileEntryServiceUtil.unlockFileEntry(folderId, name);
186     }
187 
188     protected void updateFileEntry(
189             FileEntryForm fileEntryForm, ActionRequest req, ActionResponse res)
190         throws Exception {
191 
192         UploadPortletRequest uploadReq =
193             UploadRequestUtil.getUploadPortletRequest(req);
194 
195         String cmd = ParamUtil.getString(uploadReq, Constants.CMD);
196 
197         ThemeDisplay themeDisplay =
198             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
199 
200         long folderId = ParamUtil.getLong(uploadReq, "folderId");
201         long newFolderId = ParamUtil.getLong(uploadReq, "newFolderId");
202         String name = ParamUtil.getString(uploadReq, "name");
203         String sourceFileName = uploadReq.getFileName("file");
204 
205         String title = ParamUtil.getString(uploadReq, "title");
206         String description = ParamUtil.getString(uploadReq, "description");
207 
208         String[] tagsEntries = StringUtil.split(
209             ParamUtil.getString(uploadReq, "tagsEntries"));
210 
211         String extraSettings = PropertiesUtil.toString(
212             fileEntryForm.getExtraSettingsProperties());
213 
214         File file = uploadReq.getFile("file");
215 
216         String[] communityPermissions = req.getParameterValues(
217             "communityPermissions");
218         String[] guestPermissions = req.getParameterValues(
219             "guestPermissions");
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                 req, DLFileEntry.class.getName(), entry.getFileEntryId(), -1);
236         }
237         else {
238 
239             // Update file entry
240 
241             DLFileEntryPermission.check(
242                 themeDisplay.getPermissionChecker(), folderId, name,
243                 ActionKeys.UPDATE);
244 
245             DLFileEntryLocalServiceUtil.updateFileEntry(
246                 themeDisplay.getUserId(), folderId, newFolderId, name,
247                 sourceFileName, title, description, tagsEntries, extraSettings,
248                 file);
249         }
250 
251         AssetPublisherUtil.addRecentFolderId(
252             req, DLFileEntry.class.getName(), folderId);
253     }
254 
255 }