public static GetKnowledgeResult FromPayload(JObject payload)
            {
                string synchronizationId = payload[nameof(SynchronizationId)].Value <string>();
                Dictionary <string, object> customInfo = payload[nameof(CustomInfo)].ToObject <Dictionary <string, object> >();
                PayloadAction      payloadAction       = (PayloadAction)Enum.Parse(typeof(PayloadAction), payload[nameof(PayloadAction)].Value <string>());
                GetKnowledgeResult result = new GetKnowledgeResult(payloadAction, synchronizationId, customInfo);

                result.KnowledgeInfos = payload[nameof(KnowledgeInfos)].ToObject <List <KnowledgeInfo> >();
                return(result);
            }
Exemple #2
0
        internal void GetKnowledge(GetKnowledgeParameter parameter, ref GetKnowledgeResult result)
        {
            if (parameter == null)
            {
                throw new NullReferenceException(nameof(parameter));
            }
            result = new GetKnowledgeResult(parameter.PayloadAction, parameter.SynchronizationId, parameter.CustomInfo);

            result.KnowledgeInfos = GetAllKnowledgeInfos(parameter.SynchronizationId, parameter.CustomInfo);
            if (result.KnowledgeInfos == null)
            {
                result.KnowledgeInfos = new List <KnowledgeInfo>();
            }
            parameter.Log.Add($"All KnowledgeInfos Count: {result.KnowledgeInfos.Count}");
            if (result.KnowledgeInfos.Where(w => w.IsLocal).Count() > 1)
            {
                throw new SyncEngineConstraintException("Found multiple KnowledgeInfo with IsLocal equals to true. IsLocal should be 1 (one) data only");
            }
            if (result.KnowledgeInfos.Where(w => w.IsLocal).Count() == 1)
            {
                return;
            }

            parameter.Log.Add("Local KnowledgeInfo is not found. Creating a new Local KnowledgeInfo and Provisioning existing data...");
            OperationType operationType = OperationType.ProvisionKnowledge;
            object        transaction   = StartTransaction(null, operationType, parameter.SynchronizationId, parameter.CustomInfo);

            try
            {
                parameter.Log.Add("Getting Next TimeStamp...");
                long nextTimeStamp = InvokeGetNextTimeStamp();

                UpdateLocalKnowledgeTimeStamp(nextTimeStamp, parameter.SynchronizationId, parameter.CustomInfo, parameter.Log, transaction);

                CommitTransaction(null, transaction, operationType, parameter.SynchronizationId, parameter.CustomInfo);
            }
            catch (Exception)
            {
                RollbackTransaction(null, transaction, operationType, parameter.SynchronizationId, parameter.CustomInfo);
                throw;
            }
            finally
            {
                EndTransaction(null, transaction, operationType, parameter.SynchronizationId, parameter.CustomInfo);
            }

            result.KnowledgeInfos = GetAllKnowledgeInfos(parameter.SynchronizationId, parameter.CustomInfo);
            if (result.KnowledgeInfos.Where(w => w.IsLocal).Count() != 1)
            {
                throw new SyncEngineConstraintException($"KnowledgeInfo with IsLocal equals to true is still not 1 (one) data. Check your {nameof(CreateOrUpdateKnowledgeInfo)} implementation.");
            }
        }