1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.documentlibrary.action;
16  
17  import com.liferay.documentlibrary.DuplicateFileException;
18  import com.liferay.documentlibrary.FileNameException;
19  import com.liferay.documentlibrary.FileSizeException;
20  import com.liferay.documentlibrary.SourceFileNameException;
21  import com.liferay.portal.DuplicateLockException;
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.upload.UploadPortletRequest;
24  import com.liferay.portal.kernel.util.Constants;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.PropertiesParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.UnicodeProperties;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
34  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
35  import com.liferay.portlet.documentlibrary.NoSuchFolderException;
36  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
37  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
38  import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
39  import com.liferay.portlet.tags.TagsEntryException;
40  
41  import java.io.File;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="EditFileEntryAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   * @author Alexander Chow
58   */
59  public class EditFileEntryAction extends PortletAction {
60  
61      public void processAction(
62              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
63              ActionRequest actionRequest, ActionResponse actionResponse)
64          throws Exception {
65  
66          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
67  
68          try {
69              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
70                  updateFileEntry(actionRequest, actionResponse);
71              }
72              else if (cmd.equals(Constants.DELETE)) {
73                  deleteFileEntry(actionRequest);
74              }
75              else if (cmd.equals(Constants.LOCK)) {
76                  lockFileEntry(actionRequest);
77              }
78              else if (cmd.equals(Constants.UNLOCK)) {
79                  unlockFileEntry(actionRequest);
80              }
81  
82              sendRedirect(actionRequest, actionResponse);
83          }
84          catch (Exception e) {
85              if (e instanceof DuplicateLockException ||
86                  e instanceof NoSuchFileEntryException ||
87                  e instanceof PrincipalException) {
88  
89                  if (e instanceof DuplicateLockException) {
90                      DuplicateLockException dle = (DuplicateLockException)e;
91  
92                      SessionErrors.add(
93                          actionRequest, dle.getClass().getName(), dle.getLock());
94                  }
95                  else {
96                      SessionErrors.add(actionRequest, e.getClass().getName());
97                  }
98  
99                  setForward(actionRequest, "portlet.document_library.error");
100             }
101             else if (e instanceof DuplicateFileException ||
102                      e instanceof DuplicateFolderNameException ||
103                      e instanceof FileNameException ||
104                      e instanceof FileSizeException ||
105                      e instanceof NoSuchFolderException ||
106                      e instanceof SourceFileNameException) {
107 
108                 SessionErrors.add(actionRequest, e.getClass().getName());
109             }
110             else if (e instanceof TagsEntryException) {
111                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
112             }
113             else {
114                 throw e;
115             }
116         }
117     }
118 
119     public ActionForward render(
120             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
121             RenderRequest renderRequest, RenderResponse renderResponse)
122         throws Exception {
123 
124         try {
125             ActionUtil.getFileEntry(renderRequest);
126         }
127         catch (Exception e) {
128             if (e instanceof NoSuchFileEntryException ||
129                 e instanceof PrincipalException) {
130 
131                 SessionErrors.add(renderRequest, e.getClass().getName());
132 
133                 return mapping.findForward("portlet.document_library.error");
134             }
135             else {
136                 throw e;
137             }
138         }
139 
140         String forward = "portlet.document_library.edit_file_entry";
141 
142         return mapping.findForward(getForward(renderRequest, forward));
143     }
144 
145     protected void deleteFileEntry(ActionRequest actionRequest)
146         throws Exception {
147 
148         long folderId = ParamUtil.getLong(actionRequest, "folderId");
149         String name = ParamUtil.getString(actionRequest, "name");
150         double version = ParamUtil.getDouble(actionRequest, "version");
151 
152         DLFileEntryServiceUtil.deleteFileEntry(folderId, name, version);
153     }
154 
155     protected void lockFileEntry(ActionRequest actionRequest) throws Exception {
156         long folderId = ParamUtil.getLong(actionRequest, "folderId");
157         String name = ParamUtil.getString(actionRequest, "name");
158 
159         DLFileEntryServiceUtil.lockFileEntry(folderId, name);
160     }
161 
162     protected void unlockFileEntry(ActionRequest actionRequest)
163         throws Exception {
164 
165         long folderId = ParamUtil.getLong(actionRequest, "folderId");
166         String name = ParamUtil.getString(actionRequest, "name");
167 
168         DLFileEntryServiceUtil.unlockFileEntry(folderId, name);
169     }
170 
171     protected void updateFileEntry(
172             ActionRequest actionRequest, ActionResponse actionResponse)
173         throws Exception {
174 
175         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
176             actionRequest);
177 
178         String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
179 
180         long folderId = ParamUtil.getLong(uploadRequest, "folderId");
181         long newFolderId = ParamUtil.getLong(uploadRequest, "newFolderId");
182         String name = ParamUtil.getString(uploadRequest, "name");
183         String sourceFileName = uploadRequest.getFileName("file");
184 
185         String title = ParamUtil.getString(uploadRequest, "title");
186         String description = ParamUtil.getString(uploadRequest, "description");
187 
188         String[] tagsEntries = StringUtil.split(
189             ParamUtil.getString(uploadRequest, "tagsEntries"));
190 
191         UnicodeProperties extraSettingsProperties =
192             PropertiesParamUtil.getProperties(
193                 actionRequest, "extraSettingsProperties(");
194 
195         String extraSettings = extraSettingsProperties.toString();
196 
197         File file = uploadRequest.getFile("file");
198 
199         if (Validator.isNotNull(sourceFileName) && !file.exists()) {
200             file.createNewFile();
201         }
202 
203         String[] communityPermissions = PortalUtil.getCommunityPermissions(
204             actionRequest);
205         String[] guestPermissions = PortalUtil.getGuestPermissions(
206             actionRequest);
207 
208         if (cmd.equals(Constants.ADD)) {
209 
210             // Add file entry
211 
212             DLFileEntry fileEntry = DLFileEntryServiceUtil.addFileEntry(
213                 folderId, sourceFileName, title, description, tagsEntries,
214                 extraSettings, file, communityPermissions, guestPermissions);
215 
216             AssetPublisherUtil.addAndStoreSelection(
217                 actionRequest, DLFileEntry.class.getName(),
218                 fileEntry.getFileEntryId(), -1);
219         }
220         else {
221 
222             // Update file entry
223 
224             DLFileEntryServiceUtil.updateFileEntry(
225                 folderId, newFolderId, name, sourceFileName, title, description,
226                 tagsEntries, extraSettings, file);
227         }
228 
229         AssetPublisherUtil.addRecentFolderId(
230             actionRequest, DLFileEntry.class.getName(), folderId);
231     }
232 
233 }