PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Ximura Data Persistence/Persistence Managers/SQLDB Relational Based/SQLDBRelationalBasedPersistenceManager.cs

https://github.com/mbmccormick/Ximura
C# | 222 lines | 153 code | 24 blank | 45 comment | 13 complexity | d604513b0c7adf730ab83adbdfe5f74f MD5 | raw file
  1. #region Copyright
  2. // *******************************************************************************
  3. // Copyright (c) 2000-2009 Paul Stancer.
  4. // All rights reserved. This program and the accompanying materials
  5. // are made available under the terms of the Eclipse Public License v1.0
  6. // which accompanies this distribution, and is available at
  7. // http://www.eclipse.org/legal/epl-v10.html
  8. //
  9. // Contributors:
  10. // Paul Stancer - initial implementation
  11. // *******************************************************************************
  12. #endregion
  13. #region using
  14. using System;
  15. using System.ComponentModel;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Diagnostics;
  20. using System.IO;
  21. using System.Text;
  22. using System.Data;
  23. using System.Data.SqlClient;
  24. using System.Reflection;
  25. using Ximura;
  26. using Ximura.Data;
  27. using Ximura.Data;
  28. using Ximura.Framework;
  29. using Ximura.Framework;
  30. using CH = Ximura.Common;
  31. using AH = Ximura.AttributeHelper;
  32. #endregion
  33. namespace Ximura.Data
  34. {
  35. public class SQLDBRelationalBasedPersistenceManager<CONT, DCONT> : SQLDBPersistenceManager<CONT, DCONT>
  36. where CONT : DCONT
  37. where DCONT : Content
  38. {
  39. #region Constructors
  40. /// <summary>
  41. /// This is the default constructor.
  42. /// </summary>
  43. public SQLDBRelationalBasedPersistenceManager()
  44. : this((IContainer)null)
  45. {
  46. }
  47. /// <summary>
  48. /// This is the component model constructor.
  49. /// </summary>
  50. /// <param name="container"></param>
  51. public SQLDBRelationalBasedPersistenceManager(IContainer container)
  52. : base(container)
  53. {
  54. }
  55. #endregion
  56. protected virtual bool ResolveID(CDSContext context, Guid tid, Guid cid, Guid vid, out Guid? newVid)
  57. {
  58. throw new NotImplementedException("ResolveIDs is not implemented.");
  59. }
  60. protected virtual bool ResolveID(CDSContext context, Guid tid, string refType, string refValue, out Guid? cid, out Guid? vid)
  61. {
  62. throw new NotImplementedException("ResolveIDs is not implemented.");
  63. }
  64. protected virtual int ReadData(CDSContext context, Guid tid, Guid? cid, Guid? vid, CONT data)
  65. {
  66. throw new NotImplementedException("ReadData is not implemented.");
  67. }
  68. #region ResolveReference(CDSContext context)
  69. /// <summary>
  70. /// This method resolves the reference.
  71. /// </summary>
  72. /// <param name="context"></param>
  73. /// <returns></returns>
  74. protected override bool ResolveReference(CDSContext context)
  75. {
  76. try
  77. {
  78. string refType = context.Request.DataReferenceType;
  79. string refValue = context.Request.DataReferenceValue;
  80. Guid tid = context.Request.DataType.GetContentTypeAttributeID();
  81. Guid? cid, vid;
  82. if (!ResolveID(context, tid, refType, refValue, out cid, out vid))
  83. return false;
  84. context.Request.DataContentID = cid;
  85. context.Request.DataVersionID = vid;
  86. context.Response.Status = CH.HTTPCodes.Continue_100;
  87. return true;
  88. }
  89. catch (Exception ex)
  90. {
  91. return false;
  92. }
  93. }
  94. #endregion // ResolveReference(CDSContext context)
  95. #region VersionCheck(CDSContext context)
  96. /// <summary>
  97. /// This method checks the data store to see whether the references to the entity are current.
  98. /// </summary>
  99. /// <param name="context">The current CDS context.</param>
  100. /// <returns>
  101. /// The status codes for the response are as follows:
  102. /// 200 = OK, the contentID and versionID are correct.
  103. /// 400 = missing parameter, either the contentID or versionID is null
  104. /// 404 = the content ID was not found
  105. /// 412 = the version ID is not the current version.
  106. /// 500 = there has been an internal system error. check the SubStatus parameter for the exception description.
  107. /// </returns>
  108. protected override bool VersionCheck(CDSContext context)
  109. {
  110. try
  111. {
  112. Guid? cid, vid;
  113. Guid tid = context.Request.DataType.GetContentTypeAttributeID();
  114. cid = context.Request.DataContentID;
  115. vid = context.Request.DataVersionID;
  116. if (!cid.HasValue || !vid.HasValue)
  117. {
  118. context.Response.Status = CH.HTTPCodes.BadRequest_400;
  119. return true;
  120. }
  121. Guid? newVid;
  122. if (!ResolveID(context, tid, cid.Value, vid.Value, out newVid))
  123. {
  124. context.Response.Status = CH.HTTPCodes.NotFound_404;
  125. return true;
  126. }
  127. context.Response.CurrentContentID = cid;
  128. context.Response.CurrentVersionID = newVid;
  129. if (!newVid.HasValue || newVid.Value != vid.Value)
  130. {
  131. context.Response.Status = CH.HTTPCodes.PreconditionFailed_412;
  132. return true;
  133. }
  134. context.Response.Status = CH.HTTPCodes.OK_200;
  135. }
  136. catch (Exception ex)
  137. {
  138. context.Response.CurrentVersionID = null;
  139. context.Response.Status = CH.HTTPCodes.InternalServerError_500;
  140. context.Response.Substatus = ex.Message;
  141. }
  142. return true;
  143. }
  144. #endregion // VersionCheck(CDSContext context)
  145. #region Read(CDSContext context)
  146. /// <summary>
  147. /// This method reads the data from the datastore.
  148. /// </summary>
  149. /// <param name="context">The CDS context.</param>
  150. /// <returns>
  151. /// The status codes for the response are as follows:
  152. /// 200 = OK, the contentID and versionID are correct.
  153. /// 400 = missing parameter, the contentID is null
  154. /// 404 = the content ID was not found
  155. /// 500 = there has been an internal system error. check the SubStatus parameter for the exception description.
  156. /// </returns>
  157. protected override bool Read(CDSContext context)
  158. {
  159. Guid? cid, vid;
  160. Guid tid = context.Request.DataType.GetContentTypeAttributeID();
  161. cid = context.Request.DataContentID;
  162. vid = context.Request.DataVersionID;
  163. if (!cid.HasValue)
  164. {
  165. context.Response.Status = CH.HTTPCodes.BadRequest_400;
  166. return true;
  167. }
  168. CONT data = null;
  169. int response = 100;
  170. try
  171. {
  172. data = context.GetObject<CONT>();
  173. response = ReadData(context, tid, cid, vid, data);
  174. }
  175. catch (Exception ex)
  176. {
  177. response = 500;
  178. context.Response.Substatus = ex.Message;
  179. }
  180. finally
  181. {
  182. context.Response.Status = response.ToString();
  183. if (response == 200)
  184. {
  185. context.Response.Data = data;
  186. }
  187. else
  188. {
  189. context.Response.Data = null;
  190. if (data != null && data.ObjectPoolCanReturn)
  191. data.ObjectPoolReturn();
  192. }
  193. }
  194. return true;
  195. }
  196. #endregion // Read(CDSContext context)
  197. }
  198. }