use of fi.otavanopisto.muikku.plugins.calendar.model.UserCalendar in project muikku by otavanopisto.
the class CalendarRESTService method getEvents.
@GET
@Path("/calendars/{CALID}/events/")
@RESTPermitUnimplemented
public Response getEvents(@PathParam("CALID") Long calendarId, @QueryParam("timeMin") DateTimeParameter timeMin, @QueryParam("timeMax") DateTimeParameter timeMax) {
if (!sessionController.isLoggedIn()) {
return Response.status(Response.Status.FORBIDDEN).build();
}
if (calendarId == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
UserCalendar userCalendar = calendarController.findUserCalendar(calendarId);
if (userCalendar == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
if (!userCalendar.getUserId().equals(sessionController.getLoggedUserEntity().getId())) {
return Response.status(Response.Status.FORBIDDEN).build();
}
try {
List<CalendarEvent> result = new ArrayList<>();
List<fi.otavanopisto.muikku.calendar.CalendarEvent> calendarEvents = calendarController.listCalendarEvents(userCalendar, timeMin != null ? timeMin.getDateTime() : null, timeMax != null ? timeMax.getDateTime() : null);
for (fi.otavanopisto.muikku.calendar.CalendarEvent calendarEvent : calendarEvents) {
result.add(createEventRestModel(userCalendar, calendarEvent));
}
return Response.ok(result).build();
} catch (CalendarServiceException e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
}
use of fi.otavanopisto.muikku.plugins.calendar.model.UserCalendar in project muikku by otavanopisto.
the class UserCalendarDAO method listByUserId.
public List<UserCalendar> listByUserId(Long userId) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<UserCalendar> criteria = criteriaBuilder.createQuery(UserCalendar.class);
Root<UserCalendar> root = criteria.from(UserCalendar.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(UserCalendar_.userId), userId)));
return entityManager.createQuery(criteria).getResultList();
}
use of fi.otavanopisto.muikku.plugins.calendar.model.UserCalendar in project muikku by otavanopisto.
the class CalendarController method updateCalendarEvent.
public CalendarEvent updateCalendarEvent(UserCalendar userCalendar, CalendarEvent calendarEvent) throws CalendarServiceException {
CalendarServiceProvider provider = getCalendarServiceProvider(userCalendar.getCalendarProvider());
Calendar calendar = provider.findCalendar(userCalendar.getCalendarId());
if (calendar == null) {
throw new CalendarServiceException("Could not find calendar for user calendar #" + userCalendar.getId());
}
if (StringUtils.isBlank(calendarEvent.getId())) {
throw new CalendarServiceException("Cannot update event without id");
}
return provider.updateEvent(calendarEvent);
}
use of fi.otavanopisto.muikku.plugins.calendar.model.UserCalendar in project muikku by otavanopisto.
the class CalendarController method findCalendarEvent.
public fi.otavanopisto.muikku.calendar.CalendarEvent findCalendarEvent(UserCalendar userCalendar, String eventId) throws CalendarServiceException {
CalendarServiceProvider provider = getCalendarServiceProvider(userCalendar.getCalendarProvider());
Calendar calendar = provider.findCalendar(userCalendar.getCalendarId());
if (calendar == null) {
throw new CalendarServiceException("Could not find calendar for user calendar #" + userCalendar.getId());
}
return provider.findEvent(calendar, eventId);
}
use of fi.otavanopisto.muikku.plugins.calendar.model.UserCalendar in project muikku by otavanopisto.
the class GoogleCalendarLoginListener method onLogin.
public void onLogin(@Observes LoginEvent event) {
UserEntity userEntity = userEntityController.findUserEntityById(event.getUserEntityId());
if (userEntity != null) {
UserCalendar userCalendar = calendarController.findUserCalendarByUserAndProvider(userEntity, "google");
if (userCalendar == null) {
logger.info("User does not have a calendar, creating one");
try {
userCalendar = calendarController.createCalendar(userEntity, "google", CALENDAR_SUMMARY, CALENDAR_DESCRIPTION, Boolean.TRUE);
for (String email : userEmailEntityController.getUserEmailAddresses(event.getUserIdentifier())) {
try {
logger.info(String.format("Sharing Google calendar with %s", email));
googleCalendarClient.insertCalendarUserAclRule(userCalendar.getCalendarId(), email, "owner");
} catch (CalendarServiceException e) {
logger.log(Level.WARNING, String.format("Could not share calendar with %s", email), e);
}
}
} catch (CalendarServiceException e) {
logger.log(Level.SEVERE, "Failed to create new Google calendar", e);
}
}
}
}
Aggregations