Example #1
0
        /// <summary>
        /// Validate the existence of the Foreign Key value on the referencing sheet, if not add error
        /// </summary>
        /// <param name="context">COBieContext object holding global values for this model</param>
        private void ValidateForeignKeys(COBieWorkbook workbook, ICOBieSheetValidationTemplate SheetValidator)
        {
            int rowIndex = 1;

            foreach (COBieRow row in Rows)
            {
                foreach (COBieColumn foreignKeyColumn in ForeignKeyColumns)
                {
                    // TODO: COBieColumn should own the relationship rather than creating a new one each time.
                    COBieColumnRelationship cobieReference = new COBieColumnRelationship(workbook, foreignKeyColumn);

                    if ((SheetValidator == null) ||
                        (SheetValidator.IsRequired.ContainsKey(foreignKeyColumn.ColumnOrder) && SheetValidator.IsRequired[foreignKeyColumn.ColumnOrder])
                        )
                    {
                        if (!string.IsNullOrEmpty(foreignKeyColumn.ReferenceColumnName))
                        {
                            COBieCell cell = row[foreignKeyColumn.ColumnOrder];

                            string foreignKeyValue = cell.CellValue;

                            // Don't validate nulls. Will be reported by the Foreign Key null value check, so just skip here
                            if (!string.IsNullOrEmpty(foreignKeyValue))
                            {
                                bool isValid = false;

                                bool isPickList = (cobieReference.SheetName == Constants.WORKSHEET_PICKLISTS);

                                if (isPickList)
                                {
                                    isValid = PickListMatch(cobieReference, cell);
                                }
                                else
                                {
                                    isValid = ForeignKeyMatch(cobieReference, cell);
                                }
                                //report no match
                                if (!isValid)
                                {
                                    string errorDescription = BuildErrorMessage(cobieReference, isPickList);

                                    COBieError.ErrorTypes errorType = isPickList == true
                                        ? COBieError.ErrorTypes.PickList_Violation
                                        : COBieError.ErrorTypes.ForeignKey_Violation;

                                    COBieError error = new COBieError(SheetName, foreignKeyColumn.ColumnName,
                                                                      errorDescription, errorType,
                                                                      COBieError.ErrorLevels.Error, row.InitialRowHashValue,
                                                                      foreignKeyColumn.ColumnOrder, rowIndex);
                                    _errors.Add(error);
                                }
                            }
                        }
                    }
                }
                rowIndex++;
            }
        }