// ALL VALUES SHOULD BE FORMATTED AND TRANSLATED BEFORE THIS METHOD public static AggregateIndicator Aggregate(AggregateIndicator ind, AggregateIndicator existingValue, List<IndicatorDropdownValue> dropdownOptions) { AggregateIndicator result = (existingValue == null || existingValue.Name == null) ? ind : existingValue; if (string.IsNullOrEmpty(ind.Value) && (existingValue == null || string.IsNullOrEmpty(existingValue.Value))) result.Value = ""; else if (string.IsNullOrEmpty(ind.Value)) result.Value = existingValue.Value; else if (existingValue == null || string.IsNullOrEmpty(existingValue.Value)) result.Value = ind.Value; else if (ind.AggType == (int)IndicatorAggType.Average) // Need to fix average rule? How the f**k are we going to do that? wtf. result.Value = ""; else if (ind.AggType == (int)IndicatorAggType.Combine && (ind.DataType == (int)IndicatorDataType.LargeText || ind.DataType == (int)IndicatorDataType.Text)) result.Value = AggregateString(ind, existingValue); else if (ind.AggType == (int)IndicatorAggType.Combine && (ind.DataType == (int)IndicatorDataType.Multiselect || ind.DataType == (int)IndicatorDataType.DiseaseMultiselect || ind.DataType == (int)IndicatorDataType.Partners)) result.Value = AggregateMultiselect(ind, existingValue); else if (ind.DataType == (int)IndicatorDataType.Number || ind.DataType == (int)IndicatorDataType.Month || ind.DataType == (int)IndicatorDataType.Year || ind.DataType == (int)IndicatorDataType.Integer) result.Value = AggregateNumber(ind, existingValue); else if (ind.DataType == (int)IndicatorDataType.Date) result.Value = AggregateDate(ind, existingValue); else if (ind.DataType == (int)IndicatorDataType.Dropdown) result.Value = AggregateDropdown(ind, existingValue, dropdownOptions); else if (ind.AggType == (int)IndicatorAggType.None) result.Value = Translations.NA; else result.Value = AggregateString(ind, existingValue); return result; }
public static AggregateIndicator AggregateChildren(List<AdminLevelIndicators> list, string key, AggregateIndicator startResult, List<IndicatorDropdownValue> dropdownOptions) { AggregateIndicator result = new AggregateIndicator(); if (startResult != null) result = startResult; foreach (var level in list) { if (level.Indicators.ContainsKey(key)) { result = Aggregate(level.Indicators[key], result, dropdownOptions); } else result = AggregateChildren(level.Children, key, result, dropdownOptions); } return result; }
private static string AggregateMultiselect(AggregateIndicator ind1, AggregateIndicator existingValue) { List<string> values1 = ind1.Value.Split('|').ToList(); List<string> values2 = existingValue.Value.Split('|').ToList(); var result = values1.Union(values2); return string.Join("|", result.ToArray()); }
private static string AggregateDropdown(AggregateIndicator ind1, AggregateIndicator existingValue, List<IndicatorDropdownValue> dropdownOptions) { if (ind1.AggType == (int)IndicatorAggType.Combine) { var existingVal = TranslationLookup.GetValue(existingValue.Value, existingValue.Value); var newVal = TranslationLookup.GetValue(ind1.Value,ind1.Value); return existingVal + ", " + newVal; } var ind1option = dropdownOptions.FirstOrDefault(i => i.IndicatorId == ind1.IndicatorId && (int)i.EntityType == ind1.EntityTypeId && i.TranslationKey == ind1.Value); var ind2option = dropdownOptions.FirstOrDefault(i => i.IndicatorId == existingValue.IndicatorId && (int)i.EntityType == existingValue.EntityTypeId && i.TranslationKey == existingValue.Value); if(ind1option == null) return existingValue.Value; if(ind2option == null) return ind1.Value; if (ind1.AggType == (int)IndicatorAggType.Min) { if(ind1option.WeightedValue <= ind2option.WeightedValue) return ind1.Value; else return existingValue.Value; } if (ind1.AggType == (int)IndicatorAggType.Max) { if (ind1option.WeightedValue >= ind2option.WeightedValue) return ind1.Value; else return existingValue.Value; } return TranslationLookup.GetValue("NA", "NA"); }
private static string AggregateNumber(AggregateIndicator ind1, AggregateIndicator existingValue) { double i1 = 0, i2 = 0; if (!Double.TryParse(ind1.Value, out i1) && !Double.TryParse(existingValue.Value, out i2)) return ""; if (!Double.TryParse(ind1.Value, out i1)) return i2.ToString(); if (!Double.TryParse(existingValue.Value, out i2)) return i1.ToString(); if (ind1.AggType == (int)IndicatorAggType.Sum) return (i1 + i2).ToString(); if (ind1.AggType == (int)IndicatorAggType.Min) if (i1 >= i2) return existingValue.Value; else return ind1.Value; if (ind1.AggType == (int)IndicatorAggType.Max) if (i1 >= i2) return ind1.Value; else return existingValue.Value; return ind1.Value; }
private static string AggregateString(AggregateIndicator ind1, AggregateIndicator existingValue) { if (ind1.AggType == (int)IndicatorAggType.Combine) return existingValue.Value + ", " + ind1.Value; else if (ind1.AggType == (int)IndicatorAggType.None) return Translations.NA; return "Invalid Aggregation Rule or Data Type"; }
private static string AggregateDate(AggregateIndicator ind1, AggregateIndicator existingValue) { if (ind1.AggType == (int)IndicatorAggType.Sum) return DateTime.MinValue.ToString("MM/dd/yyyy"); if (string.IsNullOrEmpty(ind1.Value)) return DateTime.MinValue.ToString("MM/dd/yyyy"); DateTime dt = DateTime.ParseExact(ind1.Value, "MM/dd/yyyy", CultureInfo.InvariantCulture); DateTime existing = DateTime.ParseExact(existingValue.Value, "MM/dd/yyyy", CultureInfo.InvariantCulture); if (ind1.AggType == (int)IndicatorAggType.Min) if (dt >= (DateTime)existing) return existing.ToString("MM/dd/yyyy"); else return dt.ToString("MM/dd/yyyy"); if (ind1.AggType == (int)IndicatorAggType.Max) if (dt >= (DateTime)existing) return dt.ToString("MM/dd/yyyy"); else return existing.ToString("MM/dd/yyyy"); return dt.ToString("MM/dd/yyyy"); }