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.blogs.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.dao.search.SearchContainer;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
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.blogs.model.BlogsEntry;
031    import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
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.List;
038    
039    /**
040     * @author Igor Spasic
041     */
042    public class BlogsEntryAtomCollectionAdapter
043            extends BaseAtomCollectionAdapter<BlogsEntry> {
044    
045            @Override
046            public String getCollectionName() {
047                    return _COLLECTION_NAME;
048            }
049    
050            @Override
051            public List<String> getEntryAuthors(BlogsEntry blogsEntry) {
052                    List<String> authors = new ArrayList<String>();
053    
054                    authors.add(blogsEntry.getUserName());
055    
056                    return authors;
057            }
058    
059            @Override
060            public AtomEntryContent getEntryContent(
061                    BlogsEntry blogsEntry, AtomRequestContext atomRequestContext) {
062    
063                    return new AtomEntryContent(blogsEntry.getContent());
064            }
065    
066            @Override
067            public String getEntryId(BlogsEntry blogsEntry) {
068                    return String.valueOf(blogsEntry.getEntryId());
069            }
070    
071            @Override
072            public String getEntrySummary(BlogsEntry blogsEntry) {
073                    return blogsEntry.getDescription();
074            }
075    
076            @Override
077            public String getEntryTitle(BlogsEntry blogsEntry) {
078                    return blogsEntry.getTitle();
079            }
080    
081            @Override
082            public Date getEntryUpdated(BlogsEntry blogsEntry) {
083                    return blogsEntry.getModifiedDate();
084            }
085    
086            @Override
087            public String getFeedTitle(AtomRequestContext atomRequestContext) {
088                    return AtomUtil.createFeedTitleFromPortletName(
089                            atomRequestContext, PortletKeys.BLOGS);
090            }
091    
092            @Override
093            protected void doDeleteEntry(
094                            String resourceName, AtomRequestContext atomRequestContext)
095                    throws Exception {
096    
097                    long blogsEntryId = GetterUtil.getLong(resourceName);
098    
099                    BlogsEntryServiceUtil.deleteEntry(blogsEntryId);
100            }
101    
102            @Override
103            protected BlogsEntry doGetEntry(
104                            String resourceName, AtomRequestContext atomRequestContext)
105                    throws Exception {
106    
107                    long blogsEntryId = GetterUtil.getLong(resourceName);
108    
109                    return BlogsEntryServiceUtil.getEntry(blogsEntryId);
110            }
111    
112            @Override
113            protected Iterable<BlogsEntry> doGetFeedEntries(
114                            AtomRequestContext atomRequestContext)
115                    throws Exception {
116    
117                    long groupId = atomRequestContext.getLongParameter("groupId");
118                    int status = WorkflowConstants.STATUS_APPROVED;
119    
120                    int max = atomRequestContext.getIntParameter(
121                            "max", SearchContainer.DEFAULT_DELTA);
122    
123                    if (groupId > 0) {
124                            int page = atomRequestContext.getIntParameter("page");
125    
126                            if (page == 0) {
127                                    return BlogsEntryServiceUtil.getGroupEntries(
128                                            groupId, status, max);
129                            }
130    
131                            int count = BlogsEntryServiceUtil.getGroupEntriesCount(
132                                    groupId, new Date(), status);
133    
134                            AtomPager atomPager = new AtomPager(page, max, count);
135    
136                            AtomUtil.saveAtomPagerInRequest(atomRequestContext, atomPager);
137    
138                            return BlogsEntryServiceUtil.getGroupEntries(
139                                    groupId, new Date(), status, atomPager.getStart(),
140                                    atomPager.getEnd() + 1);
141                    }
142    
143                    long organizationId = atomRequestContext.getLongParameter(
144                            "organizationId");
145    
146                    if (organizationId > 0) {
147                            return BlogsEntryServiceUtil.getOrganizationEntries(
148                                    organizationId, new Date(), status, max);
149                    }
150    
151                    long companyId = CompanyThreadLocal.getCompanyId();
152    
153                    if (companyId > 0) {
154                            return BlogsEntryServiceUtil.getCompanyEntries(
155                                    companyId, new Date(), status, max);
156                    }
157    
158                    return Collections.emptyList();
159            }
160    
161            @Override
162            protected BlogsEntry doPostEntry(
163                            String title, String summary, String content, Date date,
164                            AtomRequestContext atomRequestContext)
165                    throws Exception {
166    
167                    long groupId = atomRequestContext.getLongParameter("groupId");
168    
169                    Calendar cal = Calendar.getInstance();
170    
171                    cal.setTime(date);
172    
173                    int displayDateMonth = cal.get(Calendar.MONTH);
174                    int displayDateDay = cal.get(Calendar.DAY_OF_MONTH);
175                    int displayDateYear = cal.get(Calendar.YEAR);
176                    int displayDateHour = cal.get(Calendar.HOUR_OF_DAY);
177                    int displayDateMinute = cal.get(Calendar.MINUTE);
178    
179                    boolean allowPingbacks = true;
180                    boolean allowTrackbacks = true;
181                    String[] trackbacks = new String[0];
182    
183                    ServiceContext serviceContext = new ServiceContext();
184    
185                    serviceContext.setAddGroupPermissions(true);
186                    serviceContext.setAddGuestPermissions(true);
187                    serviceContext.setScopeGroupId(groupId);
188    
189                    return BlogsEntryServiceUtil.addEntry(
190                            title, StringPool.BLANK, summary, content, displayDateMonth,
191                            displayDateDay, displayDateYear, displayDateHour, displayDateMinute,
192                            allowPingbacks, allowTrackbacks, trackbacks, null, null,
193                            serviceContext);
194            }
195    
196            @Override
197            protected void doPutEntry(
198                            BlogsEntry blogsEntry, String title, String summary, String content,
199                            Date date, AtomRequestContext atomRequestContext)
200                    throws Exception {
201    
202                    Calendar cal = Calendar.getInstance();
203    
204                    cal.setTime(date);
205    
206                    int displayDateMonth = cal.get(Calendar.MONTH);
207                    int displayDateDay = cal.get(Calendar.DAY_OF_MONTH);
208                    int displayDateYear = cal.get(Calendar.YEAR);
209                    int displayDateHour = cal.get(Calendar.HOUR_OF_DAY);
210                    int displayDateMinute = cal.get(Calendar.MINUTE);
211    
212                    String[] trackbacks = StringUtil.split(blogsEntry.getTrackbacks());
213    
214                    ServiceContext serviceContext = new ServiceContext();
215    
216                    BlogsEntryServiceUtil.updateEntry(
217                            blogsEntry.getEntryId(), title, blogsEntry.getSubtitle(), summary,
218                            content, displayDateMonth, displayDateDay, displayDateYear,
219                            displayDateHour, displayDateMinute, blogsEntry.getAllowPingbacks(),
220                            blogsEntry.isAllowTrackbacks(), trackbacks, null, null,
221                            serviceContext);
222            }
223    
224            private static final String _COLLECTION_NAME = "blogs";
225    
226    }