/// <summary> /// Persists the tour outlook synchronize. /// </summary> /// <param name="tour">The tour.</param> /// <param name="toursDb">The tours database.</param> /// <param name="db">The database.</param> public void UpdateCRMOutlookMap(Tour tour, RequestOrigin?requestedFrom) { CRMOutlookSyncDb outlookSyncDb; var db = ObjectContextFactory.Create(); if (tour.Id > 0) { var taskCurrentSyncStatus = requestedFrom != RequestOrigin.Outlook ? (short)OutlookSyncStatus.NotInSync : (short)OutlookSyncStatus.InSync; outlookSyncDb = db.CRMOutlookSync.Where(o => o.EntityID == tour.Id && o.EntityType == (byte)AppModules.ContactTours).FirstOrDefault(); if (outlookSyncDb != null) { outlookSyncDb.SyncStatus = taskCurrentSyncStatus; } else { outlookSyncDb = new CRMOutlookSyncDb() { EntityID = tour.Id, SyncStatus = taskCurrentSyncStatus, LastSyncDate = tour.LastUpdatedOn, LastSyncedBy = tour.LastUpdatedBy, EntityType = (byte)AppModules.ContactTours, OutlookKey = string.Empty }; db.Entry <CRMOutlookSyncDb>(outlookSyncDb).State = System.Data.Entity.EntityState.Added; } db.SaveChanges(); } }
/// <summary> /// Gets the tours to synchronize. /// </summary> /// <param name="accountId">The account identifier.</param> /// <param name="userId">The user identifier.</param> /// <param name="maxRecords">The maximum records.</param> /// <param name="timeStamp">The time stamp.</param> /// <param name="firstSync">if set to <c>true</c> [first synchronize].</param> /// <param name="operationType">Type of the operation.</param> /// <returns></returns> public IEnumerable <Tour> GetToursToSyncBackUp(int accountId, int userId, int?maxRecords, DateTime?timeStamp, bool firstSync, CRUDOperationType operationType) { Logger.Current.Verbose("TourRepository/GetToursToSync, parameters: " + accountId + ", " + userId + ", " + maxRecords + ", " + timeStamp + ", " + firstSync); var db = ObjectContextFactory.Create(); if (firstSync) { var userFirstTours = db.CRMOutlookSync.Join(db.Tours.Where(a => a.CreatedBy == userId), co => co.EntityID, t => t.TourID, (co, t) => new { TourId = co.EntityID, UserId = t.CreatedBy, AccountId = t.AccountID }).ToList(); var tourIds = userFirstTours.Select(ut => ut.TourId).ToList().Distinct(); if (operationType == CRUDOperationType.Create) { db.CRMOutlookSync.Where(c => tourIds.Contains(c.EntityID) && c.EntityType == (byte)AppModules.ContactTours && c.SyncStatus == (short)OutlookSyncStatus.Syncing) .Each(c => { c.SyncStatus = (short)OutlookSyncStatus.NotInSync; }); } var firstTimeSyncTours = db.Tours.Where(c => c.CreatedBy == userId && !tourIds.Contains(c.TourID)) .Select(c => c.TourID); foreach (int id in firstTimeSyncTours) { CRMOutlookSyncDb tourOutlookSyncDb = new CRMOutlookSyncDb(); tourOutlookSyncDb.EntityID = id; tourOutlookSyncDb.SyncStatus = (short)OutlookSyncStatus.NotInSync; tourOutlookSyncDb.EntityType = (byte)AppModules.ContactTours; db.Entry <CRMOutlookSyncDb>(tourOutlookSyncDb).State = System.Data.Entity.EntityState.Added; } db.SaveChanges(); } var userTours = db.CRMOutlookSync.Where(co => co.SyncStatus == (short)OutlookSyncStatus.NotInSync) .Join(db.Tours.Where(a => a.CreatedBy == userId), co => co.EntityID, t => t.TourID, (co, t) => new { TourId = co.EntityID, UserId = t.CreatedBy, AccountId = t.AccountID }); var tourIdsToSync = userTours.Select(ut => ut.TourId).ToList().Distinct(); var toursToSyncDb = db.Tours .Where(c => c.AccountID == accountId && c.CreatedBy == userId && tourIdsToSync.Contains(c.TourID)) .Take((int)maxRecords).ToList(); return(Mapper.Map <IEnumerable <TourDb>, IEnumerable <Tour> >(toursToSyncDb)); }
/// <summary> /// Gets the tours to synchronize. /// </summary> /// <param name="accountId">The account identifier.</param> /// <param name="userId">The user identifier.</param> /// <param name="maxNumRecords">The maximum number records.</param> /// <param name="timeStamp">The time stamp.</param> /// <param name="firstSync">if set to <c>true</c> [first synchronize].</param> /// <param name="operationType">Type of the operation.</param> /// <returns></returns> public IEnumerable <Tour> GetToursToSync(int accountId, int userId, int?maxNumRecords, DateTime?timeStamp, bool firstSync, CRUDOperationType operationType) { Logger.Current.Verbose("TourRepository/GetToursToSync, parameters: " + accountId + ", " + userId + ", " + maxNumRecords + ", " + timeStamp + ", " + firstSync); var db = ObjectContextFactory.Create(); //IEnumerable<CRMOutlookSyncDb> contactsToSyncDb; if (firstSync) { var tourIds = db.Get <int>(@"select EntityID from CRMOutlookSync os inner join tours c on os.EntityID = c.TourId where entitytype = 7 and c.createdby = @userId" , new { userId = userId }); if (operationType == CRUDOperationType.Create) { var sql = @"update CRMOutlookSync set SyncStatus = @notInSync where (SyncStatus = 11 or syncstatus = 13) and entityid in @tourIds and entitytype = 7"; db.Execute(sql, new { notInSync = 11, tourIds = tourIds }); } var firstTimeSyncTours = db.Tours .Where(c => c.CreatedBy == userId && !tourIds.Contains(c.TourID)) .Select(c => c.TourID).ToList(); foreach (int id in firstTimeSyncTours) { CRMOutlookSyncDb tourOutlookSyncDb = new CRMOutlookSyncDb(); tourOutlookSyncDb.EntityID = id; tourOutlookSyncDb.SyncStatus = (short)OutlookSyncStatus.NotInSync; tourOutlookSyncDb.EntityType = (byte)AppModules.ContactTours; db.Entry <CRMOutlookSyncDb>(tourOutlookSyncDb).State = System.Data.Entity.EntityState.Added; } db.SaveChanges(); } var tourIdsToSync = db.CRMOutlookSync.Where(c => c.SyncStatus == (short)OutlookSyncStatus.NotInSync && c.EntityType == (byte)AppModules.ContactTours) .Select(c => c.EntityID); var tourIds1ToSyncDb = db.Tours .Where(c => c.AccountID == accountId && c.CreatedBy == userId && tourIdsToSync.Contains(c.TourID)) .Take((int)maxNumRecords) .ToList(); var toursToSync = Mapper.Map <IEnumerable <TourDb>, IEnumerable <Tour> >(tourIds1ToSyncDb); return(toursToSync); }