public AttributeDefinitionView Create(AttributeDefinition AttributeDefinition)
        {
            var cacheKey = new KeyValuePair<long, string>(AttributeDefinition.Id, AttributeDefinition.AttributeDataType);
            AttributeDefinitionView cache = null; ;
            if (container.ContainsKey(cacheKey)) cache = container[cacheKey];
            else
            {
                if (AttributeDefinition.AttributeDataType == "String")
                {
                    StringRuleAttributeDefinitionView srcdv = new StringRuleAttributeDefinitionView(AttributeDefinition);
                    StringRuleFactory factory = new StringRuleFactory();
                    AttributeDefinition.MapRules.ForEach(k => {
                        IStringRule rule = factory.CreateRule(k.RuleType, k.RuleContent);
                        if(rule!=null) srcdv.RuleCollection.Add(rule);
                    });
                    cache = container[cacheKey] = srcdv;

                }
                else if (AttributeDefinition.AttributeDataType == "Numeric")
                {
                    NumericRuleAttributeDefinitionView srcdv = new NumericRuleAttributeDefinitionView(AttributeDefinition);
                    NumericRuleFactory factory = new NumericRuleFactory();
                    AttributeDefinition.MapRules.ForEach(k =>
                    {
                        INumericRule rule = factory.CreateRule(k.RuleType, k.RuleContent);
                        if (rule != null) srcdv.RuleCollection.Add(rule);
                    });
                    cache = container[cacheKey] = srcdv;
                }
            }
            return cache;
        }
        public double? ApplyStringRule(AttributeDefinition AttributeDefinition, string sValue, StringRuleFactory stringRuleFactory)
        {
            IStringRule rule = null;
            foreach (var mapRule in AttributeDefinition.MapRules)
            {
                rule = stringRuleFactory.CreateRule(mapRule.RuleType, mapRule.RuleContent);
                var result = rule.Apply(sValue);
                if (result.HasValue)
                {
                    return result.Value;
                }
            }

            return null;
        }
        public double ApplyNumericRule(AttributeDefinition AttributeDefinition, string sValue, NumericRuleFactory numericRuleFactory)
        {
            double fValue = double.Parse(sValue);
            INumericRule rule = null;
            foreach (var mapRule in AttributeDefinition.MapRules)
            {
                rule = numericRuleFactory.CreateRule(mapRule.RuleType, mapRule.RuleContent);
                var result = rule.Apply(fValue);
                if (result.HasValue)
                {
                    fValue = result.Value;
                }
            }

            return fValue;
        }
 public static long Create(AttributeDefinition model)
 {
     long id = 0;
     using (var con = EstablishConnection())
     {
         con.Open();
         id = con.Query<long>(@"
             INSERT INTO AttributeDefinitions(name, rawName, dataFileId)
             VALUES(@Name,@RawName, @DataFileId);
             SELECT last_insert_rowid();
         ", model).First();
         model.Id = id;
         con.Close();
     }
     return id;
 }
        public bool CheckStringRuleInValidationStatus(DataFile dataFile, AttributeDefinition AttributeDefinition, List<IStringRule> rules)
        {
            IEnumerable<string> charValues = DataRepository.GetDistinctValues(AttributeDefinition, dataFile.RawTableName);
            if (rules.Count == 0) return true;
            foreach (var charValue in charValues)
            {
                var isApplied = false;
                foreach (var rule in rules)
                {
                    var result = rule.Apply(charValue);
                    if (result.HasValue)
                    {
                        isApplied = true;
                        break;
                    }
                }
                if (!isApplied) return false;
            }

            return true;
        }
        public bool CheckNumericRuleInValidationStatus(DataFile dataFile, AttributeDefinition AttributeDefinition, List<INumericRule> rules)
        {
            IEnumerable<string> charValues = DataRepository.GetDistinctValues(AttributeDefinition, dataFile.RawTableName);
            if (rules.Count == 0) return true;
            foreach (var charValue in charValues)
            {
                var isApplied = false;
                try
                {
                    foreach (var rule in rules)
                    {
                        var result = rule.Apply(double.Parse(charValue));
                    }
                    isApplied = true;
                }catch(Exception e){
                    isApplied = false;
                }
                if (!isApplied) return false;
            }

            return true;
        }
        public IEnumerable<AppliedRuleValue> GetValuesWithNoRuleAppliedInStringRule(DataFile dataFile, AttributeDefinition AttributeDefinition, List<IStringRule> rules)
        {
            IEnumerable<string> charValues = DataRepository.GetDistinctValues(AttributeDefinition, dataFile.RawTableName);
            List<AppliedRuleValue> appliedRuleValues = new List<AppliedRuleValue>();
            Dictionary<string, int> dict = new Dictionary<string, int>();
            foreach (var charValue in charValues)
            {
                if (rules.Count == 0)
                {
                    appliedRuleValues.Add(new AppliedRuleValue { IsApplied = false, Value = charValue });
                }
                else
                {
                    var appliedRuleValue = new AppliedRuleValue { Value = charValue, IsApplied = false };
                    foreach (var rule in rules)
                    {

                        var result = rule.Apply(charValue);
                        if (result.HasValue)
                        {
                            appliedRuleValue.IsApplied = true;
                            break;
                        }

                    }
                    appliedRuleValues.Add(appliedRuleValue);
                }
            }
            List<int> removedIndices = new List<int>();
            // remove duplicate applied rule values
            for (int i = appliedRuleValues.Count - 1; i >= 0 ; i--)
            {
                var appliedRuleValue = appliedRuleValues[i];
                if (dict.ContainsKey(appliedRuleValue.Value))
                {
                    removedIndices.Add(i);
                }
                else
                {
                    dict[appliedRuleValue.Value] = i;
                }
            }

            foreach (var removedIndex in removedIndices)
            {
                appliedRuleValues.RemoveAt(removedIndex);
            }
            return appliedRuleValues;
        }
 public IEnumerable<AppliedRuleValue> GetValuesWithNoRuleAppliedInNumericRule(DataFile dataFile, AttributeDefinition AttributeDefinition, List<INumericRule> rules)
 {
     IEnumerable<string> charValues = DataRepository.GetDistinctValues(AttributeDefinition, dataFile.RawTableName);
     List<AppliedRuleValue> appliedRuleValues = new List<AppliedRuleValue>();
     bool isError = false;
     foreach (var charValue in charValues)
     {
         if (rules.Count == 0 || isError)
         {
             appliedRuleValues.Add(new AppliedRuleValue {IsApplied = false, Value = charValue });
         }
         else
         {
             try
             {
                 foreach (var rule in rules)
                 {
                     var result = rule.Apply(float.Parse(charValue));
                 }
                 appliedRuleValues.Add(new AppliedRuleValue
                 {
                     IsApplied = true,
                     Value = charValue
                 });
             }
             catch (FormatException e)
             {
                 appliedRuleValues.Add(new AppliedRuleValue
                 {
                     IsApplied = false,
                     Value = charValue
                 });
                 isError = true;
             }
         }
     }
     return appliedRuleValues;
 }
 public NumericRuleAttributeDefinitionView(AttributeDefinition AttributeDefinition)
     : base(AttributeDefinition)
 {
     RuleCollection = new NumericRuleCollection();
 }
 public AttributeDefinitionView(AttributeDefinition AttributeDefinition)
 {
     this.AttributeDefinition = AttributeDefinition;
 }
 public StringRuleAttributeDefinitionView(AttributeDefinition AttributeDefinition)
     : base(AttributeDefinition)
 {
     RuleCollection = new StringRuleCollection();
 }
        public static void UploadData(string fileName, string absoluteFilePath, string relativeFilePath)
        {
            // create data file
            var dataFile = new DataFile {
                RawTableName = string.Format("RawTable{0}",DateTime.Now.ToTimeStamps()),
                Name = fileName,
                CreatedDate = DateTime.Now.ToNormalDateTimeString(),
                RelativePath = relativeFilePath
            };
            DataFileRepository.Create(dataFile);

            using (var con = EstablishConnection())
            {
                con.Open();
                var transaction = con.BeginTransaction();
                var csvReader = new CsvReader(new StreamReader(absoluteFilePath), new CsvHelper.Configuration.CsvConfiguration
                {
                    //Delimiter = "\t",
                    TrimFields = true,
                    TrimHeaders = true
                });
                csvReader.Read();
                // create column definitions
                var fieldHeaders = csvReader.FieldHeaders;
                var AttributeDefinitions = new List<AttributeDefinition>();
                for (int i = 0; i < fieldHeaders.Length; i++)
                {
                    var fieldHeader = fieldHeaders[i];
                    var AttributeDefinition = new AttributeDefinition
                    {
                        RawName = fieldHeader,
                        Name = string.Format("Column_{0}", i + 1),
                        AttributeIndex = i,
                        DataFileId = dataFile.Id
                    };
                    AttributeDefinitions.Add(AttributeDefinition);
                }
                AttributeDefinitionRepository.BulkInsert(con, AttributeDefinitions);

                // create raw table
                DataRepository.CreateRawTable(con, dataFile.RawTableName, AttributeDefinitions);

                // fill data into raw table
                var records = new List<string[]>();
                while (csvReader.Read()) records.Add(csvReader.CurrentRecord);
                DataRepository.BulkInsertRawTable(con, dataFile.RawTableName, AttributeDefinitions, records);

                transaction.Commit();
                con.Close();
            }
        }
 public static IEnumerable<string> GetDistinctValues(AttributeDefinition AttributeDefinition, string rawTableName)
 {
     IEnumerable<string> models;
     using (var con = EstablishConnection())
     {
         con.Open();
         models = GetDistinctValues(con, AttributeDefinition, rawTableName);
         con.Close();
     }
     return models;
 }
 public static IEnumerable<string> GetDistinctValues(DbConnection con, AttributeDefinition AttributeDefinition, string rawTableName)
 {
     var sql = string.Format("SELECT DISTINCT {0} FROM {1}", AttributeDefinition.Name, rawTableName);
     return con.Query<string>(sql);
 }