1   /**
2    * Copyright (c) 2000-2009 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.wiki.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.upload.UploadPortletRequest;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.ObjectValuePair;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portlet.wiki.NoSuchNodeException;
35  import com.liferay.portlet.wiki.NoSuchPageException;
36  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
37  
38  import java.io.File;
39  
40  import java.util.ArrayList;
41  import java.util.List;
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="EditPageAttachmentAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Jorge Ferrer
57   *
58   */
59  public class EditPageAttachmentAction 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)) {
70                  addAttachment(actionRequest);
71              }
72              else if (cmd.equals(Constants.DELETE)) {
73                  deleteAttachment(actionRequest);
74              }
75  
76              sendRedirect(actionRequest, actionResponse);
77          }
78          catch (Exception e) {
79              if (e instanceof NoSuchNodeException ||
80                  e instanceof NoSuchPageException ||
81                  e instanceof PrincipalException) {
82  
83                  SessionErrors.add(actionRequest, e.getClass().getName());
84  
85                  setForward(actionRequest, "portlet.wiki.error");
86              }
87              else {
88                  throw e;
89              }
90          }
91      }
92  
93      public ActionForward render(
94              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
95              RenderRequest renderRequest, RenderResponse renderResponse)
96          throws Exception {
97  
98          try {
99              ActionUtil.getNode(renderRequest);
100             ActionUtil.getPage(renderRequest);
101         }
102         catch (Exception e) {
103             if (e instanceof NoSuchNodeException ||
104                 e instanceof NoSuchPageException ||
105                 e instanceof PrincipalException) {
106 
107                 SessionErrors.add(renderRequest, e.getClass().getName());
108 
109                 return mapping.findForward("portlet.wiki.error");
110             }
111             else {
112                 throw e;
113             }
114         }
115 
116         return mapping.findForward(
117             getForward(renderRequest, "portlet.wiki.edit_page_attachment"));
118     }
119 
120     protected void addAttachment(ActionRequest actionRequest) throws Exception {
121         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
122             actionRequest);
123 
124         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
125         String title = ParamUtil.getString(actionRequest, "title");
126 
127         int numOfFiles = ParamUtil.getInteger(actionRequest, "numOfFiles");
128 
129         List<ObjectValuePair<String, byte[]>> files =
130             new ArrayList<ObjectValuePair<String, byte[]>>();
131 
132         if (numOfFiles == 0) {
133             File file = uploadRequest.getFile("file");
134             String fileName = uploadRequest.getFileName("file");
135 
136             if (file != null) {
137                 byte[] bytes = FileUtil.getBytes(file);
138 
139                 if ((bytes != null) && (bytes.length > 0)) {
140                     ObjectValuePair<String, byte[]> ovp =
141                         new ObjectValuePair<String, byte[]>(fileName, bytes);
142 
143                     files.add(ovp);
144                 }
145             }
146         }
147         else {
148             for (int i = 1; i <= numOfFiles; i++) {
149                 File file = uploadRequest.getFile("file" + i);
150 
151                 String fileName = uploadRequest.getFileName("file" + i);
152 
153                 if (file != null) {
154                     byte[] bytes = FileUtil.getBytes(file);
155 
156                     if ((bytes != null) && (bytes.length > 0)) {
157                         ObjectValuePair<String, byte[]> ovp =
158                             new ObjectValuePair<String, byte[]>(
159                                 fileName, bytes);
160 
161                         files.add(ovp);
162                     }
163                 }
164             }
165         }
166 
167         WikiPageServiceUtil.addPageAttachments(nodeId, title, files);
168     }
169 
170     protected void deleteAttachment(ActionRequest actionRequest)
171         throws Exception {
172 
173         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
174         String title = ParamUtil.getString(actionRequest, "title");
175         String attachment = ParamUtil.getString(actionRequest, "fileName");
176 
177         WikiPageServiceUtil.deletePageAttachment(nodeId, title, attachment);
178     }
179 
180 }