public DbSchemaPanel(CslaGeneratorUnit cslagenunit, CslaObjectInfo cslaobject, string connection) { _currentUnit = cslagenunit; cn = connection; _currentCslaObject = cslaobject; // This call is required by the Windows.Forms Form Designer. InitializeComponent(); }
public GeneratorForm(CslaGeneratorUnit unit) { // // Required for Windows Form Designer support // InitializeComponent(); _unit = unit; }
public void NewCslaUnit() { _currentUnit = new CslaGeneratorUnit(); _currentCslaObject = null; _currentUnit.ConnectionString = ConnectionFactory.ConnectionString; BindControls(); EnableButtons(); }
public void NewCslaUnit() { CurrentUnit = new CslaGeneratorUnit(); _currentFilePath = Path.GetTempPath() + @"\" + Guid.NewGuid().ToString(); _currentCslaObject = null; _currentUnit.ConnectionString = ConnectionFactory.ConnectionString; BindControls(); EnableButtons(); _frmGenerator.PropertyGrid.SelectedObject = null; }
internal void NewCslaUnit() { CurrentUnit = new CslaGeneratorUnit(); CurrentUnitLayout = new CslaGeneratorUnitLayout(); _currentFilePath = Path.GetTempPath() + @"\" + Guid.NewGuid().ToString(); _currentCslaObject = null; _currentUnit.ConnectionString = ConnectionFactory.ConnectionString; BindControls(); EnableButtons(); _mainForm.ObjectInfoGrid.SelectedObject = null; }
internal void NewCslaUnit() { IsLoading = true; CurrentUnit = new CslaGeneratorUnit(); CurrentUnitLayout = new CslaGeneratorUnitLayout(); _currentFilePath = Path.GetTempPath() + @"\" + Guid.NewGuid(); CurrentCslaObject = null; _currentUnit.ConnectionString = ConnectionFactory.ConnectionString; BindControls(); _mainForm.ObjectInfoGrid.SelectedObject = null; IsLoading = false; }
private bool TargetDeprecated(CslaGeneratorUnit unit) { var result = true; if (unit.GenerationParams.TargetFramework == TargetFramework.CSLA10) { var alert = MessageBox.Show(unit.ProjectName + @" targets CSLA 1.0 that isn't supported any longer; code generation will crash.", @"CslaGenFork project generation", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (alert == DialogResult.Cancel) { result = false; } } return(result); }
internal void ExportParams(string fileName) { if (!ApplyProjectProperties()) { return; } var privateUnit = new CslaGeneratorUnit(); privateUnit.GenerationParams = _genParams.Clone(); privateUnit.Params = _projParams.Clone(); privateUnit.ProjectName = GeneratorController.Current.CurrentUnit.ProjectName; privateUnit.TargetDirectory = GeneratorController.Current.CurrentUnit.TargetDirectory; FileStream fs = null; var tempFile = Path.GetTempPath() + Guid.NewGuid() + ".cslagenerator"; var success = false; try { Cursor.Current = Cursors.WaitCursor; fs = File.Open(tempFile, FileMode.Create); var s = new XmlSerializer(typeof(CslaGeneratorUnit)); s.Serialize(fs, privateUnit); success = true; } catch (Exception ex) { MessageBox.Show(@"An error occurred while trying to export: " + Environment.NewLine + ex.Message, @"Export Error"); } finally { Cursor.Current = Cursors.Default; if (fs != null) { fs.Close(); fs.Dispose(); } } if (success) { File.Delete(fileName); File.Move(tempFile, fileName); } }
internal void SelectAll(DbSchemaPanel dbSchemaPanel, CslaGeneratorUnit currentUnit) { /* * currentUnit.Params.CreateReadOnlyObjectsCopySoftDelete */ // select all columns in list, except for the exceptions for (var i = 0; i < _lstColumns.Items.Count; i++) { var columnName = ((SqlColumnInfo)_lstColumns.Items[i]).ColumnName; var columnNativeType = ((SqlColumnInfo)_lstColumns.Items[i]).NativeType; // exception for soft delete column if (dbSchemaPanel.UseBoolSoftDelete && currentUnit.Params.SpBoolSoftDeleteColumn == columnName) { continue; } // exception for auditing columns if (!currentUnit.Params.ReadOnlyObjectsCopyAuditing && IsAuditingColumn(currentUnit, columnName)) { continue; } // exception for NativeType timestamp if (!currentUnit.Params.ReadOnlyObjectsCopyTimestamp && columnNativeType == "timestamp") { continue; } // none of the exceptions, so go ahead _lstColumns.SetSelected(i, true); } RefreshColumns(); }
private static bool IsAuditingColumn(CslaGeneratorUnit currentUnit, string columnName) { if (currentUnit.Params.CreationDateColumn == columnName) { return(true); } if (currentUnit.Params.CreationUserColumn == columnName) { return(true); } if (currentUnit.Params.ChangedDateColumn == columnName) { return(true); } if (currentUnit.Params.ChangedUserColumn == columnName) { return(true); } return(false); }
private void GenerateObject(CslaObjectInfo objInfo, CslaGeneratorUnit unit) { Metadata.GenerationParameters generationParams = unit.GenerationParams; string fileName = GetFileName(objInfo, generationParams.SeparateNamespaces, generationParams.OutputLanguage); string baseFileName = GetBaseFileName(objInfo, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.UseDotDesignerFileNameConvention, generationParams.OutputLanguage); try { GenerateInheritanceFile(fileName, objInfo, generationParams.ActiveObjects, unit); string tPath = this._fullTemplatesPath + generationParams.OutputLanguage.ToString() + @"\" + GetTemplateName(objInfo); CodeTemplate template = GetTemplate(objInfo, tPath); if (template != null) { StringBuilder errorsOutput = new StringBuilder(); StringBuilder warningsOutput = new StringBuilder(); template.SetProperty("ActiveObjects", generationParams.ActiveObjects); template.SetProperty("Errors", errorsOutput); template.SetProperty("Warnings", warningsOutput); template.SetProperty("CurrentUnit", unit); template.SetProperty("DataSetLoadingScheme", objInfo.DataSetLoadingScheme); if (generationParams.BackupOldSource && File.Exists(baseFileName)) { FileInfo oldFile = new FileInfo(baseFileName); if (File.Exists(baseFileName + ".old")) { File.Delete(baseFileName + ".old"); } oldFile.MoveTo(baseFileName + ".old"); } FileInfo fi = new FileInfo(baseFileName); using (FileStream fs = fi.Open(FileMode.Create)) { OnGenerationFileName(fi.FullName); using (StreamWriter swBase = new StreamWriter(fs)) { template.Render(swBase); errorsOutput = (StringBuilder)template.GetProperty("Errors"); warningsOutput = (StringBuilder)template.GetProperty("Warnings"); if (errorsOutput.Length > 0) { objFailed++; OnGenerationInformation("Failed:" + Environment.NewLine + errorsOutput.ToString(), 2); } else { if (warningsOutput != null) { if (warningsOutput.Length > 0) { objectWarnings++; OnGenerationInformation("Warning:" + Environment.NewLine + warningsOutput, 2); } } objSuccess++; } } } } } catch (Exception e) { objFailed++; ShowExceptionInformation(e); } }
public void GenerateProject(CslaGeneratorUnit unit) { CslaTemplateHelper.PrimaryKeys.ClearCache(); CslaObjectInfo objInfo = null; objFailed = 0; objSuccess = 0; sprocFailed = 0; sprocSuccess = 0; Controls.OutputWindow.Current.ClearOutput(); Metadata.GenerationParameters generationParams = unit.GenerationParams; _generateDatabaseClass = generationParams.GenerateDatabaseClass; _targetFramework = generationParams.TargetFramework; _abortRequested = false; _fullTemplatesPath = _templatesDirectory + generationParams.TargetFramework.ToString() + @"\"; templates = new Hashtable();//to recompile templates in case they changed. //This is just in case users add/remove objects while generating... List <CslaObjectInfo> list = new List <CslaObjectInfo>(); for (int i = 0; i < unit.CslaObjects.Count; i++) { if (unit.CslaObjects[i].Generate) { list.Add(unit.CslaObjects[i]); } } foreach (CslaObjectInfo info in list) { if (info.Generate) { if (objInfo == null) { objInfo = info; } if (_abortRequested) { break; } OnStep(info.ObjectName); try { GenerateObject(info, unit); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("Object {0} failed to generate:", info.ObjectName); sb.AppendLine(); sb.Append(" "); sb.Append(ex.Message); OnGenerationInformation(sb.ToString()); } if (_abortRequested) { break; } if (generationParams.GenerateSprocs && info.GenerateSprocs) { try { if (generationParams.OneSpFilePerObject) { GenerateAllSprocsFile(info, _targetDirectory, generationParams); } else { GenerateSelectProcedure(info, _targetDirectory); if (_abortRequested) { break; } if (info.ObjectType != CslaObjectType.ReadOnlyObject && info.ObjectType != CslaObjectType.ReadOnlyCollection && info.ObjectType != CslaObjectType.EditableRootCollection && info.ObjectType != CslaObjectType.DynamicEditableRootCollection && info.ObjectType != CslaObjectType.EditableChildCollection && info.ObjectType != CslaObjectType.NameValueList) { GenerateInsertProcedure(info, _targetDirectory); if (_abortRequested) { break; } GenerateDeleteProcedure(info, _targetDirectory); if (_abortRequested) { break; } GenerateUpdateProcedure(info, _targetDirectory); if (_abortRequested) { break; } } } } catch (Exception ex) { StringBuilder GenerationErrors = new StringBuilder(); GenerationErrors.Append(Environment.NewLine + " SpGeneration Crashed:"); GenerationErrors.Append(ex.Message); GenerationErrors.AppendLine(); GenerationErrors.AppendLine(); if (ex.InnerException != null) { GenerationErrors.AppendLine(ex.InnerException.Message); GenerationErrors.AppendLine(); GenerationErrors.AppendLine(ex.InnerException.StackTrace.ToString()); } GenerationErrors.AppendLine(); OnGenerationInformation(GenerationErrors.ToString()); } } } } if (_generateDatabaseClass) { GenerateUtilityFile("Database", objInfo, generationParams.TargetFramework, unit); } else { _fileSuccess.Add("Database", null); } GenerateUtilityFile("DataPortalHookArgs", objInfo, generationParams.TargetFramework, unit); if (_abortRequested) { OnGenerationInformation(Environment.NewLine + "Code Generation Cancelled!"); } OnFinalized(); }
private void GenerateUtilityFile(string utilityFilename, CslaObjectInfo objInfo, TargetFramework framework, CslaGeneratorUnit unit) { _fileSuccess.Add(utilityFilename, null); // only implement this for more recent frameworks); if (framework != TargetFramework.CSLA40) { return; } // base directory of project string fullFilename = _targetDirectory + @"\"; if (unit.GenerationParams.SeparateNamespaces) { // check whether to use namespace as folder fullFilename += unit.GenerationParams.UtilitiesNamespace.Replace(".", @"\") + @"\"; } else if (!unit.GenerationParams.UtilitiesFolder.Equals(String.Empty)) { // output folder inside directory fullFilename += unit.GenerationParams.UtilitiesFolder + @"\"; } CheckDirectory(fullFilename); // filename w/o extension fullFilename += utilityFilename; // extension if (objInfo.OutputLanguage == CodeLanguage.CSharp) { fullFilename += ".cs"; } else if (objInfo.OutputLanguage == CodeLanguage.VB) { fullFilename += ".vb"; } // Create utility class file if it does not exist if (!File.Exists(fullFilename)) { string tPath = _fullTemplatesPath + objInfo.OutputLanguage + "\\" + utilityFilename + ".cst"; CodeTemplate template = GetTemplate(objInfo, tPath); if (template != null) { template.SetProperty("FileName", utilityFilename); template.SetProperty("CurrentUnit", unit); FileStream fs = File.Open(fullFilename, FileMode.Create); OnGenerationInformation(utilityFilename + " file:"); OnGenerationFileName(fullFilename); StreamWriter sw = new StreamWriter(fs); try { template.Render(sw); _fileSuccess[utilityFilename] = true; } catch (Exception e) { ShowExceptionInformation(e); _fileSuccess[utilityFilename] = false; } finally { sw.Close(); } } } }
private void GenerateAccessoryFile(string fileName, string templateFile, CslaObjectInfo objInfo, bool activeObjects, CslaGeneratorUnit unit) { // Create Inheritance file if it does not exist if (!File.Exists(fileName)) //&& objInfo.ObjectType != CslaObjectType.NameValueList) { // string tPath = this._fullTemplatesPath + objInfo.OutputLanguage.ToString() + "\\InheritFromBase.cst"; string tPath = _fullTemplatesPath + objInfo.OutputLanguage + templateFile; CodeTemplate template = GetTemplate(objInfo, tPath); if (template != null) { template.SetProperty("ActiveObjects", activeObjects); template.SetProperty("CurrentUnit", unit); FileStream fs = File.Open(fileName, FileMode.Create); OnGenerationFileName(fileName); StreamWriter sw = new StreamWriter(fs); try { template.Render(sw); } catch (Exception e) { ShowExceptionInformation(e); } finally { sw.Close(); } } } }
private void GenerateClassCommentFile(string fileName, CslaObjectInfo objInfo, bool activeObjects, CslaGeneratorUnit unit) { GenerateAccessoryFile(fileName, "\\ClassComment.cst", objInfo, activeObjects, unit); }
private void GenerateInheritanceFile(string fileName, CslaObjectInfo objInfo, bool activeObjects, CslaGeneratorUnit unit) { GenerateAccessoryFile(fileName, "\\InheritFromBase.cst", objInfo, activeObjects, unit); }
private void GenerateObject(CslaObjectInfo objInfo, CslaGeneratorUnit unit) { Metadata.GenerationParameters generationParams = unit.GenerationParams; string fileName = GetBaseFileName(objInfo, false, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, false, false); string baseFileName = GetBaseFileName(objInfo, true, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, false, false); string classCommentFileName = string.Empty; if (!string.IsNullOrEmpty(generationParams.ClassCommentFilenameSuffix)) { classCommentFileName = GetBaseFileName(objInfo, false, generationParams.SeparateBaseClasses, generationParams.SeparateNamespaces, generationParams.BaseFilenameSuffix, generationParams.ExtendedFilenameSuffix, generationParams.ClassCommentFilenameSuffix, true, generationParams.SeparateClassComment); } FileStream fsBase = null; StreamWriter swBase = null; StreamWriter sw = null; try { string tPath = this._fullTemplatesPath + objInfo.OutputLanguage.ToString() + @"\" + GetTemplateName(objInfo); CodeTemplate template = GetTemplate(objInfo, tPath); if (template != null) { StringBuilder errorsOutput = new StringBuilder(); StringBuilder warningsOutput = new StringBuilder(); template.SetProperty("ActiveObjects", generationParams.ActiveObjects); template.SetProperty("Errors", errorsOutput); template.SetProperty("Warnings", warningsOutput); template.SetProperty("CurrentUnit", unit); template.SetProperty("DataSetLoadingScheme", objInfo.DataSetLoadingScheme); if (generationParams.BackupOldSource && File.Exists(baseFileName)) { FileInfo oldFile = new FileInfo(baseFileName); if (File.Exists(baseFileName + ".old")) { File.Delete(baseFileName + ".old"); } oldFile.MoveTo(baseFileName + ".old"); } fsBase = File.Open(baseFileName, FileMode.Create); OnGenerationFileName(baseFileName); swBase = new StreamWriter(fsBase); template.Render(swBase); errorsOutput = (StringBuilder)template.GetProperty("Errors"); warningsOutput = (StringBuilder)template.GetProperty("Warnings"); if (errorsOutput.Length > 0) { objFailed++; OnGenerationInformation("Failed:" + Environment.NewLine + errorsOutput); } else { if (warningsOutput != null) { if (warningsOutput.Length > 0) { objectWarnings++; OnGenerationInformation("Warning:" + Environment.NewLine + warningsOutput); } } objSuccess++; //OnGenerationInformation("Success"); } } GenerateInheritanceFile(fileName, objInfo, generationParams.ActiveObjects, unit); if (!string.IsNullOrEmpty(generationParams.ClassCommentFilenameSuffix)) { GenerateClassCommentFile(classCommentFileName, objInfo, generationParams.ActiveObjects, unit); } } catch (Exception e) { objFailed++; ShowExceptionInformation(e); } finally { if (sw != null) { sw.Close(); } if (swBase != null) { swBase.Close(); } } }
void GenerateInheritanceFile(string fileName, CslaObjectInfo objInfo, bool activeObjects, CslaGeneratorUnit unit) { // Create Inheritance file if it does not exist if (!File.Exists(fileName)) //&& objInfo.ObjectType != CslaObjectType.NameValueList) { string tPath = this._fullTemplatesPath + unit.GenerationParams.OutputLanguage.ToString() + "\\InheritFromBase.cst"; CodeTemplate template = GetTemplate(objInfo, tPath); if (template != null) { template.SetProperty("ActiveObjects", activeObjects); template.SetProperty("CurrentUnit", unit); FileInfo fi = new FileInfo(fileName); using (FileStream fs = fi.Open(FileMode.Create)) { OnGenerationFileName(fi.FullName); using (StreamWriter sw = new StreamWriter(fs)) { try { template.Render(sw); } catch (Exception e) { ShowExceptionInformation(e); } } } } } }
// Obsolete. // Probeably will be removed in next versions. // // public void ExecuteDelete() // { // ExecuteProcedure(_currentCslaObject.DeleteProcedure); // } // // public void ExecuteInsert() // { // ExecuteProcedure(_currentCslaObject.InsertProcedure); // } // // public void ExecuteSelect() // { // ExecuteProcedure(_currentCslaObject.SelectProcedure); // } // // public void ExecuteUpdate() // { // ExecuteProcedure(_currentCslaObject.UpdateProcedure); // } // // public void ExecuteProcedure(string sql) // { // SqlConnection cn = null; // try // { // if (ConnectionFactory.ConnectionString != "") // { // // if multiple statements, parse and execute individually // string matchString = "\n[Gg][Oo]"; // Regex regex = new Regex(matchString,RegexOptions.Multiline); // // string[] sqlStatements = regex.Split(sql); // // cn = ConnectionFactory.NewConnection; // SqlCommand cmd = cn.CreateCommand(); // // cmd.CommandType = CommandType.Text; // cmd.Connection.Open(); // // for (int i = 0; i < sqlStatements.Length; i++) // { // cmd.CommandText = sqlStatements[i]; // cmd.ExecuteNonQuery(); // } // } // } // catch (SqlException e) // { // MessageBox.Show(frmGenerator,e.Message,"Database Error"); // } // finally // { // if (cn != null && cn.State == ConnectionState.Open) // { // cn.Close(); // } // } // } public void Load(string fileName) { FileStream fs = null; try { Cursor.Current = Cursors.WaitCursor; fs = File.Open(fileName, FileMode.Open); XmlSerializer s = new XmlSerializer(typeof(CslaGeneratorUnit)); _currentUnit = (CslaGeneratorUnit)s.Deserialize(fs); _currentUnit.ResetParent(); _currentCslaObject = null; BindControls(); ConnectionFactory.ConnectionString = _currentUnit.ConnectionString; // check if this is a valid connection, else let the user enter new connection info SqlConnection cn = null; try { cn = ConnectionFactory.NewConnection; cn.Open(); BuildSchemaTree(_currentUnit.ConnectionString); } catch (SqlException e) { // call connect function which will allow user to enter new info Connect(); } finally { if (cn != null && cn.State == ConnectionState.Open) { cn.Close(); } } foreach (CslaObjectInfo info in _currentUnit.CslaObjects) { if (_schema != null) { info.LoadColumnInfo(_schema); } info.InheritedType.Parent = info; info.ObjectNameChanged += new EventHandler(CslaObject_ObjectNameChanged); EnableButtons(); } if (_currentUnit.CslaObjects.Count > 0) { _currentCslaObject = _currentUnit.CslaObjects[0]; } else { AddNewObject(); } frmGenerator.PropertyGrid.SelectedObject = _currentCslaObject; } catch (Exception e) { MessageBox.Show(frmGenerator, "An error occurred while trying to load: " + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace, "Loading Error"); } finally { Cursor.Current = Cursors.Default; fs.Close(); } }
public void GenerateProject(CslaGeneratorUnit unit) { if (!TargetDeprecated(unit)) { OnGenerationInformation(@"Project generation cancelled." + Environment.NewLine, 1); return; } objFailed = 0; objSuccess = 0; sprocFailed = 0; sprocSuccess = 0; Controls.OutputWindow.Current.ClearOutput(); Controls.OutputWindow.Current.AddOutputInfo(string.Format("Generating code in '{0}'", _targetDirectory)); Metadata.GenerationParameters generationParams = unit.GenerationParams; _abortRequested = false; _fullTemplatesPath = _templatesDirectory + generationParams.TargetFramework + @"\"; templates = new Hashtable();//to recompile templates in case they changed. //This is just in case users add/remove objects while generating... List <CslaObjectInfo> list = new List <CslaObjectInfo>(); for (int i = 0; i < unit.CslaObjects.Count; i++) { if (unit.CslaObjects[i].Generate) { list.Add(unit.CslaObjects[i]); } } foreach (CslaObjectInfo info in list) { if (info.Generate) { if (_abortRequested) { break; } OnStep(info.ObjectName); try { GenerateObject(info, unit); } catch (Exception ex) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("Object {0} failed to generate:", info.ObjectName); sb.AppendLine(); sb.Append("\t"); sb.Append(ex.Message); OnGenerationInformation(sb.ToString(), 2); } if (_abortRequested) { break; } if (generationParams.GenerateSprocs && info.GenerateSprocs) { try { if (generationParams.OneSpFilePerObject) { GenerateAllSprocsFile(info, GetSprocFileInfo(info.ObjectName)); } else { GenerateSelectProcedure(info); if (_abortRequested) { break; } if (info.ObjectType != CslaObjectType.ReadOnlyObject && info.ObjectType != CslaObjectType.ReadOnlyCollection && info.ObjectType != CslaObjectType.EditableRootCollection && info.ObjectType != CslaObjectType.EditableChildCollection && info.ObjectType != CslaObjectType.NameValueList) { GenerateInsertProcedure(info, _targetDirectory); if (_abortRequested) { break; } GenerateDeleteProcedure(info, _targetDirectory); if (_abortRequested) { break; } GenerateUpdateProcedure(info, _targetDirectory); if (_abortRequested) { break; } } } } catch (Exception ex) { StringBuilder GenerationErrors = new StringBuilder(); GenerationErrors.Append(Environment.NewLine + " SpGeneration Crashed:"); GenerationErrors.Append(ex.Message); GenerationErrors.AppendLine(); GenerationErrors.AppendLine(); if (ex.InnerException != null) { GenerationErrors.AppendLine(ex.InnerException.Message); GenerationErrors.AppendLine(); GenerationErrors.AppendLine(ex.InnerException.StackTrace.ToString()); } GenerationErrors.AppendLine(); OnGenerationInformation(GenerationErrors.ToString(), 2); } } } } if (_abortRequested) { OnGenerationInformation(Environment.NewLine + "Code Generation Cancelled!", 2); } OnFinalized(); }