/// <summary> /// Adds a new lookup code record, footer. If adding stage lookup code, extract color value /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void AddLookupCode(object sender, EventArgs e) { CaisisTextBox lkpBox = sender as CaisisTextBox; if (lkpBox != null) { string lkpFieldName = lkpBox.FieldLabel; string lkpCode = lkpBox.Text; string lkpOrderServerId = lkpFieldName + LookupCode.LkpOrder; CaisisTextBox lkpOrderBox = lkpBox.NamingContainer.FindControl(lkpOrderServerId) as CaisisTextBox; string lkpOrder = lkpOrderBox.Value; if (!string.IsNullOrEmpty(lkpCode)) { int newLkpCodeId = AddLookupCodeValue(lkpFieldName, lkpCode, lkpOrder); lkpBox.Value = string.Empty; // Save a color atttibute for the new record for stage lookup code if (lkpBox == StageLkpCode && !string.IsNullOrEmpty(ColorAttributeId.Value) && !string.IsNullOrEmpty(NewColorCode.Value)) { LookupCodeAttribute colorAttributeValue = new LookupCodeAttribute(); colorAttributeValue[LookupCodeAttribute.LookupCodeId] = newLkpCodeId; colorAttributeValue[LookupCodeAttribute.AttributeId] = ColorAttributeId.Value; colorAttributeValue[LookupCodeAttribute.AttributeValue] = NewColorCode.Value; colorAttributeValue[LookupCodeAttribute.AttributeSuppress] = 0; colorAttributeValue.Save(); NewColorCode.Value = string.Empty; } } } }
/// <summary> /// Updates the stage record when color code is changed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void SaveStage(object sender, EventArgs e) { GridViewRow row = (sender as Control).NamingContainer as GridViewRow; if (row != null) { HiddenField colorCodeField = row.FindControl("ColorCode") as HiddenField; HiddenField lookupCodeAttributeId = row.FindControl("LookupCodeAttributeId") as HiddenField; HiddenField lkpCodeIdField = row.FindControl("LookupCodeId") as HiddenField; if (!string.IsNullOrEmpty(ColorAttributeId.Value) && !string.IsNullOrEmpty(colorCodeField.Value)) { LookupCodeAttribute biz = new LookupCodeAttribute(); if (!string.IsNullOrEmpty(lookupCodeAttributeId.Value)) { biz.Get(int.Parse(lookupCodeAttributeId.Value)); } biz[LookupCodeAttribute.LookupCodeId] = lkpCodeIdField.Value; biz[LookupCodeAttribute.AttributeId] = ColorAttributeId.Value; biz[LookupCodeAttribute.AttributeValue] = colorCodeField.Value; biz[LookupCodeAttribute.AttributeSuppress] = 0; biz.Save(); lookupCodeAttributeId.Value = biz[LookupCodeAttribute.LookupCodeAttributeId].ToString(); } } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void UpdateLookupCodeAttributes(object sender, EventArgs e) { string lkpAttributeId = LookupAttributesList.Value; string lkpAttributeValue = LookupAttributeValue.Value; // Only run update if values haven't changed, otherwise, need to rebind if (!formDirty) { // foreah row where checkbox has changed state, insert or delete foreach (int dirtyRow in dirtyRows) { // keys are maintained in grid's data keys array string lkpCodeId = LookupAttributesGrid.DataKeys[dirtyRow].Values[LookupCodeAttribute.LookupCodeId].ToString(); string lkpCodeAttributeId = LookupAttributesGrid.DataKeys[dirtyRow].Values[LookupCodeAttribute.LookupCodeAttributeId].ToString(); LookupCodeAttribute biz = new LookupCodeAttribute(); // if lkpCodeAttributeId exists, delete if (!string.IsNullOrEmpty(lkpCodeAttributeId)) { biz.Delete(int.Parse(lkpCodeAttributeId)); } // otherwise insert else { // set foreign keys biz[LookupCodeAttribute.AttributeId] = lkpAttributeId; biz[LookupCodeAttribute.LookupCodeId] = lkpCodeId; biz[LookupCodeAttribute.AttributeSuppress] = 0; biz[LookupCodeAttribute.AttributeValue] = lkpAttributeValue; biz.Save(); } } } // rebind grid BindLookupAttributeValues(sender, e); BindExistingAttributeValues(sender, e); }
/// <summary> /// Save's all "dirty" rows as well as update/delete association to current disease /// </summary> private void Save() { // validation List <string> existingCodes = new List <string>(); List <string> duplicateCodes = new List <string>(); // check each row, need to check for associate with disease foreach (GridViewRow row in LookupCodeGrid.Rows) { int? lkpCodeId = null; object rowLkpCodeId = LookupCodeGrid.DataKeys[row.RowIndex][LookupCode.LookupCodeId]; if (!string.IsNullOrEmpty(rowLkpCodeId.ToString())) { lkpCodeId = (int)rowLkpCodeId; } bool isDirtyRow = LookupCodeGrid.DirtyGridRows.Contains(row); ICaisisInputControl lkpCodeField = row.FindControl("LkpCode") as ICaisisInputControl; // validate empty values, duplicates, etc... if (!string.IsNullOrEmpty(lkpCodeField.Value)) { if (!lkpCodeId.HasValue && existingCodes.Contains(lkpCodeField.Value, StringComparer.OrdinalIgnoreCase)) { // mark as duplicate and prevent insert duplicateCodes.Add(lkpCodeField.Value); isDirtyRow = false; lkpCodeId = null; } // track running list else { existingCodes.Add(lkpCodeField.Value); } } else { isDirtyRow = false; lkpCodeId = null; } // if dirty, update/insert if (isDirtyRow) { // update/insert LookupCode lkpCode = new LookupCode(); if (lkpCodeId.HasValue) { lkpCode.Get(lkpCodeId.Value); } else { lkpCode[LookupCode.LkpFieldName] = CurrentLkpFieldName.Value; } CICHelper.SetBOValues(row.Controls, lkpCode, -1); lkpCode.Save(); // update pri key lkpCodeId = (int)lkpCode[LookupCode.LookupCodeId]; } // continue if valid if (lkpCodeId.HasValue) { // APPLY TO DISEASE CheckBox diseaseAttribute = row.FindControl("DiseaseAttribute") as CheckBox; HiddenField diseaseAttributeId = row.FindControl("DiseaseAttributeId") as HiddenField; if (diseaseAttribute.Checked) { // only insert if key not set, as already assigned if (string.IsNullOrEmpty(diseaseAttributeId.Value)) { LookupCodeAttribute lkpCodeAttribute = new LookupCodeAttribute(); // set required lkpCodeAttribute[LookupCodeAttribute.LookupCodeId] = lkpCodeId; lkpCodeAttribute[LookupCodeAttribute.AttributeId] = _lkpDiseaseAttributeId; lkpCodeAttribute[LookupCodeAttribute.AttributeValue] = _diseaseName; lkpCodeAttribute[LookupCodeAttribute.AttributeSuppress] = 0; lkpCodeAttribute.Save(); // update hidden field diseaseAttributeId.Value = lkpCodeAttribute[LookupCodeAttribute.LookupCodeAttributeId].ToString(); } } // if unchecking and pri key set, delete else { if (!string.IsNullOrEmpty(diseaseAttributeId.Value)) { int lkpCodeAttributeId = int.Parse(diseaseAttributeId.Value); LookupCodeAttribute lkpCodeAttribute = new LookupCodeAttribute(); lkpCodeAttribute.Delete(lkpCodeAttributeId); // udpate hidden field diseaseAttributeId.Value = string.Empty; } } // APPLY FILTERS: only when NOT in disease mode if (!QueryDiseaseId.HasValue) { // APPLY TO ATTRIBUTE CheckBox applyToAttribute = row.FindControl("ApplyToAttribute") as CheckBox; HiddenField applyToAttributeId = row.FindControl("ApplyToAttributeId") as HiddenField; if (applyToAttribute.Checked) { // no udpates, just inserts if (string.IsNullOrEmpty(applyToAttributeId.Value)) { LookupCodeAttribute lkpCodeAttribute = new LookupCodeAttribute(); lkpCodeAttribute[LookupCodeAttribute.LookupCodeId] = lkpCodeId.Value; lkpCodeAttribute[LookupCodeAttribute.AttributeId] = int.Parse(FilterByAttributeNames.Value); lkpCodeAttribute[LookupCodeAttribute.AttributeValue] = FilterByAttributeValues.Value; lkpCodeAttribute[LookupCodeAttribute.AttributeSuppress] = 0; lkpCodeAttribute.Save(); applyToAttributeId.Value = lkpCodeAttribute[lkpCodeAttribute.PrimaryKeyName].ToString(); } } // if unchecking, delete only if key exists else if (!string.IsNullOrEmpty(applyToAttributeId.Value)) { LookupCodeAttribute lkpCodeAttribute = new LookupCodeAttribute(); lkpCodeAttribute.Delete(int.Parse(applyToAttributeId.Value)); applyToAttributeId.Value = string.Empty; } // APPLY TO PARENT CheckBox applyToParent = row.FindControl("ApplyToParent") as CheckBox; HiddenField applyToParentId = row.FindControl("ApplyToParentId") as HiddenField; if (applyToParent.Checked) { // no udpates, just inserts if (string.IsNullOrEmpty(applyToParentId.Value)) { LookupParentChildCode parentChildCode = new LookupParentChildCode(); parentChildCode[LookupParentChildCode.ParentCodeId] = int.Parse(FilterByParentLkpCodes.Value); parentChildCode[LookupParentChildCode.ChildCodeId] = lkpCodeId.Value; parentChildCode.Save(); applyToParentId.Value = parentChildCode[parentChildCode.PrimaryKeyName].ToString(); } } // if unchecking, delete only if key exists else if (!string.IsNullOrEmpty(applyToParentId.Value)) { LookupParentChildCode parentChildCode = new LookupParentChildCode(); parentChildCode.Delete(int.Parse(applyToParentId.Value)); applyToParentId.Value = string.Empty; } } } } // Save new CSV values var bulkLkpCodesRows = (from rowCodes in BulkLkpCodes.Text.Split(System.Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries) let lkpCode = from code in rowCodes.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) select code.Trim() select lkpCode).SelectMany(s => s); foreach (string code in bulkLkpCodesRows) { LookupCode lkpCode = new LookupCode(); // set required fields lkpCode[LookupCode.LkpFieldName] = CurrentLkpFieldName.Value; lkpCode[LookupCode.LkpSuppress] = 0; // set code lkpCode[LookupCode.LkpCode] = code; // insert lkpCode.Save(); } // reset bulk codes BulkLkpCodes.Text = string.Empty; // refresh cache for item CacheManager.ResetLookupCodeCache(); // register warning for missing codes if (duplicateCodes.Count() > 0) { // create safe js array string[] duplicateCodesArray = duplicateCodes.Select(c => PageUtil.EscapeSingleQuotes(c)).ToArray(); string clientArray = "['" + string.Join("', '", duplicateCodesArray) + "']"; Page.ClientScript.RegisterStartupScript(this.GetType(), "duplicateCodesMessage", "warnDuplicateCodes(" + clientArray + ");", true); } }