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.portlet.documentlibrary.atom;
016    
017    import com.liferay.portal.atom.AtomPager;
018    import com.liferay.portal.atom.AtomUtil;
019    import com.liferay.portal.kernel.atom.AtomEntryContent;
020    import com.liferay.portal.kernel.atom.AtomException;
021    import com.liferay.portal.kernel.atom.AtomRequestContext;
022    import com.liferay.portal.kernel.atom.BaseMediaAtomCollectionAdapter;
023    import com.liferay.portal.kernel.repository.model.FileEntry;
024    import com.liferay.portal.kernel.repository.model.Folder;
025    import com.liferay.portal.kernel.util.Base64;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.MimeTypesUtil;
028    import com.liferay.portal.kernel.util.StreamUtil;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.util.PortletKeys;
031    import com.liferay.portlet.bookmarks.util.comparator.EntryNameComparator;
032    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
033    
034    import java.io.ByteArrayInputStream;
035    import java.io.ByteArrayOutputStream;
036    import java.io.InputStream;
037    
038    import java.util.ArrayList;
039    import java.util.Date;
040    import java.util.List;
041    
042    /**
043     * @author Igor Spasic
044     */
045    public class FileEntryAtomCollectionAdapter
046            extends BaseMediaAtomCollectionAdapter<FileEntry> {
047    
048            public String getCollectionName() {
049                    return COLLECTION_NAME;
050            }
051    
052            public List<String> getEntryAuthors(FileEntry fileEntry) {
053                    List<String> authors = new ArrayList<String>();
054    
055                    authors.add(fileEntry.getUserName());
056    
057                    return authors;
058            }
059    
060            public AtomEntryContent getEntryContent(
061                    FileEntry fileEntry, AtomRequestContext atomRequestContext) {
062    
063                    AtomEntryContent atomEntryContent = new AtomEntryContent(
064                            AtomEntryContent.Type.MEDIA);
065    
066                    atomEntryContent.setMimeType(fileEntry.getMimeType());
067    
068                    String srcLink = AtomUtil.createEntryLink(
069                            atomRequestContext, COLLECTION_NAME,
070                            fileEntry.getFileEntryId() + ":media");
071    
072                    atomEntryContent.setSrcLink(srcLink);
073    
074                    return atomEntryContent;
075            }
076    
077            public String getEntryId(FileEntry fileEntry) {
078                    return String.valueOf(fileEntry.getPrimaryKey());
079            }
080    
081            public String getEntrySummary(FileEntry fileEntry) {
082                    return fileEntry.getDescription();
083            }
084    
085            public String getEntryTitle(FileEntry fileEntry) {
086                    return fileEntry.getTitle();
087            }
088    
089            public Date getEntryUpdated(FileEntry fileEntry) {
090                    return fileEntry.getModifiedDate();
091            }
092    
093            public String getFeedTitle(AtomRequestContext atomRequestContext) {
094                    return AtomUtil.createFeedTitleFromPortletName(
095                            atomRequestContext, PortletKeys.DOCUMENT_LIBRARY) + " files";
096            }
097    
098            @Override
099            public String getMediaContentType(FileEntry fileEntry) {
100                    return fileEntry.getMimeType();
101            }
102    
103            @Override
104            public String getMediaName(FileEntry fileEntry) {
105                    return fileEntry.getTitle();
106            }
107    
108            @Override
109            public InputStream getMediaStream(FileEntry fileEntry)
110                    throws AtomException {
111    
112                    try {
113                            return fileEntry.getContentStream();
114                    }
115                    catch (Exception ex) {
116                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, ex);
117                    }
118            }
119    
120            @Override
121            protected void doDeleteEntry(
122                            String resourceName, AtomRequestContext atomRequestContext)
123                    throws Exception {
124    
125                    long fileEntryId = GetterUtil.getLong(resourceName);
126    
127                    DLAppServiceUtil.deleteFileEntry(fileEntryId);
128            }
129    
130            @Override
131            protected FileEntry doGetEntry(
132                            String resourceName, AtomRequestContext atomRequestContext)
133                    throws Exception {
134    
135                    long fileEntryId = GetterUtil.getLong(resourceName);
136    
137                    return DLAppServiceUtil.getFileEntry(fileEntryId);
138            }
139    
140            @Override
141            protected Iterable<FileEntry> doGetFeedEntries(
142                            AtomRequestContext atomRequestContext)
143                    throws Exception {
144    
145                    long folderId = atomRequestContext.getLongParameter("folderId");
146    
147                    long repositoryId = 0;
148    
149                    if (folderId != 0) {
150                            Folder folder = DLAppServiceUtil.getFolder(folderId);
151    
152                            repositoryId = folder.getRepositoryId();
153                    }
154                    else {
155                            repositoryId = atomRequestContext.getLongParameter("repositoryId");
156                    }
157    
158                    int count = DLAppServiceUtil.getFileEntriesCount(
159                            repositoryId, folderId);
160    
161                    AtomPager atomPager = new AtomPager(atomRequestContext, count);
162    
163                    AtomUtil.saveAtomPagerInRequest(atomRequestContext, atomPager);
164    
165                    return DLAppServiceUtil.getFileEntries(
166                            repositoryId, folderId, atomPager.getStart(),
167                            atomPager.getEnd() + 1, new EntryNameComparator());
168            }
169    
170            @Override
171            protected FileEntry doPostEntry(
172                            String title, String summary, String content, Date date,
173                            AtomRequestContext atomRequestContext)
174                    throws Exception {
175    
176                    long folderId = atomRequestContext.getLongParameter("folderId");
177    
178                    long repositoryId = 0;
179    
180                    if (folderId != 0) {
181                            Folder folder = DLAppServiceUtil.getFolder(folderId);
182    
183                            repositoryId = folder.getRepositoryId();
184                    }
185                    else {
186                            repositoryId = atomRequestContext.getLongParameter("repositoryId");
187                    }
188    
189                    String mimeType = atomRequestContext.getHeader("Media-Content-Type");
190    
191                    if (mimeType == null) {
192                            mimeType = MimeTypesUtil.getContentType(title);
193                    }
194    
195                    byte[] contentDecoded = Base64.decode(content);
196    
197                    ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
198                            contentDecoded);
199    
200                    ServiceContext serviceContext = new ServiceContext();
201    
202                    FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
203                            repositoryId, folderId, title, mimeType, title, summary, null,
204                            contentInputStream, contentDecoded.length, serviceContext);
205    
206                    return fileEntry;
207            }
208    
209            @Override
210            protected FileEntry doPostMedia(
211                            String mimeType, String slug, InputStream inputStream,
212                            AtomRequestContext atomRequestContext)
213                    throws Exception {
214    
215                    long folderId = atomRequestContext.getLongParameter("folderId");
216    
217                    long repositoryId = 0;
218    
219                    if (folderId != 0) {
220                            Folder folder = DLAppServiceUtil.getFolder(folderId);
221    
222                            repositoryId = folder.getRepositoryId();
223                    }
224                    else {
225                            repositoryId = atomRequestContext.getLongParameter("repositoryId");
226                    }
227    
228                    String title = atomRequestContext.getHeader("Title");
229                    String description = atomRequestContext.getHeader("Summary");
230    
231                    ByteArrayOutputStream byteArrayOutputStream =
232                            new ByteArrayOutputStream();
233    
234                    StreamUtil.transfer(inputStream, byteArrayOutputStream);
235    
236                    byte[] content = byteArrayOutputStream.toByteArray();
237    
238                    ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
239                            content);
240    
241                    ServiceContext serviceContext = new ServiceContext();
242    
243                    FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
244                            repositoryId, folderId, title, mimeType, title, description, null,
245                            contentInputStream, content.length, serviceContext);
246    
247                    return fileEntry;
248            }
249    
250            @Override
251            protected void doPutEntry(
252                            FileEntry fileEntry, String title, String summary, String content,
253                            Date date, AtomRequestContext atomRequestContext)
254                    throws Exception {
255    
256                    String mimeType = atomRequestContext.getHeader("Media-Content-Type");
257    
258                    if (mimeType == null) {
259                            mimeType = MimeTypesUtil.getContentType(title);
260                    }
261    
262                    byte[] contentDecoded = Base64.decode(content);
263    
264                    ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
265                            contentDecoded);
266    
267                    ServiceContext serviceContext = new ServiceContext();
268    
269                    DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(),
270                            title, mimeType, title, summary, null, true, contentInputStream,
271                            contentDecoded.length, serviceContext);
272            }
273    
274            @Override
275            protected void doPutMedia(
276                            FileEntry fileEntry, String mimeType, String slug,
277                            InputStream inputStream, AtomRequestContext atomRequestContext)
278                    throws Exception {
279    
280                    String title = atomRequestContext.getHeader("Title");
281                    String description = atomRequestContext.getHeader("Summary");
282    
283                    ByteArrayOutputStream byteArrayOutputStream =
284                            new ByteArrayOutputStream();
285    
286                    StreamUtil.transfer(inputStream, byteArrayOutputStream);
287    
288                    byte[] content = byteArrayOutputStream.toByteArray();
289    
290                    ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
291                            content);
292    
293                    ServiceContext serviceContext = new ServiceContext();
294    
295                    DLAppServiceUtil.updateFileEntry(fileEntry.getFileEntryId(),
296                            slug, mimeType, title, description, null, true, contentInputStream,
297                            content.length, serviceContext);
298            }
299    
300            protected static final String COLLECTION_NAME = "files";
301    
302    }