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