de.bremen.ugs.calendar.CalendarHelper.java Source code

Java tutorial

Introduction

Here is the source code for de.bremen.ugs.calendar.CalendarHelper.java

Source

/*
 * Bremer-Usergroup Calendar - This program centralizes Google Calendars in one
 * 
 * Copyright (C) 2012 Steve Liedtke <sliedtke57@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License as published by the Free Software Foundation; either version 3 of 
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 
 * the GNU General Public License for more details.
 * 
 * You can find a copy of the GNU General Public License on http://www.gnu.org/licenses/gpl.html.
 * 
 * Contributors:
 *     Steve Liedtke <sliedtke57@gmail.com>
 */
package de.bremen.ugs.calendar;

import static de.bremen.ugs.common.GlobalInstances.HTTP_TRANSPORT;
import static de.bremen.ugs.common.GlobalInstances.JSON_FACTORY;

import java.io.IOException;
import java.util.logging.Logger;

import com.google.api.client.extensions.appengine.auth.oauth2.AppEngineCredentialStore;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.CalendarList.List;

import de.bremen.ugs.common.DeployConstants;
import de.bremen.ugs.credential.CredentialHelper;

/**
 * This class is used by the calendar.jsp to get the calendar api service and the
 * calendars of the user.
 * 
 * @author Steve Liedtke
 * 
 */
public final class CalendarHelper {

    /**
     * holding the {@link Calendar} service.
     */
    private Calendar calendar = null;

    /**
     * holding the user id of the user.
     */
    private String userId;

    /**
     * static attribute used for logging.
     */
    private static final Logger logger = Logger.getLogger(CalendarHelper.class.getName());

    /**
     * Standard constructor.
     * 
     * @param code
     *            given code resulted by the ouath request
     * @param userId
     *            given user id
     */
    public CalendarHelper(final String code, final String userId) {
        this.userId = userId;
        try {
            if (code == null) {
                this.calendar = this.loadService();
            } else {
                this.calendar = this.setUp(code);
            }
        } catch (IOException e) {
            logger.warning("IOException occured: " + e.getMessage());
        }
    }

    /**
     * Loads the {@link Calendar} service. This method is called by the
     * constructor if 'code' is null.
     * 
     * @return {@link Calendar} object
     */
    private Calendar loadService() {
        final GoogleCredential credential = CredentialHelper.loadFromDatastore(this.userId);

        Calendar calendar = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(DeployConstants.APP_NAME + "/" + DeployConstants.APP_VERSION).build();
        return calendar;
    }

    /**
     * Sets up the {@link Calendar} service. This method is called when a valid
     * code is given.
     * 
     * @param code
     *            given code
     * @return {@link Calendar} service
     * @throws IOException
     *             thrown in case the calendar api throws it
     */
    private Calendar setUp(final String code) throws IOException {
        final AppEngineCredentialStore credentialStore = new AppEngineCredentialStore();

        GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(HTTP_TRANSPORT, JSON_FACTORY,
                DeployConstants.CLIENT_ID, DeployConstants.CLIENT_SECRET, code, DeployConstants.REDIRECT_URL)
                        .execute();

        final GoogleCredential credential = CredentialHelper.loadCredential(this.userId);

        credential.setAccessToken(response.getAccessToken());
        credential.setRefreshToken(response.getRefreshToken());
        credential.setExpiresInSeconds(response.getExpiresInSeconds());

        credentialStore.store(userId, credential);

        Calendar calendar = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(DeployConstants.APP_NAME + "/" + DeployConstants.APP_VERSION).build();

        return calendar;
    }

    /**
     * Returns all calendars of the user.
     * 
     * @return {@link List} of calendars
     */
    public List getCalendars() {
        List list = null;
        try {
            list = this.calendar.calendarList().list();
        } catch (IOException e) {
            // do nothing
        }
        return list;
    }
}