Ejemplo n.º 1
0
 public static IList <ICustomer> GetAllPaged(DBCommand cmd, IDictionary <IAttributeDefinition, object> criterias, ref int pageNum, int pageSize, ref int pageCount, ref int itemCount)
 {
     return(cmd.GetAllPaged <ICustomer>("Customer", criterias, ref pageCount, ref pageNum, ref itemCount, pageSize));
 }
Ejemplo n.º 2
0
        public static IList <IApplication> GetAllPaged(DBCommand cmd, IDictionary <IAttributeDefinition, object> criterias, ref int pageNum, int pageSize, ref int pageCount, ref int itemCount)
        {
            if (!AppCore.AppSingleton.ObjectDefinitions.ContainsKey("Application"))
            {
                Debug.Assert(false, "Unimplemented entity [Application]");
            }
            ApplicationObjDef appOd = AppCore.AppSingleton.FindObjDef <ApplicationObjDef>();

            if (!AppCore.AppSingleton.ObjectDefinitions.ContainsKey("Status"))
            {
                Debug.Assert(false, "Unimplemented entity [Status]");
            }
            StatusObjDef statusOd = AppCore.AppSingleton.FindObjDef <StatusObjDef>();

            if (!AppCore.AppSingleton.ObjectDefinitions.ContainsKey("User"))
            {
                Debug.Assert(false, "Unimplemented entity [User]");
            }
            UserObjDef userOd = AppCore.AppSingleton.FindObjDef <UserObjDef>();

            if (!criterias.ContainsKey(userOd.Role))
            {
                Debug.Assert(false, "Criterias should contains UserRole of entity [User]");
            }
            UserRole role = (UserRole)Convert.ChangeType(criterias[userOd.Role], typeof(UserRole));

            if (role == UserRole.None)
            {
                role = UserRole.Client;
            }
            criterias.Remove(userOd.Role);

            var sqlBuilder = new StringBuilder();

            string table     = string.Format("[{0}]", appOd.TableName);
            string tabStatus = string.Format("[{0}]", statusOd.TableName);
            string fileds    = string.Join(",", appOd.Attributes.Values.Select(a => string.Format("[{0}]", a.Column)).ToArray());
            string criterion = string.Join(" and ",
                                           criterias.Keys.Where(a => a != userOd.Role).Select(a => string.Format("[{0}]=@{0}", a.Column)).ToArray());

            string fieldsWithAlias    = "comp." + fileds.Replace(",", ",comp.");
            string criterionWithAlias = string.IsNullOrEmpty(criterion) ? "" : "comp." + criterion.Replace(" and ", " and comp.");

            string statusCriterion = "";

            switch (role)
            {
            case UserRole.Client:
                statusCriterion = string.Format("s.[{0}] in ( {1} ) or s.[{0}] is Null or s.[{0}]=''",
                                                statusOd.Value.Column, string.Join(",", role.GetVisibleStatus().Select(r => "'" + r.ToLabel() + "'").ToArray()));
                break;

            case UserRole.Business:
            case UserRole.Administrator:
            default:
                statusCriterion = string.Format("s.[{0}] in ( {1} )", statusOd.Value.Column, string.Join(",", role.GetVisibleStatus().Select(r => "'" + r.ToLabel() + "'").ToArray()));
                break;
            }

            if (!string.IsNullOrEmpty(criterionWithAlias))
            {
                statusCriterion = string.Format("({0}) and ({1})", statusCriterion, criterionWithAlias);
            }


            sqlBuilder.AppendLine(string.Format("SELECT {1}, (select count(org.[{2}])+1 from {0} [org] where org.[{2}] <comp.[{2}]) as Rank",
                                                table, fieldsWithAlias, appOd.PrimaryKey.Column));
            sqlBuilder.AppendLine(string.Format("FROM {0} [comp] left join {1} [s] on s.[{2}]=comp.[{3}]", table, tabStatus, statusOd.PrimaryKey.Column, appOd.PrimaryKey.Column));
            if (!string.IsNullOrEmpty(statusCriterion))
            {
                sqlBuilder.AppendLine(string.Format("Where {0}", statusCriterion));
            }
            sqlBuilder.AppendLine(string.Format("ORDER BY comp.[{0}]", appOd.PrimaryKey.Column));

            string sql = sqlBuilder.ToString();

            return(cmd.GetAllPaged <IApplication>("Application", sql, criterias, ref pageCount, ref pageNum, ref itemCount, pageSize));
        }