/**
*
*/
package ddoa.project.luminis.dashboard.paginas.calendar;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;
import org.joda.time.DateMidnight;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.CredentialStore;
import com.google.api.client.auth.oauth2.MemoryCredentialStore;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.Event;
import ddoa.project.luminis.dashboard.config.exceptions.CalendarException;
/**
* File description<br/>
* <br/>
* Project LuminisDashboardWebapp<br/>
* ddoa.project.luminis.dashboard.paginas.calendar.CalendarAPI.java<br/>
* Created on May 29, 2014 2:26:26 PM
*
* @author Brord van Wierst
*/
public class CalendarAPI {
/** Global instance of the HTTP transport. */
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
/** Global instance of the credential storage */
private static final CredentialStore credentialStore = new MemoryCredentialStore();
String emailId;
String clientId;
String clientSecret;
/**
*
* @param emailId
* @param clientId
* @param clientSecret
*/
public CalendarAPI(String emailId, String clientId, String clientSecret) {
this.emailId = emailId;
this.clientId = clientId;
this.clientSecret = clientSecret;
}
public Credential getCredential() {
try {
Credential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setClientSecrets(clientId, clientSecret)
.setJsonFactory(JSON_FACTORY).build();
credentialStore.store(emailId, credential);
if (credential != null && credentialStore.load(emailId, credential)) {
return credential;
} else {
return null;
}
} catch (IOException e) {
throw new CalendarException("Could not load credential", e);
}
}
public List<Event> getEvents() throws CalendarException {
Credential credential = getCredential();
if (credential != null) {
Calendar cal = new Calendar.Builder(
HTTP_TRANSPORT,
JSON_FACTORY,
credential)
.setApplicationName("Luminis").build();
try {
List<Event> googleEvents = cal
.events()
.list(emailId)
.setSingleEvents(true)
.setOrderBy("startTime")
.setTimeMin(
new DateTime(new DateMidnight().toDate(),
TimeZone.getDefault()))
.setTimeMax(
new DateTime(new DateMidnight().toDateTime()
.plusDays(1).minusMillis(1).toDate(),
TimeZone.getDefault())).execute()
.getItems();
// Google returns null when there are not results
if (googleEvents != null) {
return googleEvents;
} else {
return new LinkedList<Event>();
}
} catch (IOException ioe) {
throw new CalendarException(
"An error occurred when retrieving events", ioe);
}
} else {
return null;
}
}
}
Comments
0 B
|👍
/👎