List of usage examples for org.joda.time DateTime now
public static DateTime now()
ISOChronology
in the default time zone. From source file:com.almende.eve.agent.google.GoogleCalendarAgent.java
License:Apache License
/** * Get busy intervals of today. A convenience method for easy testing * //from ww w.j a v a2 s. co m * @param calendarId * optional calendar id. If not provided, the default calendar is * used * @param timeZone * Time zone used in the response. Optional. The default is UTC. * @return the busy today * @throws Exception * the exception */ public ArrayNode getBusyToday(@Optional @Name("calendarId") final String calendarId, @Optional @Name("timeZone") final String timeZone) throws Exception { final DateTime now = DateTime.now(); final DateTime timeMin = now.minusMillis(now.getMillisOfDay()); final DateTime timeMax = timeMin.plusDays(1); return getBusy(timeMin.toString(), timeMax.toString(), calendarId, timeZone); }
From source file:com.almende.eve.agent.google.GoogleCalendarAgent.java
License:Apache License
/** * Quick create an event.//from www . j av a 2s. c o m * * @param start * the start * @param end * the end * @param summary * the summary * @param location * the location * @param calendarId * the calendar id * @return the object node * @throws Exception * the exception */ public ObjectNode createEventQuick(@Optional @Name("start") String start, @Optional @Name("end") String end, @Optional @Name("summary") final String summary, @Optional @Name("location") final String location, @Optional @Name("calendarId") final String calendarId) throws Exception { final ObjectNode event = JOM.createObjectNode(); if (start == null) { // set start to current time, rounded to hours DateTime startDate = DateTime.now(); startDate = startDate.plusHours(1); startDate = startDate.minusMinutes(startDate.getMinuteOfHour()); startDate = startDate.minusSeconds(startDate.getSecondOfMinute()); startDate = startDate.minusMillis(startDate.getMillisOfSecond()); start = startDate.toString(); } final ObjectNode startObj = JOM.createObjectNode(); startObj.put("dateTime", start); event.put("start", startObj); if (end == null) { // set end to start +1 hour final DateTime startDate = new DateTime(start); final DateTime endDate = startDate.plusHours(1); end = endDate.toString(); } final ObjectNode endObj = JOM.createObjectNode(); endObj.put("dateTime", end); event.put("end", endObj); if (summary != null) { event.put("summary", summary); } if (location != null) { event.put("location", location); } return createEvent(event, calendarId); }
From source file:com.almende.eve.agent.MeetingAgent.java
License:Apache License
/** * update the activity for meeting agent. * /* ww w .j a v a2s . c o m*/ * @param updatedActivity * the updated activity * @return the activity * @throws Exception * the exception */ public Activity updateActivity(@Name("activity") final Activity updatedActivity) throws Exception { Activity activity = getState().get("activity", Activity.class); if (activity == null) { activity = new Activity(); } final Set<String> prevAttendees = getAgents(activity); // if no updated timestamp is provided, set the timestamp to now if (updatedActivity.withStatus().getUpdated() == null) { updatedActivity.withStatus().setUpdated(DateTime.now().toString()); } // synchronize with the stored activity activity = Activity.sync(activity, updatedActivity); // ensure the url of the meeting agent is filled in final URI myUrl = getFirstUrl("http"); activity.setAgent(myUrl); // create duration when missing Long duration = activity.withConstraints().withTime().getDuration(); if (duration == null) { duration = Duration.standardHours(1).getMillis(); // 1 hour in ms activity.withConstraints().withTime().setDuration(duration); } // remove calendar events from removed attendees final Set<String> currentAttendees = getAgents(activity); final Set<String> removedAttendees = new TreeSet<String>(prevAttendees); removedAttendees.removeAll(currentAttendees); for (final String attendee : removedAttendees) { clearAttendee(attendee); } getState().put("activity", activity); // update all attendees, start timer to regularly check update(); return getState().get("activity", Activity.class); }
From source file:com.almende.eve.agent.MeetingAgent.java
License:Apache License
/** * Apply the constraints of the the activity (for example duration) * /*from w ww .j av a 2 s.c o m*/ * @param activity * @return changed Returns true if the activity is changed */ private boolean applyConstraints() { final Activity activity = getState().get("activity", Activity.class); boolean changed = false; if (activity == null) { return false; } // constraints on attendees/resources /* * TODO: copy actual attendees to status.attendees * List<Attendee> constraintsAttendees = * activity.withConstraints().withAttendees(); * List<Attendee> attendees = new ArrayList<Attendee>(); * for (Attendee attendee : constraintsAttendees) { * attendees.add(attendee.clone()); * } * activity.withStatus().setAttendees(attendees); * // TODO: is it needed to check if the attendees are changed? */ // check time constraints final Long duration = activity.withConstraints().withTime().getDuration(); if (duration != null) { final String start = activity.withStatus().getStart(); final String end = activity.withStatus().getEnd(); if (start != null && end != null) { final DateTime startTime = new DateTime(start); DateTime endTime = new DateTime(end); final Interval interval = new Interval(startTime, endTime); if (interval.toDurationMillis() != duration) { LOG.info("status did not match constraints. " + "Changed end time to match the duration of " + duration + " ms"); // duration does not match. adjust the end time endTime = startTime.plus(duration); activity.withStatus().setEnd(endTime.toString()); activity.withStatus().setUpdated(DateTime.now().toString()); changed = true; } } } // location constraints final String newLocation = activity.withConstraints().withLocation().getSummary(); final String oldLocation = activity.withStatus().withLocation().getSummary(); if (newLocation != null && !newLocation.equals(oldLocation)) { activity.withStatus().withLocation().setSummary(newLocation); changed = true; } if (changed) { // store the updated activity getState().put("activity", activity); } return changed; }
From source file:com.almende.eve.agent.MeetingAgent.java
License:Apache License
/** * Schedule or re-schedule the meeting. Synchronize the events, retrieve * busy profiles, re-schedule the event. *//*from www. j a v a 2 s. c o m*/ public void update() { // TODO: optimize the update method LOG.info("update started"); // stop running tasks stopAutoUpdate(); clearIssues(); // synchronize the events boolean changedEvent = syncEvents(); if (changedEvent) { syncEvents(); } // Check if the activity is finished // If not, schedule a new update task. Else we are done Activity activity = getActivity(); final String start = (activity != null) ? activity.withStatus().getStart() : null; final String updated = (activity != null) ? activity.withStatus().getUpdated() : null; boolean isFinished = false; if (start != null && (new DateTime(start)).isBefore(DateTime.now())) { // start of the event is in the past isFinished = true; if (updated != null && (new DateTime(updated)).isAfter(new DateTime(start))) { // if changed after the last planned start time, then it is // updated afterwards, so do not mark as finished isFinished = false; } } if (activity != null && !isFinished) { // not yet finished. Reschedule the activity updateBusyIntervals(); final boolean changedConstraints = applyConstraints(); final boolean rescheduled = scheduleActivity(); if (changedConstraints || rescheduled) { changedEvent = syncEvents(); if (changedEvent) { syncEvents(); } } // TODO: not so nice adjusting the activityStatus here this way activity = getActivity(); if (activity.withStatus().getActivityStatus() != Status.ACTIVITY_STATUS.error) { // store status of a activity as "planned" activity.withStatus().setActivityStatus(Status.ACTIVITY_STATUS.planned); getState().put("activity", activity); } startAutoUpdate(); } else { // store status of a activity as "executed" activity.withStatus().setActivityStatus(Status.ACTIVITY_STATUS.executed); getState().put("activity", activity); LOG.info("The activity is over, my work is done. Goodbye world."); } }
From source file:com.almende.eve.agent.MeetingAgent.java
License:Apache License
/** * Get the timestamp rounded to the next half hour * /* ww w . j a v a2s.c om*/ * @return */ private DateTime getNextHalfHour() { DateTime next = DateTime.now(); next = next.minusMillis(next.getMillisOfSecond()); next = next.minusSeconds(next.getSecondOfMinute()); if (next.getMinuteOfHour() > 30) { next = next.minusMinutes(next.getMinuteOfHour()); next = next.plusMinutes(60); } else { next = next.minusMinutes(next.getMinuteOfHour()); next = next.plusMinutes(30); } return next; }
From source file:com.almende.eve.agent.MeetingAgent.java
License:Apache License
/** * Schedule the meeting based on currently known event status, infeasible * intervals, and preferences//from w w w. j av a 2 s. c om * * @return rescheduled Returns true if the activity has been rescheduled * When rescheduled, events must be synchronized again with * syncEvents. */ private boolean scheduleActivity() { LOG.info("scheduleActivity started"); // TODO: cleanup final State state = getState(); final Activity activity = state.get("activity", Activity.class); if (activity == null) { return false; } // read planned start and end from the activity DateTime activityStart = null; if (activity.withStatus().getStart() != null) { activityStart = new DateTime(activity.withStatus().getStart()); } DateTime activityEnd = null; if (activity.withStatus().getEnd() != null) { activityEnd = new DateTime(activity.withStatus().getEnd()); } Interval activityInterval = null; if (activityStart != null && activityEnd != null) { activityInterval = new Interval(activityStart, activityEnd); } // calculate solutions final List<Weight> solutions = calculateSolutions(); if (solutions.size() > 0) { // there are solutions. yippie! final Weight solution = solutions.get(0); if (activityInterval == null || !solution.getInterval().equals(activityInterval)) { // interval is changed, save new interval final Status status = activity.withStatus(); status.setStart(solution.getStart().toString()); status.setEnd(solution.getEnd().toString()); status.setActivityStatus(Status.ACTIVITY_STATUS.planned); status.setUpdated(DateTime.now().toString()); state.put("activity", activity); // TODO: cleanup logging LOG.info("Activity replanned at " + solution.toString()); try { // TODO: cleanup LOG.info("Replanned activity: " + JOM.getInstance().writeValueAsString(activity)); } catch (final Exception e) { } return true; } // planning did not change. nothing to do. } else { if (activityStart != null || activityEnd != null) { // no solution final Issue issue = new Issue(); issue.setCode(Issue.NO_PLANNING); issue.setType(Issue.TYPE.error); issue.setMessage("No free interval found for the meeting"); issue.setTimestamp(DateTime.now().toString()); // TODO: generate hints addIssue(issue); final Status status = activity.withStatus(); status.setStart(null); status.setEnd(null); status.setActivityStatus(Status.ACTIVITY_STATUS.error); status.setUpdated(DateTime.now().toString()); state.put("activity", activity); LOG.info(issue.getMessage()); // TODO: cleanup logging return true; } // planning did not change (no solution was already the case) } return false; }
From source file:com.almende.eve.agent.MeetingAgent.java
License:Apache License
/** * Start automatic updating// ww w. j ava 2s .c o m * The interval of the update task depends on the timestamp the activity * is last updated. When recently updated, the interval is smaller. * interval is minimum 10 sec and maximum 1 hour. */ public void startAutoUpdate() { final State state = getState(); final Activity activity = getActivity(); // determine the interval (1 hour by default) final long TEN_SECONDS = 10 * 1000; final long ONE_HOUR = 60 * 60 * 1000; long interval = ONE_HOUR; // default is 1 hour if (activity != null) { final String updated = activity.withStatus().getUpdated(); if (updated != null) { final DateTime dateUpdated = new DateTime(updated); final DateTime now = DateTime.now(); interval = new Interval(dateUpdated, now).toDurationMillis(); } } if (interval < TEN_SECONDS) { interval = TEN_SECONDS; } if (interval > ONE_HOUR) { interval = ONE_HOUR; } // stop any running task stopAutoUpdate(); // schedule an update task and store the task id final JSONRequest request = new JSONRequest("update", null); final String task = getScheduler().createTask(request, interval); state.put("updateTask", task); LOG.info("Auto update started. Interval = " + interval + " milliseconds"); }
From source file:com.almende.eve.agent.MeetingAgent.java
License:Apache License
/** * Create an issue with type, code, and message * timestamp will be set to NOW//www. ja v a2 s. co m * * @param type * @param code * @param message */ private void addIssue(final TYPE type, final Integer code, final String message) { final Issue issue = new Issue(); issue.setType(type); issue.setCode(code); issue.setMessage(message); issue.setTimestamp(DateTime.now().toString()); addIssue(issue); }
From source file:com.almende.eve.agent.MeetingAgent.java
License:Apache License
/** * Merge the busy intervals of all attendees, and the preferred intervals *//* www . j av a2 s. c om*/ private void mergeTimeConstraints() { final ArrayList<Interval> infeasibleIntervals = new ArrayList<Interval>(); final ArrayList<Weight> preferredIntervals = new ArrayList<Weight>(); final Activity activity = getActivity(); if (activity != null) { // read and merge the stored busy intervals of all attendees for (final Attendee attendee : activity.withConstraints().withAttendees()) { final String agent = attendee.getAgent(); if (attendee.getResponseStatus() != RESPONSE_STATUS.declined) { if (new Boolean(true).equals(attendee.getOptional())) { // This attendee is optional. // Add its busy intervals to the soft constraints final List<Interval> attendeeBusy = getAgentBusy(agent); if (attendeeBusy != null) { for (final Interval i : attendeeBusy) { final Weight wi = new Weight(i.getStart(), i.getEnd(), WEIGHT_BUSY_OPTIONAL_ATTENDEE); preferredIntervals.add(wi); } } } else { // this attendee is required. // Add its busy intervals to the hard constraints final List<Interval> attendeeBusy = getAgentBusy(agent); if (attendeeBusy != null) { infeasibleIntervals.addAll(attendeeBusy); } } } // else This attendee declined. Ignore this attendees busy // interval } // read the time preferences and add them to the soft constraints final List<Preference> preferences = activity.withConstraints().withTime().withPreferences(); for (final Preference p : preferences) { if (p != null) { final Weight wi = new Weight(new DateTime(p.getStart()), new DateTime(p.getEnd()), p.getWeight()); preferredIntervals.add(wi); } } } // add office hours profile to the soft constraints // TODO: don't include (hardcoded) office hours here, should be handled // by a PersonalAgent final DateTime timeMin = DateTime.now(); final DateTime timeMax = timeMin.plusDays(LOOK_AHEAD_DAYS); final List<Interval> officeHours = IntervalsUtil.getOfficeHours(timeMin, timeMax); for (final Interval i : officeHours) { final Weight wi = new Weight(i, WEIGHT_OFFICE_HOURS); preferredIntervals.add(wi); } // add delay penalties to the soft constraints final DateTime now = DateTime.now(); final MutableDateTime d = new MutableDateTime(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0, 0, 0, 0); for (int i = 0; i <= LOOK_AHEAD_DAYS; i++) { final DateTime start = d.toDateTime(); final DateTime end = start.plusDays(1); final Weight wi = new Weight(start, end, WEIGHT_DELAY_PER_DAY * i); preferredIntervals.add(wi); d.addDays(1); } // order and store the aggregated lists with intervals IntervalsUtil.order(infeasibleIntervals); getState().put("infeasible", infeasibleIntervals); WeightsUtil.order(preferredIntervals); getState().put("preferred", preferredIntervals); }