001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.action;
016    
017    import com.liferay.documentlibrary.DuplicateFileException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.upload.UploadPortletRequest;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.ObjectValuePair;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.wiki.NoSuchNodeException;
028    import com.liferay.portlet.wiki.NoSuchPageException;
029    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
030    
031    import java.io.File;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    
036    import javax.portlet.ActionRequest;
037    import javax.portlet.ActionResponse;
038    import javax.portlet.PortletConfig;
039    import javax.portlet.RenderRequest;
040    import javax.portlet.RenderResponse;
041    
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Jorge Ferrer
048     */
049    public class EditPageAttachmentAction extends PortletAction {
050    
051            public void processAction(
052                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
053                            ActionRequest actionRequest, ActionResponse actionResponse)
054                    throws Exception {
055    
056                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057    
058                    try {
059                            if (cmd.equals(Constants.ADD)) {
060                                    addAttachment(actionRequest);
061                            }
062                            else if (cmd.equals(Constants.DELETE)) {
063                                    deleteAttachment(actionRequest);
064                            }
065    
066                            sendRedirect(actionRequest, actionResponse);
067                    }
068                    catch (Exception e) {
069                            if (e instanceof DuplicateFileException ||
070                                    e instanceof NoSuchNodeException ||
071                                    e instanceof NoSuchPageException ||
072                                    e instanceof PrincipalException) {
073    
074                                    SessionErrors.add(actionRequest, e.getClass().getName());
075    
076                                    setForward(actionRequest, "portlet.wiki.error");
077                            }
078                            else {
079                                    throw e;
080                            }
081                    }
082            }
083    
084            public ActionForward render(
085                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
086                            RenderRequest renderRequest, RenderResponse renderResponse)
087                    throws Exception {
088    
089                    try {
090                            ActionUtil.getNode(renderRequest);
091                            ActionUtil.getPage(renderRequest);
092                    }
093                    catch (Exception e) {
094                            if (e instanceof NoSuchNodeException ||
095                                    e instanceof NoSuchPageException ||
096                                    e instanceof PrincipalException) {
097    
098                                    SessionErrors.add(renderRequest, e.getClass().getName());
099    
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    }