/Silex_GoogleCalendarConnector/GoogleCalendarConnector.cs

https://bitbucket.org/elodie_roumieux/silex-calendar-connector · C# · 242 lines · 145 code · 45 blank · 52 comment · 12 complexity · a330d4037c27d2b3a0ab09a38b97c7c8 MD5 · raw file

  1. using Google.GData.Calendar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Google.GData.Client;
  8. using Google.GData.Extensions;
  9. namespace Silex_GoogleCalendarConnector
  10. {
  11. public class GoogleCalendarConnector
  12. {
  13. private List<CalendarConnectionInformations> _connectionInformations;
  14. public GoogleCalendarConnector(CalendarConnectionInformations connectionInformations)
  15. {
  16. _connectionInformations = new List<CalendarConnectionInformations>{connectionInformations};
  17. }
  18. public GoogleCalendarConnector(List<CalendarConnectionInformations> connectionInformations)
  19. {
  20. _connectionInformations = connectionInformations ;
  21. }
  22. /// <summary>
  23. /// Returns a list of the Id of the created event in each calendar, should be stocked for future uses of the event (update/delete).
  24. /// </summary>
  25. /// <param name="connectionInformationsList">List of connectionInformation with feedUri of the different calendars.</param>
  26. /// <returns>Returns a list of the id of the created event in each calendar, should be stocked for future uses of the event (update/delete).</returns>
  27. public List<string> CreateEvent(EventEntry eventToCreate)
  28. {
  29. List<string> eventIdList = new List<string>();
  30. foreach (var connectionInformation in _connectionInformations)
  31. {
  32. string eventId = CreateEventInCalendar(connectionInformation, eventToCreate);
  33. if (eventId == null)
  34. {
  35. return null;
  36. }
  37. eventIdList.Add(eventId);
  38. }
  39. return eventIdList;
  40. }
  41. /// <summary>
  42. /// Returns the id of the created event, should be stocked for future uses of the event (update/delete).
  43. /// </summary>
  44. /// <returns>Returns the id of the created event, should be stocked for future uses of the event (update/delete). </returns>
  45. public string CreateEventInCalendar(CalendarConnectionInformations connectionInformations, EventEntry eventToCreate)
  46. {
  47. // Authenticate
  48. CalendarService service = new CalendarService("");
  49. service.setUserCredentials(connectionInformations.username, connectionInformations.password);
  50. Uri postUri = new Uri(connectionInformations.feedUri);
  51. AtomEntry insertedEntry = service.Insert(postUri, eventToCreate);
  52. //Verify the event has been created
  53. if (insertedEntry == null)
  54. return null;
  55. else
  56. {
  57. return insertedEntry.EditUri.ToString();
  58. }
  59. }
  60. /// <summary>
  61. /// Returns a list of the Id of the created event in each calendar, should be stocked for future uses of the event (update/delete).
  62. /// </summary>
  63. /// <param name="connectionInformationsList">List of connectionInformation with feedUri of the different calendars.</param>
  64. /// <returns>Returns a list of the id of the created event in each calendar, should be stocked for future uses of the event (update/delete).</returns>
  65. public List<string> CreateEventInMultipleCalendars(List<CalendarConnectionInformations> connectionInformationsList, EventEntry eventToCreate)
  66. {
  67. List<string> eventIdList = new List<string>();
  68. foreach (var connectionInformation in connectionInformationsList)
  69. {
  70. string eventId = CreateEventInCalendar(connectionInformation, eventToCreate);
  71. if (eventId==null)
  72. {
  73. return null;
  74. }
  75. eventIdList.Add(eventId);
  76. }
  77. return eventIdList;
  78. }
  79. /// <summary>
  80. /// Create an event in a calendar if it doesn't already exist, else update it.
  81. /// Returns the id of the created event, should be stocked for future uses of the event (update/delete).
  82. /// </summary>
  83. /// <param name="eventToCreateId">String containig eventId if it already exist, else null.</param>
  84. /// <returns>Returns the id of the created event, should be stocked for future uses of the event (update/delete).</returns>
  85. public string CreateOrUpdateEventInCalendar(CalendarConnectionInformations connectionInformations, EventEntry eventToCreate, string eventToCreateId)
  86. {
  87. if(eventToCreateId!=null)
  88. {
  89. return UpdateEvent(connectionInformations, eventToCreate, eventToCreateId);
  90. }
  91. else
  92. return CreateEventInCalendar(connectionInformations, eventToCreate);
  93. }
  94. /// <summary>
  95. /// Create an event in multiple calendars if they don't already exist, else update them.
  96. /// Returns a list of the id of the created event in each calendar, should be stocked for future uses of the event (update/delete).
  97. /// </summary>
  98. /// <param name="eventToCreateIdList">If the event already exist, it's a list of string containig eventId of each calendar, else it's null.</param>
  99. /// <returns> Returns failed event ids.</returns>
  100. public List<string> UpdateEventInMultipleCalendars(List<CalendarConnectionInformations> connectionInformationsList, EventEntry eventToCreate, List<string> eventToCreateIdList)
  101. {
  102. List<string> eventIdList = new List<string>();
  103. int i = 0;
  104. foreach (var connectionInformation in connectionInformationsList)
  105. {
  106. string eventId =UpdateEvent(connectionInformation, eventToCreate, eventToCreateIdList[i]);
  107. if (eventId==null)
  108. {
  109. eventIdList.Add(eventToCreateIdList[i]);
  110. }
  111. i++;
  112. }
  113. return eventIdList;
  114. }
  115. public void DeleteEvent(CalendarConnectionInformations connectionInformations, string eventToDeleteId)
  116. {
  117. CalendarService service = new CalendarService("");
  118. service.setUserCredentials(connectionInformations.username, connectionInformations.password);
  119. EventEntry eventRetrieved = new EventEntry();
  120. eventRetrieved = retrieveEventFromEventId(eventToDeleteId, service);
  121. if (eventRetrieved != null)
  122. {
  123. eventRetrieved.Delete();
  124. }
  125. }
  126. public void DeleteEventInMultipleCalendars(List<CalendarConnectionInformations> connectionInformationsList, List<string> eventToDeleteIdList)
  127. {
  128. int i = 0;
  129. foreach (var connectionInformation in connectionInformationsList)
  130. {
  131. DeleteEvent(connectionInformation, eventToDeleteIdList[i]);
  132. i++;
  133. }
  134. }
  135. /// <summary>
  136. /// Returns the event corresponding to the event id.
  137. /// </summary>
  138. /// <returns>Returns the event corresponding to the event id.</returns>
  139. private EventEntry retrieveEventFromEventId(string originalEventId, CalendarService service)
  140. {
  141. EventEntry eventRetrieved = new EventEntry();
  142. // Create the query object:
  143. EventQuery query = new EventQuery();
  144. string Url = originalEventId.ToString();
  145. query.Uri = new Uri(Url);
  146. // Tell the service to query:
  147. EventFeed calFeed = service.Query(query);
  148. if (calFeed.Entries.Count > 0)
  149. {
  150. eventRetrieved = (EventEntry)calFeed.Entries[0];
  151. return eventRetrieved;
  152. }
  153. return null;
  154. }
  155. /// <summary>
  156. /// Update an already created event by changind its caracteristics to thoses of a new event.
  157. /// Returns the id of the event.
  158. /// </summary>
  159. /// <returns>Returns the id of the event.</returns>
  160. private string UpdateEvent(CalendarConnectionInformations connectionInformations, EventEntry newEvent, string originalEventId)
  161. {
  162. // Authenticate
  163. CalendarService service = new CalendarService("");
  164. service.setUserCredentials(connectionInformations.username, connectionInformations.password);
  165. EventEntry eventRetrieved = new EventEntry();
  166. eventRetrieved = retrieveEventFromEventId(originalEventId, service);
  167. if (eventRetrieved != null)
  168. {
  169. eventRetrieved = MergeEvents(eventRetrieved, newEvent);
  170. eventRetrieved.Update();
  171. return eventRetrieved.EditUri.ToString();
  172. }
  173. return null;
  174. }
  175. /// <summary>
  176. /// Get the caracteristics of a new event to a first event.
  177. /// Returns the first event modified.
  178. /// </summary>
  179. /// <returns>Returns the first event modified.</returns>
  180. private EventEntry MergeEvents(EventEntry baseEvent, EventEntry newEvent)
  181. {
  182. baseEvent.Title = newEvent.Title;
  183. baseEvent.Content = newEvent.Content;
  184. Where baseEventWhere = new Where();
  185. baseEventWhere.ValueString = newEvent.Locations.ToString();
  186. baseEvent.Locations.Add(baseEventWhere);
  187. //regler Times aussi
  188. //When baseEventeWhen = new When();
  189. //baseEventWhen.StartTime = newEvent.Times.S
  190. //baseEventWhen.EndTime=newEvent.Times.
  191. //baseEvent.Times.Add(baseEventWhen);
  192. //regler participant
  193. //+reminder?
  194. return baseEvent;
  195. }
  196. }
  197. }