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