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