Newer
Older
GHFX_REFACTOR / ValidateInfo.cs
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using GeoScene.Data;
using GeoScene.Engine;
using GeoScene.Globe;

namespace Cyberpipe
{
    [XmlType(TypeName = "FieldValidate")]
    public class ValidateInfo
    {
        [XmlArray("Types")]
        public List<ValidateType> validateTypes;
    }

    [XmlType(TypeName = "Type")]
    public class ValidateType
    {
        [XmlAttribute]
        public string label;
        [XmlArray("Fields")]
        public List<FieldInfo> fieldInfos;
    }

    [XmlType(TypeName = "Field")]
    public class FieldInfo
    {
        [XmlAttribute]
        public string label;
        [XmlAttribute]
        public bool unique;
        [XmlAttribute]
        public string type;
        [XmlAttribute]
        public bool isNull;
        [XmlAttribute]
        public string precise;
    }

    public enum EnumValidateType
    {
        PipeLinePress = 0, //待压管线
        PipeLineNoPress = 1,//不带压管线
        FangGou = 2,//方沟
        Attach = 3,//管线附属物
        Charac = 4,//特征管点
        Marker = 5,//标识器
        Valve = 6 //阀门
    }

    public class ValidateConfig
    {
        public static ValidateInfo LoadConfig()
        {
            return XmlTools.DeserializeFromXml<ValidateInfo>("FieldValidate.xml");
        }
    }

    public abstract class FieldValidate
    {
        private FieldValidate _nextValidater;

        public FieldValidate NextValidater
        {
            get { return _nextValidater; }
            set { _nextValidater = value; }
        }

        private ValidateType _validateType;

        public ValidateType ValidateType
        {
            get { return _validateType; }
            set { _validateType = value; }
        }

        protected FieldValidate(ValidateType validateType)
        {
            this._validateType = validateType;
        }

      /*  public bool Validate(GSOLayer layer)
        {
            if (this.Match())
            {
                return this.Execute(layer);
            }
            else
            {
                if (this._nextValidater != null)
                {
                    return _nextValidater.Validate(layer);
                }
            }
            return true;
        }*/
        public bool Validate(GSOLayer layer)
        {
            if (this.Match())
            {
                if (this.Execute(layer))
                {
                    if (this.NextValidater != null)
                    {
                        return _nextValidater.Validate(layer);
                    }
                }
                else
                {
                   return false;
                }
            }
            else
            {
                if (this._nextValidater != null)
                {
                    return _nextValidater.Validate(layer);
                }
            }
            return true;
        }
        public abstract bool Match();
        public abstract bool Execute(GSOLayer layer);
    }

    //唯一性验证
    public class FieldUniqueValidate : FieldValidate
    {
        private List<String> uniqueFieldNames = new List<String>();

        public FieldUniqueValidate(ValidateType validateType)
            : base(validateType)
        {

        }

        public override bool Match()
        {
            foreach (FieldInfo fieldInfo in this.ValidateType.fieldInfos)
            {
                if (fieldInfo.unique == true)
                {
                    uniqueFieldNames.Add(fieldInfo.label);
                }
            }
            return this.uniqueFieldNames.Count != 0 ? true : false;
        }

        public override bool Execute(GSOLayer layer)
        {
            GSOFeatures features = layer.GetAllFeatures();
            Dictionary<String, Dictionary<String, int>> myDic = new Dictionary<string,
                Dictionary<string, int>>();
            foreach (string uniqueFieldName in this.uniqueFieldNames)
            {
                myDic.Add(uniqueFieldName, new Dictionary<string, int>());
            }

            for (int i = 0; i < features.Length; i++)
            {
                GSOFeature feature = features[i];
                foreach (string uniqueFieldName in this.uniqueFieldNames)
                {
                    Dictionary<string, int> dic = myDic[uniqueFieldName];
                    String fieldValue = feature.GetValue(uniqueFieldName).ToString();
                    if (dic.ContainsKey(fieldValue))
                    {
                        dic[fieldValue] += dic[fieldValue] + 1;
                    }
                    else
                    {
                        dic.Add(fieldValue, 1);
                    }
                }
            }

            //显示信息是否有重复
            bool isSuccess = true;
            String logStr = layer.Caption + ":字段唯一性检查失败\r\n";
            foreach (string uniqueFieldName in this.uniqueFieldNames)
            {
                Dictionary<string, int> tmp = myDic[uniqueFieldName];
                foreach (KeyValuePair<string, int> kvp in tmp)
                {
                    if (kvp.Value != 1)
                    {
                        isSuccess = false;
                        logStr += "字段" + uniqueFieldName + "重复 " + kvp.Key + "次数:" + kvp.Value+"\r\n";
                    }
                }
            }
            if (!isSuccess)
            {
//                LogHelper.WriteLog(typeof(FieldUniqueValidate), logStr);
                LogHelper.Error(logStr);
            }
            return isSuccess;
        }
    }

    //完整性验证
    public class FieldCountValidate : FieldValidate
    {
        public FieldCountValidate(ValidateType validateType)
            : base(validateType)
        {

        }

        public override bool Match()
        {
            return this.ValidateType.fieldInfos.Count == 0 ? false : true;
        }

        public override bool Execute(GSOLayer layer)
        {
            List<FieldInfo> unFoundFields = new List<FieldInfo>();

            foreach (FieldInfo fieldInfo in this.ValidateType.fieldInfos)
            {
                String name = fieldInfo.label;
                String type = fieldInfo.type;
                GSOFeatureDataset featDataSet = layer.Dataset as GSOFeatureDataset;
                bool isFound = false;

                for (int i = 0; i < featDataSet.FieldCount; i++)
                {
                    String fieldName = featDataSet.GetField(i).Name;
                    String fieldType = this.FieldTypeToString(featDataSet.GetField(i).Type);
                    if (name == fieldName && type == fieldType)
                    {
                        isFound = true;
                        break;
                    }
                }
                if (!isFound)
                {
                    unFoundFields.Add(fieldInfo);
                }
            }
            //日志输出不满足字段信息
            if (unFoundFields.Count != 0)
            {
                String err = layer.Caption + ":字段完整性检查失败\r\n";
                foreach (FieldInfo unFoundField in unFoundFields)
                {
                    err += "未找到字段或其类型匹配:" + unFoundField.label + ":"
                        + unFoundField.type + "\r\n";
                }
//                LogHelper.WriteLog(typeof(FieldCountValidate), err);
                LogHelper.Error(err);
                return false;
            }
            return true;
        }

        private String FieldTypeToString(EnumFieldType type)
        {
            String result = "string";
            switch (type)
            {
                case EnumFieldType.Text:
                    result = "string";
                    break;
                case EnumFieldType.INT32:
                    result = "int";
                    break;
                case EnumFieldType.INT16:
                    result = "int";
                    break;
                case EnumFieldType.Double:
                    result = "double";
                    break;
                case EnumFieldType.Date:
                    result = "date";
                    break;
                default:
                    result = "string";
                    break;
            }
            return result;
        }
    }

    //字段顺序检查
    public class FieldOrderValidate : FieldValidate
    {
        public FieldOrderValidate(ValidateType validateType)
            : base(validateType)
        {

        }
        public override bool Match()
        {
            return this.ValidateType.fieldInfos.Count == 0 ? false : true;
        }

        public override bool Execute(GSOLayer layer)
        {

            GSOFeatureDataset featDataSet = layer.Dataset as GSOFeatureDataset;
            List<FieldInfo> unFoundFields = new List<FieldInfo>();

            for (int i = 0; i < this.ValidateType.fieldInfos.Count; i++)
            {
                String name = this.ValidateType.fieldInfos[i].label;
                if (name != featDataSet.GetField(i).Name)
                {
                    unFoundFields.Add(this.ValidateType.fieldInfos[i]);
                }
            }

            if (unFoundFields.Count != 0)
            {
                String err = layer.Caption+":字段顺序检查失败\r\n";
                foreach (FieldInfo field in unFoundFields)
                {
                    err += "字段:" + field.label + "出错\r\n";
                }
//                LogHelper.WriteLog(typeof(FieldOrderValidate), err);
                LogHelper.Error(err);
                return false;
            }
            return true;


        }
    }

    //字段非空检查
    public class FieldEmptyValidate : FieldValidate
    {
        private List<FieldInfo> noEmptyFieldInfos = new List<FieldInfo>();

        public FieldEmptyValidate(ValidateType validateType)
            : base(validateType)
        {

        }

        public override bool Match()
        {
            foreach (FieldInfo fieldInfo in this.ValidateType.fieldInfos)
            {
                if (fieldInfo.isNull == false)
                {
                    this.noEmptyFieldInfos.Add(fieldInfo);
                }
            }
            return this.noEmptyFieldInfos.Count != 0 ? true : false;
        }

        public override bool Execute(GSOLayer layer)
        {
            GSOFeatures features = layer.GetAllFeatures();
            bool isSuccess = true;
            String err = layer.Caption;
            for (int i = 0; i < features.Length; i++)
            {
                GSOFeature feature = features[i];
                err += ":要素" + feature.ID + "\r\n";
                foreach (FieldInfo fieldInfo in noEmptyFieldInfos)
                {
                    if (feature.GetValue(fieldInfo.label) == null)
                    {
                        err += "字段:" + fieldInfo.label + "为空\r\n";
                        isSuccess = false;
                    }
                    else
                    {
                        //如果是double类型,精度检查
                        if (fieldInfo.type == "double" && fieldInfo.precise != null)
                        {
                            Double val = feature.GetFieldAsDouble(fieldInfo.label);
                            String strVal = val.ToString();
                            int index = strVal.IndexOf('.');
                            if (index != -1)
                            {
                                int length = strVal.Substring(index+1, strVal.Length - (index+1)).Length;
                                if (length > Int16.Parse(fieldInfo.precise))
                                {
                                    err += "字段:" + fieldInfo.label + "精度错误 当前精度:" +
                                           length + "requried:" + fieldInfo.precise + "\r\n";
                                    isSuccess = false;
                                }
                            }
                        }
                    }
                }
            }
            if (isSuccess == false)
            {
//                LogHelper.WriteLog(typeof(FieldEmptyValidate),
//                    "字段为空检查失败:\r\n" + err);
                LogHelper.Error("字段为空检查失败:\r\n" + err);
            }
            return isSuccess;
        }
    }

    public class FieldValidateFactory
    {
        public static FieldValidate Create(ValidateInfo validateInfo,
            EnumValidateType enumValidateType)
        {
            ValidateType validateType = null;

            switch (enumValidateType)
            {
                case EnumValidateType.PipeLinePress:
                    validateType = validateInfo.validateTypes[0];
                    break;
                case EnumValidateType.PipeLineNoPress:
                    validateType = validateInfo.validateTypes[1];
                    break;
                case EnumValidateType.FangGou:
                    validateType = validateInfo.validateTypes[2];
                    break;
                case EnumValidateType.Attach:
                    validateType = validateInfo.validateTypes[3];
                    break;
                case EnumValidateType.Charac:
                    validateType = validateInfo.validateTypes[4];
                    break;
                case EnumValidateType.Marker:
                    validateType = validateInfo.validateTypes[5];
                    break;
                default:
                    validateType = validateInfo.validateTypes[6];
                    break;
            }
            FieldValidate fieldCountVal = new FieldCountValidate(validateType);
            FieldValidate fieldUniqueVal = new FieldUniqueValidate(validateType);
            FieldValidate fieldOrderVal = new FieldOrderValidate(validateType);
            FieldValidate fieldEmptyVal = new FieldEmptyValidate(validateType);
            fieldCountVal.NextValidater = fieldUniqueVal;
            fieldUniqueVal.NextValidater = fieldOrderVal;
            fieldOrderVal.NextValidater = fieldEmptyVal;
            return fieldCountVal;
        }
    }
}