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.portlet.wiki.engines.jspwiki;
016    
017    import com.ecyrd.jspwiki.QueryItem;
018    import com.ecyrd.jspwiki.WikiEngine;
019    import com.ecyrd.jspwiki.WikiPage;
020    import com.ecyrd.jspwiki.attachment.Attachment;
021    import com.ecyrd.jspwiki.providers.ProviderException;
022    import com.ecyrd.jspwiki.providers.WikiAttachmentProvider;
023    
024    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
025    import com.liferay.portal.kernel.util.FileUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
028    
029    import java.io.InputStream;
030    
031    import java.util.ArrayList;
032    import java.util.Collection;
033    import java.util.Collections;
034    import java.util.Date;
035    import java.util.List;
036    import java.util.Properties;
037    
038    /**
039     * @author Jorge Ferrer
040     */
041    public class LiferayAttachmentProvider implements WikiAttachmentProvider {
042    
043            @Override
044            public void deleteAttachment(Attachment attachment) {
045            }
046    
047            @Override
048            public void deleteVersion(Attachment attachment) {
049            }
050    
051            @Override
052            public Collection<Attachment> findAttachments(QueryItem[] query) {
053                    return Collections.emptyList();
054            }
055    
056            @Override
057            public InputStream getAttachmentData(Attachment attachment) {
058                    return _EMPTY_STREAM;
059            }
060    
061            @Override
062            public Attachment getAttachmentInfo(WikiPage page, String name, int version)
063                    throws ProviderException {
064    
065                    com.liferay.portlet.wiki.model.WikiPage wikiPage = null;
066    
067                    try {
068                            wikiPage = WikiPageLocalServiceUtil.getPage(
069                                    _nodeId, page.getName());
070    
071                            String[] attachments = wikiPage.getAttachmentsFiles();
072    
073                            for (int i = 0; i < attachments.length; i++) {
074                                    String fileName = FileUtil.getShortFileName(attachments[i]);
075    
076                                    if (fileName.equals(name)) {
077                                            return new Attachment(_engine, page.getName(), name);
078                                    }
079                            }
080    
081                            return null;
082                    }
083                    catch (Exception e) {
084                            throw new ProviderException(e.toString());
085                    }
086            }
087    
088            @Override
089            public String getProviderInfo() {
090                    return LiferayAttachmentProvider.class.getName();
091            }
092    
093            @Override
094            public List<Attachment> getVersionHistory(Attachment attachment) {
095                    List<Attachment> history = new ArrayList<Attachment>();
096    
097                    history.add(attachment);
098    
099                    return history;
100            }
101    
102            @Override
103            public void initialize(WikiEngine engine, Properties props) {
104                    _engine = engine;
105                    _nodeId = GetterUtil.getLong(props.getProperty("nodeId"));
106            }
107    
108            @Override
109            public List<Attachment> listAllChanged(Date timestamp) {
110                    return Collections.emptyList();
111            }
112    
113            @Override
114            public Collection<Attachment> listAttachments(WikiPage page) {
115                    return Collections.emptyList();
116            }
117    
118            @Override
119            public void moveAttachmentsForPage(String oldParent, String newParent) {
120            }
121    
122            @Override
123            public void putAttachmentData(Attachment attachment, InputStream data) {
124            }
125    
126            private static final InputStream _EMPTY_STREAM =
127                    new UnsyncByteArrayInputStream(new byte[0]);
128    
129            private WikiEngine _engine;
130            private long _nodeId;
131    
132    }