public ActionResult SaveInfo(CRFFormPage page, CRFParameters model) { logger.Info("Saving started"); if (page == null || model == null || model.EntryType <= 0 || string.IsNullOrWhiteSpace(model.SubjectId)) { return(CamelJson(new { NoData = true, success = false })); } int detail = model.DetailId; int studyId = model.StudyId; int periodId = model.PeriodId; string formGroupId = model.FormGroupId; string subjectId = model.SubjectId; int entryType = model.EntryType; int compEntry = model.CompareEntryType; foreach (var oneform in page.FormGroup) { for (int i = 0; i < oneform.Value.Items.Count; i++) { var row = i + 1; var onefieldset = oneform.Value.Items[i]; foreach (var onefield in onefieldset) { var field = Entities.FieldValues .Where(x => x.FieldId == onefield.Value.FieldId) .Where(x => x.FormId == oneform.Value.FormId && x.FormGroupId == formGroupId && x.StudyId == studyId && x.PeriodId == periodId) .Where(x => x.RowIndex == row && x.SubjectId == subjectId && x.EntryType == entryType).FirstOrDefault(); if (field == null) { field = new StepCinemaDataLayer.EntityModel.FieldValue { FieldId = onefield.Value.FieldId, FormId = oneform.Value.FormId, FormGroupId = formGroupId, StudyId = studyId, PeriodId = periodId, RowIndex = row, SubjectId = subjectId, // verify the subject Id EntryType = entryType, FieldValue1 = onefield.Value.Value }; Entities.FieldValues.Add(field); } else { field.FieldId = onefield.Value.FieldId; field.FormId = oneform.Value.FormId; field.FormGroupId = formGroupId; field.StudyId = studyId; field.PeriodId = periodId; field.RowIndex = row; field.SubjectId = subjectId; // verify the subject Id field.EntryType = entryType; field.FieldValue1 = onefield.Value.Value; } try { Entities.SaveChanges(); } catch (Exception ex) { logger.Error(ex); } } } } logger.Info("Saving end"); Session.Remove("percent"); return(CamelJson(new { Success = true })); }
public ActionResult GetDynamicFormJson(CRFParameters model) { int detail = model.DetailId; int studyId = model.StudyId; int periodId = model.PeriodId; string formGroupId = model.FormGroupId; string subjectId = model.SubjectId; int entryType = model.EntryType; int compEntry = model.CompareEntryType; var page = new CRFFormPage(); page.FormGroup = Entities.FormConfigs .Where(x => x.FormGroupId == formGroupId && x.StudyId == studyId && x.PeriodId == periodId && !x.Disabled && x.Display) .Select(x => new TSForm() { FormType = x.FormType, Name = x.FormName, FormId = x.FormId, Rows = x.Rows }) .ToDictionary(x => x.FormId, y => y); page.Lovs = new Dictionary <string, CRFLov>(); foreach (var oneform in page.FormGroup) { var formName = oneform.Value.FormId; var fields = Entities.FormFields .Where(x => x.FormId == formName && x.FormGroupId == formGroupId && x.StudyId == studyId && x.PeriodId == periodId && !x.Disabled && x.Display); var extractedFields = fields.Select(y => new { y.FieldId, y.FieldType, y.LOVId, y.Unit, y.FieldLiteral, y.FieldDataType, y.Order }) .AsEnumerable(); oneform.Value.Head = extractedFields.Select(x => new CRFFieldHead() { FieldId = x.FieldId, Text = x.FieldLiteral }).ToDictionary(x => x.FieldId); oneform.Value.FieldOrder = extractedFields.OrderBy(x => x.Order).Select(x => x.FieldId).ToList(); oneform.Value.Items = new List <Dictionary <string, CRFFieldValue> >(); var rows = oneform.Value.Rows; if (rows < 1) { rows = oneform.Value.Rows = 1; } for (int i = 1; i <= rows; i++) { Dictionary <string, CRFFieldValue> item = null; Dictionary <string, string> diffDict = null; // empty initially item = ( extractedFields.Select(y => new CRFFieldValue() { FieldId = y.FieldId, Type = y.FieldDataType, LovId = y.LOVId, Unit = y.Unit, Value = "" }) .ToDictionary(x => x.FieldId) ); var values = Entities.FieldValues .Where(x => x.FormId == formName && x.FormGroupId == formGroupId && x.StudyId == studyId && x.PeriodId == periodId) .Where(x => x.RowIndex == i && x.SubjectId == subjectId && x.EntryType == entryType) .Select(x => x) .ToDictionary(x => x.FieldId, y => y.FieldValue1); var prefills = Entities.PrefilledFields .Where(x => x.FormId == formName && x.FormGroupId == formGroupId && x.StudyId == studyId && x.PeriodId == periodId) .Where(x => x.RowIndex == i).Select(x => x).ToDictionary(x => x.FieldId, y => y.FieldValue); if (compEntry > 0) { diffDict = Entities.FieldValues .Where(x => x.FormId == formName && x.FormGroupId == formGroupId && x.StudyId == studyId && x.PeriodId == periodId) .Where(x => x.RowIndex == i && x.SubjectId == subjectId && x.EntryType == compEntry) .Select(x => new { x.FieldId, x.FieldValue1 }) .ToDictionary(x => x.FieldId, y => y.FieldValue1); } foreach (CRFFieldValue onefield in item.Values) { if ((!string.IsNullOrWhiteSpace(onefield.LovId)) && (!page.Lovs.ContainsKey(onefield.LovId))) { var lovid = onefield.LovId; var lovlist = Entities.ListOfValues .Where(x => x.LOVId == lovid) .OrderBy(x => x.Order) .Select(x => new CRFLovKeyValue() { Text = x.Value, Key = x.Key }).ToList(); page.Lovs[lovid] = new CRFLov() { Items = lovlist }; } if (prefills.ContainsKey(onefield.FieldId)) { onefield.Value = prefills[onefield.FieldId]; } else if (values.ContainsKey(onefield.FieldId)) { onefield.Value = values[onefield.FieldId]; } if (diffDict != null && diffDict.ContainsKey(onefield.FieldId)) { onefield.CompareValue = diffDict[onefield.FieldId]; // if key not conatins and other scenarios should be attended } } oneform.Value.Items.Add(item); } // for } return(CamelJson(page)); }