Пример #1
0
        /// <summary>
        /// Returns allowed activity results.
        /// </summary>
        /// <param name="userConnection">User connection.</param>
        /// <param name="activityId">ID of the activity.</param>
        /// <returns>Allowed values of the activity result.</returns>
        public static string GetAllowedActivityResults(UserConnection userConnection, Guid activityId)
        {
            var select = (Select) new Select(userConnection)
                         .Column("AllowedResult")
                         .From("Activity")
                         .Where("Id").IsEqual(Column.Parameter(activityId));
            var allowedResult = select.ExecuteScalar <string>();

            if (string.IsNullOrEmpty(allowedResult))
            {
                return("[]");
            }
            EntitySchemaManager entitySchemaManager = userConnection.EntitySchemaManager;
            EntitySchema        entitySchema        = entitySchemaManager.GetInstanceByName("ActivityResult");
            var esq = new EntitySchemaQuery(entitySchemaManager, entitySchema.Name)
            {
                UseAdminRights = false
            };

            esq.PrimaryQueryColumn.IsAlwaysSelect = true;
            string columnName = entitySchema.GetPrimaryDisplayColumnName();

            esq.AddColumn(columnName);
            esq.AddColumn("Category");
            EntitySchemaQueryFilterCollection filters = esq.Filters;
            var allowedResultIds = ServiceStackTextHelper.Deserialize <string[]>(allowedResult);
            var columnParameters = new object[allowedResultIds.Length];

            for (int i = 0; i < allowedResultIds.Length; i++)
            {
                var resultId = new Guid(allowedResultIds[i]);
                columnParameters[i] = resultId;
            }
            filters.Add(esq.CreateFilterWithParameters(FilterComparisonType.Equal, "Id", columnParameters));
            EntityCollection entityCollection = esq.GetEntityCollection(userConnection);
            bool             isFirstItem      = true;
            var sb = new StringBuilder("[");

            foreach (Entity entity in entityCollection)
            {
                if (!isFirstItem)
                {
                    sb.Append(",");
                }
                else
                {
                    isFirstItem = false;
                }
                sb.AppendFormat("{{\"resultId\":\"{0}\",\"caption\":\"{1}\",\"categoryId\":\"{2}\"}}",
                                entity.GetTypedColumnValue <Guid>("Id"), entity.GetTypedColumnValue <string>(columnName),
                                entity.GetTypedColumnValue <Guid>("CategoryId"));
            }
            sb.Append("]");
            return(sb.ToString());
        }
        private EntitySchemaQuery GetBindedRecordsESQ(EntitySchema entitySchema, List <Guid> recordIds)
        {
            var bindedRecordsESQ = new EntitySchemaQuery(entitySchema);

            bindedRecordsESQ.PrimaryQueryColumn.IsAlwaysSelect = true;
            if (entitySchema.PrimaryDisplayColumn != null)
            {
                bindedRecordsESQ.AddColumn(entitySchema.GetPrimaryDisplayColumnName());
            }
            bindedRecordsESQ.Filters.Add(bindedRecordsESQ.CreateFilterWithParameters(
                                             FilterComparisonType.Equal,
                                             "Id",
                                             recordIds.Cast <object>().ToArray()));
            return(bindedRecordsESQ);
        }
        private IDictionary <int, string> GetHierarchyColumns(EntitySchema entitySchema,
                                                              IEnumerable <HierarchySettingItem> hierarchy)
        {
            var columns = new Dictionary <int, string>();

            if (!hierarchy.IsNullOrEmpty())
            {
                foreach (HierarchySettingItem settingItem in hierarchy.OrderBy(a => a.Level))
                {
                    columns[columns.Keys.Count] = settingItem.ColumnPath;
                }
            }
            var primaryDisplayColumnName = entitySchema.GetPrimaryDisplayColumnName();

            columns[columns.Keys.Count] = primaryDisplayColumnName;
            return(columns);
        }