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