Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

717 рядки
40KB

  1. Const IncludeDirectory = "F:\Development\VBS Includes\"
  2. includeFile "adovbs"
  3. includeFile "CLogger"
  4. includeFile "Public_Functions"
  5. includeFile "VBSEnvironment"
  6. includeFile "Database_Class"
  7. includeFile "lib.collections"
  8. Dim props,fieldNamesArray
  9. Dim oConn,fso,includedFile,rs
  10. Dim fixedTableName,tableName
  11. Dim PrimaryKeyDictionary
  12. Set oConn = WScript.CreateObject("ADODB.Connection")
  13. Set fso = WScript.CreateObject("Scripting.Filesystemobject")
  14. oConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Development\Tracking_Kits\Data\webdata.mdb"
  15. 'oConn.ConnectionString = "Provider=SQLOLEDB;Data Source=danielsubuntu,15789;Initial Catalog=northwind;User Id=sa;Password=SunBrightShine!;"
  16. Init
  17. fixedTableName = Replace(rs("TABLE_NAME")," ","_")
  18. tableName = rs("TABLE_NAME")
  19. Do While Not rs.EOF
  20. fixedTableName = Replace(rs("TABLE_NAME")," ","_")
  21. tableName = rs("TABLE_NAME")
  22. 'If tableName <> "meta_migrations" Then
  23. If tableName <> "meta_migrations" AND tableName = "KitLabels" Then
  24. debug.WriteLine(tableName)
  25. If Not fso.FolderExists(ScriptDirectory()& "Controllers\"& fixedTableName) Then fso.CreateFolder(ScriptDirectory()& "Controllers\"& fixedTableName)
  26. If Not fso.FolderExists(ScriptDirectory()& "Controllers\") Then fso.CreateFolder(ScriptDirectory()& "Controllers\")
  27. If Not fso.FolderExists(ScriptDirectory()& "Views\" & Replace(tableName," ","_")) Then fso.CreateFolder(ScriptDirectory()& "Views\" & Replace(tableName," ","_"))
  28. Set file = fso.CreateTextFile(ScriptDirectory()& "DomainModels\" & fixedTableName & "Repository.asp",True)
  29. includedFile.WriteLine("<!--#include file=""DomainModels/"& fixedTableName & "Repository.asp""-->")
  30. file.WriteLine("<%")
  31. file.WriteLine("'=======================================================================================================================")
  32. file.WriteLine("' " & fixedTableName & " Model")
  33. file.WriteLine("'=======================================================================================================================")
  34. file.WriteLine("")
  35. file.WriteLine("Class " & fixedTableName & "Model_Class")
  36. file.WriteLine(" Public Validator")
  37. file.WriteLine(" Public Class_Get_Properties")
  38. file.WriteLine("")
  39. InitFieldNameIterator ' This Sub is werid and needs to be looked at
  40. fieldNamesArray = props.toArray()
  41. file.WriteLine("")
  42. file.WriteLine(" Private Sub Class_Initialize")
  43. file.WriteLine(" 'ValidateExitsts Me, """",""""")
  44. file.WriteLine(" Class_Get_Properties = Array(""" & Join (fieldNamesArray,", ") & """)")
  45. file.WriteLine(" End Sub")
  46. file.WriteLine("")
  47. file.WriteLine("End CLass")
  48. file.WriteLine("")
  49. file.WriteLine("'=======================================================================================================================")
  50. file.WriteLine("' " & tableName & " Repository")
  51. file.WriteLine("'=======================================================================================================================")
  52. file.WriteLine("")
  53. file.WriteLine("Class " & fixedTableName & "Repository_Class")
  54. file.WriteLine("")
  55. file.WriteLine(" Public Function FindBy" & PrimaryKeyDictionary(tableName) & "("& PrimaryKeyDictionary(tableName) & ")")
  56. file.WriteLine(" dim sql : sql = ""Select [" & Join (fieldNamesArray,"], [") & "] FROM [" & tableName & "] WHERE " & PrimaryKeyDictionary(tableName) & " = ?""")
  57. file.WriteLine(" dim rs : set rs = DAL.Query(sql," & PrimaryKeyDictionary(tableName) & ")")
  58. File.WriteLine(" If rs.EOF then")
  59. File.WriteLine(" Err.Raise 1, """ & fixedTableName & "Repository_Class"", "& fixedTableName &"NotFoundException(""" & PrimaryKeyDictionary(tableName) &""", "& PrimaryKeyDictionary(tableName) & ")")
  60. File.WriteLine(" Else")
  61. File.WriteLine(" set FindBy" & PrimaryKeyDictionary(tableName) & " = Automapper.AutoMap(rs,""" & tableName & "Model_Class" & """)")
  62. file.WriteLine(" End If")
  63. file.WriteLine(" End Function")
  64. file.WriteLine("")
  65. file.WriteLine(" Public Function GetAll(orderBy)")
  66. file.WriteLine(" set GetAll = Find(empty,orderBy)")
  67. file.WriteLine(" End Function")
  68. file.WriteLine("")
  69. file.WriteLine(" Public Function Find(where_kvarray, order_string_or_array)")
  70. file.WriteLine(" dim sql : sql = ""Select [" & Join (fieldNamesArray,"], [") & "] FROM [" & tableName & "]""")
  71. File.WriteLine("")
  72. File.WriteLine(" If Not IsEmpty(where_kvarray) then")
  73. File.WriteLine(" sql = sql & "" WHERE """)
  74. File.WriteLine(" dim where_keys, where_values")
  75. File.WriteLine(" KVUnzip where_kvarray, where_keys, where_values")
  76. file.WriteLine("")
  77. file.WriteLine(" dim i")
  78. file.WriteLine(" For i = 0 to UBound(where_keys)")
  79. file.WriteLine(" If i > 0 then sql = sql & "" AND """)
  80. file.WriteLine(" sql = sql & "" "" & where_keys(i) & "" """)
  81. file.WriteLine(" Next")
  82. file.WriteLine(" End If")
  83. file.WriteLine("")
  84. file.WriteLine(" If Not IsEmpty(order_string_or_array) then")
  85. file.WriteLine(" sql = sql & ""ORDER BY """)
  86. file.WriteLine(" If IsArray(order_string_or_array) then")
  87. file.WriteLine(" dim order_array : order_array = order_string_or_array")
  88. file.WriteLine(" For i = 0 to UBound(order_array)")
  89. file.WriteLine(" If i > 0 then sql = sql & "", """)
  90. file.WriteLine(" sql = sql & "" "" & order_array(i)")
  91. file.WriteLine(" Next")
  92. file.WriteLine(" Else")
  93. file.WriteLine(" sql = sql & order_string_or_array & "" """)
  94. file.WriteLine(" End If")
  95. file.WriteLine(" End If")
  96. file.WriteLine("")
  97. file.WriteLine(" dim rs : set rs = DAL.Query(sql, where_values)")
  98. file.WriteLine(" set Find = " & fixedTableName & "List(rs)")
  99. file.WriteLine(" Destroy rs")
  100. file.WriteLine(" End Function")
  101. file.WriteLine("")
  102. file.WriteLine(" Public Function FindPaged(where_kvarray, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count)")
  103. file.WriteLine(" dim sql : sql = ""Select [" & Join (fieldNamesArray,"], [") & "] FROM [" & tableName & "]""")
  104. File.WriteLine("")
  105. File.WriteLine(" If Not IsEmpty(where_kvarray) then")
  106. File.WriteLine(" sql = sql & "" WHERE """)
  107. File.WriteLine(" dim where_keys, where_values")
  108. File.WriteLine(" KVUnzip where_kvarray, where_keys, where_values")
  109. file.WriteLine("")
  110. file.WriteLine(" dim i")
  111. file.WriteLine(" For i = 0 to UBound(where_keys)")
  112. file.WriteLine(" If i > 0 then sql = sql & "" AND """)
  113. file.WriteLine(" sql = sql & "" "" & where_keys(i) & "" """)
  114. file.WriteLine(" Next")
  115. file.WriteLine(" End If")
  116. file.WriteLine("")
  117. file.WriteLine(" If Not IsEmpty(order_string_or_array) then")
  118. file.WriteLine(" sql = sql & ""ORDER BY """)
  119. file.WriteLine(" If IsArray(order_string_or_array) then")
  120. file.WriteLine(" dim order_array : order_array = order_string_or_array")
  121. file.WriteLine(" For i = 0 to UBound(order_array)")
  122. file.WriteLine(" If i > 0 then sql = sql & "", """)
  123. file.WriteLine(" sql = sql & "" "" & order_array(i)")
  124. file.WriteLine(" Next")
  125. file.WriteLine(" Else")
  126. file.WriteLine(" sql = sql & order_string_or_array & "" """)
  127. file.WriteLine(" End If")
  128. file.WriteLine(" End If")
  129. file.WriteLine("")
  130. file.WriteLine(" dim list : set list = new LinkedList_Class")
  131. file.WriteLine(" dim rs : set rs = DAL.PagedQuery(sql, where_values, per_page, page_num)")
  132. file.WriteLine("")
  133. file.WriteLine(" If Not rs.EOF and Not (IsEmpty(per_page) and IsEmpty(page_num) and IsEmpty(page_count) and IsEmpty(record_count)) then")
  134. file.WriteLine(" rs.PageSize = per_page")
  135. file.WriteLine(" rs.AbsolutePage = page_num")
  136. file.WriteLine(" page_count = rs.PageCount")
  137. file.WriteLine(" record_count = rs.RecordCount")
  138. file.WriteLine(" End If")
  139. file.WriteLine("")
  140. file.WriteLine(" set FindPaged = Paged"& fixedTableName & "List(rs, per_page)")
  141. file.WriteLine(" Destroy rs")
  142. file.WriteLine(" End Function")
  143. file.WriteLine("")
  144. file.WriteLine(" Public Function SearchTablePaged(where_kvarray, order_string_or_array, per_page, page_num, ByRef page_count, ByRef record_count)")
  145. file.WriteLine(" dim sql : sql = ""Select [" & Join (fieldNamesArray,"], [") & "] FROM [" & tableName & "]""")
  146. File.WriteLine("")
  147. File.WriteLine(" If Not IsEmpty(where_kvarray) then")
  148. File.WriteLine(" sql = sql & "" WHERE """)
  149. File.WriteLine(" dim where_keys, where_values")
  150. File.WriteLine(" KVUnzip where_kvarray, where_keys, where_values")
  151. file.WriteLine("")
  152. file.WriteLine(" dim i")
  153. file.WriteLine(" For i = 0 to UBound(where_keys)")
  154. file.WriteLine(" If i > 0 then sql = sql & "" OR""")
  155. file.WriteLine(" sql = sql & "" "" & where_keys(i) & "" LIKE ?""")
  156. file.WriteLine(" Next")
  157. file.WriteLine(" End If")
  158. file.WriteLine("")
  159. file.WriteLine(" If Not IsEmpty(order_string_or_array) then")
  160. file.WriteLine(" sql = sql & "" ORDER BY """)
  161. file.WriteLine(" If IsArray(order_string_or_array) then")
  162. file.WriteLine(" dim order_array : order_array = order_string_or_array")
  163. file.WriteLine(" For i = 0 to UBound(order_array)")
  164. file.WriteLine(" If i > 0 then sql = sql & "", """)
  165. file.WriteLine(" sql = sql & "" "" & order_array(i)")
  166. file.WriteLine(" Next")
  167. file.WriteLine(" Else")
  168. file.WriteLine(" sql = sql & order_string_or_array & "" """)
  169. file.WriteLine(" End If")
  170. file.WriteLine(" End If")
  171. file.WriteLine("")
  172. file.WriteLine(" dim list : set list = new LinkedList_Class")
  173. file.WriteLine(" dim rs : set rs = DAL.PagedQuery(sql, where_values, per_page, page_num)")
  174. file.WriteLine("")
  175. file.WriteLine(" If Not rs.EOF and Not (IsEmpty(per_page) and IsEmpty(page_num) and IsEmpty(page_count) and IsEmpty(record_count)) then")
  176. file.WriteLine(" rs.PageSize = per_page")
  177. file.WriteLine(" rs.AbsolutePage = page_num")
  178. file.WriteLine(" page_count = rs.PageCount")
  179. file.WriteLine(" record_count = rs.RecordCount")
  180. file.WriteLine(" End If")
  181. file.WriteLine("")
  182. file.WriteLine(" set SearchTablePaged = Paged"& fixedTableName & "List(rs, per_page)")
  183. file.WriteLine(" Destroy rs")
  184. file.WriteLine(" End Function")
  185. file.WriteLine("")
  186. file.WriteLine(" Private Function Paged"& fixedTableName & "List(rs, per_page)")
  187. file.WriteLine(" dim list : set list = new LinkedList_Class")
  188. file.WriteLine("")
  189. file.WriteLine(" dim x : x =0")
  190. file.WriteLine(" Do While x < per_page and Not rs.EOF")
  191. file.WriteLine(" list.Push Automapper.AutoMap(rs, new " & fixedTableName & "Model_Class" & ")")
  192. file.WriteLine(" x = x +1")
  193. file.WriteLine(" rs.MoveNext")
  194. file.WriteLine(" Loop")
  195. file.WriteLine(" set Paged"& fixedTableName & "List = list")
  196. file.WriteLine(" End Function")
  197. file.WriteLine("")
  198. file.WriteLine(" Private Function " & fixedTableName &"NotFoundException(ByVal field_name, ByVal field_val)")
  199. file.WriteLine(" "& fixedTableName & "NotFoundException = """ & fixedTableName & " was not found with "" & field_name & "" of '"" & field_val & ""'.""")
  200. file.WriteLine(" End Function")
  201. file.WriteLine("")
  202. file.WriteLine(" Private Function " & fixedTableName & "List(rs)")
  203. file.WriteLine(" dim list : set list = new LinkedList_Class")
  204. file.WriteLine(" dim model")
  205. file.WriteLine("")
  206. file.WriteLine(" Do until rs.EOF")
  207. file.WriteLine(" set model = new " & fixedTableName & "Model_Class")
  208. file.WriteLine(" list.Push Automapper.AutoMap(rs, model)")
  209. file.WriteLine(" rs.MoveNext")
  210. file.WriteLine(" Loop")
  211. file.WriteLine(" set " & fixedTableName & "List = list")
  212. file.WriteLine(" End Function")
  213. file.WriteLine("")
  214. file.WriteLine(" Public Sub AddNew(ByRef model)")
  215. file.WriteLine(" dim sql : sql = ""INSERT INTO ["& fixedTableName & "] ("" &_")
  216. For Fields = 1 To UBound(fieldNamesArray)-1
  217. file.WriteLine(" ""[" & fieldNamesArray(Fields) & "],"" &_")
  218. Next
  219. file.WriteLine(" ""[" & fieldNamesArray(UBound(fieldNamesArray)) & "])"" &_")
  220. file.Write(" ""VALUES (?")
  221. For Fields = 1 To UBound(fieldNamesArray)-1
  222. file.Write(",?")
  223. Next
  224. file.Write(")""" & VbCrLf)
  225. file.WriteLine(" DAL.Execute sql, Array(model." & fieldNamesArray(1) & ", _")
  226. For Fields = 2 To UBound(fieldNamesArray) -1
  227. file.WriteLine(" model." & fieldNamesArray(Fields) & ", _")
  228. Next
  229. file.WriteLine(" model." & fieldNamesArray(UBound(fieldNamesArray)) & ")")
  230. file.WriteLine(" sql = ""SELECT TOP 1 " & PrimaryKeyDictionary(tableName) & " FROM [" & fixedTableName & "] ORDER BY "& PrimaryKeyDictionary(tableName) & " DESC""")
  231. file.WriteLine(" dim rs : set rs = DAL.Query(sql, empty)")
  232. file.WriteLine(" model." & PrimaryKeyDictionary(tableName) & " = rs(""" & PrimaryKeyDictionary(tableName) & """)")
  233. file.WriteLine(" Destroy rs")
  234. file.WriteLine(" End Sub")
  235. file.WriteLine("")
  236. file.WriteLine(" Public Sub Update(model)")
  237. file.WriteLine(" dim sql : sql = ""UPDATE ["& fixedTableName & "] SET [" & fieldNamesArray(1) & "] = ?,"" &_")
  238. For Fields = 2 To UBound(fieldNamesArray) - 1
  239. file.WriteLine(" ""[" & fieldNamesArray(Fields) & "] = ?,"" &_")
  240. Next
  241. file.Write (" ""[" & fieldNamesArray(UBound(fieldNamesArray)) & "] = ?"" &_" & VbCrLf)
  242. file.WriteLine(" "" WHERE ["& PrimaryKeyDictionary(tableName) & "] = ?""")
  243. file.WriteLine("")
  244. file.WriteLine(" DAL.Execute sql, Array(model." & fieldNamesArray(1) & ", _")
  245. For Fields = 2 To UBound(fieldNamesArray)
  246. file.WriteLine(" model." & fieldNamesArray(Fields) & ", _")
  247. Next
  248. file.Write(" model." & PrimaryKeyDictionary(tableName) & ")" & VbCrLf)
  249. file.WriteLine(" End Sub")
  250. file.WriteLine("")
  251. file.WriteLine(" Public Sub Delete(id)")
  252. file.WriteLine(" dim sql : sql = ""DELETE FROM ["& fixedTableName & "] WHERE ["& PrimaryKeyDictionary(tableName) & "] = ?""")
  253. file.WriteLine(" DAL.Execute sql, id")
  254. file.WriteLine(" End Sub")
  255. file.WriteLine("End Class")
  256. file.WriteLine("")
  257. File.WriteLine("dim " & fixedTableName & "Repository__Singleton")
  258. File.WriteLine("Function " & fixedTableName & "Repository()")
  259. File.WriteLine(" If IsEmpty(" & fixedTableName & "Repository__Singleton) then")
  260. file.WriteLine(" set " & fixedTableName & "Repository__Singleton = new " & fixedTableName & "Repository_Class")
  261. file.WriteLine(" End If")
  262. file.WriteLine(" set " & fixedTableName & "Repository = " & fixedTableName & "Repository__Singleton")
  263. file.WriteLine("End Function")
  264. file.WriteLine("%>")
  265. file.Close
  266. Set file = fso.CreateTextFile(ScriptDirectory()& "ViewModels\" & fixedTableName & "ViewModels.asp",True)
  267. file.WriteLine("<%")
  268. file.WriteLine("Class PagedIndex_ViewModel_Class")
  269. file.WriteLine(" Public Title")
  270. file.WriteLine(" Public " & fixedTableName)
  271. file.WriteLine("")
  272. file.WriteLine(" Public CurrentPageNumber")
  273. file.WriteLine(" Public PageSize")
  274. file.WriteLine(" Public PageCount")
  275. file.WriteLine(" Public RecordCount")
  276. file.WriteLine("End Class")
  277. file.WriteLine("")
  278. file.WriteLine("Class Edit_ViewModel_Class")
  279. file.WriteLine(" Public Title")
  280. file.WriteLine(" Public " & fixedTableName)
  281. file.WriteLine("")
  282. file.WriteLine("End Class")
  283. file.WriteLine("")
  284. file.WriteLine("Class Create_ViewModel_Class")
  285. For Each fieldName In fieldNamesArray
  286. If Not fieldName = PrimaryKeyDictionary(tableName) then
  287. file.WriteLine(" Public " & fieldName)
  288. End If
  289. Next
  290. file.WriteLine("End Class")
  291. file.WriteLine("")
  292. file.WriteLine("Class Delete_ViewModel_Class")
  293. file.WriteLine(" Public Title")
  294. file.WriteLine(" Public " & fixedTableName )
  295. file.WriteLine("End Class")
  296. file.WriteLine("")
  297. file.WriteLine("%>")
  298. file.Close
  299. Set file = fso.CreateTextFile(ScriptDirectory()& "Controllers\" & fixedTableName & "\" & fixedTableName & "Controller.asp",True)
  300. file.WriteLine("<% Option Explicit %>")
  301. file.WriteLine("<!--#include file=""../../include_all.asp""-->")
  302. file.WriteLine("<!--#include file=""../../ViewModels/" & fixedTableName & "ViewModels.asp""-->")
  303. file.WriteLine("<%")
  304. file.WriteLine("Class " & fixedTableName & "Controller")
  305. file.WriteLine(" Public Model")
  306. file.WriteLine("")
  307. file.WriteLine(" Public Sub Index")
  308. file.WriteLine("")
  309. file.WriteLine(" dim page_size : page_size = 10")
  310. file.WriteLine(" dim page_num : page_num = Choice(Len(Request.Querystring(""page_num"")) > 0, Request.Querystring(""page_num""), 1)")
  311. file.WriteLine(" dim page_count, record_count")
  312. file.WriteLine(" set Model = new PagedIndex_ViewModel_Class")
  313. file.WriteLine(" Model.Title = """ & tableName & """")
  314. file.WriteLine(" set Model." & fixedTableName & " = " & fixedTableName & "Repository.FindPaged(empty, """ & PrimaryKeyDictionary(tableName) & """, page_size, page_num, page_count, record_count)")
  315. file.WriteLine(" Model.CurrentPageNumber = page_num")
  316. file.WriteLine(" Model.PageSize = page_size")
  317. file.WriteLine(" Model.PageCount = page_count")
  318. file.WriteLine(" Model.RecordCount = record_count")
  319. file.WriteLine(" %> <!--#include file=""../../Views/" & fixedTableName & "/Index.asp""--> <%")
  320. file.WriteLine("")
  321. file.WriteLine(" End Sub")
  322. file.WriteLine(" Public Sub Search")
  323. file.WriteLine("")
  324. file.WriteLine(" dim searchValue:searchValue = Choice(Request.Form.Count = 0,Request.QueryString(""q""),Request.Form(""q""))")
  325. file.WriteLine(" dim page_size : page_size = 10")
  326. file.WriteLine(" dim page_num : page_num = Choice(Len(Request.Querystring(""page_num"")) > 0, Request.Querystring(""page_num""), 1)")
  327. file.WriteLine(" dim page_count, record_count")
  328. file.WriteLine(" set Model = new PagedIndex_ViewModel_Class")
  329. file.WriteLine(" Model.Title = """ & tableName & """")
  330. file.WriteLine(" set Model." & fixedTableName & " = " & fixedTableName & "Repository.SearchTablePaged(Array(""" & Join (fieldNamesArray,""",""%"" & searchValue & ""%"",""") & """,""%"" & searchValue & ""%""),""" & PrimaryKeyDictionary(tableName) & """, page_size, page_num, page_count, record_count)")
  331. file.WriteLine(" Model.CurrentPageNumber = page_num")
  332. file.WriteLine(" Model.PageSize = page_size")
  333. file.WriteLine(" Model.PageCount = page_count")
  334. file.WriteLine(" Model.RecordCount = record_count")
  335. file.WriteLine(" %> <!--#include file=""../../Views/" & fixedTableName & "/Index.asp""--> <%")
  336. file.WriteLine("")
  337. file.WriteLine(" End Sub")
  338. file.WriteLine("")
  339. file.WriteLine(" Public Sub Edit")
  340. file.WriteLine("")
  341. file.WriteLine(" dim id : id = Request.QueryString(""Id"")")
  342. file.WriteLine(" set Model = new Edit_ViewModel_Class")
  343. file.WriteLine(" set Model." & fixedTableName & " = " & fixedTableName & "Repository.FindBy" & PrimaryKeyDictionary(tableName) & "(id)")
  344. file.WriteLine(" Model.Title = ""Edit " & fixedTableName & """")
  345. file.WriteLine(" HTMLSecurity.SetAntiCSRFToken """ & fixedTableName & "EditForm""")
  346. file.WriteLine(" %> <!--#include file=""../../Views/"& fixedTableName &"/Edit.asp""--> <%")
  347. file.WriteLine(" Flash.Success = """ & fixedTableName & " " & id & " Updated.""")
  348. file.WriteLine(" End Sub")
  349. file.WriteLine("")
  350. file.WriteLine(" Public Sub EditPost")
  351. file.WriteLine(" MVC.RequirePost")
  352. File.WriteLine(" HTMLSecurity.OnInvalidAntiCsrfTokenRedirectToActionExt """ & fixedTableName & "EditForm"", Request.Form(""nonce""), ""Edit"", Array(""Id"", Request.Form(""Id""))")
  353. File.WriteLine(" dim " & PrimaryKeyDictionary(tableName) & " : " & PrimaryKeyDictionary(tableName) & " = Request.Form(""Id"")")
  354. File.WriteLine(" dim model : set model = " & fixedTableName & "Repository.FindBy" & PrimaryKeyDictionary(tableName) & "(" & PrimaryKeyDictionary(tableName) & ")")
  355. File.WriteLine(" set model = Automapper.AutoMap(Request.Form, model)")
  356. File.WriteLine(" 'model.Validate")
  357. File.WriteLine(" 'If model.Validator.HasErrors then")
  358. File.WriteLine(" FormCache.SerializeForm ""Edit" & fixedTableName & """, Request.Form")
  359. File.WriteLine(" ' Flash.Errors = model.Validator.Errors")
  360. File.WriteLine(" ' MVC.RedirectToActionExt ""Edit"", Array(""Id""," & PrimaryKeyDictionary(tableName) & ")")
  361. File.WriteLine(" 'Else")
  362. File.WriteLine(" " & fixedTableName & "Repository.Update model")
  363. File.WriteLine(" FormCache.ClearForm ""Edit" & fixedTableName & """")
  364. File.WriteLine(" Flash.Success = """ & fixedTableName & " updated.""")
  365. File.WriteLine(" MVC.RedirectToAction ""Index""")
  366. File.WriteLine(" 'End If")
  367. file.WriteLine(" End Sub")
  368. file.WriteLine("")
  369. file.WriteLine(" Public Sub Create")
  370. file.WriteLine("")
  371. file.WriteLine(" dim form_params : set form_params = FormCache.DeserializeForm(""New" & fixedTableName & """)")
  372. file.WriteLine(" If Not form_params Is Nothing then")
  373. file.WriteLine(" set Model = Automapper.AutoMap(form_params, New Create_ViewModel_Class)")
  374. file.WriteLine(" Else")
  375. file.WriteLine(" set Model = new Create_ViewModel_Class")
  376. file.WriteLine(" End If")
  377. file.WriteLine("")
  378. file.WriteLine(" HTMLSecurity.SetAntiCSRFToken """ & fixedTableName & "CreateForm""")
  379. file.WriteLine("")
  380. file.WriteLine(" %> <!--#include file=""../../Views/" & fixedTableName & "/Create.asp""--> <%")
  381. file.WriteLine(" End Sub")
  382. file.WriteLine("")
  383. file.WriteLine(" Public Sub CreatePost")
  384. file.WriteLine("")
  385. file.WriteLine(" MVC.RequirePost")
  386. file.WriteLine(" HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction """ & fixedTableName & "CreateForm"", Request.Form(""nonce""), ""Create""")
  387. file.WriteLine("")
  388. file.WriteLine(" dim new_" & fixedTableName & "_model : set new_" & fixedTableName & "_model = Automapper.AutoMap(Request.Form, new " & fixedTableName & "Model_Class)")
  389. file.WriteLine("")
  390. file.WriteLine(" 'new_" & fixedTableName & "_model.Validator.Validate")
  391. file.WriteLine("")
  392. file.WriteLine(" 'If new_" & fixedTableName & "_model.Validator.HasErrors then")
  393. file.WriteLine(" ' FormCache.SerializeForm ""New" & fixedTableName & """, Request.Form")
  394. file.WriteLine(" ' Flash.Errors = new_" & fixedTableName & "_model.Validator.Errors")
  395. file.WriteLine(" ' MVC.RedirectToAction ""Create""")
  396. file.WriteLine(" 'Else")
  397. file.WriteLine(" " & fixedTableName & "Repository.AddNew new_" & fixedTableName & "_model")
  398. File.WriteLine(" ' FormCache.ClearForm ""New" & fixedTableName & """")
  399. File.WriteLine(" Flash.Success = """ & fixedTableName & " added.""")
  400. File.WriteLine(" MVC.RedirectToAction ""Index""")
  401. File.WriteLine(" 'End If")
  402. file.WriteLine(" End Sub")
  403. file.WriteLine("")
  404. file.WriteLine(" Public Sub Delete")
  405. file.WriteLine(" dim id : id = Request.QueryString(""Id"")")
  406. file.WriteLine(" set Model = new Delete_ViewModel_Class")
  407. file.WriteLine(" set Model." & fixedTableName & " = " & fixedTableName & "Repository.FindBy" & PrimaryKeyDictionary(tableName) & "(id)")
  408. file.WriteLine(" Model.Title = ""Delete " & fixedTableName & """")
  409. file.WriteLine("")
  410. file.WriteLine(" HTMLSecurity.SetAntiCSRFToken """ & fixedTableName & "DeleteForm""")
  411. file.WriteLine("")
  412. file.WriteLine(" %> <!--#include file=""../../Views/" & fixedTableName & "/Delete.asp""--> <%")
  413. file.WriteLine(" End Sub")
  414. file.WriteLine("")
  415. file.WriteLine(" Public Sub DeletePost")
  416. file.WriteLine(" MVC.RequirePost")
  417. file.WriteLine(" HtmlSecurity.OnInvalidAntiCSRFTokenRedirectToAction """ & fixedTableName & "DeleteForm"", Request.Form(""nonce""), ""Create""")
  418. file.WriteLine("")
  419. file.WriteLine(" dim id : id = Request.Form(""Id"")")
  420. file.WriteLine(" " & fixedTableName & "Repository.Delete id")
  421. file.WriteLine("")
  422. file.WriteLine(" Flash.Success = """ & fixedTableName & " deleted.""")
  423. file.WriteLine(" MVC.RedirectToAction ""Index""")
  424. file.WriteLine(" End Sub")
  425. file.WriteLine("")
  426. file.WriteLine("End Class")
  427. file.WriteLine("MVC.Dispatch")
  428. file.WriteLine("%>")
  429. file.Close
  430. Set file = fso.CreateTextFile(ScriptDirectory()& "Views\" & fixedTableName & "\edit.asp",True)
  431. file.WriteLine("<h2><%= H(Model.Title) %></h2>")
  432. file.WriteLine("<%= HTML.FormTag("""& tableName & """, ""EditPost"", empty, empty) %>")
  433. file.WriteLine(" <%= HTML.Hidden(""nonce"", HTMLSecurity.GetAntiCSRFToken(""" & fixedTableName & "EditForm"")) %>")
  434. file.WriteLine(" <%= HTML.Hidden(""Id"", Model." & fixedTableName & "." & PrimaryKeyDictionary(tableName) & ") %>")
  435. For Each field In fieldNamesArray
  436. If Not field = PrimaryKeyDictionary(tableName) then
  437. file.WriteLine(" <div class=""row"">")
  438. file.WriteLine(" <div class=""col-md-4"">")
  439. file.WriteLine(" <div class=""form-group"">")
  440. file.WriteLine(" <label for=""" & field & """>" & field & "</label>")
  441. file.WriteLine(" <%= HTML.TextboxExt(""" & field & """, Model." & fixedTableName & "." & field & ", Array(""class"", ""form-control"")) %>")
  442. file.WriteLine(" </div>")
  443. file.WriteLine(" </div>")
  444. file.WriteLine(" </div>")
  445. End if
  446. Next
  447. file.WriteLine("<hr />")
  448. file.WriteLine("<div class=""form-group"">")
  449. file.WriteLine(" <% = HTML.Button(""submit"", ""<i class='glyphicon glyphicon-ok'></i> Save"", ""btn-primary"") %>")
  450. file.WriteLine(" &nbsp;&nbsp;")
  451. file.WriteLine(" <%= HTML.LinkToExt(""<i class='glyphicon glyphicon-remove'></i> Delete"", """ & fixedTableName & """, ""Delete"", Array(""id"", Model." & fixedTableName & "." & PrimaryKeyDictionary(tableName) & "), Array(""class"", ""btn btn-danger"")) %>")
  452. file.WriteLine(" &nbsp;&nbsp;")
  453. file.WriteLine(" <%= HTML.LinkToExt(""Cancel"", """ & fixedTableName & """, ""Index"", empty, Array(""class"", ""btn btn-default"")) %>")
  454. file.WriteLine("</div>")
  455. file.WriteLine("</form>")
  456. file.Close
  457. Set file = fso.CreateTextFile(ScriptDirectory()& "Views\" & fixedTableName & "\index.asp",True)
  458. file.WriteLine("<h2><%= H(Model.Title) %></h2>")
  459. file.WriteLine("<div class=""row"">")
  460. file.WriteLine(" <div class=""col-md-8 col-sm-8 col-xs-12"">")
  461. file.WriteLine(" <%= H(Model.RecordCount) %> " & tableName & " found. Showing <%= H(Model.PageSize) %> records per page.")
  462. file.WriteLine(" <%= HTML.LinkToExt(""<i class='bi bi-plus-square-fill'></i> New"",""" & fixedTableName & """, ""Create"", empty, Array(""class"", ""btn btn-xs btn-primary"")) %>")
  463. file.WriteLine(" </div>")
  464. file.WriteLine(" <div class=""col-md-4 col-sm-4 col-xs-12"">")
  465. file.WriteLine("")
  466. file.WriteLine(" <%= HTML.FormTag(""" & fixedTableName & """, ""Search"", empty, empty) %>")
  467. file.WriteLine("")
  468. file.WriteLine(" <div class=""col-md-10 col-sm-10 col-xs-12"">")
  469. file.WriteLine(" <label class=""sr-only"" for=""search"">Search</label>")
  470. file.WriteLine(" <div class=""input-group"">")
  471. file.WriteLine(" <input type=""text"" class=""form-control input-search"" value='<%= Choice(Request.Form.Count = 0,Request.QueryString(""q""),Request.Form(""q"")) %>' name=""q"" id=""search"" placeholder=""Search"">")
  472. file.WriteLine(" <span class=""input-group-addon group-icon""><span class=""glyphicon glyphicon-eye-open""></span>")
  473. file.WriteLine(" <button type=""submit"" class=""btn btn-success""><i class=""bi bi-search""></i>Search</buttton>")
  474. file.WriteLine(" </div>")
  475. file.WriteLine(" </div>")
  476. file.WriteLine("")
  477. file.WriteLine(" </form>")
  478. file.WriteLine(" </div>")
  479. file.WriteLine("</div>")
  480. file.WriteLine("<table id=""" & fixedTableName & """ class=""table table-striped"">")
  481. file.WriteLine(" <thead>")
  482. file.WriteLine(" <tr>")
  483. file.WriteLine(" <th></th>")
  484. For Each field In fieldNamesArray
  485. file.WriteLine(" <th style=""text-align: left"">" & field & "</th>")
  486. Next
  487. file.WriteLine(" <th></th>")
  488. file.WriteLine(" </tr>")
  489. file.WriteLine(" </thead>")
  490. file.WriteLine("<tbody>")
  491. file.WriteLine(" <% dim it : set it = Model." & fixedTableName & ".Iterator %>")
  492. file.WriteLine(" <% dim " & fixedTableName & " %>")
  493. file.WriteLine(" <% While it.HasNext %>")
  494. file.WriteLine(" <% set " & fixedTableName & " = it.GetNext() %>")
  495. file.WriteLine(" <tr>")
  496. file.WriteLine(" <td>")
  497. file.WriteLine(" <%= HTML.LinkToExt(""<i class='bi bi-search'></i>"", """& fixedTableName &""", ""Edit"", Array(""Id"", " & fixedTableName & "." & PrimaryKeyDictionary(tableName) & "), Array(""class"", ""btn btn-primary"")) %>")
  498. file.WriteLine(" </td>")
  499. For Each field In fieldNamesArray
  500. file.WriteLine(" <td><% = H(" & fixedTableName & "." & field &") %></td>")
  501. Next
  502. file.WriteLine("</tr>")
  503. file.WriteLine(" <% Wend %>")
  504. file.WriteLine(" </tbody>")
  505. file.WriteLine("</table>")
  506. file.WriteLine(" <div>")
  507. file.WriteLine(" <% If Model.CurrentPageNumber <> 1 then %>")
  508. file.WriteLine(" <%= HTML.LinkToExt(""<i class='bi bi-chevron-left'></i><i class='bi bi-chevron-left'></i>"", MVC.ControllerName, MVC.ActionName, Array(""page_num"", 1,""q"", Choice(Request.Form.Count = 0,Request.QueryString(""q""),Request.Form(""q""))), Array(""class"", ""btn btn-default"")) %>")
  509. file.WriteLine(" &nbsp;")
  510. file.WriteLine(" <%= HTML.LinkToExt(""<i class='bi bi-chevron-left'></i>"", MVC.ControllerName, MVC.ActionName, Array(""page_num"", Model.CurrentPageNumber - 1,""q"", Choice(Request.Form.Count = 0,Request.QueryString(""q""),Request.Form(""q""))), Array(""class"", ""btn btn-default"")) %>")
  511. file.WriteLine("&nbsp;")
  512. file.WriteLine(" <% Else %>")
  513. file.WriteLine(" <a class='btn btn-default disabled'><i class='bi bi-chevron-left'></i><i class='bi bi-chevron-left'></i></a>")
  514. file.WriteLine(" &nbsp;")
  515. file.WriteLine(" <a class='btn btn-default disabled'><i class='bi bi-chevron-left'></i></a>")
  516. file.WriteLine(" &nbsp; ")
  517. file.WriteLine(" <% End If %>")
  518. file.WriteLine(" <% If CInt(Model.CurrentPageNumber) < CInt(Model.PageCount) then %>")
  519. file.WriteLine(" <%= HTML.LinkToExt(""<i class='bi bi-chevron-right'></i>"", MVC.ControllerName, MVC.ActionName, Array(""page_num"", Model.CurrentPageNumber + 1,""q"", Choice(Request.Form.Count = 0,Request.QueryString(""q""),Request.Form(""q""))), Array(""class"", ""btn btn-default"")) %>")
  520. file.WriteLine(" &nbsp;")
  521. file.WriteLine(" <%= HTML.LinkToExt(""<i class='bi bi-chevron-right'></i><i class='bi bi-chevron-right'></i>"", MVC.ControllerName, MVC.ActionName, Array(""page_num"", Model.PageCount,""q"", Choice(Request.Form.Count = 0,Request.QueryString(""q""),Request.Form(""q""))), Array(""class"", ""btn btn-default"")) %>")
  522. file.WriteLine(" &nbsp;")
  523. file.WriteLine(" <% Else %>")
  524. file.WriteLine(" <a class='btn btn-default disabled'><i class='bi bi-chevron-right'></i><i class='bi bi-chevron-right'></i></a>")
  525. file.WriteLine(" &nbsp;")
  526. file.WriteLine(" <a class='btn btn-default disabled'><i class='bi bi-chevron-right'></i></a>")
  527. file.WriteLine(" &nbsp; ")
  528. file.WriteLine("<% End If %>")
  529. file.WriteLine("</div>")
  530. file.WriteLine("</div>")
  531. file.Close
  532. Set file = fso.CreateTextFile(ScriptDirectory()& "Views\" & fixedTableName & "\create.asp",True)
  533. file.WriteLine("<h2>Create " & fixedTableName &"</h2>")
  534. file.WriteLine("")
  535. file.WriteLine("<%= HTML.FormTag(""" & fixedTableName &""", ""CreatePost"", empty, empty) %>")
  536. file.WriteLine("<%= HTML.Hidden(""nonce"", HTMLSecurity.GetAntiCSRFToken("""& fixedTableName & "CreateForm"")) %>")
  537. file.WriteLine("<hr />")
  538. file.WriteLine("")
  539. file.WriteLine("<div class=""form-group"">")
  540. For Each field In fieldNamesArray
  541. If Not field = PrimaryKeyDictionary(tableName) then
  542. file.WriteLine(" <div class=""row"">")
  543. file.WriteLine(" <div class=""col-md-4"">")
  544. file.WriteLine(" <div class=""form-group"">")
  545. file.WriteLine(" <label for=""" & field & """>" & field & "</label>")
  546. file.WriteLine(" <%= HTML.TextboxExt(""" & field & """, Model." & field & ", Array(""class"", ""form-control"")) %>")
  547. file.WriteLine(" </div>")
  548. file.WriteLine(" </div>")
  549. file.WriteLine(" </div>")
  550. End if
  551. Next
  552. file.WriteLine(" <%= HTML.Button(""submit"", ""<i class='glyphicon glyphicon-ok'></i> Create"", ""btn-primary"") %>")
  553. file.WriteLine(" &nbsp;&nbsp;")
  554. file.WriteLine(" <%= HTML.LinkToExt(""<i class='glyphicon glyphicon-remove'></i> Cancel"", """ & fixedTableName & """, ""Index"", empty, Array(""class"", ""btn btn-default"")) %>")
  555. file.WriteLine("</div>")
  556. file.WriteLine("")
  557. file.WriteLine("</form>")
  558. file.Close
  559. Set file = fso.CreateTextFile(ScriptDirectory()& "Views\" & fixedTableName & "\delete.asp",True)
  560. file.WriteLine("<h2><%= H(Model.Title) %></h2>")
  561. file.WriteLine("")
  562. file.WriteLine("<p class=""alert alert-danger"">Are you sure you want to delete this " & fixedTableName & "?</p>")
  563. file.WriteLine("")
  564. file.WriteLine("<%= HTML.FormTag(""" & fixedTableName & """, ""DeletePost"", empty, Array(""class"", ""form-horizontal"")) %>")
  565. file.WriteLine("<%= HTML.Hidden(""nonce"", HTMLSecurity.GetAntiCSRFToken(""" & fixedTableName & "DeleteForm"")) %>")
  566. file.WriteLine("<%= HTML.Hidden(""Id"", Model." & fixedTableName & "." & PrimaryKeyDictionary(tableName) & ") %>")
  567. file.WriteLine("<div class=""col-md-10"">")
  568. file.WriteLine("<div class=""form-group"">")
  569. file.WriteLine(" <%= HTML.Button(""submit"", ""<i class='glyphicon glyphicon-remove'></i> Confirm Delete"", ""btn-danger"") %>")
  570. file.WriteLine(" &nbsp;&nbsp;")
  571. file.WriteLine(" <%= HTML.LinkToExt(""Cancel"", """ & fixedTableName & """, ""Index"", empty, Array(""class"", ""btn btn-success"")) %>")
  572. For Each field In fieldNamesArray
  573. If Not field = PrimaryKeyDictionary(tableName) Then
  574. file.WriteLine(" <div class=""row"">")
  575. file.WriteLine(" <div class=""col-md-4"">")
  576. file.WriteLine(" <div class=""form-group"">")
  577. file.WriteLine(" <label for=""" & field & """>" & field & "</label>")
  578. file.WriteLine(" <%= HTML.TextboxExt(""" & field & """, Model." & fixedTableName & "." & field & ", Array(""class"", ""form-control"")) %>")
  579. file.WriteLine(" </div>")
  580. file.WriteLine(" </div>")
  581. file.WriteLine(" </div>")
  582. End If
  583. Next
  584. file.WriteLine(" </div>")
  585. file.WriteLine("</div>")
  586. file.WriteLine("</form>")
  587. file.Close
  588. Set objFile = fso.OpenTextFile(ScriptDirectory() & "Views\Shared\layout.header.asp",1)
  589. headerHtml = objFile.ReadAll()
  590. objFile.Close
  591. dropDownLinks = "<li role=""presentation"" class=""dropdown"">" &_
  592. " <a class=""dropdown-toggle"" data-toggle=""dropdown"" href=""#"" role=""button"" aria-haspopup=""true"" aria-expanded=""false"">" &_
  593. " Dropdown <span class=""caret""></span></a>" &_
  594. ""
  595. If InStr(headerHtml,"<li class=""nav-item""><%= Html.LinkTOExt(""" & tableName & """,""" & fixedTableName & """,""Index"",empty,Array(""Class"",""nav-link"")) %></li>") = 0 then
  596. newHeaderHtml = Replace(headerHtml,"<!--nav bar placeholder-->"," <li class=""nav-item""><%= Html.LinkTOExt(""" & tableName & """,""" & fixedTableName & """,""Index"",empty,Array(""Class"",""nav-link"")) %></li>" & vbCrLf & "<!--nav bar placeholder-->")
  597. Set objFile = fso.OpenTextFile(ScriptDirectory() & "Views\Shared\layout.header.asp",2)
  598. objFile.WriteLine newHeaderHtml
  599. End If
  600. objFile.Close
  601. props.Clear
  602. rs.MoveNext
  603. Else
  604. rs.MoveNext
  605. End if
  606. Loop
  607. includedFile.Close
  608. WScript.Quit
  609. Sub includeFile(fSpec)
  610. executeGlobal CreateObject("Scripting.FileSystemObject").openTextFile(IncludeDirectory & fSpec & ".vbs").readAll()
  611. End Sub
  612. Function PrimaryKeysFromADOConnectionToDictionary(ADOConn)
  613. Dim rstSchema
  614. Dim returnDictionary
  615. If TypeName(ADOConn) = "Connection" Then
  616. Set rstSchema = oConn.OpenSchema(adSchemaPrimaryKeys)
  617. Set rstableNames = oConn.OpenSchema(20,Array(Empty, Empty, Empty, "Table"))
  618. Set returnDictionary = WScript.CreateObject("scripting.dictionary")
  619. While Not rstSchema.EOF
  620. If returnDictionary.Exists(rstSchema.Fields("TABLE_NAME").Value) = false Then
  621. returnDictionary.Add rstSchema.Fields("TABLE_NAME").Value,rstSchema.Fields("COLUMN_NAME").Value
  622. End If
  623. rstSchema.MoveNext
  624. Wend
  625. rstSchema.Close
  626. End If
  627. Set PrimaryKeysFromADOConnectionToDictionary = returnDictionary
  628. End Function
  629. Sub Init
  630. MsgBox(oConn.State)
  631. If oConn.State = 0 Then
  632. oConn.Open()
  633. 'End If
  634. Set PrimaryKeyDictionary = PrimaryKeysFromADOConnectionToDictionary(oConn)
  635. If Not fso.FolderExists(ScriptDirectory()& "DomainModels\") Then fso.CreateFolder(ScriptDirectory()& "DomainModels\")
  636. If Not fso.FolderExists(ScriptDirectory()& "ViewModels\") Then fso.CreateFolder(ScriptDirectory()& "ViewModels\")
  637. If Not fso.FolderExists(ScriptDirectory()& "Controllers\") Then fso.CreateFolder(ScriptDirectory()& "Controllers\")
  638. If Not fso.FolderExists(ScriptDirectory()& "Views\") Then fso.CreateFolder(ScriptDirectory()& "Views\")
  639. Set includedFile = fso.CreateTextFile(ScriptDirectory()& "include_all.asp",True)
  640. includedFile.WriteLine("<!--#include file=""../MVC/lib.all.asp""-->")
  641. includedFile.WriteLine("<!--#include file=""DAL/lib.DAL.asp""-->")
  642. includedFile.WriteLine("<!--#include file=""app.Config.asp""-->")
  643. Set rs = oConn.OpenSchema(20,Array(Empty, Empty, Empty, "Table"))
  644. End If
  645. End Sub
  646. Sub InitFieldNameIterator
  647. Dim cmd
  648. Set cmd = WScript.CreateObject("ADODB.COMMAND")
  649. cmd.ActiveConnection = oConn
  650. cmd.CommandText = "Select * From [" & tableName & "]"
  651. Set FieldsRs = cmd.Execute()
  652. Set fields = FieldsRs.Fields
  653. Set props = New Stack
  654. For Each Field In FieldsRs.Fields
  655. props.Push(Field.Name)
  656. file.WriteLine (" Public " & Field.Name & " '" & Field.Attributes)
  657. Next
  658. FieldsRs.Close
  659. If Not PrimaryKeyDictionary.Exists(tableName) Then
  660. PrimaryKeyDictionary.Add tableName,props.ToArray()(0)
  661. End If
  662. End Sub

Powered by TurnKey Linux.