protected async override Task HandleInnerAsync(SurveyAnswerWriteContext context)
        {
            await DeleteAnswer(context);

            foreach (var answer in context.Delta.Incoming as IEnumerable)
            {
                //if this becomes slow we can do in parallel
                await InsertAnswer(context, answer);
            }
        }
예제 #2
0
        protected async override Task HandleInnerAsync(SurveyAnswerWriteContext context)
        {
            //we should only see the symptoms that have changed so this should be update vs. delete
            var existingSymptom = context.Delta.Current as CaseSymptomDto;
            var incomingSymptom = context.Delta.Incoming as CaseSymptomDto;

            bool shouldDelete = existingSymptom.HasSymptom && !incomingSymptom.HasSymptom;

            if (incomingSymptom.HasSymptom)
            {
                //update, no audit field for updated id/timestamp
                await context.Connection.ExecuteAsync(
                    @"update dbo.case_symptom 
                       set dt_onset = @onsetDate,
                            ds_symptom_other = @other,
                            am_onset_time = @onsetTime
                        where ID_CASE = @caseId and CD_SYMPTOM = @symptom

                    if @@rowcount = 0
                    begin
                        insert into dbo.case_symptom(id_case, cd_symptom, dt_onset, ds_symptom_other, am_onset_time,id_added, dt_added)
                            values(@caseId,@symptom,@onsetDate,@other,@onsetTime,@userId,@timestamp)
                    end",
                    new
                {
                    incomingSymptom.OnsetDate,
                    incomingSymptom.OnsetTime,
                    context.UserId,
                    context.Timestamp,
                    context.CaseId,
                    symptom = incomingSymptom.SymptomCode,
                    incomingSymptom.Other
                }, 1, context.Transaction);
            }

            if (shouldDelete)
            {
                //delete
                await context.Connection.ExecuteAsync(
                    "delete from dbo.case_symptom where ID_CASE = @caseId and CD_SYMPTOM = @symptom",
                    new
                {
                    caseId  = context.CaseId,
                    symptom = incomingSymptom.SymptomCode
                }, 1, context.Transaction);
            }
        }
예제 #3
0
        protected async override Task HandleInnerAsync(SurveyAnswerWriteContext context)
        {
            int index = 0;

            foreach (var answers in context.Delta.Incoming as IEnumerable)
            {
                foreach (JProperty answer in answers as IEnumerable)
                {
                    if (index == 0)
                    {
                        await DeleteAnswer(context, answer.Name.ToUpper());
                    }
                    await InsertAnswer(context, answer.Value.ToString(), answer.Name.ToUpper(), index);
                }
                index++;
                //if this becomes slow we can do in parallel
                //await InsertAnswer(context, answer);
            }
        }
        protected async override Task HandleInnerAsync(SurveyAnswerWriteContext context)
        {
            var travel = (context.Delta.Incoming ?? context.Delta.Current) as TravelHistoryDto;

            var param = new {
                context.UserId,
                context.Timestamp,
                context.CaseId,
                travel.TravelId,
                travel.LocationExposed,
                travel.LocationName,
                travel.Notes,
                travel.BeginDate,
                travel.EndDate,
                travel.Address.AddressLine1,
                travel.Address.AddressLine2,
                travel.Address.City,
                travel.Address.State,
                travel.Address.County,
                travel.Address.Country,
                travel.Address.Zip
            };

            switch (context.Delta.State)
            {
            case AnswerState.Added:
                await context.Connection.ExecuteAsync(
                    @"INSERT INTO [dbo].[TRAVEL_HISTORY]
                            ([id_travel_history]
                            ,[id_case]
                            ,nm_facility
                            ,cd_traveler
                            ,[ds_address1]
                            ,[ds_address2]
                            ,[ds_city]
                            ,[cd_state]
                            ,[ds_zip]
                            ,[dt_stay_begin]
                            ,[dt_stay_end]
                            ,[ds_comments]
                            ,[id_added]
                            ,[dt_added]
                            ,[cd_country]
                            ,[cd_travel_type]
                            ,[CD_COUNTY])
                        VALUES
                            (next value for dbo.seq_travel_history
                            ,@caseId
                            ,''
                            ,'PATIENT'
                            ,@AddressLine1
                            ,@AddressLine2
                            ,@City
                            ,@State
                            ,@Zip
                            ,@BeginDate
                            ,@EndDate
                            ,@Notes
                            ,@userId
                            ,@timestamp
                            ,@Country
                            ,@LocationExposed
                            ,@County)",
                    param, 1, context.Transaction);

                break;

            case AnswerState.Modified:
                await context.Connection.ExecuteAsync(
                    @"update [dbo].[TRAVEL_HISTORY]
                            set [id_case] = @caseId
                                ,[ds_address1] = @AddressLine1
                                ,[ds_address2] = @AddressLine2
                                ,[ds_city] = @City
                                ,[cd_state] = @State
                                ,[ds_zip] = @Zip
                                ,[dt_stay_begin] = @BeginDate
                                ,[dt_stay_end] = @EndDate
                                ,[ds_comments] = @Notes
                                ,[id_changed] = @userId
                                ,[dt_changed] = @timestamp
                                ,[cd_country] = @Country
                                ,cd_travel_type = @LocationExposed
                                ,[CD_COUNTY] = @County
                            where id_travel_history = @travelId",
                    param, 1, context.Transaction);

                break;

            case AnswerState.Deleted:
                await context.Connection.ExecuteAsync(
                    "delete from dbo.travel_history where id_travel_history = @travelId",
                    param, 1, context.Transaction);

                break;

            default:
                throw new InvalidOperationException(
                          $"No save operation defined for state '{context.Delta.State}'");
            }
        }
예제 #5
0
        public async Task Execute(SaveSurveyAnswers command)
        {
            var questionStorageMap = await repos.GetQuestionStorageMap(command.SurveyId);

            var currentAnswers = await repos.Get(new GetSurveyAnswers
            {
                CaseId     = command.CaseId,
                SurveyId   = command.SurveyId,
                ProfileId  = command.ProfileId,
                OutbreakId = command.OutbreakId
            });

            //unwrap jArrays to complex types
            SurveyAnswerBindingHelpers.Bind(command.Answers);

            var answers = command.Answers.ToDictionary(entry => entry.Key.ToUpper(), entry => entry.Value);

            //get only the differences
            var deltas = SurveyAnswerHelpers.GetDeltas(currentAnswers, answers);

            //if no delta, exit
            if (!deltas.Any())
            {
                return;
            }

            var writer = COR <SurveyAnswerWriteContext> .CreateChain(
                new SymptomWriteHandler(),
                new TravelHistoryWriteHandler(),
                new MappedWriteHandler(dataServices),
                new RepeaterWriteHandler(),
                new MultipleWriteHandler(),
                new DefaultWriteHandler());

            var allkeys = deltas
                          .Select(delta => delta.Key)
                          .ToList();

            var storageMapping = writeContext.SurveyObjectMapping
                                 .Where(mapping => mapping.CdMappingtype == "TARGETDS")
                                 .Where(mapping => allkeys.Contains(mapping.IdQuestion))
                                 .ToDictionary(mapping => mapping.IdQuestion.ToUpper());

            var surveyInstanceId = await repos.GetSurveyInstanceId(command.ProfileId, command.CaseId, command.OutbreakId);

            var connection = writeContext.Database.GetDbConnection().EnsureOpen();

            using (var transaction = writeContext.Database.BeginTransaction().GetDbTransaction())
            {
                //TODO:  This needs better DB design
                //if no survey instance, create one
                if (surveyInstanceId == 0)
                {
                    surveyInstanceId = (int)await sequencerGenerator.GetNextAsync(SequenceType.SurveyInstance);

                    var instance = new SurveyInstance
                    {
                        IdSurveyInstance = surveyInstanceId.Value,
                        IdProfile        = command.ProfileId,
                    };

                    if (command.OutbreakId != null)
                    {
                        instance.CdEntityType = "O";
                        instance.IdEntity     = command.OutbreakId;
                    }

                    if (command.CaseId != null)
                    {
                        instance.CdEntityType = "C";
                        instance.IdEntity     = command.CaseId;
                    }

                    await writeContext.SurveyInstance.AddAsync(instance);

                    await writeContext.SaveChangesAsync();
                }
                //end

                var context = new SurveyAnswerWriteContext
                {
                    DB                 = writeContext,
                    CaseId             = command.CaseId,
                    ProfileId          = command.ProfileId,
                    OutbreakId         = command.OutbreakId,
                    SurveyInstanceId   = surveyInstanceId.Value, //should be assigned a value by this point
                    UserId             = usernameProvider.GetUsername(),
                    Timestamp          = DateTime.Now,
                    QuestionStorageMap = questionStorageMap
                };

                context.Transaction = transaction;
                context.Connection  = connection;

                //apply the changes
                foreach (var delta in deltas)
                {
                    context.Delta = delta;

                    await writer.HandleAsync(context);

                    //need to mark question as answered
                    if (SurveyAnswerBindingHelpers.IsQuestion(delta.Key))
                    {
                        var param = new
                        {
                            questionId = delta.Key
                        };
                        try
                        {
                            string sql = @"update survey_question_bank
                                set IN_ANSWERED = 1
                                where id_question = @questionId
                                and IN_ANSWERED = 0;";

                            await connection.ExecuteAsync(sql, param, 1, transaction);
                        }
                        catch (Exception e)
                        {
                            //do nothing for now
                        }
                    }
                }
                transaction.Commit();
            }
        }
예제 #6
0
 protected override bool CanHandle(SurveyAnswerWriteContext context)
 {
     return(context.Delta.Key.StartsWith("RG-", StringComparison.OrdinalIgnoreCase));
 }
 protected override bool CanHandle(SurveyAnswerWriteContext context)
 {
     return(SurveyAnswerBindingHelpers.HasMultipleAnswers(context.Delta.Incoming));
 }