* @throws ParseException
* @throws AccessDeniedException thown if there is no write access for this user to the calendar or event.
*/
public void importIcal(InputStream ical, CalendarVO calendar, UserVO user, EventHandling handling) throws IOException, ParserException, ParseException, AccessDeniedException
{
EventDAO eventdao = new EventDAO();
EventCalendarDAO eventcalendardao = new EventCalendarDAO();
List<EventVO> events = IcalParser.getInstance().fromIcal(ical);
List<EventVO> insertList = new ArrayList<EventVO>();
for(EventVO event : events)
{
if(log.isDebugEnabled())
log.debug("start import: " + event);
if(event.getId() != null) // parser found oc id
{
EventVO event2 = eventdao.getById(event.getId());
if(event2 != null)
{
EventCalendarVO hostEventCalendar = null;
EventCalendarVO myEventCalendar = null;
for(EventCalendarVO eventCalendar : event2.getEventCalendars()) { // search for myEventCalendar
if(calendar.equals(eventCalendar.getCalendar())) {
myEventCalendar = eventCalendar;
}
}
for(EventCalendarVO eventCalendar : event2.getEventCalendars()) { // search for the hostEventCalendar entity
if(EventCalendarVO.ParticipiantType.HOST.equals(eventCalendar.getParticipiantType())) {
hostEventCalendar = eventCalendar;
}
}
if(EntityAccessUtility.isAccessGranted(user,myEventCalendar,Access.WRITE)
&& EntityAccessUtility.isAccessGranted(calendar.getUser(),hostEventCalendar,Access.WRITE)) // write access ?
{
event2.setDescription(event.getDescription());
event2.setTitle(event.getTitle());
event2.setAllDay(event.isAllDay());
event2.setEndDate(event.getEndDate());
event2.setStartDate(event.getStartDate());
event2.setRecurrenceCycle(event.getRecurrenceCycle());
event2.setRecurrenceCycleUnit(event.getRecurrenceCycleUnit());
event2.setRecurrenceEndDate(event.getRecurrenceEndDate());
event2.setRecurrenceStartDate(event.getRecurrenceStartDate());
event2.setRecurrenceInMonth(event.getRecurrenceInMonth());
event2.setRecurrenceInWeek(event.getRecurrenceInWeek());
event2.setRecurrenceNumberOfTimes(event.getRecurrenceNumberOfTimes());
event2.setRecurrenceType(event.getRecurrenceType());
event2.setRecurrenceUntilDate(event.getRecurrenceUntilDate());
if(log.isDebugEnabled())
log.debug("event updated: " + event2);
eventdao.update(event2);
insertList.add(event2);
}
else
{
if(log.isDebugEnabled())
log.debug("event update denied: " + event2);
insertList.add(event2);
throw new AccessDeniedException(calendar.getName(),user.getUserName(),"WRITE");
}
}
else
{
if(log.isInfoEnabled())
log.info("event " + event.getId() + " not found, id incorrect?");
}
}
else
{
// create new Event, no eventid found
if(EntityAccessUtility.isAccessGranted(user,calendar,Access.WRITE))
{
EventCalendarVO ecvo = new EventCalendarVO(event,calendar);
ecvo.setParticipiantType(ParticipiantType.HOST);
ecvo.setInvitationStatus(InvitationStatus.ACCEPTED);
ecvo.setOwnerUser(calendar.getOwnerUser());
ecvo.setOwnerGroup(calendar.getOwnerGroup());
ecvo.setAccessUser(calendar.getAccessUser());
ecvo.setAccessGroup(calendar.getAccessGroup());
ecvo.setAccessGlobal(calendar.getAccessGlobal());
event.getEventCalendars().add(ecvo);
if(log.isDebugEnabled())
log.debug("new event created: " + event);
eventdao.insert(event);
insertList.add(event);
}
else
{
if(log.isDebugEnabled())
log.debug("create event denied: " + event);
insertList.add(event);
throw new AccessDeniedException(calendar.getName(),user.getUserName(),"WRITE");
}
}
}
if(handling == EventHandling.DELETE) // deleting events not in insertList
{
List<EventCalendarVO> actualList = eventcalendardao.getByCalendar(calendar, true);
for(EventCalendarVO eventCalendar : actualList)
{
if(!insertList.contains(eventCalendar.getEvent()))
{
if(log.isDebugEnabled())
log.debug("deleting event: " + eventCalendar.getEvent());
eventdao.delete(eventCalendar.getEvent());
}
}
}
}