File: GenerateDataClass.cs

package info (click to toggle)
monodevelop-database 4.0.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,216 kB
  • ctags: 4,147
  • sloc: cs: 25,913; xml: 4,043; makefile: 792; sh: 621
file content (193 lines) | stat: -rw-r--r-- 8,204 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// 
// GenerateDataClass.cs
//  
// Author:
//       Luciano N. Callero <lnc19@hotmail.com>
// 
// Copyright (c) 2010 Lucian0
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using Gtk;
using System;
using System.IO;
using MonoDevelop.Database.Sql;
using MonoDevelop.Projects;

namespace MonoDevelop.Database.CodeGenerator
{
	public partial class GenerateDataClass : Gtk.Dialog
	{
		
		public GenerateDataClass ()
		{
			this.Build ();
			if (comboProject.SelectedProject != null)
				OnComboProjectChanged (comboProject, new EventArgs ());
		}
		
		protected virtual void OnComboProviderConnectionSelectedDatabaseChanged (object sender, System.EventArgs e)
		{
			
			DatabaseConnectionContext ctx = comboProviderConnection.DatabaseConnection;
			if (ctx != null) {
				if (!(ctx.DbFactory is IDbLinq)){
					labelMessage.Markup = string.Concat ("<span color=\"red\">", 
					                                     AddinCatalog.GetString (string.Concat ("The <b>selected ",
					                                                                            "connection</b> does ",
					                                                                            "not have a valid ",
					                                                                            "provider to complete ",
					                                                                            "this operation.")),
					                                     "</span>");
				} else {
					checkSprocs.Sensitive = ((IDbLinq)ctx.DbFactory).HasProcedures;
					labelMessage.Markup = "";
				}
			}
			Validate ();
		}
		
		public void GenerateClass ()
		{
			string output;
			
			if (comboProject.SelectedDirectory != null)
				output = System.IO.Path.Combine (comboProject.SelectedDirectory, entryFile.Text);
			else {
				FileInfo info = new FileInfo (comboProject.SelectedProject.FileName);
				output = System.IO.Path.Combine (info.Directory.FullName, entryFile.Text);
			}
			bool generated = false;
			DatabaseConnectionContext ctx = comboProviderConnection.DatabaseConnection;
			if (comboOutput.ActiveText.Equals (AddinCatalog.GetString ("Code"), 
			                                   StringComparison.InvariantCultureIgnoreCase) || 
			    comboOutput.ActiveText.Equals (AddinCatalog.GetString ("Code & DBML"), 
			                                   StringComparison.InvariantCultureIgnoreCase) )
				generated = (ctx.DbFactory as IDbLinq).Generate (ctx.ConnectionSettings,
                                     comboOutput.ActiveText,
                                     output,
				                     comboLanguage.ActiveText,
				                     comboStyle.ActiveText,
                                     entryNamespace.Text,
                                     entryEntityBase.Text,
                                     entryEntityAttr.Text,
                                     entryMemberAttr.Text,
                                     entryGenerateType.Text,
                                     entryCulture.Text,
                                     checkSchema.Active,
                                     checkGenerateTimestamps.Active,
                                     checkEqualsAndHash.Active,
                                     checkSprocs.Active,
                                     checkPluralize.Active);
			else
				generated = (ctx.DbFactory as IDbLinq).Generate (ctx.ConnectionSettings,
                                     comboOutput.ActiveText,
                                     output,
                                     entryNamespace.Text,
                                     entryEntityBase.Text,
                                     entryEntityAttr.Text,	
                                     entryMemberAttr.Text,
                                     entryGenerateType.Text,
                                     entryCulture.Text,
                                     checkSchema.Active,
                                     checkGenerateTimestamps.Active,
                                     checkEqualsAndHash.Active,
                                     checkSprocs.Active,
                                     checkPluralize.Active);

			if (generated) {
				comboProject.SelectedProject.AddFile (output);
					// Add references to the project if they do not exist
					string[] references = { 
						"System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
						"System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
					};
				ProjectReference gacRef;
				DotNetProject project = ((DotNetProject)comboProject.SelectedProject);
				foreach(string refName in references) {
					string targetName = project.TargetRuntime.AssemblyContext.GetAssemblyNameForVersion (refName, null, project.TargetFramework);
					if (targetName != null) {
						gacRef = new ProjectReference (ReferenceType.Gac, refName);
						if (!project.References.Contains (gacRef))
							project.References.Add (gacRef);
					}
				}
			}
		}
		
		protected void Validate ()
		{
			if (comboProviderConnection.DatabaseConnection == null)
				buttonOk.Sensitive = false;
			else if (!(comboProviderConnection.DatabaseConnection.DbFactory is IDbLinq)
			    || comboProject.SelectedProject == null)
				buttonOk.Sensitive = false;
			else
				buttonOk.Sensitive = true;
			
		}
		
		protected virtual void OnComboOutputChanged (object sender, System.EventArgs e)
		{
			if (comboOutput.Active == 0 || comboOutput.Active == 2) {
				comboLanguage.Sensitive = true;
				comboStyle.Sensitive = true;
				comboLanguage.Active = 0;
				comboStyle.Active = 0;
			} else {
				entryFile.Text = string.Concat(System.IO.Path.GetFileNameWithoutExtension (entryFile.Text), ".dbml");
				comboLanguage.Sensitive = false;
				comboStyle.Sensitive = false;
				comboLanguage.Active = -1;
				comboStyle.Active = -1;
			}
			Validate ();
		}
		
		protected virtual void OnComboLanguageChanged (object sender, System.EventArgs e)
		{
			if (comboLanguage.ActiveText == "VB")
				entryFile.Text = string.Concat(System.IO.Path.GetFileNameWithoutExtension (entryFile.Text), ".vb");
			else if (comboLanguage.ActiveText != null)
				entryFile.Text = string.Concat(System.IO.Path.GetFileNameWithoutExtension (entryFile.Text), ".cs");
			Validate ();
		}
		
		protected virtual void OnComboProjectChanged (object sender, System.EventArgs e)
		{
			if (comboProject.SelectedProject != null)
				if (comboProject.SelectedDirectory != null)
					entryNamespace.Text = string.Concat (((DotNetProject)comboProject.SelectedProject).DefaultNamespace, 
					                                ".",
					                                GetNamespace(comboProject.SelectedProject.ItemDirectory.FullPath, 
					                                                  comboProject.SelectedDirectory), ".DbLinq");
			else
				entryNamespace.Text = string.Concat (((DotNetProject)comboProject.SelectedProject).DefaultNamespace,
				                                     ".DbLinq");
			Validate ();
		}
		
		private string GetNamespace (string projectPath, string folder)
		{
			return folder.Substring (projectPath.Length+1).Replace ('/', '.');
		}
		
		
	}
}