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