czwartek, 6 stycznia 2011

everything I know about Google Calendar Api

source: http://code.google.com/intl/pl-PL/apis/calendar/data/2.0/developers_guide_java.html
// CONNECTING
        CalendarService service = new CalendarService("isod-export");
        service.setUserCredentials("login", "password");

        // RETRIVING ALL CALENDARS USER OWN
        String url = "https://www.google.com/calendar/feeds/default/owncalendars/full";
        URL feedUrl = new URL(url);
        CalendarFeed calendars = service.getFeed(feedUrl, CalendarFeed.class);

        // ITERATING ON CALENDARS
        for (int i = 0; i < calendars.getEntries().size(); i++) {
            CalendarEntry entry = calendars.getEntries().get(i);
            // GETTING CONTENT - CALENDAR DESCRIPTION
            entry.getPlainTextContent();
        }

        // CREATING NEW CALENDAR
        CalendarEntry calendar = new CalendarEntry();
        // FILLING UP CALENDAR
        calendar.setTitle(new PlainTextConstruct("CALENDAR NAME"));
        calendar.setSummary(new PlainTextConstruct("CALENDAR DESCRIPTION"));
        calendar.setTimeZone(new TimeZoneProperty("Europe/Warsaw"));
        calendar.setHidden(HiddenProperty.FALSE);
        calendar.setColor(new ColorProperty("#2952A3"));
        calendar.addLocation(new Where("", "", "NAME OF LOCATION"));
        // SENDING CALENDAR TO GOOGLE
        url = "https://www.google.com/calendar/feeds/default/owncalendars/full";
        URL postUrl = new URL(url);
        CalendarEntry inserted = service.insert(postUrl, calendar);

        // CREATING CALENDAR URL FROM ID
        String id = inserted.getId();
        url = id.replace("%40", "@").replace("http", "https").replace("default/calendars/", "") + "/private/full";

        // RETRIVING ENTRIES FROM CALENDAR
        feedUrl = new URL(url);
        CalendarEventFeed myFeed = service.getFeed(feedUrl, CalendarEventFeed.class);

        // ITERATING ON ENTRIES
        for (int i = 0; i < myFeed.getEntries().size(); i++) {
            CalendarEventEntry eventEntry = myFeed.getEntries().get(i);
        }
        // CREATING NEW ENTRY
        CalendarEventEntry newEntry = new CalendarEventEntry();
        Recurrence recur = new Recurrence();
        // CREATING RECURRENCE
        /*
         * DTSTART - start of event
         * TZID - time zone (must be same as calendar)
         * 20110110T081500 - date: yyyymmddThhmmss
         * DTEND - end of event
         * RRULE - repeating
         * BYDAY - available options: SU,MO,TU,WE,TH,FR,SA
         */
        String recurrence = "DTSTART;TZID=Europe/Warsaw:20110110T081500;\r\n"
                + "DTEND;TZID=Europe/Warsaw:20110110T100000;\r\n"
                + "RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=Mo,Tu;COUNT=15\r\n";
        recur.setValue(recurrence);
        // FILLING ENTRY WITH DATA
        newEntry.setRecurrence(recur);
        newEntry.addLocation(new Where("", "", "CLASSROOM"));
        newEntry.setTitle(new PlainTextConstruct("NAME OF SUBJECT"));
        newEntry.setContent(new PlainTextConstruct("DESCRIPTION OF SUBJECT (contains ID)"));

        // AND BATCHING LIKE ON http://code.google.com/intl/pl-PL/apis/calendar/data/2.0/developers_guide_java.html#batch
        // Get some events to operate on.
        URL defaultEventFeedUrl = new URL("https://www.google.com/calendar/feeds/default/private/full");
        CalendarEventFeed feed = service.getFeed(defaultEventFeedUrl, CalendarEventFeed.class);

// Create an batch entry to insert a new event.
        CalendarEventEntry toInsert = new CalendarEventEntry();
        toInsert.setTitle(new PlainTextConstruct("first batch event"));
        BatchUtils.setBatchId(toInsert, "1");
        BatchUtils.setBatchOperationType(toInsert, BatchOperationType.INSERT);

// Create an batch entry to query an event.
        CalendarEventEntry toQuery = feed.getEntries().get(0);
        BatchUtils.setBatchId(toQuery, "2");
        BatchUtils.setBatchOperationType(toQuery, BatchOperationType.QUERY);

// Create an batch entry to update an existing event.
        CalendarEventEntry toUpdate = feed.getEntries().get(1);
        toUpdate.setTitle(new PlainTextConstruct("updated via batch"));
        BatchUtils.setBatchId(toUpdate, "3");
        BatchUtils.setBatchOperationType(toUpdate, BatchOperationType.UPDATE);

// Create an batch entry to delete an existing event.
        CalendarEventEntry toDelete = feed.getEntries().get(2);
        BatchUtils.setBatchId(toDelete, "4");
        BatchUtils.setBatchOperationType(toDelete, BatchOperationType.DELETE);

// Add the entries to a new feed.
        CalendarEventFeed batchRequest = new CalendarEventFeed();
        batchRequest.getEntries().add(toInsert);
        batchRequest.getEntries().add(toQuery);
        batchRequest.getEntries().add(toUpdate);
        batchRequest.getEntries().add(toDelete);

// Get the batch link URL and send the batch request there.
        Link batchLink = feed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
        CalendarEventFeed batchResponse = service.batch(new URL(batchLink.getHref()), batchRequest);

// Ensure that all the operations were successful.
        boolean isSuccess = true;
        for (CalendarEventEntry entry : batchResponse.getEntries()) {
            String batchId = BatchUtils.getBatchId(entry);
            if (!BatchUtils.isSuccess(entry)) {
                isSuccess = false;
                BatchStatus status = BatchUtils.getBatchStatus(entry);
                System.out.println("\n" + batchId + " failed (" + status.getReason() + ") " + status.getContent());
            }
        }
        if (isSuccess) {
            System.out.println("Successfully deleted all events via batch request.");
        }
    }

Brak komentarzy:

Prześlij komentarz