Пример #1
0
        public Json.View GetJsonView(View view, DataAction dataAction, string pk, DataRow dataRow, string guid, BeforeSelectEventHandler beforeSelectCallback, AfterSelectEventHandler afterSelectCallback)
        {
            //DataRow dataRow = view.GetDataRow(pk, null, beforeSelectCallback, afterSelectCallback);

            if (dataRow == null)
            {
                return(null);
            }

            Json.View jsonView = new Json.View();

            foreach (Field field in view.GetVisibleFieldsForRow(dataAction))
            {
                if (field.FieldType == FieldType.Column)
                {
                    string type = field.GetHtmlControlType().ToString();

                    string format = string.Empty;
                    if (field.FieldType == FieldType.Column)
                    {
                        ColumnFieldType columnFieldType = field.GetColumnFieldType();

                        format = field.Validation.Format;
                    }

                    string validationType = "Children";
                    if (field.FieldType != FieldType.Children)
                    {
                        validationType = field.GetValidationType();
                    }
                    ColumnField columnField = (ColumnField)field;
                    //string value= columnField.ConvertToString(dataRow);
                    //if (columnField.DataColumn.DataType == typeof(bool) && string.IsNullOrEmpty(value))
                    //    value = false.ToString();
                    object value = dataRow[columnField.DataColumn.ColumnName];


                    if (view is Durados.Config.IConfigView && field.Name == "Formula")
                    {
                        string parentViewname = dataRow.GetParentRow("Fields")["Name"].ToString();

                        Map map = Maps.Instance.GetMap();

                        View currentView = (View)map.Database.Views[parentViewname];

                        value = DataAccessHelper.ReplaceFieldDisplayNames(value.ToString(), false, currentView);
                    }

                    if (value is DBNull || value.Equals("null"))
                    {
                        value = string.Empty;
                    }
                    else if (columnField.DataColumn.DataType == typeof(DateTime) || columnField.DataColumn.DataType == typeof(byte[]))
                    {
                        value = columnField.ConvertToString(dataRow);
                    }

                    bool disbled;
                    if (dataAction == DataAction.Create)
                    {
                        disbled = field.IsDisableForCreate() || field.DisableConfigClonedField(dataRow);
                    }
                    else
                    {
                        disbled = field.IsDisableForEdit(guid) || field.DisableConfigClonedField(dataRow);
                    }

                    if (dataAction == DataAction.Create)
                    {
                        if (!field.IncludeInDuplicate)
                        {
                            if (field.DefaultValue != null)
                            {
                                value = field.DefaultValue.ToString();
                            }
                            else
                            {
                                value = string.Empty;
                            }
                        }
                    }

                    if (columnField.Encrypted && columnField.SpecialColumn == SpecialColumn.Password)
                    {
                        value = columnField.View.Database.EncryptedPlaceHolder;
                    }

                    jsonView.Fields.Add(field.Name, new Json.Field()
                    {
                        Name = field.Name, Value = value, Type = field.GetHtmlControlType().ToString(), ValidationType = validationType, Format = format, Required = field.Required, Searchable = field.IsSearchable(), Disabled = disbled, DisDup = field.DisableInDuplicate, Refresh = field.Refresh, Min = field.Validation.GetMinForJson(), Max = field.Validation.GetMaxForJson(), DependencyData = field.DependencyData
                    });
                }
                else if (field.FieldType == FieldType.Parent)
                {
                    //, DependencyChildren = parentField.GetDependencyChildrenNames()
                    ParentField parentField = (ParentField)field;

                    string  fk        = string.Empty;
                    DataRow parentRow = null;
                    try
                    {
                        parentRow = dataRow.GetParentRow(parentField.DataRelation.RelationName);
                        fk        = parentField.ParentView.GetPkValue(parentRow);
                    }
                    catch (Exception)
                    {
                        DataColumn[] columns = parentField.GetDataColumns();
                        if (columns.Length == 1)
                        {
                            fk = dataRow[columns[0].ColumnName].ToString();
                        }
                        else
                        {
                            foreach (DataColumn column in columns)
                            {
                                fk += dataRow[column.ColumnName].ToString() + ',';
                            }
                            fk.TrimEnd(',');
                        }
                    }

                    DataRow dependencyRow = parentRow;

                    if (parentField.GetHtmlControlType() == HtmlControlType.OutsideDependency)
                    {
                        List <string> dependencyFks = new List <string>();

                        List <Durados.ParentField>   dependencyDynasty = parentField.GetDependencyDynasty();
                        Durados.DataAccess.SqlAccess sqlAccess         = new SqlAccess();

                        foreach (ParentField dependencyField in dependencyDynasty)
                        {
                            DataRow row = dependencyRow.GetParentRow(dependencyField.DataRelation.RelationName);
                            if (row == null)
                            {
                                string key = dependencyField.View.GetFkValue(dependencyRow, dependencyField.DataRelation.RelationName);
                                row = sqlAccess.GetDataRow(dependencyField.ParentView, key, dependencyRow.Table.DataSet);
                            }
                            dependencyRow = row;
                            string dependencyFk = dependencyField.ParentView.GetPkValue(dependencyRow);
                            dependencyFks.Add(dependencyFk);
                        }

                        dependencyFks.Reverse();
                        dependencyDynasty.Reverse();

                        int i = 0;
                        foreach (ParentField dependencyField in dependencyDynasty)
                        {
                            Json.Field jsonDependencyField = GetParentJsonField(dependencyField, dependencyFks[i], dataAction, guid);

                            jsonView.Fields.Add(jsonDependencyField.Name, jsonDependencyField);

                            i++;
                        }
                    }

                    Json.Field jsonField = GetParentJsonField(parentField, fk, dataAction, guid);

                    if (parentField.DisableConfigClonedField(dataRow))
                    {
                        jsonField.Disabled = true;
                    }

                    jsonView.Fields.Add(field.Name, jsonField);
                }

                else if (field.FieldType == FieldType.Children)
                {
                    if (((ChildrenField)field).ChildrenHtmlControlType == ChildrenHtmlControlType.CheckList)
                    {
                        string value = string.Empty;
                        if (dataAction == DataAction.Create)
                        {
                            if (field.IncludeInDuplicate)
                            {
                                value = ((ChildrenField)field).GetSelectedChildrenPKDelimited(pk);
                            }
                            else
                            {
                                if (field.DefaultValue != null)
                                {
                                    value = field.DefaultValue.ToString();
                                }
                            }
                        }
                        else
                        {
                            value = ((ChildrenField)field).GetSelectedChildrenPKDelimited(pk);
                        }

                        Json.Field jsonField = GetChildrenJsonField(((ChildrenField)field), value);

                        jsonView.Fields.Add(field.Name, jsonField);
                    }
                    else
                    {
                        Json.Field jsonField = GetChildrenJsonField(((ChildrenField)field), null);
                        jsonView.Fields.Add(field.Name, jsonField);
                    }
                }
            }

            try
            {
                LoadDerivation(view, jsonView);
            }
            catch { }

            jsonView.InlineAddingCreateUrl = view.GetInlineAddingCreateUrl();
            jsonView.ViewName = view.Name;

            return(jsonView);
        }