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