/// <summary> /// Retourne si la collection contient l'élément. /// </summary> /// <param name="collection">Collection a traiter.</param> /// <param name="pkValue">Valeur de la clef primaire.</param> /// <returns>True si l'objet est inclus, False sinon.</returns> /// <remarks>Utilisé dans Kinetix.WebControls.</remarks> public static bool Contains(ICollection collection, object pkValue) { if (collection == null) { throw new ArgumentNullException("collection"); } if (pkValue == null) { throw new ArgumentNullException("pkValue"); } BeanDefinition definition = BeanDescriptor.GetCollectionDefinition(collection); if (definition.PrimaryKey == null) { throw new NotSupportedException(); } IEnumerator enumerator = collection.GetEnumerator(); while (enumerator.MoveNext()) { object tmp = enumerator.Current; if (pkValue.Equals(definition.PrimaryKey.GetValue(tmp))) { return(true); } } return(false); }
/// <summary> /// Crée un nouveau reader. /// </summary> /// <param name="list">Liste interne.</param> internal TestDbDataReader(IList list) { if (list == null) { throw new ArgumentNullException("list"); } _list = list; _descriptorMap = BeanDescriptor.GetCollectionDefinition(list).Properties; _descriptors = new List <BeanPropertyDescriptor>(_descriptorMap); }
/// <summary> /// Constructeur. /// </summary> /// <param name="name">Nom de la feuille de données.</param> /// <param name="dataSource">Source de données de la feuille.</param> /// <param name="properties">Propriétés affichées.</param> /// <exception cref="System.ArgumentException">Si une des propriétés à afficher n'existe pas dans le bean.</exception> public ExportSheet(string name, object dataSource, ICollection <ExportPropertyDefinition> properties) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (dataSource == null) { throw new ArgumentNullException("dataSource"); } if (properties == null) { throw new ArgumentNullException("properties"); } this.DisplayedProperties = new List <ExportPropertyDefinition>(); this.Name = name; this.DataSource = dataSource; this.Orientation = ExportOrientation.Portrait; BeanDefinition definition = this.IsCollection ? BeanDescriptor.GetCollectionDefinition(this.DataSource) : BeanDescriptor.GetDefinition(this.DataSource); foreach (ExportPropertyDefinition property in properties) { if (property.PropertyPath.IndexOf('.') != -1) { string[] propertyTab = property.PropertyPath.Split('.'); BeanPropertyDescriptor composedProperty = definition.Properties[propertyTab[0]]; BeanDefinition composedBeanDefinition = BeanDescriptor.GetDefinition(composedProperty.PropertyType); if (!composedBeanDefinition.Properties.Contains(propertyTab[1])) { throw new ArgumentException("Unable to find the property " + property.PropertyPath + " in type " + definition.BeanType.FullName); } } else if (!definition.Properties.Contains(property.PropertyPath)) { throw new ArgumentException("Unable to find the property " + property.PropertyPath + " in type " + definition.BeanType.FullName); } DisplayedProperties.Add(property); } }
/// <summary> /// Renvoie à partir d'une collection de bean un dictionnaire associant à une position de colonne dans le fichier plat la propriété correspondante du bean. /// </summary> /// <param name="dataSource">Collection de bean.</param> /// <returns>Dictionnaire des colonnes de fichiers plat indéxées par leur position.</returns> private static IDictionary <int, EncodingFlatFile> BuildSortedDictionnary(IEnumerable dataSource) { BeanDefinition beanDefinition = BeanDescriptor.GetCollectionDefinition(dataSource); BeanPropertyDescriptorCollection properties = beanDefinition.Properties; IDictionary <int, EncodingFlatFile> propertyMapSortedByPosition = new SortedDictionary <int, EncodingFlatFile>(Comparer <int> .Default); foreach (BeanPropertyDescriptor property in properties) { PropertyInfo basePropertyInfo = beanDefinition.BeanType.GetProperty(property.PropertyName); object[] attrs = basePropertyInfo.GetCustomAttributes(typeof(FlatFileField), false); if (attrs != null && attrs.Length > 0) { EncodingFlatFile encodingFlatFile = new EncodingFlatFile { Property = property, Attr = (FlatFileField)attrs[0] }; propertyMapSortedByPosition.Add(encodingFlatFile.Attr.Position, encodingFlatFile); } } if (propertyMapSortedByPosition.Count == 0) { throw new NotSupportedException("No exportable properties"); } if (propertyMapSortedByPosition.Keys.Min() != 0) { throw new NotSupportedException("The position must start at 0"); } if ((propertyMapSortedByPosition.Keys.Max() - propertyMapSortedByPosition.Keys.Min() + 1) != propertyMapSortedByPosition.Keys.Count) { throw new NotSupportedException("Inconsistency between the total number of exportable properties and the min and max position, there is some hole in your positions"); } return(propertyMapSortedByPosition); }
/// <summary> /// Création d'un document CSV à partir d'une collection et de ses propriétés à exporter. /// </summary> /// <param name="properties">Liste des proprités à exporter.</param> /// <param name="dataSourceList">Liste des données à exporter.</param> /// <param name="showHeader">Affiche ou pas un header.</param> /// <returns>Le fichier binaire.</returns> public static byte[] CreateCsvFile(ICollection <ExportPropertyDefinition> properties, object dataSourceList, bool showHeader = true) { if (dataSourceList == null) { throw new ArgumentNullException("dataSourceList"); } if (properties == null) { throw new ArgumentNullException("properties"); } IList <string> colonnes = new List <string>(properties.Count); IList <string> headers = new List <string>(properties.Count); BeanDefinition definition = BeanDescriptor.GetCollectionDefinition((ICollection)dataSourceList); foreach (ExportPropertyDefinition propertyDefinition in properties) { colonnes.Add(propertyDefinition.PropertyPath); if (!string.IsNullOrEmpty(propertyDefinition.PropertyLabel)) { headers.Add(propertyDefinition.PropertyLabel); } else { if (propertyDefinition.PropertyPath.IndexOf('.') != -1) { throw new ArgumentException("La composition n'est pas supporté"); } headers.Add(definition.Properties[propertyDefinition.PropertyPath].Description); } } return(CreateCsvFile(colonnes, dataSourceList, headers, showHeader)); }
public void GetDefinitionCollectionNotGenericCollection() { Generic <Bean> generic = new Generic <Bean>(); BeanDefinition beanDefinition = BeanDescriptor.GetCollectionDefinition(generic); }
public void GetDefinitionCollectionNull() { BeanDefinition beanDefinition = BeanDescriptor.GetCollectionDefinition(null); }
/// <summary> /// Création d'un document CSV à partir d'une collection et de ses colonnes à exporter. /// </summary> /// <param name="colonnes">Colonnes à exporter.</param> /// <param name="dataSourceList">Liste des données à exporter.</param> /// <param name="headers">Liste des headers(si null alors on utilise les colonnes).</param> /// <param name="showHeader">Affiche ou pas un header.</param> /// <returns>Fichier binaire.</returns> private static byte[] CreateCsvFile(IList <string> colonnes, object dataSourceList, IList <string> headers, bool showHeader = true) { if (dataSourceList == null) { throw new ArgumentNullException("dataSourceList"); } if (colonnes == null) { throw new ArgumentNullException("colonnes"); } StringBuilder sb = new StringBuilder(); BeanDefinition beanDefinition = BeanDescriptor.GetCollectionDefinition(dataSourceList); BeanPropertyDescriptorCollection properties = beanDefinition.Properties; if (showHeader) { bool first = true; if (headers == null) { foreach (string colonne in colonnes) { BeanPropertyDescriptor descriptor = properties[colonne]; if (!first) { sb.Append(';'); } sb.Append(descriptor.Description); first = false; } } else { if (headers.Count != colonnes.Count) { throw new ArgumentException("colonnes et headers n'ont pas la même taille"); } foreach (string header in headers) { if (!first) { sb.Append(';'); } sb.Append(header); first = false; } } sb.Append("\r\n"); } foreach (object valeur in (ICollection)dataSourceList) { bool first = true; foreach (string colonne in colonnes) { BeanPropertyDescriptor descriptor = properties[colonne]; if (!first) { sb.Append(';'); } sb.Append(descriptor.GetValue(valeur)); first = false; } sb.Append("\r\n"); } return(Encoding.Default.GetBytes(sb.ToString())); }