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            public void deleteAttachment(Attachment attachment) {
044            }
045    
046            public void deleteVersion(Attachment attachment) {
047            }
048    
049            public Collection<Attachment> findAttachments(QueryItem[] query) {
050                    return Collections.emptyList();
051            }
052    
053            public InputStream getAttachmentData(Attachment attachment) {
054                    return _EMPTY_STREAM;
055            }
056    
057            public Attachment getAttachmentInfo(WikiPage page, String name, int version)
058                    throws ProviderException {
059    
060                    com.liferay.portlet.wiki.model.WikiPage wikiPage = null;
061    
062                    try {
063                            wikiPage = WikiPageLocalServiceUtil.getPage(
064                                    _nodeId, page.getName());
065    
066                            for (FileEntry fileEntry : wikiPage.getAttachmentsFileEntries()) {
067                                    String title = fileEntry.getTitle();
068    
069                                    if (title.equals(name)) {
070                                            return new Attachment(_engine, page.getName(), name);
071                                    }
072                            }
073    
074                            return null;
075                    }
076                    catch (Exception e) {
077                            throw new ProviderException(e.toString());
078                    }
079            }
080    
081            public String getProviderInfo() {
082                    return LiferayAttachmentProvider.class.getName();
083            }
084    
085            public List<Attachment> getVersionHistory(Attachment attachment) {
086                    List<Attachment> history = new ArrayList<Attachment>();
087    
088                    history.add(attachment);
089    
090                    return history;
091            }
092    
093            public void initialize(WikiEngine engine, Properties props) {
094                    _engine = engine;
095                    _nodeId = GetterUtil.getLong(props.getProperty("nodeId"));
096            }
097    
098            public List<Attachment> listAllChanged(Date timestamp) {
099                    return Collections.emptyList();
100            }
101    
102            public Collection<Attachment> listAttachments(WikiPage page) {
103                    return Collections.emptyList();
104            }
105    
106            public void moveAttachmentsForPage(String oldParent, String newParent) {
107            }
108    
109            public void putAttachmentData(Attachment attachment, InputStream data) {
110            }
111    
112            private static final InputStream _EMPTY_STREAM =
113                    new UnsyncByteArrayInputStream(new byte[0]);
114    
115            private WikiEngine _engine;
116            private long _nodeId;
117    
118    }