Ejemplo n.º 1
0
        public ActionResult Manage(string id)
        {
            // Note: id is a Parent FieldID
            using (Entity db = new Entity())
            {
                t_fieldid parent_fieldid = db.t_fieldid.Where(e => e.fieldid == id).FirstOrDefault();

                List <t_fieldid> tfieldids = db.t_fieldid.Where(e => e.parent_fieldid == id).OrderBy(e => e.value).ToList();

                ICollection <FieldIDModel> fieldids = new List <FieldIDModel>();
                // This is a slight hack but it works - I'm sure there's a more elegant way :)
                foreach (t_fieldid f in tfieldids)
                {
                    FieldIDModel m = new FieldIDModel();
                    m.FieldID       = f.fieldid;
                    m.ParentFieldID = f.parent_fieldid;
                    m.Value         = f.value;
                    m.SortKey       = f.sort_key;
                    m.Active        = f.active;
                    fieldids.Add(m);
                }

                if (fieldids.Count == 0)
                {
                    throw new ArgumentException("Error getting FieldID entries.");
                }

                return(View(FromDatabase(id, parent_fieldid.value, fieldids)));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Edit(FieldID model)
        {
            //   if (ModelState.IsValid)
            //   {
            using (Entity db = new Entity())
            {
                if (model._FieldID.Length == 0)
                {
                    throw new ArgumentException("FieldID not provided");
                }

                t_fieldid rec = db.t_fieldid.Find(model._FieldID);
                if (rec == null)
                {
                    throw new ArgumentException("Invalid FieldID: " + model._FieldID);
                }

                // Set and save user fields.
                SetCoreFields(ref rec, model);
                rec.updatedby_userid = CurrentUser.Id;
                rec.updated_date     = DateTime.Now;
                db.SaveChanges();

                return(RedirectToAction("Manage", new { id = rec.parent_fieldid }));
            }
            //}
            //else
            //{
            //    return View(model);
            //}
        }
Ejemplo n.º 3
0
        private string GetNextKey(string ParentFieldID)
        {
            int    x          = 0;
            string strSeq     = "";
            string newFieldID = "";

            do
            {
                using (Entity db = new Entity())
                {
                    strSeq     = x.ToString("000");
                    newFieldID = ParentFieldID.Substring(0, 3) + strSeq;
                    t_fieldid rec = db.t_fieldid.Where(e => e.fieldid == newFieldID).FirstOrDefault();

                    if (rec == null)  // we have a winner
                    {
                        x = 99999;
                    }
                    else
                    {
                        x++;
                    }
                }
            } while (x < 999);  // this limits at most 999 new inserts for a given parent

            return(newFieldID);
        }
Ejemplo n.º 4
0
        public FieldID(string fieldid)
        {
            using (Entity db = new Entity())
            {
                t_fieldid item = db.t_fieldid.Find(fieldid);

                if (item == null)
                {
                    throw new ArgumentException("Invalid FieldID: " + fieldid);
                }

                _FieldID      = item.fieldid;
                ParentFieldID = item.parent_fieldid;
                Value         = item.value;
                if (item.active == true)
                {
                    Active = true;
                }
                else
                {
                    Active = false;
                }

                SortKey = item.sort_key;

                CreatedByUserId = item.createdby_userid;
                CreatedDate     = item.created_date;
                UpdatedDate     = item.updated_date;
                UpdatedByUserId = item.updatedby_userid;
            }
        }
Ejemplo n.º 5
0
 public ActionResult Add(FieldID model)
 {
     if (CurrentUser.IsInRole(Role.SystemAdministrator) && model.Value.Length > 0 && model.ParentFieldID.Length > 0)
     {
         using (Entity db = new Entity())
         {
             // Set and save user fields.
             t_fieldid rec = new t_fieldid();
             db.t_fieldid.Add(rec);
             rec.fieldid          = GetNextKey(model.ParentFieldID);
             rec.value            = model.Value;
             rec.parent_fieldid   = model.ParentFieldID;
             rec.createdby_userid = CurrentUser.Id;
             rec.created_date     = DateTime.Now;
             rec.active           = true;
             db.SaveChanges();
         }
     }
     return(RedirectToAction("Manage", new { id = model.ParentFieldID }));
 }
Ejemplo n.º 6
0
 private void SetCoreFields(ref t_fieldid rec, FieldID model)
 {
     rec.value  = model.Value;
     rec.active = model.Active;
 }
Ejemplo n.º 7
0
        private t_indicator SetEntityFromModel(Indicator model)
        {
            using (Entity context = new Entity())
            {
                t_indicator indicator = null;
                if (model.IndicatorId.HasValue)
                {
                    indicator = context.t_indicator.Find(model.IndicatorId.Value);
                    if (indicator == null)
                    {
                        throw new ArgumentException("Invalid indicatorId: " + model.IndicatorId.ToString());
                    }
                    indicator.updatedby_userid = CurrentUser.Id;
                    indicator.updated_date     = DateTime.Now;
                }
                else
                {
                    indicator = new t_indicator();
                    indicator.createdby_userid = CurrentUser.Id;
                    indicator.created_date     = DateTime.Now;

                    byte maxSort = 0;
                    var  aim     = context.t_aim.Find(model.AimId);
                    if (aim.indicators.Count > 0)
                    {
                        maxSort = aim.indicators.Max(e => e.sort);
                    }
                    indicator.sort = maxSort += 1;

                    context.t_indicator.Add(indicator);
                }

                var language = IidCulture.CurrentLanguage;

                indicator.aim_id = model.AimId;

                // Only apply t_indicator.name if the language is English or this is
                // a new indicator (in which case, we have to supply some value).
                if (language == Language.English || !model.IndicatorId.HasValue)
                {
                    indicator.name       = model.Name;
                    indicator.definition = model.Definition;
                }

                t_fieldid     indicatorTypeField = context.t_fieldid.Find(model.TypeFieldId);
                IndicatorType indicatorType      = Enumerations.Parse <IndicatorType>(indicatorTypeField.value);
                switch (indicatorType)
                {
                case IndicatorType.Percentage:
                case IndicatorType.Average:
                case IndicatorType.Ratio:
                    if (language == Language.English || !model.IndicatorId.HasValue)
                    {
                        indicator.numerator_name         = model.NumeratorName;
                        indicator.numerator_definition   = model.NumeratorDefinition;
                        indicator.numerator_source       = model.NumeratorSource;
                        indicator.denominator_name       = model.DenominatorName;
                        indicator.denominator_definition = model.DenominatorDefinition;
                        indicator.denominator_source     = model.DenominatorSource;
                    }
                    break;

                default:
                    indicator.numerator_name         = null;
                    indicator.numerator_definition   = null;
                    indicator.denominator_name       = null;
                    indicator.denominator_definition = null;
                    indicator.denominator_source     = null;
                    break;
                }
                indicator.numerator_source                  = model.NumeratorSource;
                indicator.indicator_type_fieldid            = model.TypeFieldId;
                indicator.group_fieldid                     = model.GroupFieldId;
                indicator.data_collection_frequency_fieldid = model.DataCollectionFrequencyFieldId;
                indicator.sampling_fieldid                  = model.SamplingFieldId;
                indicator.report_class_fieldid              = model.ReportClassFieldId;
                switch (indicatorType)
                {
                case IndicatorType.Percentage:
                case IndicatorType.Average:
                case IndicatorType.Count:
                case IndicatorType.Ratio:
                    indicator.change_variable = model.ChangeVariable;
                    break;

                default:
                    indicator.change_variable = null;
                    break;
                }
                switch (indicatorType)
                {
                case IndicatorType.Percentage:
                case IndicatorType.Average:
                case IndicatorType.Count:
                case IndicatorType.Ratio:
                    indicator.disaggregate_by_sex = model.DisaggregateBySex;
                    indicator.disaggregate_by_age = model.DisaggregateByAge;
                    break;

                default:
                    indicator.disaggregate_by_sex = false;
                    indicator.disaggregate_by_age = false;
                    break;
                }
                switch (indicatorType)
                {
                case IndicatorType.Percentage:
                case IndicatorType.Average:
                case IndicatorType.Count:
                case IndicatorType.Ratio:
                    indicator.target_performance         = model.TargetPerformance;
                    indicator.threshold_good_performance = model.ThresholdGoodPerformance;
                    indicator.threshold_poor_performance = model.ThresholdPoorPerformance;
                    indicator.increase_is_good           = model.IncreaseIsGood;
                    break;

                default:
                    indicator.target_performance         = null;
                    indicator.threshold_good_performance = null;
                    indicator.threshold_poor_performance = null;
                    indicator.increase_is_good           = false;
                    break;
                }
                switch (indicatorType)
                {
                case IndicatorType.Ratio:
                    indicator.rate_per_fieldid = model.RatePerFieldId;
                    break;

                default:
                    indicator.rate_per_fieldid = null;
                    break;
                }
                indicator.active = model.Active;


                context.SaveChanges();

                // Defer translation logic until after the indicator has been saved to the db.
                // This is necessary because we depend on indicator_id, which may not be known sooner.
                if (language != Language.English)
                {
                    var languageId = IidCulture.CurrentLanguageId;
                    var userId     = CurrentUser.Id;
                    indicator.set_name_translated(languageId, model.Name, userId);
                    indicator.set_definition_translated(languageId, model.Definition, userId);
                    indicator.set_numerator_name_translated(languageId, model.NumeratorName, userId);
                    indicator.set_numerator_definition_translated(languageId, model.NumeratorDefinition, userId);
                    indicator.set_numerator_source_translated(languageId, model.NumeratorSource, userId);
                    indicator.set_denominator_name_translated(languageId, model.DenominatorName, userId);
                    indicator.set_denominator_definition_translated(languageId, model.DenominatorDefinition, userId);
                    indicator.set_denominator_source_translated(languageId, model.DenominatorSource, userId);
                }

                // NOTE: Once observations have been recorded, age ranges cannot be modified,
                // as this would cause a primary key constraint violation.
                if (indicator.observations?.Count() == 0)
                {
                    context.t_indicator_age_range.RemoveRange(indicator.indicator_age_ranges);
                    if (model.DisaggregateByAge && model.AgeRangeNames != null)
                    {
                        foreach (string ageRange in model.AgeRangeNames)
                        {
                            indicator.indicator_age_ranges.Add(
                                new t_indicator_age_range()
                            {
                                indicator_id = indicator.indicator_id,     //entity.IndicatorId.Value,
                                age_range    = ageRange
                            });
                        }

                        context.SaveChanges();
                    }
                }

                return(indicator);
            }
        }