aegis123

AsyncBatchInsertEvent.java

Jan 16th, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.95 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2012 Google Inc.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5.  * in compliance with the License. You may obtain a copy of the License at
  6.  *
  7.  * http://www.apache.org/licenses/LICENSE-2.0
  8.  *
  9.  * Unless required by applicable law or agreed to in writing, software distributed under the License
  10.  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11.  * or implied. See the License for the specific language governing permissions and limitations under
  12.  * the License.
  13.  */
  14.  
  15. package com.sanoma.hockeynl.standenmotor.calender;
  16.  
  17. import android.util.Log;
  18.  
  19. import com.google.api.client.googleapis.GoogleHeaders;
  20. import com.google.api.client.googleapis.batch.BatchRequest;
  21. import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
  22. import com.google.api.client.googleapis.json.GoogleJsonError;
  23. import com.google.api.client.util.DateTime;
  24. import com.google.api.services.calendar.model.Event;
  25. import com.google.api.services.calendar.model.EventDateTime;
  26. import com.sanoma.hockeynl.standenmotor.ListController;
  27. import com.sanoma.hockeynl.standenmotor.ListController.Teams;
  28.  
  29. import java.io.IOException;
  30. import java.text.ParseException;
  31. import java.text.SimpleDateFormat;
  32. import java.util.ArrayList;
  33. import java.util.Date;
  34. import java.util.List;
  35. import java.util.Locale;
  36. import java.util.TimeZone;
  37.  
  38. /**
  39.  * Asynchronously insert a new event on a given calendar.
  40.  *
  41.  * original author Yaniv Inbar on http://code.google.com/p/google-api-java-client/source/browse/calendar-android-sample/src/main/java/com/google/api/services/samples/calendar/android/AsyncBatchInsertCalendars.java?repo=samples
  42.  * @author Dylan Drost
  43.  */
  44. public class AsyncBatchInsertEvent extends EventAsyncTask {
  45.  
  46.     private final static String TAG = "AsyncBatchInsertEvent";
  47.  
  48.     private final ArrayList<Teams> teamList;
  49.     private final List<Event> events;
  50.     private final CalendarInfo calenderInfo;
  51.     private final String id;
  52.  
  53.     AsyncBatchInsertEvent(ListController activity, CalendarInfo calendar, ArrayList<Teams> teamList, String id) {
  54.         super(activity);
  55.         this.teamList = teamList;
  56.         this.calenderInfo = calendar;
  57.         this.id = id;
  58.         events = new ArrayList<Event>();
  59.     }
  60.  
  61.     @Override
  62.     protected void doInBackground() throws IOException {
  63.         Event tmpEvent;
  64.         Log.d(TAG, "team ID: " + id);
  65.         for (final Teams team : teamList){
  66.             final String[] array = team.getProperties();
  67.             Log.d(TAG, "team 1 ID: " + array[2]);
  68.             Log.d(TAG, "team 2 ID: " + array[3]);
  69.             if(array[2] == id || array[3] == id) {
  70.                 tmpEvent = new Event();
  71.                 tmpEvent.setSummary(array[4] + " - " + array[5]);
  72.                 tmpEvent.setLocation(array[11] + ", " + array[12]);
  73.                 Date startDate = null;
  74.                 final String[] formats = new String[] {
  75.                         "yyyy-MM-dd HH:mm",
  76.                         "yyyy-MM-dd",
  77.                 };
  78.                 for (final String format : formats) {
  79.                     String tmp = array[0] + " " + array[1];
  80.                     tmp = tmp.trim();
  81.                     try {
  82.                         startDate = new SimpleDateFormat(format, Locale.US).parse(tmp);
  83.                     } catch (final ParseException e) {
  84.                         //throw new RuntimeException(e);
  85.                     }
  86.                     Log.d(TAG, tmp);
  87.                 }
  88.                 if (startDate == null) {
  89.                     startDate = new Date();
  90.                 }
  91.                 final Date endDate = new Date(startDate.getTime() + 3600000);
  92.                 final DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
  93.                 tmpEvent.setStart(new EventDateTime().setDateTime(start));
  94.                 final DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
  95.                 tmpEvent.setEnd(new EventDateTime().setDateTime(end));
  96.                 events.add(tmpEvent);
  97.             }
  98.         }
  99.  
  100.         final BatchRequest batch = client.batch();
  101.         /*final JsonBatchCallback<Event> callback = new JsonBatchCallback<Event>() {
  102.             @Override
  103.             public void onSuccess(Event t, GoogleHeaders responseHeaders) throws IOException {
  104.                 // TODO Calender ADD succes message on batch event adding
  105.             }
  106.             @Override
  107.             public void onFailure(GoogleJsonError err, GoogleHeaders responseHeaders)
  108.                     throws IOException {
  109.                 Utils.logAndShowError(activity, ListController.LOGTAG, err.getMessage());
  110.             }
  111.         };*/
  112.         for (final Event event : events) {
  113.             client.events().insert(calenderInfo.id, event).queue(batch, new JsonBatchCallback<Event>() {
  114.                 @Override
  115.                 public void onSuccess(Event t, GoogleHeaders responseHeaders) throws IOException {
  116.                     // TODO Calender ADD succes message on batch event adding
  117.                 }
  118.                 @Override
  119.                 public void onFailure(GoogleJsonError err, GoogleHeaders responseHeaders)
  120.                         throws IOException {
  121.                     Utils.logAndShowError(activity, ListController.LOGTAG, err.getMessage());
  122.                 }
  123.             });
  124.         }
  125.         try {
  126.             batch.execute();
  127.         } catch (final Exception e) {
  128.             Log.e("ERR", "", e);
  129.             throw new RuntimeException(e);
  130.         }
  131.     }
  132.  
  133.     public static void batchInsertEvents(ListController activity, CalendarInfo calender, ArrayList<Teams> teamList, String id) {
  134.         new AsyncBatchInsertEvent(activity, calender, teamList, id).execute();
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment