001    /**
002     * Copyright (c) 2000-2011 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.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, status);
131    
132                            AtomPager atomPager = new AtomPager(page, max, count);
133    
134                            AtomUtil.saveAtomPagerInRequest(atomRequestContext, atomPager);
135    
136                            return BlogsEntryServiceUtil.getGroupEntries(
137                                    groupId, status, atomPager.getStart(), atomPager.getEnd() + 1);
138                    }
139    
140                    long organizationId = atomRequestContext.getLongParameter(
141                            "organizationId");
142    
143                    if (organizationId > 0) {
144                            return BlogsEntryServiceUtil.getOrganizationEntries(
145                                    organizationId, status, max);
146                    }
147    
148                    long companyId = CompanyThreadLocal.getCompanyId();
149    
150                    if (companyId > 0) {
151                            return BlogsEntryServiceUtil.getCompanyEntries(
152                                    companyId, status, max);
153                    }
154    
155                    return Collections.emptyList();
156            }
157    
158            @Override
159            protected BlogsEntry doPostEntry(
160                            String title, String summary, String content, Date date,
161                            AtomRequestContext atomRequestContext)
162                    throws Exception {
163    
164                    long groupId = atomRequestContext.getLongParameter("groupId");
165    
166                    Calendar cal = Calendar.getInstance();
167    
168                    cal.setTime(date);
169    
170                    int displayDateMonth = cal.get(Calendar.MONTH);
171                    int displayDateDay = cal.get(Calendar.DAY_OF_MONTH);
172                    int displayDateYear = cal.get(Calendar.YEAR);
173                    int displayDateHour = cal.get(Calendar.HOUR_OF_DAY);
174                    int displayDateMinute = cal.get(Calendar.MINUTE);
175    
176                    boolean allowPingbacks = true;
177                    boolean allowTrackbacks = true;
178                    String[] trackbacks = new String[0];
179    
180                    ServiceContext serviceContext = new ServiceContext();
181    
182                    serviceContext.setAddGroupPermissions(true);
183                    serviceContext.setAddGuestPermissions(true);
184                    serviceContext.setScopeGroupId(groupId);
185    
186                    return BlogsEntryServiceUtil.addEntry(
187                            title, summary, content, displayDateMonth, displayDateDay,
188                            displayDateYear, displayDateHour, displayDateMinute,
189                            allowPingbacks, allowTrackbacks, trackbacks, false, null, null,
190                            null, serviceContext);
191            }
192    
193            @Override
194            protected void doPutEntry(
195                            BlogsEntry blogsEntry, String title, String summary, String content,
196                            Date date, AtomRequestContext atomRequestContext)
197                    throws Exception {
198    
199                    Calendar cal = Calendar.getInstance();
200    
201                    cal.setTime(date);
202    
203                    int displayDateMonth = cal.get(Calendar.MONTH);
204                    int displayDateDay = cal.get(Calendar.DAY_OF_MONTH);
205                    int displayDateYear = cal.get(Calendar.YEAR);
206                    int displayDateHour = cal.get(Calendar.HOUR_OF_DAY);
207                    int displayDateMinute = cal.get(Calendar.MINUTE);
208    
209                    String[] trackbacks = StringUtil.split(blogsEntry.getTrackbacks());
210    
211                    String smallImageFileName = null;
212                    InputStream smallImageInputStream = null;
213    
214                    try {
215                            long smallImageId = blogsEntry.getSmallImageId();
216    
217                            if (smallImageId != 0) {
218                                    Image smallImage = ImageLocalServiceUtil.getImage(smallImageId);
219    
220                                    if (smallImage != null) {
221                                            smallImageFileName =
222                                                    smallImageId + StringPool.PERIOD +
223                                                            blogsEntry.getSmallImageType();
224    
225                                            byte[] smallImageBytes = smallImage.getTextObj();
226    
227                                            smallImageInputStream = new ByteArrayInputStream(
228                                                    smallImageBytes);
229                                    }
230                            }
231    
232                            ServiceContext serviceContext = new ServiceContext();
233    
234                            BlogsEntryServiceUtil.updateEntry(
235                                    blogsEntry.getEntryId(), title, summary, content,
236                                    displayDateMonth, displayDateDay, displayDateYear,
237                                    displayDateHour, displayDateMinute,
238                                    blogsEntry.getAllowPingbacks(), blogsEntry.isAllowTrackbacks(),
239                                    trackbacks, blogsEntry.isSmallImage(),
240                                    blogsEntry.getSmallImageURL(), smallImageFileName,
241                                    smallImageInputStream, serviceContext);
242                    }
243                    finally {
244                            StreamUtil.cleanUp(smallImageInputStream);
245                    }
246    
247            }
248    
249            private static final String _COLLECTION_NAME = "blogs";
250    
251    }