//格式转换后,检验字段的有效性并修复字段 YH 15/4/09 public static bool FieldsCheckAfterTransaction(IWorkspace pInputWorkspace, IWorkspace pOutPutWorkspace, IFields pInputFields, ref IFields pOoutPutFields) { bool functionReturnValue = false; IFieldChecker pFieldChecker = default(IFieldChecker); IEnumFieldError pEnumFieldError = null; pFieldChecker = new FieldChecker(); pFieldChecker.InputWorkspace = pInputWorkspace; pFieldChecker.ValidateWorkspace = pOutPutWorkspace; pFieldChecker.Validate(pInputFields, out pEnumFieldError, out pOoutPutFields); //处理字段中不符合语义规则的情况 if ((pEnumFieldError != null)) { pEnumFieldError.Reset(); IFieldError pFieldError = default(IFieldError); pFieldError = pEnumFieldError.Next(); //g_clsErrorHandle.DisplayInformation("导出字段中存在无效字段: " + pOoutPutFields.Field(pFieldError.FieldIndex).Name + "导出终止!!", false); MessageBoxEx.Show("导出字段中存在无效字段: " + pOoutPutFields.get_Field(pFieldError.FieldIndex).Name + "导出终止!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return(false); return(functionReturnValue); } return(true); return(functionReturnValue); }
public void Initialize() { _checker = new DefaultFieldChecker(); _checkerDict = new Dictionary<FieldType, IFieldChecker>(); _checkerDict.Add(new FieldType {Type = FieldType.TypEField.Author}, _checker); _fv = new FieldValidator(_checkerDict); }
public static bool IsHluLayer(IFeatureLayer layer, IFields origFields, IFieldChecker fieldChecker, object[] validWorkspaces, Dictionary <Type, int> typeMapSystemToSQL, ref HluGISLayer.incid_mm_polygonsDataTable hluLayerStructure, out int[] hluFieldMap, out string[] hluFieldNames) { hluFieldMap = null; hluFieldNames = null; if ((layer == null) || !layer.Valid || (((IFeatureLayerDefinition)layer).DefinitionSelectionSet != null)) { return(false); } bool isHlu = true; try { IFeatureClass testFeatureClass = layer.FeatureClass; if (hluLayerStructure == null) { hluLayerStructure = new HluGISLayer.incid_mm_polygonsDataTable(); } IFeatureWorkspace testWorkspace = ((IDataset)testFeatureClass).Workspace as IFeatureWorkspace; if (testWorkspace == null) { throw (new Exception("Invalid feature workspace.")); } if (System.Array.IndexOf(validWorkspaces, ((IWorkspace)testWorkspace).WorkspaceFactory.GetClassID().Value) == -1) { throw (new Exception("Invalid workspace type.")); } if (testFeatureClass.ShapeType != esriGeometryType.esriGeometryPolygon) { throw (new Exception("Invalid geometry type.")); } IFieldsEdit fieldsEdit = (IFieldsEdit)origFields; foreach (DataColumn c in hluLayerStructure.Columns) { IField newField = new FieldClass(); IFieldEdit2 newFieldEdit = (IFieldEdit2)newField; newFieldEdit.Name_2 = c.ColumnName; int fieldType; if (!typeMapSystemToSQL.TryGetValue(c.DataType, out fieldType)) { throw (new Exception("Invalid field type.")); } newFieldEdit.Type_2 = (esriFieldType)fieldType; if ((c.MaxLength != -1) && (newField.Type == esriFieldType.esriFieldTypeString)) { newFieldEdit.Length_2 = c.MaxLength; } fieldsEdit.AddField(newField); } fieldChecker.ValidateWorkspace = (IWorkspace)testWorkspace; IEnumFieldError error; IFields fixedFields; fieldChecker.Validate(origFields, out error, out fixedFields); hluFieldMap = new int[origFields.FieldCount]; hluFieldNames = new string[hluFieldMap.Length]; for (int i = 0; i < fixedFields.FieldCount; i++) { IField fixedField = fixedFields.get_Field(i); int ordinal = testFeatureClass.Fields.FindField(fixedField.Name); if (ordinal == -1) { throw (new Exception("Field name does not match the HLU GIS layer structure.")); } IField fcField = testFeatureClass.Fields.get_Field(ordinal); if (fcField.Type != fixedField.Type) { throw (new Exception("Field type does not match the HLU GIS layer structure.")); } if ((fcField.Type == esriFieldType.esriFieldTypeString) && (fcField.Length > fixedField.Length)) { throw (new Exception("Field length does not match the HLU GIS layer structure.")); } hluFieldMap[i] = ordinal; hluFieldNames[i] = fixedField.Name; } } catch { return(false); } return(isHlu); }
public static bool IsHluLayer(IFeatureLayer layer, IFields origFields, IFieldChecker fieldChecker, object[] validWorkspaces, Dictionary<Type, int> typeMapSystemToSQL, ref HluGISLayer.incid_mm_polygonsDataTable hluLayerStructure, out int[] hluFieldMap, out string[] hluFieldNames) { hluFieldMap = null; hluFieldNames = null; if ((layer == null) || !layer.Valid || (((IFeatureLayerDefinition)layer).DefinitionSelectionSet != null)) return false; bool isHlu = true; try { IFeatureClass testFeatureClass = layer.FeatureClass; if (hluLayerStructure == null) hluLayerStructure = new HluGISLayer.incid_mm_polygonsDataTable(); IFeatureWorkspace testWorkspace = ((IDataset)testFeatureClass).Workspace as IFeatureWorkspace; if (testWorkspace == null) throw (new Exception("Invalid feature workspace.")); if (System.Array.IndexOf(validWorkspaces, ((IWorkspace)testWorkspace).WorkspaceFactory.GetClassID().Value) == -1) throw (new Exception("Invalid workspace type.")); if (testFeatureClass.ShapeType != esriGeometryType.esriGeometryPolygon) throw (new Exception("Invalid geometry type.")); IFieldsEdit fieldsEdit = (IFieldsEdit)origFields; foreach (DataColumn c in hluLayerStructure.Columns) { IField newField = new FieldClass(); IFieldEdit2 newFieldEdit = (IFieldEdit2)newField; newFieldEdit.Name_2 = c.ColumnName; int fieldType; if (!typeMapSystemToSQL.TryGetValue(c.DataType, out fieldType)) throw (new Exception("Invalid field type.")); newFieldEdit.Type_2 = (esriFieldType)fieldType; if ((c.MaxLength != -1) && (newField.Type == esriFieldType.esriFieldTypeString)) newFieldEdit.Length_2 = c.MaxLength; fieldsEdit.AddField(newField); } fieldChecker.ValidateWorkspace = (IWorkspace)testWorkspace; IEnumFieldError error; IFields fixedFields; fieldChecker.Validate(origFields, out error, out fixedFields); hluFieldMap = new int[origFields.FieldCount]; hluFieldNames = new string[hluFieldMap.Length]; for (int i = 0; i < fixedFields.FieldCount; i++) { IField fixedField = fixedFields.get_Field(i); int ordinal = testFeatureClass.Fields.FindField(fixedField.Name); if (ordinal == -1) throw (new Exception("Field name does not match the HLU GIS layer structure.")); IField fcField = testFeatureClass.Fields.get_Field(ordinal); if (fcField.Type != fixedField.Type) throw (new Exception("Field type does not match the HLU GIS layer structure.")); if ((fcField.Type == esriFieldType.esriFieldTypeString) && (fcField.Length > fixedField.Length)) throw (new Exception("Field length does not match the HLU GIS layer structure.")); hluFieldMap[i] = ordinal; //--------------------------------------------------------------------- // FIXED: KI107 (GIS layer column names) // Use the field names from the GIS layer so that they can be found // when performing any SELECT statements. hluFieldNames[i] = fcField.Name; //hluFieldNames[i] = fixedField.Name; //--------------------------------------------------------------------- } } catch { return false; } return isHlu; }
/// <summary> /// Constructs a new <see cref="FieldValidator"/>. /// </summary> /// <param name="checkers">A dictionary of field checkers. If not specified, <see cref="DefaultFieldChecker"/> is used.</param> /// <param name="defaultChecker">The <see cref="IFieldChecker"/> implementation to use as default</param> public FieldValidator(IFieldChecker defaultChecker = null, IDictionary <Item.FieldType, IFieldChecker> checkers = null) { _checkers = checkers ?? new Dictionary <Item.FieldType, IFieldChecker>(); _defaultChecker = defaultChecker ?? new DefaultFieldChecker(); }