1   /**
2    * Copyright (c) 2000-2008 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.util.Constants;
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.UploadRequestUtil;
31  import com.liferay.portlet.wiki.NoSuchNodeException;
32  import com.liferay.portlet.wiki.NoSuchPageException;
33  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
34  import com.liferay.util.FileUtil;
35  import com.liferay.util.servlet.SessionErrors;
36  import com.liferay.util.servlet.UploadPortletRequest;
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 config,
63              ActionRequest req, ActionResponse res)
64          throws Exception {
65  
66          String cmd = ParamUtil.getString(req, Constants.CMD);
67  
68          try {
69              if (cmd.equals(Constants.ADD)) {
70                  addAttachment(req);
71              }
72              else if (cmd.equals(Constants.DELETE)) {
73                  deleteAttachment(req);
74              }
75  
76              sendRedirect(req, res);
77          }
78          catch (Exception e) {
79              if (e instanceof NoSuchNodeException ||
80                  e instanceof NoSuchPageException ||
81                  e instanceof PrincipalException) {
82  
83                  SessionErrors.add(req, e.getClass().getName());
84  
85                  setForward(req, "portlet.wiki.error");
86              }
87              else {
88                  throw e;
89              }
90          }
91      }
92  
93      public ActionForward render(
94              ActionMapping mapping, ActionForm form, PortletConfig config,
95              RenderRequest req, RenderResponse res)
96          throws Exception {
97  
98          try {
99              ActionUtil.getNode(req);
100             ActionUtil.getPage(req);
101         }
102         catch (Exception e) {
103             if (e instanceof NoSuchNodeException ||
104                 e instanceof NoSuchPageException ||
105                 e instanceof PrincipalException) {
106 
107                 SessionErrors.add(req, 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(req, "portlet.wiki.edit_page_attachment"));
118     }
119 
120     protected void deleteAttachment(ActionRequest req) throws Exception {
121         long nodeId = ParamUtil.getLong(req, "nodeId");
122         String title = ParamUtil.getString(req, "title");
123         String attachment = ParamUtil.getString(req, "fileName");
124 
125         WikiPageServiceUtil.deletePageAttachment(nodeId, title, attachment);
126     }
127 
128     protected void addAttachment(ActionRequest req) throws Exception {
129         UploadPortletRequest uploadReq =
130             UploadRequestUtil.getUploadPortletRequest(req);
131 
132         long nodeId = ParamUtil.getLong(req, "nodeId");
133         String title = ParamUtil.getString(req, "title");
134         int numOfFiles = ParamUtil.getInteger(req, "numOfFiles");
135 
136         List<ObjectValuePair<String, byte[]>> files =
137             new ArrayList<ObjectValuePair<String, byte[]>>();
138 
139         if (numOfFiles == 0) {
140             File file = uploadReq.getFile("file");
141             String fileName = uploadReq.getFileName("file");
142 
143             if (file != null) {
144                 byte[] bytes = FileUtil.getBytes(file);
145 
146                 if ((bytes != null) && (bytes.length > 0)) {
147                     ObjectValuePair<String, byte[]> ovp =
148                         new ObjectValuePair<String, byte[]>(fileName, bytes);
149 
150                     files.add(ovp);
151                 }
152             }
153         }
154         else {
155             for (int i = 1; i <= numOfFiles; i++) {
156                 File file = uploadReq.getFile("file" + i);
157 
158                 String fileName = uploadReq.getFileName("file" + i);
159 
160                 if (file != null) {
161                     byte[] bytes = FileUtil.getBytes(file);
162 
163                     if ((bytes != null) && (bytes.length > 0)) {
164                         ObjectValuePair<String, byte[]> ovp =
165                             new ObjectValuePair<String, byte[]>(
166                                 fileName, bytes);
167 
168                         files.add(ovp);
169                     }
170                 }
171             }
172         }
173 
174         WikiPageServiceUtil.addPageAttachments(nodeId, title, files);
175     }
176 
177 }