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.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  public class EditPageAttachmentAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
66  
67          try {
68              if (cmd.equals(Constants.ADD)) {
69                  addAttachment(actionRequest);
70              }
71              else if (cmd.equals(Constants.DELETE)) {
72                  deleteAttachment(actionRequest);
73              }
74  
75              sendRedirect(actionRequest, actionResponse);
76          }
77          catch (Exception e) {
78              if (e instanceof NoSuchNodeException ||
79                  e instanceof NoSuchPageException ||
80                  e instanceof PrincipalException) {
81  
82                  SessionErrors.add(actionRequest, e.getClass().getName());
83  
84                  setForward(actionRequest, "portlet.wiki.error");
85              }
86              else {
87                  throw e;
88              }
89          }
90      }
91  
92      public ActionForward render(
93              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
94              RenderRequest renderRequest, RenderResponse renderResponse)
95          throws Exception {
96  
97          try {
98              ActionUtil.getNode(renderRequest);
99              ActionUtil.getPage(renderRequest);
100         }
101         catch (Exception e) {
102             if (e instanceof NoSuchNodeException ||
103                 e instanceof NoSuchPageException ||
104                 e instanceof PrincipalException) {
105 
106                 SessionErrors.add(renderRequest, e.getClass().getName());
107 
108                 return mapping.findForward("portlet.wiki.error");
109             }
110             else {
111                 throw e;
112             }
113         }
114 
115         return mapping.findForward(
116             getForward(renderRequest, "portlet.wiki.edit_page_attachment"));
117     }
118 
119     protected void addAttachment(ActionRequest actionRequest) throws Exception {
120         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
121             actionRequest);
122 
123         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
124         String title = ParamUtil.getString(actionRequest, "title");
125 
126         int numOfFiles = ParamUtil.getInteger(actionRequest, "numOfFiles");
127 
128         List<ObjectValuePair<String, byte[]>> files =
129             new ArrayList<ObjectValuePair<String, byte[]>>();
130 
131         if (numOfFiles == 0) {
132             File file = uploadRequest.getFile("file");
133             String fileName = uploadRequest.getFileName("file");
134 
135             if (file != null) {
136                 byte[] bytes = FileUtil.getBytes(file);
137 
138                 if ((bytes != null) && (bytes.length > 0)) {
139                     ObjectValuePair<String, byte[]> ovp =
140                         new ObjectValuePair<String, byte[]>(fileName, bytes);
141 
142                     files.add(ovp);
143                 }
144             }
145         }
146         else {
147             for (int i = 1; i <= numOfFiles; i++) {
148                 File file = uploadRequest.getFile("file" + i);
149 
150                 String fileName = uploadRequest.getFileName("file" + i);
151 
152                 if (file != null) {
153                     byte[] bytes = FileUtil.getBytes(file);
154 
155                     if ((bytes != null) && (bytes.length > 0)) {
156                         ObjectValuePair<String, byte[]> ovp =
157                             new ObjectValuePair<String, byte[]>(
158                                 fileName, bytes);
159 
160                         files.add(ovp);
161                     }
162                 }
163             }
164         }
165 
166         WikiPageServiceUtil.addPageAttachments(nodeId, title, files);
167     }
168 
169     protected void deleteAttachment(ActionRequest actionRequest)
170         throws Exception {
171 
172         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
173         String title = ParamUtil.getString(actionRequest, "title");
174         String attachment = ParamUtil.getString(actionRequest, "fileName");
175 
176         WikiPageServiceUtil.deletePageAttachment(nodeId, title, attachment);
177     }
178 
179 }