/// <summary>
 /// Get an instance of InspectionReportModel class
 /// </summary>
 public InspectionReportModel()
 {
     Links = new List<string>();
     GridColumns = new DynamicDataGrid();
     ServiceOrderHeader = new List<Field>();
     FormDefinition = new Form();
     OrderIdentifier = new Field();
     PictureModel = new PictureReportModel();
 }
        /// <summary>
        /// Validate at server side the form values entered by the user. These values are validated comparing with the attibutes defined in the XML associated to the form
        /// </summary>
        /// <param name="formDefinition">XML file converted to an object</param>
        /// <param name="formCollection">Field and value entered by the user in the form</param>
        protected void ValidateForm(Form formDefinition, FormCollection formCollection)
        {
            //Field name
            string fieldName;
            //Form field value
            string valData;
            //Lenght allowed defined as a rule
            int length;
            //String value set in a xml attribute
            string stringXml;

            foreach (var section in formDefinition.Sections)
            {
                foreach (var element in section.FormElements)
                {
                    fieldName = element.Field.FieldName;
                    ValueProviderResult val = formCollection.GetValue(fieldName);
                    //Control check box input that is not sent when it isn't checked
                    valData = val != null ? val.AttemptedValue.ToString() : String.Empty;

                    element.Field.FieldValue = valData;

                    if (String.IsNullOrEmpty(valData))
                    {
                        //Validate the field as mandatory
                        ValidateMandatoryField(element, fieldName);
                    }
                    else
                    {
                        foreach (var rulesForm in element.Field.RulesForms)
                        {
                            switch (rulesForm)
                            {
                                //Rule Atributte -> EndDate
                                case RulesForm.RuleEndDate:
                                    {
                                        ValidateRuleEndDate(element, fieldName, valData);
                                        break;
                                    }
                                //Rule Atributte -> Expression
                                case RulesForm.RuleExpression:
                                    {
                                        stringXml = element.Field.GetPropertyValue<string>("Expression");
                                        Match match = Regex.Match(valData, stringXml,
                                                                  RegexOptions.IgnoreCase);
                                        //If the value doesn't match witht he regular expression then an error
                                        if (!match.Success)
                                        {
                                            ModelState.AddModelError("",
                                                                     String.Format(
                                                                         Resources.ServiceOrder.
                                                                             ErrorFieldRegularExpression,
                                                                         fieldName, stringXml));
                                        }
                                        break;
                                    }
                                //Rule Atributte -> MaxLenght
                                case RulesForm.RuleMaxLength:
                                    {
                                        length = Convert.ToInt32(element.Field.GetPropertyValue<string>("MaxLength"));

                                        //If the lenght of value is greater than the allowed one  then an error
                                        if (valData.Length > length)
                                        {
                                            ModelState.AddModelError("",
                                                                     String.Format(
                                                                         Resources.ServiceOrder.ErrorFieldTextMaxLength,
                                                                         fieldName));
                                        }
                                        break;
                                    }
                                //Rule Atributte -> MaxValue
                                case RulesForm.RuleMaxValue:
                                    {
                                        ValidateRuleMaxValue(element, fieldName, valData);
                                        break;
                                    }
                                //Rule Atributte -> MinLenght
                                case RulesForm.RuleMinLength:
                                    {
                                        length = Convert.ToInt32(element.Field.GetPropertyValue<string>("MinLength"));

                                        //If the lenght of the value is less than a one  then an error
                                        if (valData.Length < length)
                                        {
                                            ModelState.AddModelError("",
                                                                     String.Format(
                                                                         Resources.ServiceOrder.ErrorFieldTextMinLength,
                                                                         fieldName));
                                        }
                                        break;
                                    }
                                //Rule Atributte -> MinValue
                                case RulesForm.RuleMinValue:
                                    {
                                        ValidateRuleMinValue(element, fieldName, valData);
                                        break;
                                    }
                                //Rule Atributte -> NumDigit
                                case RulesForm.RuleNumDigit:
                                    {
                                        //Validate the number of the digits of the value entered by the user
                                        ValidateNumDigit(element, fieldName, valData);
                                        break;
                                    }
                                //Rule Atributte -> StartDate
                                case RulesForm.RuleStartDate:
                                    {
                                        ValidateRuleStartDate(element, fieldName, valData);
                                        break;
                                    }
                            }
                        }
                    }
                }
            }
        }
 public ExportInspectionReportsModel()
 {
     InspectionReports = new Dictionary<string, DynamicDataGrid>();
     ServiceOrderData = new Form();
 }
        /// <summary>
        /// Validate the rules specified in the xml difinitions
        /// </summary>
        /// <param name="formDefinition">The definition of the form</param>
        /// <param name="formCollection">The collection of values obtained from the form</param>
        protected void ValidateBusinessRules(Form formDefinition, FormCollection formCollection)
        {
            FormRulesDateIntervalRule dateRule = null;
            FormRulesSummatoryRule summatoryRule = null;

            if (formDefinition.Rules == null) return;

            dateRule = formDefinition.Rules.DateIntervalRule;
            summatoryRule = formDefinition.Rules.SummatoryRule;

            //validate date range rule
            if (dateRule != null)
            {

                string[] beginDateValue = formCollection[dateRule.StartField].ToString().Split(new[] { '-', '/' });
                string[] endDateValue = formCollection[dateRule.EndField].ToString().Split(new[] { '-', '/' });
                DateTime beginDate = new DateTime();
                DateTime endDate = new DateTime();

                //if the begin date isn't filled, the system will add an error message
                if (beginDateValue.Length > 1)
                {
                    beginDate = new DateTime(int.Parse(beginDateValue[2]), int.Parse(beginDateValue[1]), int.Parse(beginDateValue[0]));
                }

                //if the end date isn't filled, the system will add an error messaage
                if (endDateValue.Length > 1)
                {
                    endDate = new DateTime(int.Parse(endDateValue[2]), int.Parse(endDateValue[1]), int.Parse(endDateValue[0]));
                }

                if (beginDateValue.Length > 1 && endDateValue.Length == 1)
                {
                    ModelState.AddModelError("EndDateValue", Resources.ServiceOrder.EndDateValue);
                }
                else if (beginDateValue.Length == 1 && endDateValue.Length > 1)
                {
                    ModelState.AddModelError("BeginDateValue", Resources.ServiceOrder.BeginDateValue);
                }

                //if the begin date is bigger than end date, the system will add an error message
                if ((beginDate > endDate) && (beginDateValue.Length > 1 && endDateValue.Length > 1))
                {
                    ModelState.AddModelError("DateRule", Resources.ServiceOrder.DateRule);
                }
            }
            //validate summatory rule
            if (summatoryRule != null)
            {
                decimal sumatoryValue = 0;
                decimal totalValue = summatoryRule.Value;
                //retreive the elements to get the sum value
                List<FormRulesSummatoryRuleElement> summatoryElements = summatoryRule.FormElements.ToList();

                summatoryElements.ForEach(element =>
                {
                    string tempSumValue = formCollection[element.Identifier].ToString();
                    //if any element hasn't value, the system won't sum the values.
                    if (!string.IsNullOrEmpty(tempSumValue))
                        sumatoryValue += decimal.Parse(tempSumValue);
                });

                //if the summatory value is different than expected value, the system will add an error message
                if (sumatoryValue > 0 && sumatoryValue != totalValue)
                {
                    ModelState.AddModelError("SummatoryRule", Resources.ServiceOrder.SummatoryRule);
                }

            }
        }