public async Task SynchronizeNoThrow(ISynchronizationLogger logger) { s_logger.InfoFormat("Entered. Syncstrategy '{0}' with Atype='{1}' and Btype='{2}'", _initialSyncStateCreationStrategy.GetType().Name, typeof(TAtypeEntity).Name, typeof(TBtypeEntity).Name); try { using (var totalProgress = _totalProgressFactory.Create()) { var knownEntityRelations = _entityRelationDataAccess.LoadEntityRelationData() ?? new IEntityRelationData <TAtypeEntityId, TAtypeEntityVersion, TBtypeEntityId, TBtypeEntityVersion>[] { }; using (var interceptor = _synchronizationInterceptorFactory.Create()) { var synchronizationContext = await _contextFactory.Create(); var newAVersionsTask = _atypeRepository.GetAllVersions(knownEntityRelations.Select(r => r.AtypeId), synchronizationContext); var newBVersionsTask = _btypeRepository.GetAllVersions(knownEntityRelations.Select(r => r.BtypeId), synchronizationContext); var newAVersions = CreateDictionary( await newAVersionsTask, _atypeIdComparer); var newBVersions = CreateDictionary( await newBVersionsTask, _btypeIdComparer); using (var entityContainer = new EntityContainer(this, totalProgress, logger.ALoadEntityLogger, logger.BLoadEntityLogger)) { var newEntityRelations = await Synchronize( totalProgress, knownEntityRelations, newAVersions, newBVersions, entityContainer, logger, synchronizationContext, interceptor); _entityRelationDataAccess.SaveEntityRelationData(newEntityRelations); } await _contextFactory.SynchronizationFinished(synchronizationContext); } } } catch (Exception x) { logger.LogAbortedDueToError(x); _exceptionLogger.LogException(x, s_logger); } s_logger.DebugFormat("Exiting."); }
public async Task SynchronizePartialNoThrow( IEnumerable <IIdWithHints <TAtypeEntityId, TAtypeEntityVersion> > aIds, IEnumerable <IIdWithHints <TBtypeEntityId, TBtypeEntityVersion> > bIds, ISynchronizationLogger logger) { s_logger.InfoFormat( "Entered partial. Syncstrategy '{0}' with Atype='{1}' and Btype='{2}'", _initialSyncStateCreationStrategy.GetType().Name, typeof(TAtypeEntity).Name, typeof(TBtypeEntity).Name); try { var knownEntityRelations = _entityRelationDataAccess.LoadEntityRelationData(); if (knownEntityRelations == null) { await SynchronizeNoThrow(logger); return; } var requestedAIdsById = aIds.ToDictionary(e => e.Id, _atypeIdComparer); var requestedBIdsById = bIds.ToDictionary(e => e.Id, _btypeIdComparer); var aIdsWithAwarenessLevel = new List <IdWithAwarenessLevel <TAtypeEntityId> >(); var bIdsWithAwarenessLevel = new List <IdWithAwarenessLevel <TBtypeEntityId> >(); using (var totalProgress = _totalProgressFactory.Create()) { var entityRelationsToUse = new List <IEntityRelationData <TAtypeEntityId, TAtypeEntityVersion, TBtypeEntityId, TBtypeEntityVersion> >(); var entityRelationsNotToUse = new List <IEntityRelationData <TAtypeEntityId, TAtypeEntityVersion, TBtypeEntityId, TBtypeEntityVersion> >(); foreach (var entityRelation in knownEntityRelations) { var isACausingSync = RemoveAFromRequestedAndCheckIfCausesSync(requestedAIdsById, entityRelation); var isBCausingSync = RemoveBFromRequestedAndCheckIfCausesSync(requestedBIdsById, entityRelation); if (isACausingSync || isBCausingSync) { aIdsWithAwarenessLevel.Add(new IdWithAwarenessLevel <TAtypeEntityId> (entityRelation.AtypeId, true)); bIdsWithAwarenessLevel.Add(new IdWithAwarenessLevel <TBtypeEntityId> (entityRelation.BtypeId, true)); entityRelationsToUse.Add(entityRelation); } else { entityRelationsNotToUse.Add(entityRelation); } } aIdsWithAwarenessLevel.AddRange(requestedAIdsById.Where(kv => !(kv.Value.WasDeletedHint ?? false)).Select(kv => new IdWithAwarenessLevel <TAtypeEntityId> (kv.Key, false))); bIdsWithAwarenessLevel.AddRange(requestedBIdsById.Where(kv => !(kv.Value.WasDeletedHint ?? false)).Select(kv => new IdWithAwarenessLevel <TBtypeEntityId> (kv.Key, false))); if (aIdsWithAwarenessLevel.Count == 0 && bIdsWithAwarenessLevel.Count == 0) { s_logger.InfoFormat("Exiting partial since there is nothing to synchronize."); return; } var synchronizationContext = await _contextFactory.Create(); Task <IReadOnlyList <EntityVersion <TAtypeEntityId, TAtypeEntityVersion> > > newAVersionsTask; if (aIdsWithAwarenessLevel.Count > 0) { newAVersionsTask = _atypeRepository.GetVersions(aIdsWithAwarenessLevel, synchronizationContext); } else { newAVersionsTask = Task.FromResult <IReadOnlyList <EntityVersion <TAtypeEntityId, TAtypeEntityVersion> > > (new EntityVersion <TAtypeEntityId, TAtypeEntityVersion>[] { }); } Task <IReadOnlyList <EntityVersion <TBtypeEntityId, TBtypeEntityVersion> > > newBVersionsTask; if (bIdsWithAwarenessLevel.Count > 0) { newBVersionsTask = _btypeRepository.GetVersions(bIdsWithAwarenessLevel, synchronizationContext); } else { newBVersionsTask = Task.FromResult <IReadOnlyList <EntityVersion <TBtypeEntityId, TBtypeEntityVersion> > > (new EntityVersion <TBtypeEntityId, TBtypeEntityVersion>[] { }); } var newAVersions = CreateDictionary( await newAVersionsTask, _atypeIdComparer); var newBVersions = CreateDictionary( await newBVersionsTask, _btypeIdComparer); using (var entityContainer = new EntityContainer(this, totalProgress, logger.ALoadEntityLogger, logger.BLoadEntityLogger)) { var newEntityRelations = await Synchronize( totalProgress, entityRelationsToUse, newAVersions, newBVersions, entityContainer, logger, synchronizationContext); entityRelationsNotToUse.AddRange(newEntityRelations); _entityRelationDataAccess.SaveEntityRelationData(entityRelationsNotToUse); } } } catch (Exception x) { logger.LogAbortedDueToError(x); _exceptionLogger.LogException(x, s_logger); } s_logger.DebugFormat("Exiting."); }
private static void LogError(Exception exception, ISynchronizationLogger logger) { logger.LogAbortedDueToError(exception); ExceptionHandler.Instance.LogException(exception, s_logger); }
public async Task SynchronizePartialNoThrow( IEnumerable <TAtypeEntityId> aEntityIds, IEnumerable <TBtypeEntityId> bEntityIds, ISynchronizationLogger logger) { s_logger.InfoFormat("Entered. Syncstrategy '{0}' with Atype='{1}' and Btype='{2}'", _initialSyncStateCreationStrategy.GetType().Name, typeof(TAtypeEntity).Name, typeof(TBtypeEntity).Name); try { var knownEntityRelations = _entityRelationDataAccess.LoadEntityRelationData(); if (knownEntityRelations == null) { await SynchronizeNoThrow(logger); return; } var aEntitesToSynchronize = new HashSet <TAtypeEntityId> (aEntityIds, _atypeIdComparer); var bEntitesToSynchronize = new HashSet <TBtypeEntityId> (bEntityIds, _btypeIdComparer); var aIdsWithAwarenessLevel = new List <IdWithAwarenessLevel <TAtypeEntityId> >(); var bIdsWithAwarenessLevel = new List <IdWithAwarenessLevel <TBtypeEntityId> >(); using (var totalProgress = _totalProgressFactory.Create()) { var entityRelationsToUse = new List <IEntityRelationData <TAtypeEntityId, TAtypeEntityVersion, TBtypeEntityId, TBtypeEntityVersion> >(); var entityRelationsNotToUse = new List <IEntityRelationData <TAtypeEntityId, TAtypeEntityVersion, TBtypeEntityId, TBtypeEntityVersion> >(); foreach (var entityRelation in knownEntityRelations) { if (aEntitesToSynchronize.Contains(entityRelation.AtypeId) || bEntitesToSynchronize.Contains(entityRelation.BtypeId)) { aIdsWithAwarenessLevel.Add(new IdWithAwarenessLevel <TAtypeEntityId> (entityRelation.AtypeId, true)); bIdsWithAwarenessLevel.Add(new IdWithAwarenessLevel <TBtypeEntityId> (entityRelation.BtypeId, true)); aEntitesToSynchronize.Remove(entityRelation.AtypeId); bEntitesToSynchronize.Remove(entityRelation.BtypeId); entityRelationsToUse.Add(entityRelation); } else { entityRelationsNotToUse.Add(entityRelation); } } aIdsWithAwarenessLevel.AddRange(aEntitesToSynchronize.Select(id => new IdWithAwarenessLevel <TAtypeEntityId> (id, false))); bIdsWithAwarenessLevel.AddRange(bEntitesToSynchronize.Select(id => new IdWithAwarenessLevel <TBtypeEntityId> (id, false))); Task <IReadOnlyList <EntityVersion <TAtypeEntityId, TAtypeEntityVersion> > > newAVersionsTask; if (aIdsWithAwarenessLevel.Count > 0) { newAVersionsTask = _atypeRepository.GetVersions(aIdsWithAwarenessLevel); } else { newAVersionsTask = Task.FromResult <IReadOnlyList <EntityVersion <TAtypeEntityId, TAtypeEntityVersion> > > (new EntityVersion <TAtypeEntityId, TAtypeEntityVersion>[] { }); } Task <IReadOnlyList <EntityVersion <TBtypeEntityId, TBtypeEntityVersion> > > newBVersionsTask; if (bIdsWithAwarenessLevel.Count > 0) { newBVersionsTask = _btypeRepository.GetVersions(bIdsWithAwarenessLevel); } else { newBVersionsTask = Task.FromResult <IReadOnlyList <EntityVersion <TBtypeEntityId, TBtypeEntityVersion> > > (new EntityVersion <TBtypeEntityId, TBtypeEntityVersion>[] { }); } var newAVersions = CreateDictionary( await newAVersionsTask, _atypeIdComparer); var newBVersions = CreateDictionary( await newBVersionsTask, _btypeIdComparer); using (var entityContainer = new EntityContainer(this, totalProgress, logger)) { var newEntityRelations = await Synchronize( totalProgress, entityRelationsToUse, newAVersions, newBVersions, entityContainer, logger); entityRelationsNotToUse.AddRange(newEntityRelations); _entityRelationDataAccess.SaveEntityRelationData(entityRelationsNotToUse); } } } catch (Exception x) { logger.LogAbortedDueToError(x); _exceptionLogger.LogException(x, s_logger); } s_logger.DebugFormat("Exiting."); }