예제 #1
0
        /// <summary>
        /// Ajoute comme nouveaux paramètres toutes les propriétés publiques primitives du bean considéré.
        /// Le nom du paramètre correspond au nom de la propriété.
        /// </summary>
        /// <param name="bean">Le bean en question.</param>
        /// <returns>La liste des paramètres ajoutés.</returns>
        /// <exception cref="System.NotSupportedException">Si le bean contient une propriété non primitive.</exception>
        public ICollection <SqlServerParameter> AddBeanProperties(object bean)
        {
            if (bean == null)
            {
                throw new ArgumentNullException("bean");
            }

            ICollection <SqlServerParameter> parameterList = new List <SqlServerParameter>();
            BeanDefinition beanDefinition = BeanDescriptor.GetDefinition(bean);

            foreach (BeanPropertyDescriptor property in
                     beanDefinition.Properties.Where(property => property.PropertyType != typeof(ChangeAction)))
            {
                if (property.PrimitiveType == null)
                {
                    continue;
                }

                if (property.PrimitiveType.Name == "ICollection`1")
                {
                    continue;
                }

                string             parameterName = property.MemberName ?? property.PropertyName;
                SqlServerParameter parameter     = AddWithValue(parameterName, property.GetValue(bean, true));
                parameterList.Add(parameter);
            }

            return(parameterList);
        }
예제 #2
0
        public void CheckInsert()
        {
            Bean b = new Bean();

            b.LibelleNotNull = "libelle";
            BeanDescriptor.Check(b, true);
        }
예제 #3
0
        public void GetDefinitionContract()
        {
            BeanDefinition beanDefinition = BeanDescriptor.GetDefinition(new Bean());

            Assert.IsNotNull(beanDefinition.ContractName);
            Assert.AreEqual("BEAN", beanDefinition.ContractName);
        }
예제 #4
0
        /// <summary>
        /// Ecrit une ligne démarrant à la cellule courante.
        /// Les valeurs de cellules sont lues dans l'ordre des propriétés du bean <code>row</code>.
        /// </summary>
        /// <typeparam name="TRow">Type de la ligne.</typeparam>
        /// <param name="row">Bean de la ligne.</param>
        /// <returns>Le builder.</returns>
        public ExcelTemplateBuilder WriteRow <TRow>(TRow row)
        {
            var def           = BeanDescriptor.GetDefinition(row);
            var initialColumn = _currentCell.ColumnName;

            /* Parcourt des propriétés du bean. */
            foreach (var propDesc in def.Properties)
            {
                /* Valeur de la propriété pour le bean. */
                object value = propDesc.GetValue(row);

                var cellValue = CellValues.SharedString;

                /* Cas d'une cellule vide. */
                var strValue = value == null ? string.Empty : value.ToString();

                /* Cas d'un decimal. */
                if (propDesc.PrimitiveType == typeof(decimal))
                {
                    cellValue = CellValues.Number;
                    strValue  = strValue.Replace(" ", string.Empty).Replace(",", ".");
                }

                /* Ecrit dans la cellule courante. */
                WriteCellCore(strValue, cellValue);

                /* Passe à la colonne suivante. */
                _currentCell = _currentCell.ShiftColumn(1);
            }

            /* On retourne à la cellule initiale de la ligne. */
            _currentCell = _currentCell.ChangeColumn(initialColumn);
            return(this);
        }
예제 #5
0
        public void GetDefinitionDynamic()
        {
            BeanDefinition beanDefinition         = BeanDescriptor.GetDefinition(new BeanDynamic());
            BeanPropertyDescriptorCollection coll = beanDefinition.Properties;
            bool idOk      = false;
            bool otherIdOk = false;

            foreach (BeanPropertyDescriptor prop in coll)
            {
                if ("Id".Equals(prop.PropertyName))
                {
                    Assert.AreEqual("IDENTIFIANT", prop.DomainName);
                    Assert.AreEqual("BEA_ID", prop.MemberName);
                    Assert.IsTrue(prop.IsRequired);
                    idOk = true;
                }
                else if ("OtherId".Equals(prop.PropertyName))
                {
                    Assert.AreEqual("IDENTIFIANT", prop.DomainName);
                    Assert.AreEqual("OTH_ID", prop.MemberName);
                    Assert.IsFalse(prop.IsRequired);
                    otherIdOk = true;
                }
            }
            Assert.IsTrue(idOk);
            Assert.IsTrue(otherIdOk);
        }
        /// <summary>
        /// Crée un dictionnaire { nom de la propriété => valeur } pour un item à insérer.
        /// </summary>
        /// <param name="modelClass">Modele de la classe.</param>
        /// <param name="initItem">Item a insérer.</param>
        /// <param name="isPrimaryKeyIncluded">True si le script d'insert doit comporter la clef primaire.</param>
        /// <returns>Dictionnaire contenant { nom de la propriété => valeur }.</returns>
        protected override Dictionary <string, string> CreatePropertyValueDictionary(ModelClass modelClass, ItemInit initItem, bool isPrimaryKeyIncluded)
        {
            Dictionary <string, string> nameValueDict = new Dictionary <string, string>();
            BeanDefinition definition = BeanDescriptor.GetDefinition(initItem.Bean);

            foreach (ModelProperty property in modelClass.PersistentPropertyList)
            {
                if (!property.DataDescription.IsPrimaryKey || isPrimaryKeyIncluded)
                {
                    BeanPropertyDescriptor propertyDescriptor = definition.Properties[property.Name];
                    object propertyValue    = propertyDescriptor.GetValue(initItem.Bean);
                    string propertyValueStr = propertyValue == null ? string.Empty : propertyValue.ToString();
                    if (property.DataType == "byte[]")
                    {
                        nameValueDict[property.DataMember.Name] = GetBulkColumn(propertyValueStr);
                    }
                    else if (propertyDescriptor.PrimitiveType == typeof(string))
                    {
                        nameValueDict[property.DataMember.Name] = "'" + propertyValueStr.Replace("'", "''") + "'";
                    }
                    else
                    {
                        nameValueDict[property.DataMember.Name] = propertyValueStr;
                    }
                }
            }

            return(nameValueDict);
        }
예제 #7
0
        public string GetReferenceValueByPrimaryKey(Type referenceType, object primaryKey, object dataSource, string defaultPropertyName = null)
        {
            BeanDefinition         definition         = BeanDescriptor.GetDefinition(referenceType);
            BeanPropertyDescriptor primaryKeyProperty = definition.PrimaryKey;
            BeanPropertyDescriptor valueProperty      = string.IsNullOrEmpty(defaultPropertyName) ? definition.DefaultProperty : definition.Properties[defaultPropertyName];
            ICollection            dataSourceColl     = (ICollection)dataSource;

            if (dataSourceColl == null)
            {
                throw new NotSupportedException("DataSource must be an ICollection.");
            }

            object candidate = null;

            foreach (object item in dataSourceColl)
            {
                if (primaryKeyProperty.GetValue(item).Equals(primaryKey))
                {
                    candidate = item;
                    break;
                }
            }

            if (candidate == null)
            {
                throw new NotSupportedException("The datasource does not contain an object with this primary key.");
            }

            return(valueProperty.ConvertToString(valueProperty.GetValue(candidate)));
        }
예제 #8
0
        public void GetCollectionUnit()
        {
            BeanDefinition         beanDefinition = BeanDescriptor.GetDefinition(new Bean());
            BeanPropertyDescriptor property       = beanDefinition.Properties["RoleList"];

            Assert.IsNull(property.Unit);
        }
예제 #9
0
        public void CheckValueTypeBadType()
        {
            BeanDefinition         beanDefinition = BeanDescriptor.GetDefinition(new Bean());
            BeanPropertyDescriptor primaryKey     = beanDefinition.PrimaryKey;

            primaryKey.CheckValueType(String.Empty);
        }
예제 #10
0
        public void ValidConstraintsError()
        {
            BeanDefinition         beanDefinition = BeanDescriptor.GetDefinition(new Bean());
            BeanPropertyDescriptor property       = beanDefinition.Properties["Libelle"];

            property.ValidConstraints("0123456789012345678901234567890123456789", null);
        }
예제 #11
0
        public void GetObjectUnit()
        {
            BeanDefinition         beanDefinition = BeanDescriptor.GetDefinition(new Bean());
            BeanPropertyDescriptor property       = beanDefinition.Properties["Child"];

            Assert.IsNull(property.Unit);
        }
예제 #12
0
        public void ConvertFromStringFormatteur()
        {
            BeanDefinition         beanDefinition = BeanDescriptor.GetDefinition(new Bean());
            BeanPropertyDescriptor propertyDate   = beanDefinition.Properties["Date"];

            propertyDate.ConvertFromString("02/01/2007");
        }
예제 #13
0
        public void GetUnit()
        {
            BeanDefinition         beanDefinition      = BeanDescriptor.GetDefinition(new Bean());
            BeanPropertyDescriptor propertyPourcentage = beanDefinition.Properties["Pourcentage"];

            Assert.AreEqual(FormatterPercent.UnitPercentage, propertyPourcentage.Unit);
        }
예제 #14
0
        public void ConvertFromStringBadType()
        {
            BeanDefinition         beanDefinition = BeanDescriptor.GetDefinition(new Bean());
            BeanPropertyDescriptor primaryKey     = beanDefinition.PrimaryKey;

            primaryKey.ConvertFromString("aaa");
        }
예제 #15
0
        /// <summary>
        /// Applique le tri et le filtrage à la liste.
        /// </summary>
        /// <typeparam name="TSource">Type source.</typeparam>
        /// <param name="list">Liste source.</param>
        /// <returns>Liste triée.</returns>
        public ICollection <TSource> Apply <TSource>(ICollection <TSource> list)
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            if (_sortList.Count != 1)
            {
                throw new NotImplementedException();
            }

            if (this.InlineCount)
            {
                QueryContext.InlineCount = list.Count;
            }

            string    sortColumn = _sortList[0];
            SortOrder sortOrder  = _mapSort[sortColumn];

            BeanDefinition         beanDef            = BeanDescriptor.GetDefinition(typeof(TSource));
            BeanPropertyDescriptor propertyDescriptor = beanDef.Properties[FirstToUpper(sortColumn)];

            if (sortOrder == SortOrder.Asc)
            {
                list = list.OrderBy(x => propertyDescriptor.GetValue(x)).ToList();
            }
            else
            {
                list = list.OrderByDescending(x => propertyDescriptor.GetValue(x)).ToList();
            }

            // If this.Limit == 0 we disable pagination.
            return(list.Skip(this.Offset).Take(this.Limit > 0 ? this.Limit : list.Count).ToList());
        }
예제 #16
0
        public void CheckValueTypeUnsupportedGenericType()
        {
            BeanDefinition         beanDefinition = BeanDescriptor.GetDefinition(new Bean());
            BeanPropertyDescriptor primaryKey     = beanDefinition.PrimaryKey;

            primaryKey.CheckValueType(new Generic <Bean>());
        }
예제 #17
0
        /// <summary>
        /// Indique si l'autocompletion peut être utilisée pour un type.
        /// </summary>
        /// <param name="type">Type d'objet.</param>
        /// <param name="autoCompleteName">Nom de la liste spécifique à utiliser. Peut être nul.</param>
        /// <returns>True si l'autocompletion peut être utilisé.</returns>
        public bool CanUseAutoComplete(Type type, string autoCompleteName = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            BeanDefinition definition;

            if (_primaryKeyAccessors.ContainsKey(type.FullName))
            {
                Accessor primaryKeyAccessor = _primaryKeyAccessors[type.FullName];
                definition = BeanDescriptor.GetDefinition(primaryKeyAccessor.ReturnType, true);
            }
            else if (_referenceAccessors.ContainsKey(type.FullName))
            {
                Accessor referenceAccessor = _referenceAccessors[type.FullName];
                definition = BeanDescriptor.GetDefinition(referenceAccessor.ReturnType, true);
            }
            else
            {
                return(false);
            }

            return(definition.DefaultProperty != null && _autoCompleteAccessors.ContainsKey(string.IsNullOrEmpty(autoCompleteName) ? type.FullName : autoCompleteName));
        }
예제 #18
0
        public void ConvertToStringFormatteur()
        {
            BeanDefinition         beanDefinition = BeanDescriptor.GetDefinition(new Bean());
            BeanPropertyDescriptor propertyDate   = beanDefinition.Properties["Date"];

            propertyDate.ConvertToString(DateTime.Now);
        }
예제 #19
0
        /// <summary>
        /// Retourne la liste de référence du type TReferenceType à partir d'un objet de critères.
        /// </summary>
        /// <typeparam name="TReferenceType">Type de la liste de référence.</typeparam>
        /// <typeparam name="TCriteria">Type du critère.</typeparam>
        /// <param name="criteria">Objet contenant les critères.</param>
        /// <returns>Les éléments de la liste qui correspondent aux critères.</returns>
        public ICollection <TReferenceType> GetReferenceListByCriteria <TReferenceType, TCriteria>(TCriteria criteria)
            where TCriteria : TReferenceType
        {
            ICollection <TReferenceType>         beanColl = new List <TReferenceType>();
            IEnumerable <BeanPropertyDescriptor> beanPropertyDescriptorList = BeanDescriptor.GetDefinition(criteria)
                                                                              .Properties.Where(property => /* property.PropertyType != typeof(ChangeAction) &&*/ property.GetValue(criteria) != null);

            foreach (TReferenceType bean in GetReferenceList <TReferenceType>())
            {
                bool add = true;
                foreach (BeanPropertyDescriptor property in beanPropertyDescriptorList)
                {
                    if (property.PrimitiveType == null)
                    {
                        continue;
                    }

                    if (!property.GetValue(criteria).Equals(property.GetValue(bean)))
                    {
                        add = false;
                        break;
                    }
                }

                if (add)
                {
                    beanColl.Add(bean);
                }
            }

            return(beanColl);
        }
예제 #20
0
        public void IsReadonly()
        {
            BeanDefinition definition = BeanDescriptor.GetDefinition(new object());
            ICollection <BeanPropertyDescriptor> coll = definition.Properties;

            Assert.IsTrue(coll.IsReadOnly);
        }
예제 #21
0
        /// <summary>
        /// Récupère une ligne du tableau excel du type passé en paramètre de la méthode générique.
        /// </summary>
        /// <typeparam name="TRow">Le type de l'objet de retour.</typeparam>
        /// <returns>Une ligne du fichier excel.</returns>
        public TRow ReadRow <TRow>()
            where TRow : new()
        {
            var row = new TRow();
            var def = BeanDescriptor.GetDefinition(row);

            var initialColumn = _currentCell.ColumnName;
            var emptyCellsNb  = 0;

            /* Parcourt des propriétés du bean. */
            foreach (var propDesc in def.Properties)
            {
                var value = ReadCellCore(propDesc.PrimitiveType);

                // On suppose que la première ligne vide correspond à la fin du document.
                if (value == null)
                {
                    emptyCellsNb++;
                    if (emptyCellsNb == def.Properties.Count)
                    {
                        return(default(TRow));
                    }
                }

                propDesc.SetValue(row, value);

                /* Passe à la colonne suivante. */
                _currentCell = _currentCell.ShiftColumn(1);
            }

            /* On retourne à la cellule initiale de la ligne. */
            _currentCell = _currentCell.ChangeColumn(initialColumn);

            return(row);
        }
예제 #22
0
        public void Count()
        {
            BeanDefinition definition = BeanDescriptor.GetDefinition(new object());
            ICollection <BeanPropertyDescriptor> coll = definition.Properties;

            Assert.AreEqual(0, coll.Count);
        }
예제 #23
0
        /// <summary>
        /// Vérifie que deux ont les mêmes propriétés.
        /// </summary>
        /// <param name="expected">Bean attendu.</param>
        /// <param name="actual">Bean constaté.</param>
        private static void AreBeanEqualCore(object expected, object actual)
        {
            if (expected == null && actual == null)
            {
                return;
            }

            if (expected == null || actual == null)
            {
                Assert.Fail("Un des beans est null.");
            }

            var beanDef = BeanDescriptor.GetDefinition(expected);

            foreach (var prop in beanDef.Properties)
            {
                if (prop.PrimitiveType != null)
                {
                    Assert.AreEqual(prop.GetValue(expected), prop.GetValue(actual), $"Propriété {prop.PropertyName}");
                }
                else
                {
                    AreBeanEqualCore(prop.GetValue(expected), prop.GetValue(actual));
                }
            }
        }
예제 #24
0
        public void Clear()
        {
            BeanDefinition definition = BeanDescriptor.GetDefinition(new object());
            ICollection <BeanPropertyDescriptor> coll = definition.Properties;

            coll.Clear();
        }
예제 #25
0
        public void GetDefinitionUnsupportedType()
        {
            BeanDefinition         definition = BeanDescriptor.GetDefinition(new BeanInvalidUnsupportedType());
            BeanPropertyDescriptor property   = definition.Properties["OtherId"];

            property.ValidConstraints(3, null);
        }
예제 #26
0
        public void Remove()
        {
            BeanDefinition definition = BeanDescriptor.GetDefinition(new object());
            ICollection <BeanPropertyDescriptor> coll = definition.Properties;

            coll.Remove(null);
        }
예제 #27
0
        public void GetDefinitionPrimaryKey()
        {
            BeanDefinition beanDefinition = BeanDescriptor.GetDefinition(new Bean());

            Assert.IsNotNull(beanDefinition.PrimaryKey);
            Assert.AreEqual("Id", beanDefinition.PrimaryKey.PropertyName);
        }
예제 #28
0
        public void Item()
        {
            BeanDefinition         definition = BeanDescriptor.GetDefinition(new Bean());
            BeanPropertyDescriptor primaryKey = definition.PrimaryKey;

            Assert.AreEqual(primaryKey, definition.Properties[primaryKey.PropertyName]);
        }
예제 #29
0
        /// <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);
        }
예제 #30
0
        /// <summary>
        /// Prend en charge l'alimentation de la classe pour les valeurs d'initialisation.
        /// </summary>
        /// <param name="classe">Classe concernée.</param>
        /// <param name="item">Bean d'initialisation.</param>
        protected void HandleCorrectInit(ModelClass classe, TableInit item)
        {
            if (!DictionaryItemInit.ContainsKey(classe))
            {
                DictionaryItemInit.Add(classe, item);
            }

            foreach (ItemInit itemInit in item.ItemInitList)
            {
                BeanDefinition         definition         = BeanDescriptor.GetDefinition(itemInit.Bean);
                BeanPropertyDescriptor propertyDescriptor = GetReferenceKeyDescriptor(classe, definition);
                object propertyValue = propertyDescriptor.GetValue(itemInit.Bean);
                if (propertyDescriptor.PrimitiveType == typeof(string))
                {
                    propertyValue = "\"" + propertyValue + "\"";
                }

                BeanPropertyDescriptor libelleDescriptor = GetLibelleDescriptor(classe, definition);
                string libelle = null;
                if (libelleDescriptor != null)
                {
                    libelle = (string)libelleDescriptor.GetValue(itemInit.Bean);
                }
                else
                {
                    libelle = itemInit.VarName;
                }

                classe.ConstValues.Add(itemInit.VarName, new StaticListElement()
                {
                    Code = propertyValue, Libelle = libelle, CodeType = propertyDescriptor.PrimitiveType.ToString()
                });
            }
        }