Advertisement
Brord

fkn googl

May 29th, 2014
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package ddoa.project.luminis.dashboard.paginas.calendar;
  5.  
  6. import java.io.IOException;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.TimeZone;
  10.  
  11. import org.joda.time.DateMidnight;
  12.  
  13. import com.google.api.client.auth.oauth2.Credential;
  14. import com.google.api.client.auth.oauth2.CredentialStore;
  15. import com.google.api.client.auth.oauth2.MemoryCredentialStore;
  16. import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
  17. import com.google.api.client.http.HttpTransport;
  18. import com.google.api.client.http.javanet.NetHttpTransport;
  19. import com.google.api.client.json.JsonFactory;
  20. import com.google.api.client.json.jackson2.JacksonFactory;
  21. import com.google.api.client.util.DateTime;
  22. import com.google.api.services.calendar.Calendar;
  23. import com.google.api.services.calendar.model.Event;
  24.  
  25. import ddoa.project.luminis.dashboard.config.exceptions.CalendarException;
  26.  
  27. /**
  28.  * File description<br/>
  29.  * <br/>
  30.  * Project LuminisDashboardWebapp<br/>
  31.  * ddoa.project.luminis.dashboard.paginas.calendar.CalendarAPI.java<br/>
  32.  * Created on May 29, 2014 2:26:26 PM
  33.  *
  34.  * @author Brord van Wierst
  35.  */
  36. public class CalendarAPI {
  37.     /** Global instance of the HTTP transport. */
  38.     private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
  39.  
  40.     /** Global instance of the JSON factory. */
  41.     private static final JsonFactory JSON_FACTORY = new JacksonFactory();
  42.  
  43.     /** Global instance of the credential storage */
  44.     private static final CredentialStore credentialStore = new MemoryCredentialStore();
  45.  
  46.     String emailId;
  47.     String clientId;
  48.     String clientSecret;
  49.  
  50.     /**
  51.      *
  52.      * @param emailId
  53.      * @param clientId
  54.      * @param clientSecret
  55.      */
  56.     public CalendarAPI(String emailId, String clientId, String clientSecret) {
  57.         this.emailId = emailId;
  58.         this.clientId = clientId;
  59.         this.clientSecret = clientSecret;
  60.     }
  61.  
  62.     public Credential getCredential() {
  63.         try {
  64.             Credential credential = new GoogleCredential.Builder()
  65.                     .setTransport(HTTP_TRANSPORT)
  66.                     .setClientSecrets(clientId, clientSecret)
  67.                     .setJsonFactory(JSON_FACTORY).build();
  68.             credentialStore.store(emailId, credential);
  69.             if (credential != null && credentialStore.load(emailId, credential)) {
  70.                 return credential;
  71.             } else {
  72.                 return null;
  73.             }
  74.         } catch (IOException e) {
  75.             throw new CalendarException("Could not load credential", e);
  76.         }
  77.     }
  78.  
  79.     public List<Event> getEvents() throws CalendarException {
  80.         Credential credential = getCredential();
  81.         if (credential != null) {
  82.             Calendar cal = new Calendar.Builder(
  83.                     HTTP_TRANSPORT,
  84.                     JSON_FACTORY,
  85.                     credential)
  86.             .setApplicationName("Luminis").build();
  87.  
  88.             try {
  89.                 List<Event> googleEvents = cal
  90.                         .events()
  91.                         .list(emailId)
  92.                         .setSingleEvents(true)
  93.                         .setOrderBy("startTime")
  94.                         .setTimeMin(
  95.                                 new DateTime(new DateMidnight().toDate(),
  96.                                         TimeZone.getDefault()))
  97.                         .setTimeMax(
  98.                                 new DateTime(new DateMidnight().toDateTime()
  99.                                         .plusDays(1).minusMillis(1).toDate(),
  100.                                         TimeZone.getDefault())).execute()
  101.                         .getItems();
  102.  
  103.                 // Google returns null when there are not results
  104.                 if (googleEvents != null) {
  105.                     return googleEvents;
  106.                 } else {
  107.                     return new LinkedList<Event>();
  108.                 }
  109.             } catch (IOException ioe) {
  110.                 throw new CalendarException(
  111.                         "An error occurred when retrieving events", ioe);
  112.             }
  113.         } else {
  114.             return null;
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement