1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.action;
21  
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.FileUtil;
26  import com.liferay.portal.kernel.util.ObjectValuePair;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.wiki.NoSuchNodeException;
32  import com.liferay.portlet.wiki.NoSuchPageException;
33  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
34  
35  import java.io.File;
36  
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="EditPageAttachmentAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Jorge Ferrer
54   *
55   */
56  public class EditPageAttachmentAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
60              ActionRequest actionRequest, ActionResponse actionResponse)
61          throws Exception {
62  
63          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
64  
65          try {
66              if (cmd.equals(Constants.ADD)) {
67                  addAttachment(actionRequest);
68              }
69              else if (cmd.equals(Constants.DELETE)) {
70                  deleteAttachment(actionRequest);
71              }
72  
73              sendRedirect(actionRequest, actionResponse);
74          }
75          catch (Exception e) {
76              if (e instanceof NoSuchNodeException ||
77                  e instanceof NoSuchPageException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  setForward(actionRequest, "portlet.wiki.error");
83              }
84              else {
85                  throw e;
86              }
87          }
88      }
89  
90      public ActionForward render(
91              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
92              RenderRequest renderRequest, RenderResponse renderResponse)
93          throws Exception {
94  
95          try {
96              ActionUtil.getNode(renderRequest);
97              ActionUtil.getPage(renderRequest);
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchNodeException ||
101                 e instanceof NoSuchPageException ||
102                 e instanceof PrincipalException) {
103 
104                 SessionErrors.add(renderRequest, e.getClass().getName());
105 
106                 return mapping.findForward("portlet.wiki.error");
107             }
108             else {
109                 throw e;
110             }
111         }
112 
113         return mapping.findForward(
114             getForward(renderRequest, "portlet.wiki.edit_page_attachment"));
115     }
116 
117     protected void addAttachment(ActionRequest actionRequest) throws Exception {
118         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
119             actionRequest);
120 
121         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
122         String title = ParamUtil.getString(actionRequest, "title");
123 
124         int numOfFiles = ParamUtil.getInteger(actionRequest, "numOfFiles");
125 
126         List<ObjectValuePair<String, byte[]>> files =
127             new ArrayList<ObjectValuePair<String, byte[]>>();
128 
129         if (numOfFiles == 0) {
130             File file = uploadRequest.getFile("file");
131             String fileName = uploadRequest.getFileName("file");
132 
133             if (file != null) {
134                 byte[] bytes = FileUtil.getBytes(file);
135 
136                 if ((bytes != null) && (bytes.length > 0)) {
137                     ObjectValuePair<String, byte[]> ovp =
138                         new ObjectValuePair<String, byte[]>(fileName, bytes);
139 
140                     files.add(ovp);
141                 }
142             }
143         }
144         else {
145             for (int i = 1; i <= numOfFiles; i++) {
146                 File file = uploadRequest.getFile("file" + i);
147 
148                 String fileName = uploadRequest.getFileName("file" + i);
149 
150                 if (file != null) {
151                     byte[] bytes = FileUtil.getBytes(file);
152 
153                     if ((bytes != null) && (bytes.length > 0)) {
154                         ObjectValuePair<String, byte[]> ovp =
155                             new ObjectValuePair<String, byte[]>(
156                                 fileName, bytes);
157 
158                         files.add(ovp);
159                     }
160                 }
161             }
162         }
163 
164         WikiPageServiceUtil.addPageAttachments(nodeId, title, files);
165     }
166 
167     protected void deleteAttachment(ActionRequest actionRequest)
168         throws Exception {
169 
170         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
171         String title = ParamUtil.getString(actionRequest, "title");
172         String attachment = ParamUtil.getString(actionRequest, "fileName");
173 
174         WikiPageServiceUtil.deletePageAttachment(nodeId, title, attachment);
175     }
176 
177 }