Ejemplo n.º 1
0
        /// <summary>
        /// Generates the next auto number based on the date, and the specified
        /// entity.
        /// </summary>
        /// <param name="date"></param>
        /// <param name="entity"></param>
        public string GenerateNextAutoNumber <T>(DateTime date, T entity) where T : BaseEntity
        {
            //The separators must be / or -
            char[] separators = new char[] { '/', '-' };
            // Gets the entity type of the entity.
            string     entityType = typeof(T).Name;
            AutoNumber autoNumber = _autoNumberRepository.GetAll().Where(r => r.EntityType == entityType).FirstOrDefault();
            int        nextNumber = GenerateNextNumber(autoNumber);

            //default the format string {0:000000}
            if (autoNumber == null)
            {
                return(string.Format("{0:000000}", nextNumber));
            }

            var formatString = autoNumber.FormatString;
            //The current separator is using in this autoNumber
            var separator = formatString.Contains(separators[0]) ? separators[0] : separators[1];

            string[]      formatStringSections = formatString.Split(separators);
            StringBuilder result = new StringBuilder();

            foreach (var s in formatStringSections)
            {
                //format entity
                if (s.Contains("e:"))
                {
                    string entityPropertyValue = FleeExpression.Evaluate(entity, "entity." + s.Replace(" ", "").Replace("e:", "")).ToString();
                    result.Append(entityPropertyValue);
                }
                //format date
                else if (s.Contains("d:"))
                {
                    string dateFormat = GetFormat(s, "d:");
                    result.Append(string.Format(dateFormat, date));
                }
                //format number
                else if (s.Contains("n:"))
                {
                    //update the last number for this entity
                    autoNumber.LastNumber = nextNumber;
                    _autoNumberRepository.UpdateAndCommit(autoNumber);

                    string numberFormat = GetFormat(s, "n:");
                    result.Append(string.Format(numberFormat, nextNumber));
                }
                //label
                else
                {
                    result.Append(s);
                }
                result.Append(separator);
            }
            //remove the last character "/" from result
            return(result.Remove(result.Length - 1, 1).ToString());
        }
        private long ValidateACExpression(AuditEntity auditEntity, string expression)
        {
            long result = 0;
            // get key
            long       id      = (long)auditEntity.Keys.SingleOrDefault(k => k.Name == "Id").Value;
            BaseEntity entity  = this.GetByEntityIdAndType(id, auditEntity.EntityType.Name) as BaseEntity;
            bool       isMatch = FleeExpression.Evaluate <bool>(entity, expression);

            if (isMatch == true)
            {
                result = id;
            }
            return(result);
        }