예제 #1
0
        public async Task <IEnumerable <InstallItem> > SortByDependencies(IEnumerable <InstallItem> items, IAsyncConnection conn)
        {
            var metadata = ArasMetadataProvider.Cached(conn);
            await metadata.ReloadTask().ConfigureAwait(false);

            return(SortByDependencies(items, metadata));
        }
예제 #2
0
 /// <summary>
 /// Construct a new <see cref="ExportProcessor"/> from a connection to Aras
 /// </summary>
 /// <param name="conn"></param>
 public ExportProcessor(IAsyncConnection conn)
 {
   _conn = conn;
   _metadata = ArasMetadataProvider.Cached(conn);
   _metadata.Reset();
   _dependAnalyzer = new DependencyAnalyzer(_metadata);
 }
        /// <summary>
        /// Return a cached metadata object for a given connection
        /// </summary>
        public static ArasMetadataProvider Cached(IAsyncConnection conn)
        {
            ArasMetadataProvider result;
            var key    = conn.Database + "|" + conn.UserId;
            var remote = conn as IRemoteConnection;

            if (remote != null)
            {
                key += "|" + remote.Url;
            }
            if (!_cache.TryGetValue(key, out result) || string.IsNullOrEmpty(result._conn.UserId))
            {
                result      = new ArasMetadataProvider(conn);
                _cache[key] = result;
            }
            return(result);
        }
예제 #4
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);
        }
예제 #5
0
 public DependencyAnalyzer(ArasMetadataProvider metadata)
 {
   _metadata = metadata;
 }
 /// <summary>
 /// Return a cached metadata object for a given connection
 /// </summary>
 public static ArasMetadataProvider Cached(IAsyncConnection conn)
 {
   ArasMetadataProvider result;
   if (!_cache.TryGetValue(conn, out result))
   {
     result = new ArasMetadataProvider(conn);
     _cache[conn] = result;
   }
   return result;
 }