use of com.google.api.services.calendar.model.Event in project camel by apache.
the class CalendarEventsIntegrationTest method testManipulatingAnEvent.
@Test
public void testManipulatingAnEvent() throws Exception {
// Add an event
Map<String, Object> headers = new HashMap<String, Object>();
// parameter type is String
headers.put("CamelGoogleCalendar.calendarId", getCalendar().getId());
// parameter type is String
headers.put("CamelGoogleCalendar.text", "Feed the Camel");
com.google.api.services.calendar.model.Event result = requestBodyAndHeaders("direct://QUICKADD", null, headers);
assertNotNull("quickAdd result", result);
// Check if it is in the list of events for this calendar
com.google.api.services.calendar.model.Events events = requestBody("direct://LIST", getCalendar().getId());
Event item = events.getItems().get(0);
String eventId = item.getId();
assertEquals("Feed the Camel", item.getSummary());
// Get the event metadata
headers = new HashMap<String, Object>();
// parameter type is String
headers.put("CamelGoogleCalendar.calendarId", getCalendar().getId());
// parameter type is String
headers.put("CamelGoogleCalendar.eventId", eventId);
result = requestBodyAndHeaders("direct://GET", null, headers);
assertEquals("Feed the Camel", result.getSummary());
// Change the event
result.setSummary("Feed the Camel later");
// parameter type is com.google.api.services.calendar.model.Event
headers.put("CamelGoogleCalendar.content", result);
Event newResult = requestBodyAndHeaders("direct://UPDATE", null, headers);
assertEquals("Feed the Camel later", newResult.getSummary());
// Delete the event
headers = new HashMap<String, Object>();
// parameter type is String
headers.put("CamelGoogleCalendar.calendarId", getCalendar().getId());
// parameter type is String
headers.put("CamelGoogleCalendar.eventId", eventId);
result = requestBodyAndHeaders("direct://DELETE", null, headers);
// Check if it is NOT in the list of events for this calendar
events = requestBody("direct://LIST", getCalendar().getId());
assertEquals(0, events.getItems().size());
}
use of com.google.api.services.calendar.model.Event in project openhab1-addons by openhab.
the class GCalPersistenceService method store.
/**
* Creates a new Google Calendar Entry for each <code>item</code> and adds
* it to the processing queue. The entries' title will either be the items
* name or <code>alias</code> if it is <code>!= null</code>.
*
* The new Calendar Entry will contain a single command to be executed e.g.<br>
* <p>
* <code>send <item.name> <item.state></code>
* </p>
*
* @param item the item which state should be persisted.
* @param alias the alias under which the item should be persisted.
*/
@Override
public void store(final Item item, final String alias) {
if (initialized) {
String newAlias = alias != null ? alias : item.getName();
Event event = new Event();
event.setSummary("[PresenceSimulation] " + newAlias);
event.setDescription(String.format(executeScript, item.getName(), item.getState().toString()));
Date now = new Date();
Date startDate = new Date(now.getTime() + 3600000L * 24 * offset);
Date endDate = startDate;
DateTime start = new DateTime(startDate);
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(endDate);
event.setEnd(new EventDateTime().setDateTime(end));
entries.offer(event);
logger.trace("added new entry '{}' for item '{}' to upload queue", event.getSummary(), item.getName());
} else {
logger.debug("GCal PresenceSimulation Service isn't initialized properly! No entries will be uploaded to your Google Calendar");
}
}
use of com.google.api.services.calendar.model.Event in project openhab1-addons by openhab.
the class GCalEventDownloader method processEntries.
/**
* <p>
* Iterates through <code>entries</code>, extracts the event content and
* creates quartz calendars, jobs and corresponding triggers for each event.
* </p>
* <p>
* The following steps are done at event processing:
* <ul>
* <li>find events with empty content</li>
* <li>create a {@link TimeRangeCalendar} for each event (unique by title) and add a TimeRange for each {@link When}
* </li>
* <li>add each {@link TimeRangeCalendar} to the {@link Scheduler}</li>
* <li>find events with content</li>
* <li>add a Job with the corresponding Triggers for each event</li>
* </ul>
*
* @param entries the GCalendar events to create quart jobs for.
* @throws SchedulerException if there is an internal Scheduler error.
*/
private void processEntries(List<Event> entries) throws SchedulerException {
Map<String, TimeRangeCalendar> calendarCache = new HashMap<String, TimeRangeCalendar>();
// the scheduler
for (Event event : entries) {
String eventContent = event.getDescription();
String eventTitle = event.getSummary();
if (StringUtils.isBlank(eventContent)) {
logger.debug("found event '{}' with no content, add this event to the excluded TimeRangesCalendar - this event could be referenced by the modifiedBy clause", eventTitle);
if (!calendarCache.containsKey(eventTitle)) {
calendarCache.put(eventTitle, new TimeRangeCalendar());
}
TimeRangeCalendar timeRangeCalendar = calendarCache.get(eventTitle);
timeRangeCalendar.addTimeRange(new LongRange(event.getStart().getDateTime().getValue(), event.getEnd().getDateTime().getValue()));
}
}
// the calendars has to be added first, to schedule Triggers successfully
for (Entry<String, TimeRangeCalendar> entry : calendarCache.entrySet()) {
scheduler.addCalendar(entry.getKey(), entry.getValue(), true, true);
}
// now we process all events with content
for (Event event : entries) {
String eventContent = event.getDescription();
String eventTitle = event.getSummary();
if (StringUtils.isNotBlank(eventContent)) {
CalendarEventContent cec = parseEventContent(eventContent, (eventTitle != null) && eventTitle.startsWith("[PresenceSimulation]"));
String modifiedByEvent = null;
if (calendarCache.containsKey(cec.modifiedByEvent)) {
modifiedByEvent = cec.modifiedByEvent;
}
JobDetail startJob = createJob(cec.startCommands, event, true);
boolean triggersCreated = createTriggerAndSchedule(startJob, event, modifiedByEvent, true);
if (triggersCreated) {
logger.debug("created new startJob '{}' with details '{}'", eventTitle, createJobInfo(event, startJob));
}
// do only create end-jobs if there are end-commands ...
if (StringUtils.isNotBlank(cec.endCommands)) {
JobDetail endJob = createJob(cec.endCommands, event, false);
triggersCreated = createTriggerAndSchedule(endJob, event, modifiedByEvent, false);
if (triggersCreated) {
logger.debug("created new endJob '{}' with details '{}'", eventTitle, createJobInfo(event, endJob));
}
}
}
}
}
use of com.google.api.services.calendar.model.Event in project openhab1-addons by openhab.
the class GCalEventDownloader method execute.
/**
* @{inheritDoc}
*/
@Override
protected void execute() {
Events myFeed = downloadEventFeed();
if (myFeed != null) {
List<Event> entries = myFeed.getItems();
if (entries.size() > 0) {
logger.debug("found {} calendar events to process", entries.size());
try {
if (scheduler.isShutdown()) {
logger.warn("Scheduler has been shut down - probably due to exceptions?");
}
cleanJobs();
processEntries(entries);
} catch (SchedulerException se) {
logger.error("scheduling jobs throws exception", se);
}
} else {
logger.debug("gcal feed contains no events ...");
}
}
}
use of com.google.api.services.calendar.model.Event in project camel by apache.
the class CalendarEventsIntegrationTest method testInsert.
@Test
public void testInsert() throws Exception {
Event event = new Event();
event.setSummary("Feed the Camel");
event.setLocation("Somewhere");
ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
attendees.add(new EventAttendee().setEmail("camel-google-calendar.janstey@gmail.com"));
event.setAttendees(attendees);
Date startDate = new Date();
Date endDate = new Date(startDate.getTime() + 3600000);
DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
event.setStart(new EventDateTime().setDateTime(start));
DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
event.setEnd(new EventDateTime().setDateTime(end));
final Map<String, Object> headers = new HashMap<String, Object>();
// parameter type is String
headers.put("CamelGoogleCalendar.calendarId", getCalendar().getId());
// parameter type is com.google.api.services.calendar.model.Event
headers.put("CamelGoogleCalendar.content", event);
final com.google.api.services.calendar.model.Event result = requestBodyAndHeaders("direct://INSERT", null, headers);
assertEquals("Feed the Camel", result.getSummary());
LOG.debug("insert: " + result);
}
Aggregations