/**
   * 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>.
   *
   * <p>The new Calendar Entry will contain a single command to be executed e.g.<br>
   *
   * <p><code>send &lt;item.name&gt; &lt;item.state&gt;</code>
   *
   * @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();

      CalendarEventEntry myEntry = new CalendarEventEntry();
      myEntry.setTitle(new PlainTextConstruct("[PresenceSimulation] " + newAlias));
      myEntry.setContent(
          new PlainTextConstruct(
              String.format(executeScript, item.getName(), item.getState().toString())));

      DateTime nowPlusOffset = new DateTime().plusDays(offset);

      com.google.gdata.data.DateTime time =
          com.google.gdata.data.DateTime.parseDateTime(nowPlusOffset.toString());
      When eventTimes = new When();
      eventTimes.setStartTime(time);
      eventTimes.setEndTime(time);
      myEntry.addTime(eventTimes);

      entries.offer(myEntry);

      logger.trace(
          "added new entry '{}' for item '{}' to upload queue",
          myEntry.getTitle().getPlainText(),
          item.getName());
    } else {
      logger.debug(
          "GCal PresenceSimulation Service isn't initialized properly! No entries will be uploaded to your Google Calendar");
    }
  }
 private void upload(CalendarEventEntry entry) {
   try {
     long startTime = System.currentTimeMillis();
     CalendarEventEntry createdEvent = createCalendarEvent(username, password, url, entry);
     logger.debug(
         "succesfully created new calendar event (title='{}', date='{}', content='{}') in {}ms",
         new Object[] {
           createdEvent.getTitle().getPlainText(),
           createdEvent.getTimes().get(0).getStartTime().toString(),
           createdEvent.getPlainTextContent(),
           System.currentTimeMillis() - startTime
         });
   } catch (AuthenticationException ae) {
     logger.error("authentication failed: {}", ae.getMessage());
   } catch (Exception e) {
     logger.error("creating a new calendar entry throws an exception: {}", e.getMessage());
   }
 }
  /**
   * chargement des évéments d'1 calendrier
   *
   * @param email
   * @param dateDebut
   * @param dateFin
   * @return
   * @throws Exception
   */
  public List<EvenementCalendrier> loadEvenements(String email, Date dateDebut, Date dateFin)
      throws Exception {

    List<EvenementCalendrier> result = new ArrayList<EvenementCalendrier>();

    try {
      URL feedUrl = new URL("https://www.google.com/calendar/feeds/" + email + "/private/full");

      CalendarQuery myQuery = new CalendarQuery(feedUrl);
      myQuery.setMinimumStartTime(new DateTime(dateDebut.getTime(), TIME_OFFSET));
      myQuery.setMaximumStartTime(new DateTime(dateFin.getTime(), TIME_OFFSET));

      CalendarEventFeed resultFeed = getService().query(myQuery, CalendarEventFeed.class);
      for (CalendarEventEntry entry : resultFeed.getEntries()) {
        if (!entry.getTimes().isEmpty()) {

          long dateDebEvent = entry.getTimes().get(0).getStartTime().getValue();
          long dateFinEvent = entry.getTimes().get(0).getEndTime().getValue();
          String desc = entry.getContent().toString();
          String titre = entry.getTitle().getPlainText();

          // on ne recupère pas les entrées créées par l'application
          if (titre.startsWith(TypeAbsence.RTT.toString())
              || titre.startsWith(TypeAbsence.CONGE.toString())) {
            break;
          }

          EvenementCalendrier event = new EvenementCalendrier();
          event.setTitre(titre);
          event.setDescription(desc);
          event.setDateDebut(new Date(dateDebEvent));
          event.setDateFin(new Date(dateFinEvent));
          result.add(event);
        }
      }
      Collections.sort(result);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return result;
  }
  /**
   * supprime un événement
   *
   * @param email
   * @param dateDebut
   * @param dateFin
   * @param titre
   * @throws Exception
   */
  public void deleteEvent(String email, Date dateDebut, Date dateFin, String titre)
      throws Exception {

    try {
      URL feedUrl = new URL("https://www.google.com/calendar/feeds/" + email + "/private/full");

      CalendarQuery myQuery = new CalendarQuery(feedUrl);
      myQuery.setMinimumStartTime(new DateTime(dateDebut.getTime(), TIME_OFFSET));
      myQuery.setMaximumStartTime(new DateTime(dateFin.getTime(), TIME_OFFSET));

      CalendarEventFeed resultFeed = getService().query(myQuery, CalendarEventFeed.class);

      for (CalendarEventEntry entry : resultFeed.getEntries()) {
        if (entry.getTitle().getPlainText().startsWith(titre)) {
          entry.delete();
          break;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * créée un nouvl évenement dans un calendrier
   *
   * @param email
   * @param dateDebut
   * @param dateFin
   * @param titre
   * @param description
   * @throws Exception
   */
  public void createEvent(
      String email, Date dateDebut, Date dateFin, String titre, String description)
      throws Exception {

    try {
      CalendarEventEntry myEntry = new CalendarEventEntry();

      myEntry.setTitle(new PlainTextConstruct(titre));
      myEntry.setContent(new PlainTextConstruct(description));

      When eventTimes = new When();
      eventTimes.setStartTime(new DateTime(dateDebut.getTime(), TIME_OFFSET));
      eventTimes.setEndTime(new DateTime(dateFin.getTime(), TIME_OFFSET));
      myEntry.addTime(eventTimes);

      getService()
          .insert(
              new URL("https://www.google.com/calendar/feeds/" + email + "/private/full"), myEntry);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 6
0
    public void accept(CalendarService calendarService) throws IOException, ServiceException {
      final URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/private/full");

      final CalendarQuery calendarQuery = new CalendarQuery(feedUrl);
      calendarQuery.setMinimumStartTime(DateTime.parseDateTime("2011-04-16T00:00:00"));
      calendarQuery.setMaximumStartTime(DateTime.parseDateTime("2011-09-24T23:59:59"));

      final CalendarEventFeed resultFeed =
          calendarService.query(calendarQuery, CalendarEventFeed.class);
      int i = 0;
      for (final CalendarEventEntry eventEntry : resultFeed.getEntries()) {
        final StringBuilder s = new StringBuilder();
        s.append("event #");
        s.append(++i);
        s.append(": '");
        s.append(eventEntry.getTitle().getPlainText());
        s.append("', content: '");
        s.append(eventEntry.getContent().toString());
        s.append("'.");

        System.out.println(s.toString());
      }
    }