示例#1
0
        /////////////////////////////////////////////////////////////
        private void AddFields(Type leType, Hashtable tableIdsDefinisDansTableAttribute, Hashtable tableIds, bool bRelationsOnly)
        {
            if (leType == null)
            {
                return;
            }
            if (!bRelationsOnly)
            {
                //Relation vers la table Versions
                #region Relation vers la table versions
                if (!typeof(IObjetSansVersion).IsAssignableFrom(leType))
                {
                    CInfoChampTable champVersion = new CInfoChampTable(CSc2iDataConst.c_champIdVersion,
                                                                       typeof(int),
                                                                       0,
                                                                       false,
                                                                       false,
                                                                       true,
                                                                       false,
                                                                       true);
                    //Stef 06 10 2009 : le champ version n'est plus indexé seul, il est indexé
                    //avec le champ deleted
                    champVersion.IsIndex   = true;
                    champVersion.Propriete = "IdVersionDatabase";
                    m_listeChamps.Add(champVersion);
                    m_tableChamps[champVersion.NomChamp] = champVersion;

                    //Stef 29/08/2008 : suppression du lien à la version, trop lent !

                    /*CInfoRelation relationVersion = new CInfoRelation(
                     *      CVersionDonnees.c_nomTable,
                     *      NomTable,
                     *      new string[] { CVersionDonnees.c_champId },
                     *      new string[] { champVersion.NomChamp },
                     *      false,
                     *      true,
                     *      true,
                     *      false);
                     * relationVersion.NomConvivial = "Version";
                     * m_listeRelationsParentes.Add(relationVersion);*/

                    CInfoChampTable champIdOriginal = new CInfoChampTable(CSc2iDataConst.c_champOriginalId,
                                                                          typeof(int),
                                                                          0,
                                                                          false,
                                                                          false,
                                                                          true,
                                                                          false,
                                                                          true);
                    champIdOriginal.IsIndex = true;
                    m_listeChamps.Add(champIdOriginal);

                    CInfoChampTable champIsDeleted = new CInfoChampTable(CSc2iDataConst.c_champIsDeleted,
                                                                         typeof(bool),
                                                                         0,
                                                                         false,
                                                                         false,
                                                                         false,
                                                                         false,
                                                                         true);
                    champIsDeleted.Propriete = "IsDeleted";
                    m_listeChamps.Add(champIsDeleted);

                    //Stef 06 10 2009 : le champ version n'est plus indexé seul, il est indexé
                    //avec le champ deleted

                    /*CInfoIndexTable infoIndex = new CInfoIndexTable(
                     *  CSc2iDataConst.c_champIdVersion, CSc2iDataConst.c_champIsDeleted);
                     * m_listeIndexsSupplementaires.Add(infoIndex);*/
                }
            }

            #endregion


            foreach (PropertyInfo property in leType.GetProperties())
            {
                string   strNomConvivial = "";
                object[] attribs;
                attribs = property.GetCustomAttributes(typeof(DynamicFieldAttribute), true);
                //si le champ a l'attribut dynamique, il a un nom convivial
                if (attribs.Length != 0)
                {
                    strNomConvivial = ((DynamicFieldAttribute)attribs[0]).NomConvivial;
                }

                //Attribut TableField
                attribs = property.GetCustomAttributes(typeof(TableFieldPropertyAttribute), true);
                if (attribs.Length == 1 && !bRelationsOnly)
                {
                    TableFieldPropertyAttribute attrib = (TableFieldPropertyAttribute)attribs[0];
                    if (attrib.IsInDb)                    //Ne prend que les champs qui sont dans la base de données
                    {
                        if (property.Name != "IdUniversel" ||
                            leType.GetCustomAttributes(typeof(NoIdUniverselAttribute), true).Length == 0)
                        {
                            if (property.PropertyType == typeof(string) && attrib.Longueur < 1)
                            {
                                throw (new Exception(I.T("The field attribute @1 of class @2 doesn't define a length|168", property.Name, leType.Name)));
                            }
                            bool bIsId = tableIdsDefinisDansTableAttribute[attrib.NomChamp] != null;

                            CInfoChampTable info = new CInfoChampTable(
                                attrib.NomChamp,
                                property.PropertyType,
                                attrib.Longueur,
                                attrib.IsLongString,
                                bIsId,
                                attrib.NullAutorise && !bIsId,
                                attrib.ExclureUpdateStandard,
                                attrib.IsInDb);

                            info.Propriete    = property.Name;
                            info.NomConvivial = strNomConvivial;

                            if (property.GetCustomAttributes(typeof(IndexFieldAttribute), true).Length > 0)
                            {
                                info.IsIndex = true;
                            }

                            if (m_tableChamps[info.NomChamp] == null)
                            {
                                m_listeChamps.Add(info);
                                m_tableChamps[info.NomChamp] = info;
                            }
                            if (bIsId)
                            {
                                tableIds[info.NomChamp] = info;
                            }
                        }
                    }
                }

                //Attribut Relation
                if (bRelationsOnly)
                {
                    m_bIsRelationsInitialisees = true;
                    attribs = property.GetCustomAttributes(typeof(RelationAttribute), true);
                    if (attribs.Length != 0)
                    {
                        RelationAttribute relAttrib        = (RelationAttribute)attribs[0];
                        CStructureTable   structureParente = GetStructure(CContexteDonnee.GetTypeForTable(relAttrib.TableMere));
                        if (relAttrib.TableMere == NomTable)                        //Pour les tables autoliées
                        {
                            structureParente = this;
                        }
                        //Si la structure parente est moi-même, les champs Id n'ont pas encore
                        //été renseignés. Il y a donc un contrôle en moins sur les tables à lien récursif
                        if (structureParente.ChampsId != null && relAttrib.ChampsFils.Length != relAttrib.ChampsParent.Length)
                        {
                            throw new Exception(I.T("The relation between the table @1 and the parental table @2 doesn't have the right number of link fields|169", NomTable, relAttrib.TableMere));
                        }

                        int nIndexCle = 0;
                        foreach (string strChamp in relAttrib.ChampsFils)
                        {
                            bool            bIsId = tableIdsDefinisDansTableAttribute[strChamp] != null;
                            CInfoChampTable info  = new CInfoChampTable(
                                strChamp,
                                structureParente.GetChamp(relAttrib.ChampsParent[nIndexCle]).TypeDonnee,
                                structureParente.GetChamp(relAttrib.ChampsParent[nIndexCle]).Longueur,
                                false,
                                bIsId,
                                !relAttrib.Obligatoire,
                                false,
                                true);
                            info.IsIndex = relAttrib.Index;

                            //13/3/2005 : les deux lignes suivants avaient été enlevées. Pourquoi ?
                            //En tout cas, moi j'en ai besoin pour pouvoir tester
                            //Les attributs de la propriété
                            //20/4/2005
                            //Ca avait été enlevé car le champ pointe sur le type de donnée
                            //de la clé (un entier en général), c'est un champ qui ne doit pas être affiché
                            //dans les listes de propriétés , c'est juste un champ de la table

                            /*info.Propriete = property.Name;
                             * info.NomConvivial = strNomConvivial;*/
                            /* 9/5/2005, si on souhaite trouver la propriété correspondantes,
                             * il faut parcourir les relations et regarder les propriétés des relations
                             * */
                            nIndexCle++;
                            if (m_tableChamps[info.NomChamp] == null)
                            {
                                m_listeChamps.Add(info);
                                m_tableChamps[info.NomChamp] = info;
                            }
                            if (tableIdsDefinisDansTableAttribute[strChamp] != null)
                            {
                                //Ce champ fait partie de la clé de l'objet
                                tableIds[info.NomChamp] = info;
                            }
                        }
                        CInfoRelation infoRelation = new CInfoRelation(
                            relAttrib.TableMere,
                            NomTable,
                            relAttrib.ChampsParent,
                            relAttrib.ChampsFils,
                            relAttrib.Obligatoire,
                            relAttrib.Composition,
                            relAttrib.Index,
                            relAttrib.PasserLesFilsANullLorsDeLaSuppression,
                            relAttrib.DeleteEnCascadeManuel);
                        infoRelation.IsInDb             = relAttrib.IsInDb;
                        infoRelation.IsClustered        = relAttrib.IsCluster;
                        infoRelation.NomConvivial       = strNomConvivial;
                        infoRelation.Propriete          = property.Name;
                        infoRelation.NePasClonerLesFils = relAttrib.NePasClonerLesFils;
                        m_listeRelationsParentes.Add(infoRelation);
                    }

                    //Attribut RelationFille
                    attribs = property.GetCustomAttributes(typeof(RelationFilleAttribute), true);
                    if (attribs.Length != 0)
                    {
                        RelationFilleAttribute relFille             = (RelationFilleAttribute)attribs[0];
                        PropertyInfo           propParenteDansFille = relFille.TypeFille.GetProperty(relFille.ProprieteFille);
                        if (propParenteDansFille == null)
                        {
                            throw new Exception(I.T("The @1 type defines a child relation on the child property @2.@3 which doesn't exist|170", leType.Name, relFille.TypeFille.Name, relFille.ProprieteFille));
                        }
                        //Récupère les infos de la relation
                        attribs = propParenteDansFille.GetCustomAttributes(typeof(RelationAttribute), true);
                        if (attribs.Length != 1)
                        {
                            throw new Exception(I.T("The @1 type defines a child relation on the child property @2.@3 which doesn't define a parental relation|171", leType.Name, relFille.TypeFille.Name, relFille.ProprieteFille));
                        }
                        RelationAttribute relInfo = (RelationAttribute)attribs[0];
                        //Récupère le nom de la table fille
                        attribs = relFille.TypeFille.GetCustomAttributes(typeof(TableAttribute), true);
                        if (attribs.Length != 1)
                        {
                            throw new Exception(I.T("The @1 type define a child relation on the @2 type which doesn't define a table|172", leType.Name, relFille.TypeFille.Name));
                        }
                        object[] attr = property.GetCustomAttributes(typeof(DynamicChildsAttribute), false);
                        if (attr.Length > 0)
                        {
                            strNomConvivial = ((DynamicChildsAttribute)attr[0]).NomConvivial;
                        }
                        CInfoRelation infoRelation = new CInfoRelation(
                            NomTable,
                            ((TableAttribute)attribs[0]).NomTable,
                            relInfo.ChampsParent,
                            relInfo.ChampsFils,
                            relInfo.Obligatoire,
                            relInfo.Composition,
                            relInfo.Index,
                            relInfo.PasserLesFilsANullLorsDeLaSuppression,
                            relInfo.DeleteEnCascadeManuel);
                        infoRelation.NomConvivial       = strNomConvivial;
                        infoRelation.Propriete          = property.Name;
                        infoRelation.NePasClonerLesFils = relInfo.NePasClonerLesFils;
                        m_listeRelationsFilles.Add(infoRelation);
                    }
                }
            }
            ///Relations TypeID
            foreach (RelationTypeIdAttribute relTypeId in CContexteDonnee.RelationsTypeIds)
            {
                if (relTypeId.IsAppliqueToType(leType))
                {
                    m_listeRelationsFillesTypeId.Add(relTypeId);
                }
            }
            //AddFields ( leType.BaseType, tableIdsDefinisDansTableAttribute, tableIds );
        }
        /// <summary>
        /// Retourne un filtre de recherche rapide pour le type demandé
        ///
        /// </summary>
        /// <param name="tp"></param>
        /// <param name="bContient">Indique que le filtre est un filtre 'Contient' et non 'Commence par'</param>
        /// <returns></returns>
        public static CFiltreData GetFiltreRapideForType(Type tp)
        {
            if (tp == null)
            {
                return(null);
            }
            CFiltreData filtreRetour = null;

            if (m_filtreRapideDelegate != null)
            {
                m_filtreRapideDelegate.Invoke(tp, ref filtreRetour);
            }
            if (filtreRetour != null)
            {
                return(filtreRetour);
            }
            bool      bAvance   = false;
            ArrayList lstChamps = new ArrayList();

            foreach (PropertyInfo prop in tp.GetProperties())
            {
                if (prop.PropertyType == typeof(string))
                {
                    bool bPrendre = false;
                    if (prop.Name.ToUpper() == "LIBELLE" || prop.Name.ToUpper() == "NOM")
                    {
                        bPrendre = true;
                    }
                    else
                    {
                        object[] attribs = prop.GetCustomAttributes(typeof(RechercheRapideAttribute), true);
                        bPrendre = attribs.Length > 0;
                    }
                    if (bPrendre)
                    {
                        //Cherche le nom du champ correspondant
                        object[] attribs = prop.GetCustomAttributes(typeof(TableFieldPropertyAttribute), true);
                        if (attribs.Length > 0)
                        {
                            TableFieldPropertyAttribute attr = (TableFieldPropertyAttribute)attribs[0];
                            lstChamps.Add(attr.NomChamp);
                        }
                    }
                }
            }
            object[] attribsClasse = tp.GetCustomAttributes(typeof(RechercheRapideAttribute), true);
            foreach (RechercheRapideAttribute attr in attribsClasse)
            {
                if (attr.ChampFiltre != "")
                {
                    lstChamps.Add(attr.ChampFiltre);
                }
                if (attr.ChampFiltre.IndexOf('.') >= 0)
                {
                    bAvance = true;
                }
            }
            if (lstChamps.Count == 0)
            {
                return(null);
            }
            string strFiltre = "";

            foreach (string strChamp in lstChamps)
            {
                strFiltre += strChamp + " like @1 or ";
            }
            strFiltre = strFiltre.Substring(0, strFiltre.Length - 4);
            if (bAvance)
            {
                return(new CFiltreDataAvance(
                           CContexteDonnee.GetNomTableForType(tp),
                           strFiltre, ""));
            }
            return(new CFiltreData(strFiltre, ""));
        }