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.repository.model.FileEntry;
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                            for (FileEntry fileEntry : wikiPage.getAttachmentsFileEntries()) {
072                                    String title = fileEntry.getTitle();
073    
074                                    if (title.equals(name)) {
075                                            return new Attachment(_engine, page.getName(), name);
076                                    }
077                            }
078    
079                            return null;
080                    }
081                    catch (Exception e) {
082                            throw new ProviderException(e.toString());
083                    }
084            }
085    
086            @Override
087            public String getProviderInfo() {
088                    return LiferayAttachmentProvider.class.getName();
089            }
090    
091            @Override
092            public List<Attachment> getVersionHistory(Attachment attachment) {
093                    List<Attachment> history = new ArrayList<Attachment>();
094    
095                    history.add(attachment);
096    
097                    return history;
098            }
099    
100            @Override
101            public void initialize(WikiEngine engine, Properties props) {
102                    _engine = engine;
103                    _nodeId = GetterUtil.getLong(props.getProperty("nodeId"));
104            }
105    
106            @Override
107            public List<Attachment> listAllChanged(Date timestamp) {
108                    return Collections.emptyList();
109            }
110    
111            @Override
112            public Collection<Attachment> listAttachments(WikiPage page) {
113                    return Collections.emptyList();
114            }
115    
116            @Override
117            public void moveAttachmentsForPage(String oldParent, String newParent) {
118            }
119    
120            @Override
121            public void putAttachmentData(Attachment attachment, InputStream data) {
122            }
123    
124            private static final InputStream _EMPTY_STREAM =
125                    new UnsyncByteArrayInputStream(new byte[0]);
126    
127            private WikiEngine _engine;
128            private long _nodeId;
129    
130    }