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