示例#1
0
        public static DetailsReadModel CreateDetailsReadModel(IEditableRoot model, int childIndex = 0)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            try
            {
                var result = new DetailsReadModel(model.Id, model.ProcessName, model.ProcessDisplayName)
                {
                    ChildIndex = childIndex
                };

                var props = model.GetAllPropertiesForInstance();
                var visibleFields = new HashSet<string>(model.Sections.SelectMany(s => s.Fields).Where(f => !f.IsHidden).Select(f => f.SystemName));

                var sections = model.Sections.OrderBy(s => s.Position).Select(s => new SectionReadModel {Name = s.Name}).ToList();

                foreach (var prop in props.Keys)
                {
                    if (!visibleFields.Contains(prop.Name))
                    {
                        continue;
                    }
                    
                    var field = new FieldReadModel(props[prop], prop);
                    
                    var section = sections.FirstOrDefault(s => s.Name == field.SectionName);
                    if (section == null)
                    {
                        section = new SectionReadModel {Name = field.SectionName};
                        sections.Add(section);
                    }
                    else
                    {
                        var sect = model.Sections.First(s => s.Name == field.SectionName);
                        field.Position = sect.Fields.First(f => f.SystemName == field.SystemName).Position;
                    }

                    section.AddField(field);
                }

                foreach (var section in sections)
                {
                    section.SortFields();
                }

                result.Sections = sections;

                var supportStates = model as ISupportStates;
                if (supportStates != null)
                {
                    result.CurrentState = Guid.Empty == supportStates.CurrentStateGuid ? null : supportStates.CurrentStateGuid.ToString();

                    var nextStates = new List<IState>();
                    if (supportStates.CurrentState.HasValue)
                    {
                        var currentState = supportStates.StateManager.States.FirstOrDefault(s => s.Guid == supportStates.CurrentStateGuid);
                        if (currentState != null)
                        {
                            nextStates.Add(currentState);
                        }
                    }

                    nextStates.AddRange(supportStates.GetNextAvailableStates());
                    result.NextStates = nextStates.Select(s => new StateReadModel {Id = s.Id, Name = s.Name, Guid = s.Guid});
                }

                result.Title = GetTitle(model);
                result.BrokenRules = GetBrokenRules(model);

                //var editableRootType = TheDynamicTypeManager.Value.GetEditableRootType(processName);

                //return BusinessRules.HasPermission(AuthorizationActions.CreateObject, editableRootType);
                result.CanAdd = BusinessRules.HasPermission(AuthorizationActions.CreateObject, model);
                return result;
                
            }
            catch (Exception)
            {
                //Logger.Log(LogSeverity.Error, "CreateDetailsReadModel", ex);
                throw;
            }
        }
示例#2
0
        private List<FieldReadModel> GetFields(IEditableRoot model)
        {
            var props = model.GetAllPropertiesForInstance();
            var fields = new List<FieldReadModel>();

            var displayFields = AnswerDisplayFields.ToDictionary(x => x.FieldName);

            try
            {
                foreach (var prop in props.Keys)
                {
                    if (!displayFields.ContainsKey(prop.Name))
                    {
                        continue;
                    }

                    var displayField = displayFields[prop.Name];

                    var field = new FieldReadModel(props[prop], prop);

                    field.Position = displayField.Position;

                    fields.Add(field);
                }
            }
            catch (Exception ex)
            {
                Logger.Log(LogSeverity.Error, GetType().ToString(), ex);
            }

            fields = fields.Where(f => !string.IsNullOrEmpty(f.SectionName)).ToList();

            return fields;
        }