1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.upload.UploadPortletRequest;
19  import com.liferay.portal.kernel.util.Constants;
20  import com.liferay.portal.kernel.util.FileUtil;
21  import com.liferay.portal.kernel.util.ObjectValuePair;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portlet.wiki.NoSuchNodeException;
27  import com.liferay.portlet.wiki.NoSuchPageException;
28  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
29  
30  import java.io.File;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="EditPageAttachmentAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Jorge Ferrer
49   */
50  public class EditPageAttachmentAction extends PortletAction {
51  
52      public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              ActionRequest actionRequest, ActionResponse actionResponse)
55          throws Exception {
56  
57          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
58  
59          try {
60              if (cmd.equals(Constants.ADD)) {
61                  addAttachment(actionRequest);
62              }
63              else if (cmd.equals(Constants.DELETE)) {
64                  deleteAttachment(actionRequest);
65              }
66  
67              sendRedirect(actionRequest, actionResponse);
68          }
69          catch (Exception e) {
70              if (e instanceof NoSuchNodeException ||
71                  e instanceof NoSuchPageException ||
72                  e instanceof PrincipalException) {
73  
74                  SessionErrors.add(actionRequest, e.getClass().getName());
75  
76                  setForward(actionRequest, "portlet.wiki.error");
77              }
78              else {
79                  throw e;
80              }
81          }
82      }
83  
84      public ActionForward render(
85              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
86              RenderRequest renderRequest, RenderResponse renderResponse)
87          throws Exception {
88  
89          try {
90              ActionUtil.getNode(renderRequest);
91              ActionUtil.getPage(renderRequest);
92          }
93          catch (Exception e) {
94              if (e instanceof NoSuchNodeException ||
95                  e instanceof NoSuchPageException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(renderRequest, e.getClass().getName());
99  
100                 return mapping.findForward("portlet.wiki.error");
101             }
102             else {
103                 throw e;
104             }
105         }
106 
107         return mapping.findForward(
108             getForward(renderRequest, "portlet.wiki.edit_page_attachment"));
109     }
110 
111     protected void addAttachment(ActionRequest actionRequest) throws Exception {
112         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
113             actionRequest);
114 
115         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
116         String title = ParamUtil.getString(actionRequest, "title");
117 
118         int numOfFiles = ParamUtil.getInteger(actionRequest, "numOfFiles");
119 
120         List<ObjectValuePair<String, byte[]>> files =
121             new ArrayList<ObjectValuePair<String, byte[]>>();
122 
123         if (numOfFiles == 0) {
124             File file = uploadRequest.getFile("file");
125             String fileName = uploadRequest.getFileName("file");
126 
127             if (file != null) {
128                 byte[] bytes = FileUtil.getBytes(file);
129 
130                 if ((bytes != null) && (bytes.length > 0)) {
131                     ObjectValuePair<String, byte[]> ovp =
132                         new ObjectValuePair<String, byte[]>(fileName, bytes);
133 
134                     files.add(ovp);
135                 }
136             }
137         }
138         else {
139             for (int i = 1; i <= numOfFiles; i++) {
140                 File file = uploadRequest.getFile("file" + i);
141 
142                 String fileName = uploadRequest.getFileName("file" + i);
143 
144                 if (file != null) {
145                     byte[] bytes = FileUtil.getBytes(file);
146 
147                     if ((bytes != null) && (bytes.length > 0)) {
148                         ObjectValuePair<String, byte[]> ovp =
149                             new ObjectValuePair<String, byte[]>(
150                                 fileName, bytes);
151 
152                         files.add(ovp);
153                     }
154                 }
155             }
156         }
157 
158         WikiPageServiceUtil.addPageAttachments(nodeId, title, files);
159     }
160 
161     protected void deleteAttachment(ActionRequest actionRequest)
162         throws Exception {
163 
164         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
165         String title = ParamUtil.getString(actionRequest, "title");
166         String attachment = ParamUtil.getString(actionRequest, "fileName");
167 
168         WikiPageServiceUtil.deletePageAttachment(nodeId, title, attachment);
169     }
170 
171 }