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.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.AtomRequestContext;
021    import com.liferay.portal.kernel.atom.BaseAtomCollectionAdapter;
022    import com.liferay.portal.kernel.repository.model.Folder;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.bookmarks.util.comparator.EntryNameComparator;
027    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
028    
029    import java.util.ArrayList;
030    import java.util.Date;
031    import java.util.List;
032    
033    /**
034     * @author Igor Spasic
035     */
036    public class FolderAtomCollectionAdapter
037            extends BaseAtomCollectionAdapter<Folder> {
038    
039            @Override
040            public String getCollectionName() {
041                    return _COLLECTION_NAME;
042            }
043    
044            @Override
045            public List<String> getEntryAuthors(Folder folder) {
046                    List<String> authors = new ArrayList<String>();
047    
048                    authors.add(folder.getUserName());
049    
050                    return authors;
051            }
052    
053            @Override
054            public AtomEntryContent getEntryContent(
055                    Folder folder, AtomRequestContext atomRequestContext) {
056    
057                    AtomEntryContent atomEntryContent = new AtomEntryContent(
058                            AtomEntryContent.Type.XML);
059    
060                    String srcLink = AtomUtil.createCollectionLink(
061                            atomRequestContext, FileEntryAtomCollectionAdapter.COLLECTION_NAME);
062    
063                    srcLink += "?folderId=" + folder.getFolderId();
064    
065                    atomEntryContent.setSrcLink(srcLink);
066    
067                    return atomEntryContent;
068            }
069    
070            @Override
071            public String getEntryId(Folder folder) {
072                    return String.valueOf(folder.getPrimaryKey());
073            }
074    
075            @Override
076            public String getEntrySummary(Folder folder) {
077                    return folder.getDescription();
078            }
079    
080            @Override
081            public String getEntryTitle(Folder folder) {
082                    return folder.getName();
083            }
084    
085            @Override
086            public Date getEntryUpdated(Folder folder) {
087                    return folder.getModifiedDate();
088            }
089    
090            @Override
091            public String getFeedTitle(AtomRequestContext atomRequestContext) {
092                    return AtomUtil.createFeedTitleFromPortletName(
093                            atomRequestContext, PortletKeys.DOCUMENT_LIBRARY) + " folders";
094            }
095    
096            @Override
097            protected void doDeleteEntry(
098                            String resourceName, AtomRequestContext atomRequestContext)
099                    throws Exception {
100    
101                    long folderEntryId = GetterUtil.getLong(resourceName);
102    
103                    DLAppServiceUtil.deleteFolder(folderEntryId);
104            }
105    
106            @Override
107            protected Folder doGetEntry(
108                            String resourceName, AtomRequestContext atomRequestContext)
109                    throws Exception {
110    
111                    long folderEntryId = GetterUtil.getLong(resourceName);
112    
113                    return DLAppServiceUtil.getFolder(folderEntryId);
114            }
115    
116            @Override
117            protected Iterable<Folder> doGetFeedEntries(
118                            AtomRequestContext atomRequestContext)
119                    throws Exception {
120    
121                    long repositoryId = 0;
122    
123                    long parentFolderId = atomRequestContext.getLongParameter(
124                            "parentFolderId");
125    
126                    if (parentFolderId != 0) {
127                            Folder parentFolder = DLAppServiceUtil.getFolder(parentFolderId);
128    
129                            repositoryId = parentFolder.getRepositoryId();
130                    }
131                    else {
132                            repositoryId = atomRequestContext.getLongParameter("repositoryId");
133                    }
134    
135                    int count = DLAppServiceUtil.getFoldersCount(
136                            repositoryId, parentFolderId);
137    
138                    AtomPager atomPager = new AtomPager(atomRequestContext, count);
139    
140                    AtomUtil.saveAtomPagerInRequest(atomRequestContext, atomPager);
141    
142                    return DLAppServiceUtil.getFolders(
143                            repositoryId, parentFolderId, atomPager.getStart(),
144                            atomPager.getEnd() + 1, new EntryNameComparator());
145            }
146    
147            @Override
148            protected Folder doPostEntry(
149                            String title, String summary, String content, Date date,
150                            AtomRequestContext atomRequestContext)
151                    throws Exception {
152    
153                    long repositoryId = 0;
154    
155                    long parentFolderId = atomRequestContext.getLongParameter(
156                            "parentFolderId");
157    
158                    if (parentFolderId != 0) {
159                            Folder parentFolder = DLAppServiceUtil.getFolder(parentFolderId);
160    
161                            repositoryId = parentFolder.getRepositoryId();
162                    }
163                    else {
164                            repositoryId = atomRequestContext.getLongParameter("repositoryId");
165                    }
166    
167                    ServiceContext serviceContext = new ServiceContext();
168    
169                    Folder folder = DLAppServiceUtil.addFolder(
170                            repositoryId, parentFolderId, title, summary, serviceContext);
171    
172                    return folder;
173            }
174    
175            @Override
176            protected void doPutEntry(
177                            Folder folder, String title, String summary, String content,
178                            Date date, AtomRequestContext atomRequestContext)
179                    throws Exception {
180    
181                    ServiceContext serviceContext = new ServiceContext();
182    
183                    DLAppServiceUtil.updateFolder(
184                            folder.getFolderId(), title, summary, serviceContext);
185            }
186    
187            private static final String _COLLECTION_NAME = "folders";
188    
189    }