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