001    /**
002     * Copyright (c) 2000-present 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 ddmStructureKey = null;
140                    String ddmTemplateKey = null;
141                    Date displayDateGT = null;
142                    Date displayDateLT = new Date();
143                    int status = WorkflowConstants.STATUS_APPROVED;
144                    Date reviewDate = null;
145    
146                    OrderByComparator<JournalArticle> obc = new ArticleVersionComparator();
147    
148                    int count = JournalArticleServiceUtil.searchCount(
149                            companyId, groupId, folderIds, classNameId, keywords, version,
150                            ddmStructureKey, ddmTemplateKey, displayDateGT, displayDateLT,
151                            status, reviewDate);
152    
153                    AtomPager atomPager = new AtomPager(atomRequestContext, count);
154    
155                    AtomUtil.saveAtomPagerInRequest(atomRequestContext, atomPager);
156    
157                    journalArticles = JournalArticleServiceUtil.search(
158                            companyId, groupId, folderIds, classNameId, keywords, version,
159                            ddmStructureKey, ddmTemplateKey, displayDateGT, displayDateLT,
160                            status, reviewDate, atomPager.getStart(), atomPager.getEnd() + 1,
161                            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 ddmStructureKey = null;
188                    String ddmTemplateKey = null;
189                    String layoutUuid = null;
190    
191                    Calendar cal = Calendar.getInstance();
192    
193                    cal.setTime(date);
194    
195                    int displayDateMonth = cal.get(Calendar.MONTH);
196                    int displayDateDay = cal.get(Calendar.DAY_OF_MONTH);
197                    int displayDateYear = cal.get(Calendar.YEAR);
198                    int displayDateHour = cal.get(Calendar.HOUR_OF_DAY);
199                    int displayDateMinute = cal.get(Calendar.MINUTE);
200    
201                    int expirationDateMonth = 0;
202                    int expirationDateDay = 0;
203                    int expirationDateYear = 0;
204                    int expirationDateHour = 0;
205                    int expirationDateMinute = 0;
206                    boolean neverExpire = true;
207                    int reviewDateMonth = 0;
208                    int reviewDateDay = 0;
209                    int reviewDateYear = 0;
210                    int reviewDateHour = 0;
211                    int reviewDateMinute = 0;
212                    boolean neverReview = true;
213                    boolean indexable = true;
214                    String articleURL = StringPool.BLANK;
215    
216                    ServiceContext serviceContext = new ServiceContext();
217    
218                    serviceContext.setAddGroupPermissions(false);
219                    serviceContext.setAddGuestPermissions(false);
220                    serviceContext.setScopeGroupId(groupId);
221    
222                    JournalArticle journalArticle = JournalArticleServiceUtil.addArticle(
223                            groupId, folderId, classNameId, classPK, articleId, autoArticleId,
224                            titleMap, descriptionMap, content, ddmStructureKey, ddmTemplateKey,
225                            layoutUuid, displayDateMonth, displayDateDay, displayDateYear,
226                            displayDateHour, displayDateMinute, expirationDateMonth,
227                            expirationDateDay, expirationDateYear, expirationDateHour,
228                            expirationDateMinute, neverExpire, reviewDateMonth, reviewDateDay,
229                            reviewDateYear, reviewDateHour, reviewDateMinute, neverReview,
230                            indexable, articleURL, serviceContext);
231    
232                    double version = journalArticle.getVersion();
233                    int status = WorkflowConstants.STATUS_APPROVED;
234    
235                    journalArticle = JournalArticleServiceUtil.updateStatus(
236                            groupId, journalArticle.getArticleId(), version, status, articleURL,
237                            serviceContext);
238    
239                    return journalArticle;
240            }
241    
242            @Override
243            protected void doPutEntry(
244                            JournalArticle journalArticle, String title, String summary,
245                            String content, Date date, AtomRequestContext atomRequestContext)
246                    throws Exception {
247    
248                    long groupId = journalArticle.getGroupId();
249                    long folderId = journalArticle.getFolderId();
250                    String articleId = journalArticle.getArticleId();
251                    double version = journalArticle.getVersion();
252    
253                    ServiceContext serviceContext = new ServiceContext();
254    
255                    serviceContext.setScopeGroupId(groupId);
256    
257                    journalArticle = JournalArticleServiceUtil.updateArticle(
258                            groupId, folderId, articleId, version, content, serviceContext);
259    
260                    int status = WorkflowConstants.STATUS_APPROVED;
261                    String articleURL = StringPool.BLANK;
262    
263                    JournalArticleServiceUtil.updateStatus(
264                            groupId, journalArticle.getArticleId(), journalArticle.getVersion(),
265                            status, articleURL, serviceContext);
266            }
267    
268            private static final String _COLLECTION_NAME = "web-content";
269    
270    }