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