Esempio n. 1
0
        public void ParseItemSelect_Simple()
        {
            var cols     = SelectNode.FromString("config_id,name,is_relationship");
            var expected = new string[] { "config_id", "name", "is_relationship" };

            CollectionAssert.AreEqual(expected, cols.Select(c => c.Name).ToArray());
        }
Esempio n. 2
0
        public void ParseItemSelect_Complex()
        {
            var cols     = SelectNode.FromString("first, second (thing, another2(id, config_id)), no_paren, third (stuff), another (id)");
            var expected = new string[] { "first", "second", "no_paren", "third", "another" };

            CollectionAssert.AreEqual(expected, cols.Select(c => c.Name).ToArray());
        }
Esempio n. 3
0
        public void SelectNode_UnionWith()
        {
            var node = SelectNode.FromString("first,second(thing,another2(id,config_id))");
            var add  = SelectNode.FromString("second(thing,another2(id,config_id)),no_paren,third(stuff),another(id)");

            node.UnionWith(add);
            Assert.AreEqual("first,second(thing,another2(id,config_id)),no_paren,third(stuff),another(id)", node.ToString());
        }
Esempio n. 4
0
        public void ParseItemSelect_ComplexFunction()
        {
            const string str  = "major_rev,owned_by_id[is_not_null()],managed_by_id[is_not_null()],team_id,id";
            var          cols = SelectNode.FromString(str);

            Assert.AreEqual(str, cols.ToString());
            var expected = new string[] { "major_rev", "owned_by_id", "managed_by_id", "team_id", "id" };

            CollectionAssert.AreEqual(expected, cols.Select(c => c.Name).ToArray());
        }
Esempio n. 5
0
        public void ParseItemSelect_ConfigPath()
        {
            const string str  = "id|related_id(id)|created_by_id,config_id";
            var          cols = SelectNode.FromString("id|related_id(id)|created_by_id,config_id");

            Assert.AreEqual(str, cols.ToString());
            var expected = new string[] { "id", "related_id", "created_by_id", "config_id" };
            var names    = cols.SelectMany(c => c).Select(c => c.Name).ToArray();

            CollectionAssert.AreEqual(expected, names);
        }
Esempio n. 6
0
        public void ParseItemSelect_ExtendedClassification()
        {
            const string str  = "xp-*[is_not_null()],id";
            var          cols = SelectNode.FromString(str);

            Assert.AreEqual(str, cols.ToString());
            var expected = new string[] { "xp-*", "id" };

            CollectionAssert.AreEqual(expected, cols.Select(c => c.Name).ToArray());

            cols     = SelectNode.FromString("xp-*(@explicit,@defined_as,@permission_id,$value)");
            expected = new string[] { "@explicit", "@defined_as", "@permission_id", "$value" };
            var subCols = cols.First().Select(c => c.Name).ToArray();

            CollectionAssert.AreEqual(expected, subCols);
        }
Esempio n. 7
0
        //public static int ColumnWidth(this DataColumn column)
        //{
        //  if (column.ExtendedProperties.ContainsKey("column_width"))
        //    return (int)column.ExtendedProperties["column_width"];
        //  return 100;
        //}
        //private static void ColumnWidth(this DataColumn column, int value)
        //{
        //  column.ExtendedProperties["column_width"] = value;
        //}
        //public static int SortOrder(this DataColumn column)
        //{
        //  if (column.ExtendedProperties.ContainsKey("sort_order"))
        //    return (int)column.ExtendedProperties["sort_order"];
        //  return 999999;
        //}
        //public static void SortOrder(this DataColumn column, int value)
        //{
        //  column.ExtendedProperties["sort_order"] = value;
        //}

        public static DataSet GetItemTable(IReadOnlyResult res, ArasMetadataProvider metadata, string select)
        {
            var    ds           = new DataSet();
            string mainType     = null;
            string mainId       = null;
            var    selectedCols = SelectNode.FromString(select);

            List <IReadOnlyItem> items;

            try
            {
                items = res.Items().ToList();
                if (items.Count == 1)
                {
                    mainType = items[0].Type().Value;
                    mainId   = items[0].Id();
                    items.AddRange(items[0].Relationships());
                }

                if (items.Count < 1)
                {
                    return(ds);
                }
            }
            catch (ServerException)
            {
                return(ds);
            }

            var    types = new TypeProperties();
            string type;
            bool   idAdded;
            bool   keyedNameAdded;

            foreach (var i in items)
            {
                type = (i.Type().Exists || i.Property("id").Type().Exists)
          ? i.Type().AsString(i.Property("id").Type().AsString(""))
          : string.Empty;

                if (!string.IsNullOrEmpty(type))
                {
                    types.Add(type, AmlTable_TypeName);
                }
                if (i.TypeId().Exists || i.Property("itemtype").Exists)
                {
                    types.Add(type, AmlTable_TypeId);
                }

                idAdded        = false;
                keyedNameAdded = false;
                foreach (var elem in i.Elements().OfType <IReadOnlyProperty>())
                {
                    if (elem.Name != "config_id")
                    {
                        if (elem.Type().Exists)
                        {
                            types.Add(type, elem.Name + "/type");
                        }
                        if (elem.KeyedName().Exists)
                        {
                            types.Add(type, elem.Name + "/keyed_name");
                        }
                    }
                    types.Add(type, elem.Name);
                    idAdded        = idAdded || elem.Name == "id";
                    keyedNameAdded = keyedNameAdded || elem.Name == "keyed_name";
                }
                if (!string.IsNullOrEmpty(i.Id()) && !idAdded)
                {
                    types.Add(type, "id");
                }
                if (i.Property("id").KeyedName().Exists&& !keyedNameAdded)
                {
                    types.Add(type, "keyed_name");
                }
            }


            ItemType   itemType;
            Property   pMeta;
            DataRow    row;
            string     propName;
            string     propAddendum;
            int        split;
            DataColumn newColumn;

            var typesEnum = types.Count > 1 && types.AllTypesTheSame()
        ? Enumerable.Repeat(new KeyValuePair <string, HashSet <string> >(string.Empty, types.First().Value), 1)
        : types;

            foreach (var kvp in typesEnum)
            {
                var result = new DataTable(kvp.Key);
                try
                {
                    result.BeginLoadData();

                    if (string.IsNullOrEmpty(kvp.Key) ||
                        metadata == null ||
                        !metadata.ItemTypeByName(kvp.Key, out itemType))
                    {
                        itemType = null;
                    }
                    else
                    {
                        result.RowChanging += (s, e) =>
                        {
                            if (e.Action == DataRowAction.Add)
                            {
                                var newId = Guid.NewGuid().ToString("N").ToUpperInvariant();
                                e.Row.Table.Columns["id"].DefaultValue = newId;
                            }
                        };
                    }

                    var allProps = new HashSet <string>(kvp.Value);
                    if (itemType != null)
                    {
                        allProps.UnionWith(metadata.GetProperties(itemType).Wait().Select(p => p.Name));
                    }

                    foreach (var prop in allProps)
                    {
                        if (prop != AmlTable_TypeName &&
                            prop != AmlTable_TypeId &&
                            itemType != null)
                        {
                            result.TableName = itemType.TabLabel ?? (itemType.Label ?? itemType.Name);

                            split        = prop.IndexOf('/');
                            propAddendum = string.Empty;
                            propName     = prop;
                            if (split > 0)
                            {
                                propName     = prop.Substring(0, split);
                                propAddendum = prop.Substring(split);
                            }

                            try
                            {
                                pMeta = metadata.GetProperty(itemType, propName).Wait();

                                if (string.IsNullOrEmpty(propAddendum))
                                {
                                    switch (pMeta.Type)
                                    {
                                    case PropertyType.boolean:
                                        newColumn = new DataColumn(prop, typeof(bool));
                                        if (pMeta.IsRequired && pMeta.DefaultValue == null)
                                        {
                                            newColumn.DefaultValue = false;
                                        }
                                        break;

                                    case PropertyType.date:
                                        newColumn = new DataColumn(prop, typeof(DateTime));
                                        break;

                                    case PropertyType.number:
                                        if (string.Equals(pMeta.TypeName, "integer", StringComparison.OrdinalIgnoreCase))
                                        {
                                            newColumn = new DataColumn(prop, typeof(int));
                                        }
                                        else
                                        {
                                            newColumn = new DataColumn(prop, typeof(double));
                                        }
                                        break;

                                    default:
                                        newColumn = new DataColumn(prop, typeof(string));
                                        if (pMeta.StoredLength > 0)
                                        {
                                            newColumn.MaxLength = pMeta.StoredLength;
                                        }
                                        break;
                                    }
                                }
                                else
                                {
                                    newColumn = new DataColumn(prop, typeof(string));
                                }
                                newColumn.Caption = (pMeta.Label ?? pMeta.Name) + propAddendum;
                                if (pMeta.DefaultValue != null)
                                {
                                    newColumn.DefaultValue = pMeta.DefaultValue;
                                }
                                if (kvp.Key != mainType && !string.IsNullOrEmpty(mainId) && pMeta.Name == "source_id")
                                {
                                    newColumn.DefaultValue = mainId;
                                }
                                newColumn.ReadOnly    = pMeta.ReadOnly && pMeta.Name != "related_id" && pMeta.Name != "source_id";
                                newColumn.AllowDBNull = selectedCols.Any() || !pMeta.IsRequired || !string.IsNullOrEmpty(propAddendum);
                                // Ignore constraints on the following columns
                                switch (pMeta.Name)
                                {
                                case "config_id":
                                case "created_by_id":
                                case "created_on":
                                case "permission_id":
                                    newColumn.AllowDBNull = true;
                                    break;
                                }
                                if (selectedCols.Any())
                                {
                                    newColumn.IsUiVisible(selectedCols.Any(c => string.Equals(c.Name, propName)));
                                }
                                else
                                {
                                    newColumn.IsUiVisible(!string.IsNullOrEmpty(mainType) && itemType.Name != mainType
                    ? (pMeta.Visibility & PropertyVisibility.RelationshipGrid) > 0
                    : (pMeta.Visibility & PropertyVisibility.MainGrid) > 0);
                                }
                                newColumn.PropMetadata(pMeta);
                            }
                            catch (KeyNotFoundException)
                            {
                                newColumn = new DataColumn(prop, typeof(string));
                                newColumn.IsUiVisible(string.IsNullOrEmpty(kvp.Key) || metadata == null);
                            }
                        }
                        else
                        {
                            newColumn = new DataColumn(prop, typeof(string));
                            newColumn.IsUiVisible(string.IsNullOrEmpty(kvp.Key) || metadata == null);
                            if (prop == AmlTable_TypeName && !string.IsNullOrEmpty(kvp.Key))
                            {
                                newColumn.DefaultValue = kvp.Key;
                            }
                        }
                        result.Columns.Add(newColumn);
                    }

                    foreach (var item in items
                             .Where(i => string.IsNullOrEmpty(kvp.Key) ||
                                    i.Type().AsString(i.Property("id").Type().Value) == kvp.Key))
                    {
                        row = result.NewRow();
                        row.BeginEdit();
                        foreach (var prop in kvp.Value)
                        {
                            switch (prop)
                            {
                            case "id":
                                row[prop] = item.Id();
                                break;

                            case "keyed_name":
                                row[prop] = item.KeyedName().AsString(item.Property("id").KeyedName().Value);
                                break;

                            case AmlTable_TypeName:
                                row[prop] = item.Type().AsString(item.Property("id").Type().Value);
                                break;

                            case AmlTable_TypeId:
                                row[prop] = item.TypeId().AsString(item.Property("itemtype").Value);
                                break;

                            default:
                                split = prop.IndexOf('/');
                                if (split > 0)
                                {
                                    row[prop] = item.Property(prop.Substring(0, split)).Attribute(prop.Substring(split + 1)).Value;
                                }
                                else if (item.Property(prop).HasValue())
                                {
                                    newColumn = result.Columns[prop];
                                    if (newColumn.DataType == typeof(bool))
                                    {
                                        row[prop] = item.Property(prop).AsBoolean(false);
                                    }
                                    else if (newColumn.DataType == typeof(DateTime))
                                    {
                                        row[prop] = item.Property(prop).AsDateTime(DateTime.MinValue);
                                    }
                                    else if (newColumn.DataType == typeof(int))
                                    {
                                        row[prop] = item.Property(prop).AsInt(int.MinValue);
                                    }
                                    else if (newColumn.DataType == typeof(double))
                                    {
                                        row[prop] = item.Property(prop).AsDouble(double.MinValue);
                                    }
                                    else
                                    {
                                        row[prop] = item.Property(prop).Value;
                                    }
                                }
                                else
                                {
                                    row[prop] = DBNull.Value;
                                }
                                break;
                            }
                        }
                        row.EndEdit();
                        row.AcceptChanges();
                        result.Rows.Add(row);
                    }


                    if (result.Rows.Count > 0 &&
                        result.Columns.OfType <DataColumn>().Where(c => c.ColumnName == "source_id" || c.ColumnName == "related_id").Count() == 2)
                    {
                        var firstRelated = result.Rows[0]["related_id"];
                        var firstSource  = result.Rows[0]["source_id"];
                        if (result.Rows.Count > 1 &&
                            result.AsEnumerable().All(r => r["related_id"] == firstRelated) &&
                            !result.AsEnumerable().All(r => r["source_id"] == firstSource))
                        {
                            result.Columns["source_id/keyed_name"].IsUiVisible(true);
                        }
                        else
                        {
                            result.Columns["related_id/keyed_name"].IsUiVisible(true);
                        }
                    }
                }
                finally
                {
                    try
                    {
                        result.EndLoadData();
                    }
                    catch (ConstraintException) { }
                    result.AcceptChanges();
                }

                ds.Tables.Add(result);
            }

            return(ds);
        }