2 using System.Collections.Generic;
4 using Sage.O365Management.Classes.OData;
5 using System.Web.Script.Serialization;
11 namespace sage.ew.o365
16 public class CalendarClass : o365consumer
18 #region Public properties 25 get {
return _cName; }
26 set { _cName = value; }
28 private string _cName =
"";
33 public CalendarColor _Color
35 get {
return _oColor; }
36 set { _oColor = value; }
38 private CalendarColor _oColor = CalendarColor.Auto;
43 public Dictionary<string, string> _AvailableCalendars
48 if (!
String.IsNullOrWhiteSpace(_Sage50User) && (_Sage50User != _CurrentUserCode))
50 _dicCalendariosUsuario = null;
55 if (_dicCalendariosUsuario == null)
57 if (!
String.IsNullOrWhiteSpace(_UserCalendars))
60 Dictionary<string, string> ldicCalendarios =
new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(_UserCalendars);
61 if (ldicCalendarios != null && ldicCalendarios.Count > 0) _dicCalendariosUsuario = ldicCalendarios;
70 return _dicCalendariosUsuario;
73 set { _dicCalendariosUsuario = value; }
75 private Dictionary<string, string> _dicCalendariosUsuario = null;
80 public string _RegisteredSharedEmail
82 get {
return _SharedEmail; }
88 public string _RegisteredSharedCalendar
90 get {
return _SharedCalendarId; }
96 public bool _RegisteredShareCalendar
98 get {
return _SharedCalendarActive; }
101 if(_SharedCalendarActive != value)
102 _SharedCalendarActive = value;
106 #endregion Public properties 108 #region Private properties 113 private Calendars _Calendars
115 get {
return _oCalendars; }
119 _dicCalendariosUsuario = _Montar_Diccionario();
120 UpdateCalendarList();
123 private Calendars _oCalendars = null;
128 private Calendar _Calendar
130 get {
return _oCalendar; }
131 set { _oCalendar = value; }
133 private Calendar _oCalendar = null;
136 bool _OldSharedCalendarActive =
false;
137 string _OldSharedCalendarId =
"";
138 string _OldSharedEmail =
"";
139 string _OldSharedRefreshToken =
"";
140 string _OldSharedServiceResourceIds =
"";
142 #endregion Private properties 149 public CalendarClass()
151 _GetSharedBackupData();
158 public CalendarClass(
bool tlRegisterMode)
161 _lRegisterMode = tlRegisterMode;
163 _GetSharedBackupData();
166 #endregion Constructors 168 #region Public Methods 177 Calendar loCalendar =
new Calendar();
178 loCalendar.Name = _Name;
179 loCalendar.Color = _Color;
183 Calendars loCals = (Calendars)O365Management.GetCalendars();
184 bool llExists =
false;
185 foreach (Calendar loCalItem
in loCals.Data)
188 if (loCalItem.Name.Contains(
"Calendario " + Convert.ToString(EW_GLOBAL._GetVariable(
"wc_nameproducto"))))
191 _Calendar = loCalItem;
197 _Calendar = (Calendar)O365Management.CreateCalendar(_Name);
200 llOk = (_Calendar != null);
201 if (llOk && _lRegisterMode)
203 Sage.O365Management.Classes.OData.AccountInfo loAccount = (
Sage.O365Management.Classes.OData.AccountInfo)O365Management.GetAccountInfo();
206 _SharedCalendarId = _Calendar.Id;
207 _SharedEmail = loAccount.Id;
208 _SharedRefreshToken = O365Management.RefreshToken;
209 _SharedServiceResourceIds =
new JavaScriptSerializer().Serialize(O365Management.ServiceResourceIds);
210 OSharedCalendarData._Save();
215 catch (Exception loException)
217 _RaiseException(loException);
227 internal bool _RegisterSharedCalendar()
229 if (!_lRegisterMode)
return false;
232 Calendar loCalendar =
new Calendar();
233 loCalendar.Name = _Name;
234 loCalendar.Color = _Color;
241 Calendars loCals = (Calendars)O365ManagementSage50.GetCalendars();
242 bool llExists =
false;
243 foreach (Calendar loCalItem
in loCals.Data)
246 if (loCalItem.Name.Contains(
"Calendario " + Convert.ToString(EW_GLOBAL._GetVariable(
"wc_nameproducto"))))
249 _Calendar = loCalItem;
255 _Calendar = (Calendar)O365ManagementSage50.CreateCalendar(_Name);
258 llOk = (_Calendar != null);
259 if (llOk && _lRegisterMode)
261 Sage.O365Management.Classes.OData.AccountInfo loAccount = (
Sage.O365Management.Classes.OData.AccountInfo)O365ManagementSage50.GetAccountInfo();
264 _SharedCalendarId = _Calendar.Id;
265 _SharedEmail = loAccount.Id;
266 _SharedRefreshToken = O365ManagementSage50.RefreshToken;
267 _SharedServiceResourceIds =
new JavaScriptSerializer().Serialize(O365ManagementSage50.ServiceResourceIds);
268 OSharedCalendarData._Save();
271 _ResetUserStoredCalendars();
277 catch (Exception loException)
279 _RaiseException(loException);
285 #region Rollback Shared Calendar 293 private void _GetSharedBackupData()
295 _OldSharedCalendarActive = _SharedCalendarActive;
296 _OldSharedCalendarId = _SharedCalendarId;
297 _OldSharedEmail = _SharedEmail;
298 _OldSharedRefreshToken = _SharedRefreshToken;
299 _OldSharedServiceResourceIds = _SharedServiceResourceIds;
306 internal void _RestoreSharedBackupData()
309 _SharedCalendarActive = _OldSharedCalendarActive;
310 _SharedCalendarId = _OldSharedCalendarId;
311 _SharedEmail = _OldSharedEmail;
312 _SharedRefreshToken = _OldSharedRefreshToken;
313 _SharedServiceResourceIds = _OldSharedServiceResourceIds;
314 OSharedCalendarData._Save();
317 #endregion Rollback Shared Calendar 323 internal bool _UnregisterSharedCalendar()
328 if (!_lRegisterMode || !_IsSharingCalendar)
return false;
330 _SharedCalendarId =
"";
332 _SharedRefreshToken =
"";
333 _SharedServiceResourceIds =
"";
334 llOk = OSharedCalendarData._Save();
340 _ResetUserStoredCalendars();
349 public bool _Delete(
string tcIdCalendar)
352 return O365Management.DeleteCalendar(tcIdCalendar);
361 public bool _Add(Appointment toAppointment,
string tcIdCalendar =
"")
367 return ((Appointment)toAppointment)._New(tcIdCalendar); ;
369 catch (Exception loException)
371 _RaiseException(loException);
384 public bool _Add(Meeting toMeeting,
string tcIdCalendar =
"")
390 return toMeeting._New(tcIdCalendar);
392 catch (Exception loException)
394 _RaiseException(loException);
405 public void _GetCalendar(
string tcIdCalendar =
"")
409 _oCalendar = (Calendar)O365Management.GetCalendar(tcIdCalendar);
411 catch (Exception loEx)
413 _RaiseException(loEx);
414 _ErrorMessage = loEx.Message;
422 public void UpdateCalendarList()
424 _UserCalendars =
new JavaScriptSerializer().Serialize(_dicCalendariosUsuario);
431 public Dictionary<string, string> _GetCalendars()
436 _Calendars = (Calendars)O365Management.GetCalendars();
437 return _dicCalendariosUsuario;
439 catch (Exception loEx)
441 _RaiseException(loEx);
445 return new Dictionary<string, string>();
448 #endregion Public Methods 450 #region Private Methods 458 void _This__Evento_UsuarioCambiado(
object sender, EventArgs e)
460 _dicCalendariosUsuario = null;
468 private Events _GetEvents(
string tcIdCalendar)
470 Events loEvents =
new Events();
474 loEvents = (Events)O365Management.GetEvents(tcIdCalendar);
476 catch (Exception loException)
478 _RaiseException(loException);
491 public DataTable _GetEvents(
string tcIdCalendar,
string tcPattern,
bool tlLiteral =
false)
494 CheckForSharedCalendarOperation(tcIdCalendar);
496 DataTable ldtEventos =
new DataTable();
499 ldtEventos.Columns.Add(
"Subject", typeof(
string));
500 ldtEventos.Columns.Add(
"Start", typeof(DateTime));
501 ldtEventos.Columns.Add(
"End", typeof(DateTime));
502 ldtEventos.Columns.Add(
"Id", typeof(
string));
504 Events loEvents = _GetEvents(tcIdCalendar);
506 if (loEvents.Data == null)
return ldtEventos;
508 foreach (Event loEvent
in loEvents.Data)
510 TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(loEvent.Start.TimeZone);
512 DataRow ldrNewRow = ldtEventos.NewRow();
513 ldrNewRow[
"Subject"] = loEvent.Subject;
515 if (loEvent.IsAllDay ==
true)
517 ldrNewRow[
"Start"] = Convert.ToDateTime(loEvent.Start.DateTime);
518 ldrNewRow[
"End"] = Convert.ToDateTime(loEvent.End.DateTime).AddMinutes(-1);
522 ldrNewRow[
"Start"] = TimeZoneInfo.ConvertTime(Convert.ToDateTime(loEvent.Start.DateTime), timeZone, TimeZoneInfo.Local);
523 ldrNewRow[
"End"] = TimeZoneInfo.ConvertTime(Convert.ToDateTime(loEvent.End.DateTime), timeZone, TimeZoneInfo.Local);
528 ldrNewRow[
"Id"] = loEvent.Id;
531 DateTime endEvent = TimeZoneInfo.ConvertTime(Convert.ToDateTime(loEvent.End.DateTime), timeZone, TimeZoneInfo.Local);
532 if (DateTime.Compare(endEvent, DateTime.Now) > 0)
533 ldtEventos.Rows.Add(ldrNewRow);
537 ldtEventos.DefaultView.Sort =
"Start ASC, End ASC";
538 ldtEventos = ldtEventos.DefaultView.ToTable();
541 if (!
String.IsNullOrWhiteSpace(tcPattern))
543 string lcFilterString =
"";
546 char[] laDelimiterChars = {
'[',
']'};
547 string[] laWords = tcPattern.Split(laDelimiterChars,StringSplitOptions.RemoveEmptyEntries);
548 lcFilterString = laWords[0];
549 ldtEventos.DefaultView.RowFilter =
"Subject like '[[]" + lcFilterString +
"[]]%'";
553 lcFilterString = _GetFilterString(tcPattern);
554 ldtEventos.DefaultView.RowFilter =
"Subject like '" + lcFilterString.Replace(
"[",
"[[]") +
"%'";
556 return ldtEventos.DefaultView.ToTable();
566 private void CheckForSharedCalendarOperation(
string tcIdCalendar)
569 _UseSharedCalendar = (_RegisteredSharedCalendar == tcIdCalendar);
576 public string _GetFilterString(
string tcPantalla)
580 DataTable ldtResult =
new DataTable();
581 string lcFilterString =
"";
584 lcSql =
"SELECT pantalla, valor FROM " +
585 DB.SQLDatabase(
"comunes",
"calendar") +
586 " WHERE pantalla = " + DB.SQLString(tcPantalla.Trim().ToUpper());
588 llOk = DB.SQLExec(lcSql, ref ldtResult);
589 if (llOk && ldtResult != null && ldtResult.Rows.Count > 0)
591 string lcValue = Convert.ToString(ldtResult.Rows[0][
"valor"]).Trim();
592 string [] laKeyParts = lcValue.Split(
':');
593 if (laKeyParts.Count() > 1)
595 lcFilterString = laKeyParts[0];
599 laKeyParts = lcValue.Split(
']');
600 if (laKeyParts.Count() > 1)
602 lcFilterString = laKeyParts[0];
604 else lcFilterString =
"";
608 return lcFilterString;
617 private Dictionary<string, string> _Montar_Diccionario()
620 SortedDictionary<string, string> loDic =
new SortedDictionary<string, string>();
621 foreach (Calendar loCalendar
in _Calendars.Data)
624 if (loCalendar.Id == _SharedCalendarId || loCalendar.Name.Contains(
"Calendario " + Convert.ToString(EW_GLOBAL._GetVariable(
"wc_nameproducto"))))
continue;
627 if (loCalendar.Name.Contains(
"Cumpleaños") || loCalendar.Name.Contains(
"ías festivos de"))
continue;
628 loDic.Add(loCalendar.Name, loCalendar.Id);
632 if(_IsSharingCalendar)
633 loDic.Add(
"Calendario " + Convert.ToString(EW_GLOBAL._GetVariable(
"wc_nameproducto")), _SharedCalendarId);
635 return new Dictionary<string, string>(loDic);
638 #endregion Private Methods
static sage.ew.usuario.Usuario _This
Instancia estática del objeto usuario con los datos del usuario actual (wc_usuario) ...
override bool _Save()
Override del método SAVE
Definición de la clase Usuario