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.calendar.service.impl;
016    
017    import com.liferay.portal.kernel.cal.TZSRecurrence;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.ListUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.calendar.model.CalEvent;
025    import com.liferay.portlet.calendar.service.base.CalEventServiceBaseImpl;
026    import com.liferay.portlet.calendar.service.permission.CalEventPermission;
027    import com.liferay.portlet.calendar.service.permission.CalendarPermission;
028    
029    import java.io.File;
030    import java.io.InputStream;
031    
032    import java.util.Calendar;
033    import java.util.Iterator;
034    import java.util.List;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Mate Thurzo
039     */
040    public class CalEventServiceImpl extends CalEventServiceBaseImpl {
041    
042            public CalEvent addEvent(
043                            String title, String description, String location,
044                            int startDateMonth, int startDateDay, int startDateYear,
045                            int startDateHour, int startDateMinute, int durationHour,
046                            int durationMinute, boolean allDay, boolean timeZoneSensitive,
047                            String type, boolean repeating, TZSRecurrence recurrence,
048                            int remindBy, int firstReminder, int secondReminder,
049                            ServiceContext serviceContext)
050                    throws PortalException, SystemException {
051    
052                    CalendarPermission.check(
053                            getPermissionChecker(), serviceContext.getScopeGroupId(),
054                            ActionKeys.ADD_EVENT);
055    
056                    return calEventLocalService.addEvent(
057                            getUserId(), title, description, location, startDateMonth,
058                            startDateDay, startDateYear, startDateHour, startDateMinute,
059                            durationHour, durationMinute, allDay, timeZoneSensitive, type,
060                            repeating, recurrence, remindBy, firstReminder, secondReminder,
061                            serviceContext);
062            }
063    
064            /**
065             * @deprecated {@link #addEvent(String, String, String, int, int, int, int,
066             *             int, int, int, boolean, boolean, String, boolean,
067             *             TZSRecurrence, int, int, int, ServiceContext)}
068             */
069            public CalEvent addEvent(
070                            String title, String description, String location,
071                            int startDateMonth, int startDateDay, int startDateYear,
072                            int startDateHour, int startDateMinute, int endDateMonth,
073                            int endDateDay, int endDateYear, int durationHour,
074                            int durationMinute, boolean allDay, boolean timeZoneSensitive,
075                            String type, boolean repeating, TZSRecurrence recurrence,
076                            int remindBy, int firstReminder, int secondReminder,
077                            ServiceContext serviceContext)
078                    throws PortalException, SystemException {
079    
080                    CalendarPermission.check(
081                            getPermissionChecker(), serviceContext.getScopeGroupId(),
082                            ActionKeys.ADD_EVENT);
083    
084                    return calEventLocalService.addEvent(
085                            getUserId(), title, description, location, startDateMonth,
086                            startDateDay, startDateYear, startDateHour, startDateMinute,
087                            endDateMonth, endDateDay, endDateYear, durationHour, durationMinute,
088                            allDay, timeZoneSensitive, type, repeating, recurrence, remindBy,
089                            firstReminder, secondReminder, serviceContext);
090            }
091    
092            public void deleteEvent(long eventId)
093                    throws PortalException, SystemException {
094    
095                    CalEventPermission.check(
096                            getPermissionChecker(), eventId, ActionKeys.DELETE);
097    
098                    calEventLocalService.deleteEvent(eventId);
099            }
100    
101            public File exportEvent(long eventId)
102                    throws PortalException, SystemException {
103    
104                    CalEventPermission.check(
105                            getPermissionChecker(), eventId, ActionKeys.VIEW);
106    
107                    return calEventLocalService.exportEvent(getGuestOrUserId(), eventId);
108            }
109    
110            public File exportEvents(List<CalEvent> events, String fileName)
111                    throws PortalException, SystemException {
112    
113                    for (CalEvent event : events) {
114                            CalEventPermission.check(
115                                    getPermissionChecker(), event.getEventId(), ActionKeys.VIEW);
116                    }
117    
118                    return calEventLocalService.exportEvents(
119                            getGuestOrUserId(), events, fileName);
120            }
121    
122            public File exportGroupEvents(long groupId, String fileName)
123                    throws PortalException, SystemException {
124    
125                    CalendarPermission.check(
126                            getPermissionChecker(), groupId, ActionKeys.EXPORT_ALL_EVENTS);
127    
128                    return calEventLocalService.exportGroupEvents(
129                            getGuestOrUserId(), groupId, fileName);
130            }
131    
132            public CalEvent getEvent(long eventId)
133                    throws PortalException, SystemException {
134    
135                    CalEventPermission.check(
136                            getPermissionChecker(), eventId, ActionKeys.VIEW);
137    
138                    return calEventLocalService.getEvent(eventId);
139            }
140    
141            public List<CalEvent> getEvents(long groupId, Calendar cal, String type)
142                    throws PortalException, SystemException {
143    
144                    return getEvents(groupId, cal, new String[] {type});
145            }
146    
147            public List<CalEvent> getEvents(long groupId, Calendar cal, String[] types)
148                    throws PortalException, SystemException {
149    
150                    List<CalEvent> events = calEventLocalService.getEvents(
151                            groupId, cal, types);
152    
153                    events = ListUtil.copy(events);
154    
155                    Iterator<CalEvent> itr = events.iterator();
156    
157                    while (itr.hasNext()) {
158                            CalEvent event = itr.next();
159    
160                            if (!CalEventPermission.contains(
161                                            getPermissionChecker(), event, ActionKeys.VIEW)) {
162    
163                                    itr.remove();
164                            }
165                    }
166    
167                    return events;
168            }
169    
170            public List<CalEvent> getEvents(
171                            long groupId, String type, int start, int end)
172                    throws SystemException {
173    
174                    return getEvents(groupId, new String[] {type}, start, end);
175            }
176    
177            public List<CalEvent> getEvents(
178                            long groupId, String[] types, int start, int end)
179                    throws SystemException {
180    
181                    if ((types != null) && (types.length > 0) &&
182                            ((types.length > 1) || Validator.isNotNull(types[0]))) {
183    
184                            return calEventPersistence.filterFindByG_T(
185                                    groupId, types, start, end);
186                    }
187                    else {
188                            return calEventPersistence.filterFindByGroupId(groupId, start, end);
189                    }
190            }
191    
192            public int getEventsCount(long groupId, String type)
193                    throws SystemException {
194    
195                    return getEventsCount(groupId, new String[] {type});
196            }
197    
198            public int getEventsCount(long groupId, String[] types)
199                    throws SystemException {
200    
201                    if ((types != null) && (types.length > 0) &&
202                            ((types.length > 1) || Validator.isNotNull(types[0]))) {
203    
204                            return calEventPersistence.filterCountByG_T(groupId, types);
205                    }
206                    else {
207                            return calEventPersistence.filterCountByGroupId(groupId);
208                    }
209            }
210    
211            public boolean hasEvents(long groupId, Calendar cal)
212                    throws PortalException, SystemException {
213    
214                    return hasEvents(groupId, cal, new String[0]);
215            }
216    
217            public boolean hasEvents(long groupId, Calendar cal, String type)
218                    throws PortalException, SystemException {
219    
220                    return hasEvents(groupId, cal, new String[] {type});
221            }
222    
223            public boolean hasEvents(long groupId, Calendar cal, String[] types)
224                    throws PortalException, SystemException {
225    
226                    List<CalEvent> events = getEvents(groupId, cal, types);
227    
228                    if (events.isEmpty()) {
229                            return false;
230                    }
231                    else {
232                            return true;
233                    }
234            }
235    
236            public void importICal4j(long groupId, InputStream inputStream)
237                    throws PortalException, SystemException {
238    
239                    CalendarPermission.check(
240                            getPermissionChecker(), groupId, ActionKeys.ADD_EVENT);
241    
242                    calEventLocalService.importICal4j(getUserId(), groupId, inputStream);
243            }
244    
245            public CalEvent updateEvent(
246                            long eventId, String title, String description, String location,
247                            int startDateMonth, int startDateDay, int startDateYear,
248                            int startDateHour, int startDateMinute, int durationHour,
249                            int durationMinute, boolean allDay, boolean timeZoneSensitive,
250                            String type, boolean repeating, TZSRecurrence recurrence,
251                            int remindBy, int firstReminder, int secondReminder,
252                            ServiceContext serviceContext)
253                    throws PortalException, SystemException {
254    
255                    CalEventPermission.check(
256                            getPermissionChecker(), eventId, ActionKeys.UPDATE);
257    
258                    return calEventLocalService.updateEvent(
259                            getUserId(), eventId, title, description, location, startDateMonth,
260                            startDateDay, startDateYear, startDateHour, startDateMinute,
261                            durationHour, durationMinute, allDay, timeZoneSensitive, type,
262                            repeating, recurrence, remindBy, firstReminder, secondReminder,
263                            serviceContext);
264            }
265    
266            /**
267             * @deprecated {@link #updateEvent(long, String, String, String, int, int,
268             *             int, int, int, int, int, boolean, boolean, String, boolean,
269             *             TZSRecurrence, int, int, int, ServiceContext)}
270             */
271            public CalEvent updateEvent(
272                            long eventId, String title, String description, String location,
273                            int startDateMonth, int startDateDay, int startDateYear,
274                            int startDateHour, int startDateMinute, int endDateMonth,
275                            int endDateDay, int endDateYear, int durationHour,
276                            int durationMinute, boolean allDay, boolean timeZoneSensitive,
277                            String type, boolean repeating, TZSRecurrence recurrence,
278                            int remindBy, int firstReminder, int secondReminder,
279                            ServiceContext serviceContext)
280                    throws PortalException, SystemException {
281    
282                    CalEventPermission.check(
283                            getPermissionChecker(), eventId, ActionKeys.UPDATE);
284    
285                    return calEventLocalService.updateEvent(
286                            getUserId(), eventId, title, description, location, startDateMonth,
287                            startDateDay, startDateYear, startDateHour, startDateMinute,
288                            endDateMonth, endDateDay, endDateYear, durationHour, durationMinute,
289                            allDay, timeZoneSensitive, type, repeating, recurrence, remindBy,
290                            firstReminder, secondReminder, serviceContext);
291            }
292    
293    }