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