001    /**
002     * Copyright (c) 2000-2013 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.portal.editor.fckeditor.receiver.impl;
016    
017    import com.liferay.portal.editor.fckeditor.command.CommandArgument;
018    import com.liferay.portal.editor.fckeditor.exception.FCKException;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.util.ObjectValuePair;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portlet.wiki.model.WikiPage;
025    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
026    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
027    
028    import java.io.InputStream;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    import org.w3c.dom.Document;
036    import org.w3c.dom.Element;
037    import org.w3c.dom.Node;
038    
039    /**
040     * @author Julio Camarero
041     */
042    public class AttachmentCommandReceiver extends BaseCommandReceiver {
043    
044            @Override
045            protected String createFolder(CommandArgument commandArgument) {
046                    return "0";
047            }
048    
049            @Override
050            protected String fileUpload(
051                    CommandArgument commandArgument, String fileName,
052                    InputStream inputStream, String extension, long size) {
053    
054                    try {
055                            HttpServletRequest request =
056                                    commandArgument.getHttpServletRequest();
057    
058                            long resourcePK = ParamUtil.getLong(
059                                    request, "wikiPageResourcePrimKey");
060    
061                            WikiPage page = WikiPageLocalServiceUtil.getPage(resourcePK);
062    
063                            String title = page.getTitle();
064    
065                            long nodeId = page.getNodeId();
066    
067                            List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
068                                    new ArrayList<ObjectValuePair<String, InputStream>>(1);
069    
070                            ObjectValuePair<String, InputStream> inputStreamOVP =
071                                    new ObjectValuePair<String, InputStream>(fileName, inputStream);
072    
073                            inputStreamOVPs.add(inputStreamOVP);
074    
075                            WikiPageServiceUtil.addPageAttachments(
076                                    nodeId, title, inputStreamOVPs);
077                    }
078                    catch (Exception e) {
079                            throw new FCKException(e);
080                    }
081    
082                    return "0";
083            }
084    
085            @Override
086            protected void getFolders(
087                    CommandArgument commandArgument, Document document, Node rootNode) {
088            }
089    
090            @Override
091            protected void getFoldersAndFiles(
092                    CommandArgument commandArgument, Document document, Node rootNode) {
093    
094                    try {
095                            _getFiles(commandArgument, document, rootNode);
096                    }
097                    catch (Exception e) {
098                            throw new FCKException(e);
099                    }
100            }
101    
102            @Override
103            protected boolean isStagedData(Group group) {
104                    return group.isStagedPortlet(PortletKeys.WIKI);
105            }
106    
107            private void _getFiles(
108                            CommandArgument commandArgument, Document document, Node rootNode)
109                    throws Exception {
110    
111                    Element filesElement = document.createElement("Files");
112    
113                    rootNode.appendChild(filesElement);
114    
115                    HttpServletRequest request = commandArgument.getHttpServletRequest();
116    
117                    long wikiPageResourcePrimKey = ParamUtil.getLong(
118                            request, "wikiPageResourcePrimKey");
119    
120                    WikiPage wikiPage = WikiPageLocalServiceUtil.getPage(
121                            wikiPageResourcePrimKey);
122    
123                    String attachmentURLPrefix = ParamUtil.getString(
124                            request, "attachmentURLPrefix");
125    
126                    for (FileEntry fileEntry : wikiPage.getAttachmentsFileEntries()) {
127                            Element fileElement = document.createElement("File");
128    
129                            filesElement.appendChild(fileElement);
130    
131                            fileElement.setAttribute("name", fileEntry.getTitle());
132                            fileElement.setAttribute("desc", fileEntry.getTitle());
133                            fileElement.setAttribute(
134                                    "size", String.valueOf(fileEntry.getSize()));
135                            fileElement.setAttribute(
136                                    "url", attachmentURLPrefix + fileEntry.getTitle());
137                    }
138            }
139    
140    }