/// <summary> /// Changeds the records. /// </summary> /// <param name="rootRecord">The root record.</param> /// <param name="data">The data.</param> /// <param name="ignoreDefault">if set to <c>true</c> [ignore default].</param> /// <returns></returns> public List <UPCRMRecord> ChangedRecords(UPCRMRecord rootRecord, Dictionary <string, object> data, bool ignoreDefault) { if (!this.Multiple) { return(this.SingleAnswerChangedRecords(rootRecord, data, false, ignoreDefault)); } if (!this.Changed) { return(null); } string infoAreaId = this.Root.SurveyAnswerSearchAndList.InfoAreaId; List <string> recordsToDelete = new List <string>(); foreach (UPSurveyAnswerSingle singleRecord in this.singleAnswerDictionary.Values) { if (singleRecord.Deleted && !string.IsNullOrEmpty(singleRecord.RecordIdentification)) { recordsToDelete.Add(singleRecord.RecordIdentification); } } List <UPCRMRecord> changedRecords = new List <UPCRMRecord>(); foreach (UPSurveyAnswerSingle singleRecord in this.singleAnswerDictionary.Values) { if (!singleRecord.Deleted && string.IsNullOrEmpty(singleRecord.RecordIdentification)) { UPCRMRecord record; if (recordsToDelete.Count > 0) { string reUseRecordIdentification = recordsToDelete[0]; recordsToDelete.RemoveAt(0); record = new UPCRMRecord(reUseRecordIdentification); } else { record = new UPCRMRecord(infoAreaId); record.AddLink(new UPCRMLink(rootRecord, -1)); if (this.Root.AnswerQuestionNumberField != null) { record.AddValue(new UPCRMFieldValue(this.Question.QuestionId, infoAreaId, this.Root.AnswerQuestionNumberField.FieldId)); } } if (this.Root.SurveyAnswerTemplateFilter != null) { UPConfigFilter filter = this.Root.SurveyAnswerTemplateFilter.FilterByApplyingValueDictionaryDefaults(data, true); record.ApplyValuesFromTemplateFilter(filter); } if (!string.IsNullOrEmpty(this.Question.RecordIdentification) && this.Question.Questionnaire.Manager.LinkAnswerToQuestion) { record.AddLink(new UPCRMLink(this.Question.RecordIdentification)); } if (this.Question.HasAnswerOptions) { if (this.Root.AnswerAnswerNumberField != null) { record.AddValue(new UPCRMFieldValue(singleRecord.AnswerId, infoAreaId, this.Root.AnswerAnswerNumberField.FieldId)); } if (this.Question.Questionnaire.Manager.LinkAnswerToQuestionAnswer && this.Question.HasAnswerOptions) { IQuestionnaireAnswerOption answerOption = this.Question.AnswerForId(singleRecord.AnswerId); if (answerOption != null) { record.AddLink(new UPCRMLink(answerOption.RecordIdentification)); } } } else { if (this.Root.AnswerAnswerTextField != null) { string text = singleRecord.AnswerId; if (this.Question.AnswersFromCatalog && this.Question.Questionnaire.Manager.SaveCatalogValuesAsText) { text = this.Question.TextForAnswerId(this.AnswerText) ?? string.Empty; } record.AddValue(new UPCRMFieldValue(text, infoAreaId, this.Root.AnswerAnswerTextField.FieldId)); } } changedRecords.Add(record); } } changedRecords.AddRange(recordsToDelete.Select(deleteRecordId => new UPCRMRecord(deleteRecordId, "Delete"))); return(changedRecords); }
/// <summary> /// Singles the answer changed records. /// </summary> /// <param name="rootRecord">The root record.</param> /// <param name="data">The data.</param> /// <param name="ignoreExisting">if set to <c>true</c> [ignore existing].</param> /// <param name="ignoreDefault">if set to <c>true</c> [ignore default].</param> /// <returns></returns> public List <UPCRMRecord> SingleAnswerChangedRecords(UPCRMRecord rootRecord, Dictionary <string, object> data, bool ignoreExisting, bool ignoreDefault) { if (this.Changed) { UPCRMRecord record; string infoAreaId = this.Root.SurveyAnswerSearchAndList.InfoAreaId; bool existsRecord = false; if (!ignoreExisting && !string.IsNullOrEmpty(this.RecordIdentification)) { record = new UPCRMRecord(this.RecordIdentification); existsRecord = true; } else if (!ignoreDefault || !this.IsDefaultAnswer) { record = new UPCRMRecord(infoAreaId); record.AddLink(new UPCRMLink(rootRecord, -1)); if (this.Root.AnswerQuestionNumberField != null) { record.AddValue(new UPCRMFieldValue(this.Question.QuestionId, infoAreaId, this.Root.AnswerQuestionNumberField.FieldId)); } if (this.Question.Questionnaire.Manager.LinkAnswerToQuestionnaire) { record.AddLink(new UPCRMLink(this.Question.Questionnaire.RecordIdentification)); } if (this.Question.Questionnaire.Manager.LinkAnswerToQuestion) { record.AddLink(new UPCRMLink(this.Question.RecordIdentification)); } } else { return(null); } if (this.Root.SurveyAnswerTemplateFilter != null) { UPConfigFilter filter = this.Root.SurveyAnswerTemplateFilter.FilterByApplyingValueDictionaryDefaults(data, true); record.ApplyValuesFromTemplateFilter(filter); } if (this.Root.AnswerAnswerNumberField != null) { record.AddValue(new UPCRMFieldValue(this.AnswerNumber, infoAreaId, this.Root.AnswerAnswerNumberField.FieldId)); } if (this.Root.AnswerAnswerTextField != null) { string text = this.AnswerText; if (this.Question.AnswersFromCatalog && this.Question.Questionnaire.Manager.SaveCatalogValuesAsText) { text = this.Question.TextForAnswerId(this.AnswerText) ?? string.Empty; } record.AddValue(new UPCRMFieldValue(text, infoAreaId, this.Root.AnswerAnswerTextField.FieldId)); } if (this.Question.Questionnaire.Manager.LinkAnswerToQuestionAnswer && this.Question.HasAnswerOptions) { IQuestionnaireAnswerOption answerOption = this.Question.AnswerForId(this.AnswerNumber); if (answerOption != null) { if (existsRecord && this.Question.Questionnaire.Manager.DeleteAndInsertOnAnswerOptionChange) { UPCRMRecord deleteRecord = new UPCRMRecord(this.RecordIdentification, "Delete"); List <UPCRMRecord> createdRecords = this.SingleAnswerChangedRecords(rootRecord, data, true, ignoreDefault); List <UPCRMRecord> changedRecords = new List <UPCRMRecord> { deleteRecord }; changedRecords.AddRange(createdRecords); return(changedRecords); } record.AddLink(new UPCRMLink(answerOption.RecordIdentification)); } else if (this.Question.Questionnaire.Manager.DeleteSingleAnswerOptionIfEmpty) { return(existsRecord ? new List <UPCRMRecord> { new UPCRMRecord(record.RecordIdentification, "Delete") } : null); } } return(new List <UPCRMRecord> { record }); } return(null); }