001    /**
002     * Copyright (c) 2000-2012 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.util.ObjectValuePair;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.model.CompanyConstants;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
027    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
028    import com.liferay.portlet.wiki.model.WikiPage;
029    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
030    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
031    
032    import java.io.InputStream;
033    
034    import java.util.ArrayList;
035    import java.util.List;
036    
037    import javax.servlet.http.HttpServletRequest;
038    
039    import org.w3c.dom.Document;
040    import org.w3c.dom.Element;
041    import org.w3c.dom.Node;
042    
043    /**
044     * @author Julio Camarero
045     */
046    public class AttachmentCommandReceiver extends BaseCommandReceiver {
047    
048            @Override
049            protected String createFolder(CommandArgument commandArgument) {
050                    return "0";
051            }
052    
053            @Override
054            protected String fileUpload(
055                    CommandArgument commandArgument, String fileName,
056                    InputStream inputStream, String extension, long size) {
057    
058                    try {
059                            HttpServletRequest request =
060                                    commandArgument.getHttpServletRequest();
061    
062                            long resourcePK = ParamUtil.getLong(
063                                    request, "wikiPageResourcePrimKey");
064    
065                            WikiPage page = WikiPageLocalServiceUtil.getPage(resourcePK);
066    
067                            String title = page.getTitle();
068    
069                            long nodeId = page.getNodeId();
070    
071                            List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
072                                    new ArrayList<ObjectValuePair<String, InputStream>>(1);
073    
074                            ObjectValuePair<String, InputStream> inputStreamOVP =
075                                    new ObjectValuePair<String, InputStream>(fileName, inputStream);
076    
077                            inputStreamOVPs.add(inputStreamOVP);
078    
079                            WikiPageServiceUtil.addPageAttachments(
080                                    nodeId, title, inputStreamOVPs);
081                    }
082                    catch (Exception e) {
083                            throw new FCKException(e);
084                    }
085    
086                    return "0";
087            }
088    
089            @Override
090            protected void getFolders(
091                    CommandArgument commandArgument, Document document, Node rootNode) {
092            }
093    
094            @Override
095            protected void getFoldersAndFiles(
096                    CommandArgument commandArgument, Document document, Node rootNode) {
097    
098                    try {
099                            _getFiles(commandArgument, document, rootNode);
100                    }
101                    catch (Exception e) {
102                            throw new FCKException(e);
103                    }
104            }
105    
106            @Override
107            protected boolean isStagedData(Group group) {
108                    return group.isStagedPortlet(PortletKeys.WIKI);
109            }
110    
111            private void _getFiles(
112                            CommandArgument commandArgument, Document document, Node rootNode)
113                    throws Exception {
114    
115                    Element filesElement = document.createElement("Files");
116    
117                    rootNode.appendChild(filesElement);
118    
119                    HttpServletRequest request = commandArgument.getHttpServletRequest();
120    
121                    long wikiPageResourcePrimKey = ParamUtil.getLong(
122                            request, "wikiPageResourcePrimKey");
123    
124                    WikiPage wikiPage = WikiPageLocalServiceUtil.getPage(
125                            wikiPageResourcePrimKey);
126    
127                    long repositoryId = CompanyConstants.SYSTEM;
128    
129                    String dirName = wikiPage.getAttachmentsDir();
130    
131                    String[] fileNames = null;
132    
133                    try {
134                            fileNames = DLStoreUtil.getFileNames(
135                                    wikiPage.getCompanyId(), repositoryId, dirName);
136                    }
137                    catch (NoSuchDirectoryException nsde) {
138                            DLStoreUtil.addDirectory(
139                                    wikiPage.getCompanyId(), repositoryId, dirName);
140    
141                            fileNames = DLStoreUtil.getFileNames(
142                                    wikiPage.getCompanyId(), repositoryId, dirName);
143                    }
144    
145                    String attachmentURLPrefix = ParamUtil.getString(
146                            request, "attachmentURLPrefix");
147    
148                    for (String fileName : fileNames) {
149                            byte[] fileEntry = DLStoreUtil.getFileAsBytes(
150                                    wikiPage.getCompanyId(), repositoryId, fileName);
151    
152                            String[] parts = StringUtil.split(fileName, StringPool.SLASH);
153    
154                            fileName = parts[3];
155    
156                            Element fileElement = document.createElement("File");
157    
158                            filesElement.appendChild(fileElement);
159    
160                            fileElement.setAttribute("name", fileName);
161                            fileElement.setAttribute("desc", fileName);
162                            fileElement.setAttribute("size", getSize(fileEntry.length));
163                            fileElement.setAttribute("url", attachmentURLPrefix + fileName);
164                    }
165            }
166    
167    }