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.journal.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.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.security.auth.CompanyThreadLocal;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portlet.journal.model.JournalArticle;
030    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
031    import com.liferay.portlet.journal.util.comparator.ArticleVersionComparator;
032    
033    import java.util.ArrayList;
034    import java.util.Calendar;
035    import java.util.Collections;
036    import java.util.Date;
037    import java.util.HashMap;
038    import java.util.List;
039    import java.util.Locale;
040    import java.util.Map;
041    
042    /**
043     * @author Igor Spasic
044     */
045    public class JournalArticleAtomCollectionProvider
046            extends BaseAtomCollectionAdapter<JournalArticle> {
047    
048            @Override
049            public String getCollectionName() {
050                    return _COLLECTION_NAME;
051            }
052    
053            @Override
054            public List<String> getEntryAuthors(JournalArticle journalArticle) {
055                    List<String> authors = new ArrayList<String>(1);
056    
057                    authors.add(journalArticle.getUserName());
058    
059                    return authors;
060            }
061    
062            @Override
063            public AtomEntryContent getEntryContent(
064                    JournalArticle journalArticle, AtomRequestContext atomRequestContext) {
065    
066                    return new AtomEntryContent(
067                            journalArticle.getContent(), AtomEntryContent.Type.XML);
068            }
069    
070            @Override
071            public String getEntryId(JournalArticle journalArticle) {
072                    return journalArticle.getArticleId();
073            }
074    
075            @Override
076            public String getEntrySummary(JournalArticle entry) {
077                    return null;
078            }
079    
080            @Override
081            public String getEntryTitle(JournalArticle journalArticle) {
082                    return journalArticle.getTitle();
083            }
084    
085            @Override
086            public Date getEntryUpdated(JournalArticle journalArticle) {
087                    return journalArticle.getModifiedDate();
088            }
089    
090            @Override
091            public String getFeedTitle(AtomRequestContext atomRequestContext) {
092                    return AtomUtil.createFeedTitleFromPortletName(
093                            atomRequestContext, PortletKeys.JOURNAL);
094            }
095    
096            @Override
097            protected void doDeleteEntry(
098                            String resourceName, AtomRequestContext atomRequestContext)
099                    throws Exception {
100    
101                    long groupId = atomRequestContext.getLongParameter("groupId");
102                    String articleId = resourceName;
103    
104                    ServiceContext serviceContext = new ServiceContext();
105    
106                    JournalArticleServiceUtil.deleteArticle(
107                            groupId, articleId, null, serviceContext);
108            }
109    
110            @Override
111            protected JournalArticle doGetEntry(
112                            String resourceName, AtomRequestContext atomRequestContext)
113                    throws Exception {
114    
115                    long groupId = atomRequestContext.getLongParameter("groupId");
116                    String articleId = resourceName;
117    
118                    return JournalArticleServiceUtil.getArticle(groupId, articleId);
119            }
120    
121            @Override
122            protected Iterable<JournalArticle> doGetFeedEntries(
123                            AtomRequestContext atomRequestContext)
124                    throws Exception {
125    
126                    List<JournalArticle> journalArticles = new ArrayList<JournalArticle>();
127    
128                    long companyId = CompanyThreadLocal.getCompanyId();
129                    long groupId = atomRequestContext.getLongParameter("groupId");
130    
131                    if ((companyId <= 0) || (groupId <= 0)) {
132                            return journalArticles;
133                    }
134    
135                    List<Long> folderIds = Collections.emptyList();
136                    long classNameId = 0;
137                    String keywords = null;
138                    Double version = null;
139                    String type = atomRequestContext.getParameter("type", "general");
140                    String structureId = null;
141                    String templateId = null;
142                    Date displayDateGT = null;
143                    Date displayDateLT = new Date();
144                    int status = WorkflowConstants.STATUS_APPROVED;
145                    Date reviewDate = null;
146    
147                    OrderByComparator obc = new ArticleVersionComparator();
148    
149                    int count = JournalArticleServiceUtil.searchCount(
150                            companyId, groupId, folderIds, classNameId, keywords, version, type,
151                            structureId, templateId, displayDateGT, displayDateLT, status,
152                            reviewDate);
153    
154                    AtomPager atomPager = new AtomPager(atomRequestContext, count);
155    
156                    AtomUtil.saveAtomPagerInRequest(atomRequestContext, atomPager);
157    
158                    journalArticles = JournalArticleServiceUtil.search(
159                            companyId, groupId, folderIds, classNameId, keywords, version, type,
160                            structureId, templateId, displayDateGT, displayDateLT, status,
161                            reviewDate, atomPager.getStart(), atomPager.getEnd() + 1, obc);
162    
163                    return journalArticles;
164            }
165    
166            @Override
167            protected JournalArticle doPostEntry(
168                            String title, String summary, String content, Date date,
169                            AtomRequestContext atomRequestContext)
170                    throws Exception {
171    
172                    long groupId = atomRequestContext.getLongParameter("groupId");
173                    long folderId = 0;
174                    long classNameId = 0;
175                    long classPK = 0;
176                    String articleId = StringPool.BLANK;
177                    boolean autoArticleId = true;
178    
179                    Locale locale = LocaleUtil.getDefault();
180    
181                    Map<Locale, String> titleMap = new HashMap<Locale, String>();
182    
183                    titleMap.put(locale, title);
184    
185                    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
186    
187                    String type = atomRequestContext.getParameter("type", "general");
188                    String structureId = null;
189                    String templateId = null;
190                    String layoutUuid = null;
191    
192                    Calendar cal = Calendar.getInstance();
193    
194                    cal.setTime(date);
195    
196                    int displayDateMonth = cal.get(Calendar.MONTH);
197                    int displayDateDay = cal.get(Calendar.DAY_OF_MONTH);
198                    int displayDateYear = cal.get(Calendar.YEAR);
199                    int displayDateHour = cal.get(Calendar.HOUR_OF_DAY);
200                    int displayDateMinute = cal.get(Calendar.MINUTE);
201    
202                    int expirationDateMonth = 0;
203                    int expirationDateDay = 0;
204                    int expirationDateYear = 0;
205                    int expirationDateHour = 0;
206                    int expirationDateMinute = 0;
207                    boolean neverExpire = true;
208                    int reviewDateMonth = 0;
209                    int reviewDateDay = 0;
210                    int reviewDateYear = 0;
211                    int reviewDateHour = 0;
212                    int reviewDateMinute = 0;
213                    boolean neverReview = true;
214                    boolean indexable = true;
215                    String articleURL = StringPool.BLANK;
216    
217                    ServiceContext serviceContext = new ServiceContext();
218    
219                    serviceContext.setAddGroupPermissions(false);
220                    serviceContext.setAddGuestPermissions(false);
221                    serviceContext.setScopeGroupId(groupId);
222    
223                    JournalArticle journalArticle = JournalArticleServiceUtil.addArticle(
224                            groupId, folderId, classNameId, classPK, articleId, autoArticleId,
225                            titleMap, descriptionMap, content, type, structureId, templateId,
226                            layoutUuid, displayDateMonth, displayDateDay, displayDateYear,
227                            displayDateHour, displayDateMinute, expirationDateMonth,
228                            expirationDateDay, expirationDateYear, expirationDateHour,
229                            expirationDateMinute, neverExpire, reviewDateMonth, reviewDateDay,
230                            reviewDateYear, reviewDateHour, reviewDateMinute, neverReview,
231                            indexable, articleURL, serviceContext);
232    
233                    double version = journalArticle.getVersion();
234                    int status = WorkflowConstants.STATUS_APPROVED;
235    
236                    journalArticle = JournalArticleServiceUtil.updateStatus(
237                            groupId, journalArticle.getArticleId(), version, status, articleURL,
238                            serviceContext);
239    
240                    return journalArticle;
241            }
242    
243            @Override
244            protected void doPutEntry(
245                            JournalArticle journalArticle, String title, String summary,
246                            String content, Date date, AtomRequestContext atomRequestContext)
247                    throws Exception {
248    
249                    long groupId = journalArticle.getGroupId();
250                    long folderId = journalArticle.getFolderId();
251                    String articleId = journalArticle.getArticleId();
252                    double version = journalArticle.getVersion();
253    
254                    ServiceContext serviceContext = new ServiceContext();
255    
256                    serviceContext.setScopeGroupId(groupId);
257    
258                    journalArticle = JournalArticleServiceUtil.updateArticle(
259                            groupId, folderId, articleId, version, content, serviceContext);
260    
261                    int status = WorkflowConstants.STATUS_APPROVED;
262                    String articleURL = StringPool.BLANK;
263    
264                    JournalArticleServiceUtil.updateStatus(
265                            groupId, journalArticle.getArticleId(), journalArticle.getVersion(),
266                            status, articleURL, serviceContext);
267            }
268    
269            private static final String _COLLECTION_NAME = "web-content";
270    
271    }