/// <summary> /// Updates the Patient assoication for the specificed Repeater, i.e., list of patient institutions /// </summary> /// <param name="patientId"></param> /// <param name="rptr"></param> private void UpdatePatientRptr(int patientId, Repeater rptr) { foreach (RepeaterItem item in rptr.Items) { ICaisisInputControl foreignKeyField = item.FindControl("ForeignKeyField") as ICaisisInputControl; ICaisisInputControl primaryKeyField = item.FindControl("PrimaryKeyField") as ICaisisInputControl; CheckBox isAssociated = item.FindControl("AssociateCheckBox") as CheckBox; if (isAssociated.Checked) { // only add association if none exists if (string.IsNullOrEmpty(primaryKeyField.Value)) { IBusinessObject biz = BusinessObjectFactory.BuildBusinessObject(primaryKeyField.Table); // set keys (i.e, InstitutionId + PatientId) biz[foreignKeyField.Field] = int.Parse(foreignKeyField.Value); biz[Patient.PatientId] = patientId; biz.Save(); primaryKeyField.Value = biz[biz.PrimaryKeyName].ToString(); } } else { // remove if assocaition exists if (!string.IsNullOrEmpty(primaryKeyField.Value)) { IBusinessObject biz = BusinessObjectFactory.BuildBusinessObject(primaryKeyField.Table); // delete by pri key (i.e., PatientInstitutionId) int priKey = int.Parse(primaryKeyField.Value); biz.Delete(priKey); primaryKeyField.Value = biz[biz.PrimaryKeyName].ToString(); } } } }
/// <summary> /// Updates the metadta attibutes and attribute values /// </summary> protected virtual void Update() { GridView grid = GetMetadataGrid(); if (grid == null) { return; } foreach (var dirtyRow in dirtyRows) { // the "dirty" grid row GridViewRow row = dirtyRow.Key; int rowIndex = row.RowIndex; // lookup of key fields in row var rowDataKeys = grid.DataKeys[rowIndex].Values; // a list of controls which are "dirty" in row var dirtyControls = dirtyRow.Value; // a list of "dirty" controls which represent an attribute value var dirtyAttributes = dirtyControls.Where(c => c.Table == attributeValueTableName); // a list of "dirty" contorls which represents a dirty metdata item var dirtyMetadataItemFields = dirtyControls.Where(c => c.Table == metadataItemTableName); IBusinessObject metadataItemBiz = BusinessObjectFactory.BuildBusinessObject(metadataItemTableName); int metadataItemKey = (int)rowDataKeys[metadataItemBiz.PrimaryKeyName]; // for each row, only updates values(attribuet values) which have changed, not all attribute values foreach (ICaisisInputControl dirtyAttributeValue in dirtyAttributes) { string attributeName = dirtyAttributeValue.Field; // only update if there is an attribute by this name if (AttributeToHiddenField.ContainsKey(attributeName)) { IBusinessObject attributeBiz = BusinessObjectFactory.BuildBusinessObject(BIZ_ATTRIBUTE_TABLE.TableName); int? attributeId = null; int? attributeValueId = null; // get the table attribute id ICaisisInputControl attributeIdField = AttributeToHiddenField[attributeName]; // CREATE/LOAD: Attribute (i.e., "FieldLabel" -> MetadataFieldAttribute) // if attribute exists, set value if (!string.IsNullOrEmpty(attributeIdField.Value)) { attributeId = int.Parse(attributeIdField.Value); } // otherwise insert attribute else { // set required attribute name attributeBiz[attributeFieldName] = attributeName; attributeBiz.Save(); // update hidden attribute id field attributeId = (int)attributeBiz[attributeBiz.PrimaryKeyName]; attributeIdField.Value = attributeId.Value.ToString(); } // INSERT/UPDATE: Attribute Value (i.e., "FieldLabel" + "My Field" -> MetadataFieldAttributeValue) // the field representing the AttributeValueId ICaisisInputControl attributeValueIdField = GetTableAttributeValueIdControl(row, attributeName); // the field representing the input control containing the AttributeValue ICaisisInputControl attributeValueControl = GetTableAttributeValueControl(row, attributeName); // the disease mapping to the attribute value ICaisisInputControl diseaseAttributeValueId = GetDiseaseAttributeValueId(row, attributeName); // validate fields exists if (attributeValueIdField != null && attributeValueControl != null) { IBusinessObject attributeValueBiz = BusinessObjectFactory.BuildBusinessObject(attributeValueTableName); bool isDiseaseSpecific = QueryDiseaseId.HasValue && diseaseAttributeValueId != null; // load attribute value, if isn't disease specific or disease specific has a value if (!string.IsNullOrEmpty(attributeValueIdField.Value) && (!isDiseaseSpecific || !string.IsNullOrEmpty(diseaseAttributeValueId.Value))) { attributeValueId = int.Parse(attributeValueIdField.Value); attributeValueBiz.Get(attributeValueId.Value); } // set required foreign keys for insert else { // set required attribute id key (i.e., AttributeId) attributeValueBiz[attributeBiz.PrimaryKeyName] = attributeId; // set required metadata id (i.e., FieldId) attributeValueBiz[metadataItemBiz.PrimaryKeyName] = metadataItemKey; } // get attribute value (i.e., "ControlType" = "CaisisTextBox") string attributeValue = GetInputControlValue(attributeValueControl); // update table attribute value // validate (no empty values allowed) if (string.IsNullOrEmpty(attributeValue)) { // delete empty values if (attributeValueId.HasValue) { // delete atttibute value attributeValueBiz.Delete(attributeValueId.Value); attributeValueId = null; attributeValueIdField.Value = string.Empty; // delete disease specific mapping if (isDiseaseSpecific && !string.IsNullOrEmpty(diseaseAttributeValueId.Value)) { DiseaseAttributeValue dav = new DiseaseAttributeValue(); dav.Delete(int.Parse(diseaseAttributeValueId.Value)); diseaseAttributeValueId.Value = string.Empty; } } continue; } else { attributeValueBiz[attributeValueFieldName] = attributeValue; attributeValueBiz.Save(); // update hidden field attributeValueId = (int)attributeValueBiz[attributeValueBiz.PrimaryKeyName]; attributeValueIdField.Value = attributeValueId.ToString(); // handle disease specific mapping if (isDiseaseSpecific) { var diseaseAttributeValue = new DiseaseAttributeValue(); var diseaseAttributeId = GetMetadataDiseaseAttributeId(); // load existing mapping if (!string.IsNullOrEmpty(diseaseAttributeValueId.Value)) { diseaseAttributeValue.Get(int.Parse(diseaseAttributeValueId.Value)); } // new else { diseaseAttributeValue[DiseaseAttributeValue.DiseaseId] = QueryDiseaseId.Value; diseaseAttributeValue[DiseaseAttributeValue.DiseaseAttributeId] = diseaseAttributeId.Value; } diseaseAttributeValue[DiseaseAttributeValue.DiseaseAttributeValue_Field] = attributeValueIdField.Value; diseaseAttributeValue.Save(); diseaseAttributeValueId.Value = diseaseAttributeValue[diseaseAttributeValue.PrimaryKeyName].ToString(); } } } } } // update metadata item (i.e., MetadataField) if (dirtyMetadataItemFields.Count() > 0) { // load by pri key (i.e., FieldId) metadataItemBiz.Get(metadataItemKey); // update fields which have changed (i.e., FieldOrder, FieldSupress) foreach (ICaisisInputControl input in dirtyMetadataItemFields) { metadataItemBiz[input.Field] = GetInputControlValue(input); } // save record metadataItemBiz.Save(); } } // after update rebuilds lists PopulateAttributeValues(); }