/// <summary> /// Génération du mapping des propriétés /// </summary> /// <param name="level"></param> /// <param name="root"></param> /// <param name="componentName"></param> private void GenerateMappingProperties(int level, Entity clazz, string componentName) { this.WriteLineWithIndent(level, "<!-- Properties -->"); foreach (DSLFactory.Candle.SystemModel.Property prop in clazz.Properties) { if (prop.IsPrimaryKey || prop.IsForeignKey) { continue; } string typeName = prop.Type; DataType refType = _inheritanceTree.FindEntityByName(prop.Type); if (refType != null) { typeName = refType.AssemblyQualifiedName; } else { // C'est peut-être une énumération refType = clazz.DataLayer.FindType(prop.Type) as Enumeration; if (refType != null) { typeName = refType.FullName; } } string columnName = prop.ColumnName; if (string.IsNullOrEmpty(columnName)) { columnName = StrategyManager.GetInstance(clazz.Store).NamingStrategy.CreateSQLColumnName(componentName, prop.Name); } // Propriété simple IList <string> types = ClrTypeParser.GetModelNamesFromClrType(prop.Type); if (types == null || types.Count == 0 || refType is Enumeration) { this.WriteLineWithIndent(level, String.Format("<property name=\"{0}\" column=\"{1}\" type=\"{2}\"/>", prop.Name, columnName, typeName)); } else { // Component this.WriteLineWithIndent(level, String.Format("<component name=\"{0}\" class=\"{1}\" >", prop.Name, typeName)); Entity entity = refType as Entity; if (entity != null) { GenerateMappingProperties(level + 1, entity, columnName); // Propriétés des classes de bases while (entity.SuperClass != null) { entity = entity.SuperClass; GenerateMappingProperties(level + 1, entity, columnName); } } this.WriteLineWithIndent(level, "</component>"); } } IList <Association> list = Association.GetLinksToTargets(clazz); foreach (Association association in list) { if (association.SourceMultiplicity == Multiplicity.OneMany || association.SourceMultiplicity == Multiplicity.ZeroMany) { string lazy = NHibernateStrategy.AssociationLazyLoadingProperty.GetValue(association).ToString().ToLower(); this.WriteLine(""); this.WriteLineWithIndent(level, "<!-- Relation 0..* -->"); this.WriteLineWithIndent(level, "<bag "); this.WriteLineWithIndent(level, String.Format(" name=\"{0}\" inverse=\"true\" lazy=\"{1}\" >", association.SourceRoleName, lazy)); this.WriteLineWithIndent(level, String.Format(" <key column=\"{0}\"/>", association.SourceRoleName)); this.WriteLineWithIndent(level, String.Format(" <one-to-many class=\"{0}\" />", association.Target.AssemblyQualifiedName)); this.WriteLineWithIndent(level, "</bag>"); } else if (association.SourceMultiplicity != Multiplicity.NotApplicable) { string insert = NHibernateStrategy.AssociationInsertProperty.GetValue(association).ToString().ToLower(); string update = NHibernateStrategy.AssociationUpdateProperty.GetValue(association).ToString().ToLower(); string outerJoin = NHibernateStrategy.AssociationOuterJoinProperty.GetValue(association).ToString().ToLower(); this.WriteLineWithIndent(level, String.Format("<many-to-one name=\"{0}\" class=\"{1}\" insert=\"{2}\" update=\"{3}\" outer-join=\"{4}\">", association.SourceRoleName, association.Target.AssemblyQualifiedName, insert, update, outerJoin)); foreach (ForeignKey fk in association.ForeignKeys) { WriteColumn(level + 1, "<column name=\"{0}\"/>", fk.Column); } this.WriteLineWithIndent(level, "</many-to-one>"); } } list = Association.GetLinksToSources(clazz); foreach (Association association in list) { if (association.TargetMultiplicity != Multiplicity.NotApplicable && !String.IsNullOrEmpty(association.TargetRoleName)) { string lazy = NHibernateStrategy.AssociationLazyLoadingProperty.GetValue(association).ToString().ToLower(); this.WriteLine(""); this.WriteLineWithIndent(level, "<!-- Relation 0..* -->"); this.WriteLineWithIndent(level, "<bag "); this.WriteLineWithIndent(level, String.Format(" name=\"{0}\" inverse=\"true\" lazy=\"{1}\" >", association.TargetRoleName, lazy)); if (association.ForeignKeys.Count == 1) { WriteColumn(level + 1, "<key column=\"{0}\"/>", association.ForeignKeys[0].Column); } else { this.WriteLineWithIndent(level + 1, "<key>"); foreach (ForeignKey fk in association.ForeignKeys) { WriteColumn(level + 2, "<column name=\"{0}\"/>", fk.Column); } this.WriteLineWithIndent(level + 1, "</key>"); } this.WriteLineWithIndent(level + 1, String.Format("<one-to-many class=\"{0}\" />", association.Source.AssemblyQualifiedName)); this.WriteLineWithIndent(level, "</bag>"); } } }
/// <summary> /// Génération du mapping d'une classe /// </summary> /// <param name="root">Classe sur lequel s'effectue le mapping</param> /// <param name="level">Niveau d'indentation dans le fichier de sortie</param> private void GenerateClassMapping(ClassInheritanceNode root, int level, int discriminator) { if (root.IsExternal) { return; } // Si cette classe n'est pas persistable, on ne fait rien y compris sur ses sous classes bool isPersistable = NHibernateStrategy.EntityIsPersistableProperty.GetValue(root.clazz); if (isPersistable == false) { return; } string endTag = "</class>"; if (level == 1) { bool isRootClass = root.childs.Count > 0; string tableName = root.clazz.TableName; if (String.IsNullOrEmpty(tableName)) { tableName = root.clazz.Name; } string schema = root.clazz.TableOwner; if (!String.IsNullOrEmpty(schema)) { schema = String.Format("schema=\"{0}\"", schema); } this.WriteLine(""); string comment = String.Format("<!-- Mapping de la classe {0} -->", root.clazz.Name); string asterix = new string('*', comment.Length - 9); this.WriteLineWithIndent(0, String.Format("<!-- {0} -->", asterix)); this.WriteLineWithIndent(0, comment); this.WriteLineWithIndent(0, String.Format("<!-- {0} -->", asterix)); this.WriteLineWithIndent(0, String.Format("<class name=\"{0}\" table=\"{1}\" {2}>", root.clazz.AssemblyQualifiedName, tableName, (isRootClass ? " discriminator-value=\"0\"" : ""))); // Identifiant if (root.clazz.PrimaryKeys.Count == 0) { this.WriteLineWithIndent(1, "<id><generator/></id>"); } else if (root.clazz.PrimaryKeys.Count == 1) { Property prop = root.clazz.PrimaryKeys[0]; string columnName = prop.ColumnName; if (string.IsNullOrEmpty(columnName)) { columnName = StrategyManager.GetInstance(root.clazz.Store).NamingStrategy.CreateSQLColumnName(root.clazz.Name, prop.Name); } this.WriteLineWithIndent(1, String.Format("<id name=\"{0}\" column=\"{1}\">", prop.Name, columnName)); GeneratorInfo gi = NHibernateStrategy.EntityIdGeneratorProperty.GetValue(root.clazz); if (gi != null) { this.WriteLineWithIndent(2, String.Format("<generator class=\"{0}\">", gi.Name)); foreach (GeneratorInfo.GeneratorParm parm in gi.Parms) { this.WriteLineWithIndent(3, String.Format("<param name=\"{0}\">{1}</param>", parm.Name, parm.Value)); } this.WriteLineWithIndent(2, "</generator>"); } this.WriteLineWithIndent(1, "</id>"); } else // Count > 1 { this.WriteLineWithIndent(1, String.Format("<composite-id name=\"Key\" class=\"{0}+PrimaryKey,{1}\">", root.clazz.FullName, root.clazz.DataLayer.AssemblyName)); foreach (Property property in root.clazz.PrimaryKeys) { string columnName = property.ColumnName; if (string.IsNullOrEmpty(columnName)) { columnName = StrategyManager.GetInstance(root.clazz.Store).NamingStrategy.CreateSQLColumnName(root.clazz.Name, property.Name); } this.WriteLineWithIndent(2, String.Format("<key-property name=\"{0}\" column=\"{1}\"/>", property.Name, columnName)); } this.WriteLineWithIndent(1, "</composite-id>"); } if (isRootClass) { this.WriteLineWithIndent(1, "<!-- Mapping utilise la stratégie de mapping filtré -->"); this.WriteLineWithIndent(1, "<discriminator column=\"DiscriminatorValue\" type=\"Int16\" force=\"true\"/>"); } } else { endTag = "</subclass>"; this.WriteLine(""); string comment = String.Format("<!-- sub-class mapping : {0} -->", root.clazz.Name); string asterix = new string('*', comment.Length - 9); this.WriteLineWithIndent(0, String.Format("<!-- {0} -->", asterix)); this.WriteLineWithIndent(0, comment); this.WriteLineWithIndent(0, String.Format("<!-- {0} -->", asterix)); this.WriteLineWithIndent(level, String.Format("<subclass name=\"{0}\" discriminator-value=\"{1}\" >", root.clazz.AssemblyQualifiedName, (1 << (discriminator - 1)))); } // Génération des propriétés de la classe GenerateMappingProperties(level, root.clazz, null); // Génération des sous classes foreach (ClassInheritanceNode childNode in root.childs) { GenerateClassMapping(childNode, level + 1, discriminator); discriminator++; } // Fermeture du tag this.WriteLineWithIndent(level - 1, endTag); }
/// <summary> /// Création du fichier dans le projet /// </summary> /// <param name="prj">Projet contenant le fichier</param> /// <param name="element">Elément concerné ou null</param> /// <param name="fileName">Nom du fichier ou null (default: element.Name)</param> /// <returns>Chemin complet du fichier</returns> protected string CreateOutputFileName(Project prj, ICustomizableElement element, string fileName) { if (String.IsNullOrEmpty(fileName)) { fileName = element.Name; } if (!Path.HasExtension(fileName)) { fileName = String.Format("{0}{1}", fileName, StrategyManager.GetInstance(_currentElement.Store).TargetLanguage.Extension); } // On le prend tel quel string tmpFileName = fileName; if (tmpFileName.Length > 0 && tmpFileName[0] == '~') { tmpFileName = tmpFileName.Substring(1); } else { // Si il n'y a que le nom du fichier, on prend le pattern par défaut if (Path.GetFileName(tmpFileName) == tmpFileName) { string pattern = DefaultGeneratedFilePathPattern; if (String.IsNullOrEmpty(pattern)) { pattern = StrategyManager.GetInstance(_currentElement.Store).NamingStrategy. DefaultGeneratedCodeFilePattern; } tmpFileName = String.Format(pattern, tmpFileName); } } tmpFileName = ResolvePattern(element, tmpFileName); // Suppression du '/' de debut if (tmpFileName.Length > 1 && (tmpFileName[0] == Path.AltDirectorySeparatorChar || tmpFileName[0] == Path.DirectorySeparatorChar)) { tmpFileName = tmpFileName.Substring(1); } string pathName = tmpFileName.Replace('/', '\\'); if (pathName.Length > 0 && pathName[0] == '\\') { pathName = pathName.Substring(1); } // Permet de surcharger pathName = CreateRelativeOutputFileName(element, pathName); Debug.Assert(pathName.Length > 0 && pathName[0] != '\\', "the file name must be relatif"); Debug.Assert(Path.HasExtension(pathName), "file name must have an extension"); Debug.Assert(pathName.IndexOf('{') < 0, "incorrect file name"); Context.RelativeGeneratedFileName = pathName; return(Context.ProjectFolder != null?Path.Combine(Context.ProjectFolder, pathName) : pathName); }
/// <summary> /// Initializes a new instance of the <see cref="StrategiesForm"/> class. /// </summary> /// <param name="component">The component.</param> /// <param name="model">The model.</param> public StrategiesForm(SoftwareComponent component, CandleElement model) { InitializeComponent(); if (component != null) { _store = component.Store; } else { _store = model.Store; } specificStrategies = new List <StrategiesListControl>(); globalsStrategies = new StrategiesListControl(); namingStrategy = new NamingStrategyControl(); languageConfig = new LanguageConfigurationControl(); globalsStrategies.Dock = DockStyle.Fill; globalsStrategies.Name = "globalsStrategies"; globalsStrategies.TabIndex = 1; namingStrategy.Dock = DockStyle.Fill; namingStrategy.Name = "namingStrategy"; namingStrategy.TabIndex = 1; languageConfig.Dock = DockStyle.Fill; languageConfig.Name = "languageConfig"; languageConfig.TabIndex = 1; tabStrategies.TabPages.Clear(); // Création des onglets (une par couche + une globale + une pour la stratégie de nommage) // L'onglet courant est sélectionné // D'abord le global globalsStrategies.Initialize(_store, null); tabGlobals.Controls.Add(globalsStrategies); tabStrategies.TabPages.Add(tabGlobals); // Puis un par couche int index = 1; foreach (SoftwareLayer layer in component.Layers) { StrategiesListControl specificStrategy = new StrategiesListControl(); specificStrategy.Dock = DockStyle.Fill; specificStrategy.Name = String.Format("specificStrategies{0}", index); specificStrategy.TabIndex = 0; specificStrategy.Initialize(_store, layer); specificStrategy.StrategyRemoved += Strategies_StrategyRemoved; TabPage tabSpecific = new TabPage(); tabStrategies.TabPages.Add(tabSpecific); tabSpecific.Location = new Point(4, 22); tabSpecific.Name = String.Format("tabSpecific{0}", index); tabSpecific.Padding = new Padding(3); tabSpecific.Size = new Size(741, 348); tabSpecific.TabIndex = index; tabSpecific.UseVisualStyleBackColor = true; tabSpecific.Text = layer.Name; if (layer == model.StrategiesOwner) { tabSpecific.Text += "*"; tabStrategies.SelectedTab = tabSpecific; } tabSpecific.Controls.Add(specificStrategy); } // Stratégie de nommage namingStrategy.Initialize(StrategyManager.GetInstance(_store).NamingStrategy); tabNaming.Controls.Add(namingStrategy); tabStrategies.TabPages.Add(tabNaming); // Et le language languageConfig.Initialize(StrategyManager.GetInstance(_store).TargetLanguage); tabLanguage.Controls.Add(languageConfig); tabStrategies.TabPages.Add(tabLanguage); globalsStrategies.StrategyRemoved += new EventHandler <StrategyRemovedEventArgs>(Strategies_StrategyRemoved); }
/// <summary> /// Initialisation de la liste des stratégies /// </summary> /// <param name="strategiesOwner">The strategies owner.</param> /// <param name="selectedStrategy">The selected strategy.</param> private void PopulateTreeView(CandleElement strategiesOwner, StrategyBase selectedStrategy) { this._strategiesOwner = strategiesOwner != null ? strategiesOwner.StrategiesOwner : null; _initialize = true; tvStrategies.Nodes.Clear(); // Remplissage à partir des stratégies liées au modèle TreeNode selectedNode = null; foreach (StrategyBase strategy in StrategyManager.GetInstance(_store).GetStrategies(strategiesOwner, false)) { TreeNodeCollection nodes = tvStrategies.Nodes; // Insertion dans l'arbre en tenant compte du path if (!String.IsNullOrEmpty(strategy.StrategyPath)) { string[] pathParts = strategy.StrategyPath.Split('/'); // Création ou recherche de l'arborescence liée au path foreach (string part in pathParts) { // Recherche du noeud parent pour chaque part bool bFind = false; foreach (TreeNode node in nodes) { if (Utils.StringCompareEquals(node.Text, part)) { nodes = node.Nodes; bFind = true; break; } } // Si pas trouvé, on le crèe if (!bFind) { TreeNode parent = new TreeNode(part); nodes.Add(parent); nodes = parent.Nodes; parent.Expand(); } } } // Création du noeud de la stratégie TreeNode tmpNode = new TreeNode(strategy.DisplayName); tmpNode.Tag = strategy; nodes.Add(tmpNode); tmpNode.Checked = strategy.IsEnabled; if (selectedStrategy != null && selectedStrategy.StrategyId == strategy.StrategyId) { selectedNode = tmpNode; } } if (selectedNode != null) { tvStrategies.SelectedNode = selectedNode; } _initialize = false; }