LinqDataSourceView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. //
  2. // LinqDataSourceView.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc http://novell.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_3_5
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.ComponentModel;
  34. using System.Reflection;
  35. using System.Security.Permissions;
  36. using System.Web.DynamicData;
  37. using System.Web.UI;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. public class LinqDataSourceView : DataSourceView, IStateManager
  43. {
  44. public LinqDataSourceView (LinqDataSource owner, string name, HttpContext context)
  45. : base (owner, name)
  46. {
  47. source = owner;
  48. }
  49. LinqDataSource source;
  50. bool tracking;
  51. ParameterCollection delete_parameters,
  52. insert_parameters,
  53. select_new_parameters,
  54. update_parameters;
  55. ParameterCollection group_by_parameters,
  56. order_by_parameters,
  57. order_group_by_parameters,
  58. where_parameters;
  59. IEnumerable<ParameterCollection> AllParameters {
  60. get {
  61. yield return delete_parameters;
  62. yield return insert_parameters;
  63. yield return select_new_parameters;
  64. yield return update_parameters;
  65. yield return group_by_parameters;
  66. yield return order_by_parameters;
  67. yield return order_group_by_parameters;
  68. yield return where_parameters;
  69. }
  70. }
  71. [MonoTODO]
  72. public bool AutoGenerateOrderByClause { get; set; }
  73. [MonoTODO]
  74. public bool AutoGenerateWhereClause { get; set; }
  75. [MonoTODO]
  76. public bool AutoPage { get; set; }
  77. [MonoTODO]
  78. public bool AutoSort { get; set; }
  79. public override bool CanDelete {
  80. get { return EnableDelete; }
  81. }
  82. public override bool CanInsert {
  83. get { return EnableInsert; }
  84. }
  85. public override bool CanPage {
  86. get { return true; }
  87. }
  88. public override bool CanRetrieveTotalRowCount {
  89. get { return true; }
  90. }
  91. public override bool CanSort {
  92. get { return true; }
  93. }
  94. public override bool CanUpdate {
  95. get { return EnableUpdate; }
  96. }
  97. protected virtual Type ContextType {
  98. get { return ((IDynamicDataSource) source).ContextType; }
  99. }
  100. public virtual string ContextTypeName {
  101. get { return source.ContextTypeName; }
  102. set { source.ContextTypeName = value; }
  103. }
  104. [MonoTODO]
  105. public bool EnableDelete { get; set; }
  106. [MonoTODO]
  107. public bool EnableInsert { get; set; }
  108. [MonoTODO]
  109. public bool EnableObjectTracking { get; set; }
  110. [MonoTODO]
  111. public bool EnableUpdate { get; set; }
  112. [MonoTODO]
  113. public string TableName { get; set; }
  114. [MonoTODO]
  115. public string GroupBy { get; set; }
  116. [MonoTODO]
  117. public string OrderBy { get; set; }
  118. [MonoTODO]
  119. public string OrderGroupsBy { get; set; }
  120. [MonoTODO]
  121. public string SelectNew { get; set; }
  122. [MonoTODO]
  123. public string Where { get; set; }
  124. public ParameterCollection DeleteParameters {
  125. get { return GetParameterCollection (ref delete_parameters, false, false); }
  126. }
  127. public ParameterCollection InsertParameters {
  128. get { return GetParameterCollection (ref insert_parameters, false, false); }
  129. }
  130. public ParameterCollection UpdateParameters {
  131. get { return GetParameterCollection (ref update_parameters, true, true); }
  132. }
  133. public ParameterCollection SelectNewParameters {
  134. get { return GetParameterCollection (ref select_new_parameters, false, false); }
  135. }
  136. public ParameterCollection WhereParameters {
  137. get { return GetParameterCollection (ref where_parameters, true, true); }
  138. }
  139. public ParameterCollection GroupByParameters {
  140. get { return GetParameterCollection (ref group_by_parameters, true, true); }
  141. }
  142. public ParameterCollection OrderByParameters {
  143. get { return GetParameterCollection (ref order_by_parameters, true, true); }
  144. }
  145. public ParameterCollection OrderGroupsByParameters {
  146. get { return GetParameterCollection (ref order_group_by_parameters, true, true); }
  147. }
  148. ParameterCollection GetParameterCollection (ref ParameterCollection output, bool propagateTrackViewState, bool subscribeChanged)
  149. {
  150. if (output != null)
  151. return output;
  152. output = new ParameterCollection ();
  153. if (subscribeChanged)
  154. output.ParametersChanged += new EventHandler (ParametersChanged);
  155. if (IsTrackingViewState && propagateTrackViewState)
  156. ((IStateManager) output).TrackViewState ();
  157. return output;
  158. }
  159. void ParametersChanged (object source, EventArgs args)
  160. {
  161. OnDataSourceViewChanged (EventArgs.Empty);
  162. }
  163. public int Delete (IDictionary keys, IDictionary oldValues)
  164. {
  165. return ExecuteDelete (keys, oldValues);
  166. }
  167. public int Insert (IDictionary values)
  168. {
  169. return ExecuteInsert (values);
  170. }
  171. public IEnumerable Select (DataSourceSelectArguments arguments)
  172. {
  173. return ExecuteSelect (arguments);
  174. }
  175. public int Update (IDictionary keys, IDictionary values, IDictionary oldValues)
  176. {
  177. return ExecuteUpdate (keys, values, oldValues);
  178. }
  179. [MonoTODO]
  180. protected override int ExecuteDelete (IDictionary keys, IDictionary oldValues)
  181. {
  182. throw new NotImplementedException ();
  183. }
  184. [MonoTODO]
  185. protected override int ExecuteInsert (IDictionary values)
  186. {
  187. throw new NotImplementedException ();
  188. }
  189. [MonoTODO]
  190. protected override int ExecuteUpdate (IDictionary keys, IDictionary values, IDictionary oldValues)
  191. {
  192. throw new NotImplementedException ();
  193. }
  194. [MonoTODO]
  195. protected internal override IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
  196. {
  197. throw new NotImplementedException ();
  198. }
  199. [MonoTODO]
  200. protected virtual void DeleteDataObject (object dataContext, object table, object oldDataObject)
  201. {
  202. throw new NotImplementedException ();
  203. }
  204. [MonoTODO]
  205. protected virtual void InsertDataObject (object dataContext, object table, object newDataObject)
  206. {
  207. throw new NotImplementedException ();
  208. }
  209. [MonoTODO]
  210. protected virtual void ResetDataObject (object table, object dataObject)
  211. {
  212. throw new NotImplementedException ();
  213. }
  214. [MonoTODO]
  215. protected virtual void UpdateDataObject (object dataContext, object table, object oldDataObject, object newDataObject)
  216. {
  217. throw new NotImplementedException ();
  218. }
  219. [MonoTODO]
  220. protected virtual object CreateContext (Type contextType)
  221. {
  222. throw new NotImplementedException ();
  223. }
  224. [MonoTODO]
  225. protected virtual Type GetDataObjectType (Type tableType)
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. [MonoTODO]
  230. protected virtual MemberInfo GetTableMemberInfo (Type contextType)
  231. {
  232. throw new NotImplementedException ();
  233. }
  234. #region Validation
  235. [MonoTODO]
  236. protected virtual void ValidateContextType (Type contextType, bool selecting)
  237. {
  238. throw new NotImplementedException ();
  239. }
  240. [MonoTODO]
  241. protected virtual void ValidateDeleteSupported (IDictionary keys, IDictionary oldValues)
  242. {
  243. throw new NotImplementedException ();
  244. }
  245. [MonoTODO]
  246. protected virtual void ValidateInsertSupported (IDictionary values)
  247. {
  248. throw new NotImplementedException ();
  249. }
  250. [MonoTODO]
  251. protected virtual void ValidateOrderByParameter (string name, string value)
  252. {
  253. throw new NotImplementedException ();
  254. }
  255. [MonoTODO]
  256. protected virtual void ValidateParameterName (string name)
  257. {
  258. throw new NotImplementedException ();
  259. }
  260. [MonoTODO]
  261. protected virtual void ValidateTableType (Type tableType, bool selecting)
  262. {
  263. throw new NotImplementedException ();
  264. }
  265. [MonoTODO]
  266. protected virtual void ValidateUpdateSupported (IDictionary keys, IDictionary values, IDictionary oldValues)
  267. {
  268. throw new NotImplementedException ();
  269. }
  270. #endregion
  271. #region ViewState
  272. bool IStateManager.IsTrackingViewState {
  273. get { return IsTrackingViewState; }
  274. }
  275. protected bool IsTrackingViewState {
  276. get { return tracking; }
  277. }
  278. [MonoTODO]
  279. public bool StoreOriginalValuesInViewState { get; set; }
  280. void IStateManager.LoadViewState (object savedState)
  281. {
  282. LoadViewState (savedState);
  283. }
  284. object IStateManager.SaveViewState ()
  285. {
  286. return SaveViewState ();
  287. }
  288. void IStateManager.TrackViewState ()
  289. {
  290. TrackViewState ();
  291. }
  292. protected virtual void LoadViewState (object savedState)
  293. {
  294. object [] vs = savedState as object [];
  295. if (vs == null)
  296. return;
  297. int i = 0;
  298. foreach (var p in AllParameters) {
  299. if (vs [i] != null)
  300. ((IStateManager) p).LoadViewState (vs [i]);
  301. i++;
  302. }
  303. }
  304. protected virtual object SaveViewState ()
  305. {
  306. object [] vs = new object [8];
  307. int i = 0;
  308. foreach (var p in AllParameters) {
  309. if (p != null)
  310. vs [i] = ((IStateManager) p).SaveViewState ();
  311. i++;
  312. }
  313. foreach (object o in vs)
  314. if (o != null)
  315. return vs;
  316. return null;
  317. }
  318. protected virtual void TrackViewState ()
  319. {
  320. tracking = true;
  321. foreach (var p in AllParameters)
  322. if (p != null)
  323. ((IStateManager) p).TrackViewState ();
  324. }
  325. #endregion
  326. #region Events and Overridable Event Handler Invocations
  327. [MonoTODO]
  328. public event EventHandler<LinqDataSourceStatusEventArgs> ContextCreated;
  329. [MonoTODO]
  330. public event EventHandler<LinqDataSourceContextEventArgs> ContextCreating;
  331. [MonoTODO]
  332. public event EventHandler<LinqDataSourceDisposeEventArgs> ContextDisposing;
  333. [MonoTODO]
  334. public event EventHandler<LinqDataSourceStatusEventArgs> Deleted;
  335. [MonoTODO]
  336. public event EventHandler<LinqDataSourceDeleteEventArgs> Deleting;
  337. [MonoTODO]
  338. internal event EventHandler<DynamicValidatorEventArgs> Exception;
  339. [MonoTODO]
  340. public event EventHandler<LinqDataSourceStatusEventArgs> Inserted;
  341. [MonoTODO]
  342. public event EventHandler<LinqDataSourceInsertEventArgs> Inserting;
  343. [MonoTODO]
  344. public event EventHandler<LinqDataSourceStatusEventArgs> Selected;
  345. [MonoTODO]
  346. public event EventHandler<LinqDataSourceSelectEventArgs> Selecting;
  347. [MonoTODO]
  348. public event EventHandler<LinqDataSourceStatusEventArgs> Updated;
  349. [MonoTODO]
  350. public event EventHandler<LinqDataSourceUpdateEventArgs> Updating;
  351. protected virtual void OnContextCreated (LinqDataSourceStatusEventArgs e)
  352. {
  353. if (ContextCreated != null)
  354. ContextCreated (this, e);
  355. }
  356. protected virtual void OnContextCreating (LinqDataSourceContextEventArgs e)
  357. {
  358. if (ContextCreating != null)
  359. ContextCreating (this, e);
  360. }
  361. protected virtual void OnContextDisposing (LinqDataSourceDisposeEventArgs e)
  362. {
  363. if (ContextDisposing != null)
  364. ContextDisposing (this, e);
  365. }
  366. protected virtual void OnDeleted (LinqDataSourceStatusEventArgs e)
  367. {
  368. if (Deleted != null)
  369. Deleted (this, e);
  370. }
  371. protected virtual void OnDeleting (LinqDataSourceDeleteEventArgs e)
  372. {
  373. if (Deleting != null)
  374. Deleting (this, e);
  375. }
  376. protected virtual void OnException (DynamicValidatorEventArgs e)
  377. {
  378. if (Exception != null)
  379. Exception (this, e);
  380. }
  381. protected virtual void OnInserted (LinqDataSourceStatusEventArgs e)
  382. {
  383. if (Inserted != null)
  384. Inserted (this, e);
  385. }
  386. protected virtual void OnInserting (LinqDataSourceInsertEventArgs e)
  387. {
  388. if (Inserting != null)
  389. Inserting (this, e);
  390. }
  391. protected virtual void OnSelected (LinqDataSourceStatusEventArgs e)
  392. {
  393. if (Selected != null)
  394. Selected (this, e);
  395. }
  396. protected virtual void OnSelecting (LinqDataSourceSelectEventArgs e)
  397. {
  398. if (Selecting != null)
  399. Selecting (this, e);
  400. }
  401. protected virtual void OnUpdated (LinqDataSourceStatusEventArgs e)
  402. {
  403. if (Updated != null)
  404. Updated (this, e);
  405. }
  406. protected virtual void OnUpdating (LinqDataSourceUpdateEventArgs e)
  407. {
  408. if (Updating != null)
  409. Updating (this, e);
  410. }
  411. #endregion
  412. }
  413. }
  414. #endif