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