PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/mojoPortal.Features.Business/Survey/SurveyResponse.cs

#
C# | 279 lines | 177 code | 67 blank | 35 comment | 12 complexity | bc0ba8dffbee9758568a60e1aa1acaa2 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause, CPL-1.0, CC-BY-SA-3.0, GPL-2.0
  1. /// Author: Rob Henry
  2. /// Created: 2007-10-16
  3. /// Last Modified: 2009-02-01
  4. ///
  5. /// The use and distribution terms for this software are covered by the
  6. /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
  7. /// which can be found in the file CPL.TXT at the root of this distribution.
  8. /// By using this software in any fashion, you are agreeing to be bound by
  9. /// the terms of this license.
  10. ///
  11. /// You must not remove this notice, or any other, from this software.
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using System.Globalization;
  16. using SurveyFeature.Data;
  17. namespace SurveyFeature.Business
  18. {
  19. /// <summary>
  20. /// Reprsents a response to asurvey
  21. /// </summary>
  22. public class SurveyResponse
  23. {
  24. #region Constructors
  25. public SurveyResponse()
  26. { }
  27. public SurveyResponse(Guid responseGuid)
  28. {
  29. GetSurveyResponse(responseGuid);
  30. }
  31. #endregion
  32. #region Private Properties
  33. private Guid responseGuid = Guid.Empty;
  34. private Guid surveyGuid = Guid.Empty;
  35. private Guid userGuid = Guid.Empty;
  36. private DateTime submissionDate = DateTime.MinValue;
  37. private bool annonymous = false;
  38. private bool complete = false;
  39. #endregion
  40. #region Public Properties
  41. public Guid ResponseGuid
  42. {
  43. get { return responseGuid; }
  44. //set { responseGuid = value; }
  45. }
  46. public Guid SurveyGuid
  47. {
  48. get { return surveyGuid; }
  49. set { surveyGuid = value; }
  50. }
  51. public Guid UserGuid
  52. {
  53. get { return userGuid; }
  54. set { userGuid = value; }
  55. }
  56. public DateTime SubmissionDate
  57. {
  58. get { return submissionDate; }
  59. set { submissionDate = value; }
  60. }
  61. public bool Annonymous
  62. {
  63. get { return annonymous; }
  64. set { annonymous = value; }
  65. }
  66. public bool Complete
  67. {
  68. get { return complete; }
  69. set { complete = value; }
  70. }
  71. #endregion
  72. #region Private Methods
  73. /// <summary>
  74. /// Gets an instance of SurveyResponse.
  75. /// </summary>
  76. /// <param name="responseGuid"> responseGuid </param>
  77. private void GetSurveyResponse(Guid responseGuid)
  78. {
  79. using (IDataReader reader = DBSurveyResponse.GetOne(responseGuid))
  80. {
  81. if (reader.Read())
  82. {
  83. this.responseGuid = new Guid(reader["ResponseGuid"].ToString());
  84. this.surveyGuid = new Guid(reader["SurveyGuid"].ToString());
  85. this.userGuid = new Guid(reader["UserGuid"].ToString());
  86. if (reader["SubmissionDate"] != DBNull.Value)
  87. {
  88. this.submissionDate = Convert.ToDateTime(reader["SubmissionDate"], CultureInfo.InvariantCulture);
  89. }
  90. this.annonymous = Convert.ToBoolean(reader["Annonymous"]);
  91. this.complete = Convert.ToBoolean(reader["Complete"]);
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Persists a new instance of SurveyResponse. Returns true on success.
  97. /// </summary>
  98. /// <returns></returns>
  99. private bool Create()
  100. {
  101. this.responseGuid = Guid.NewGuid();
  102. int rowsAffected = DBSurveyResponse.Add(
  103. this.responseGuid,
  104. this.surveyGuid,
  105. this.userGuid,
  106. this.annonymous,
  107. this.complete);
  108. return (rowsAffected > 0);
  109. }
  110. #endregion
  111. #region Public Methods
  112. /// <summary>
  113. /// Saves this instance of SurveyResponse. Returns true on success.
  114. /// </summary>
  115. /// <returns>bool</returns>
  116. public bool Save()
  117. {
  118. bool result = false;
  119. if(responseGuid == Guid.Empty)
  120. result = Create();
  121. if (submissionDate > DateTime.MinValue)
  122. {
  123. complete = true;
  124. result = DBSurveyResponse.Update(responseGuid, submissionDate, complete);
  125. }
  126. return result;
  127. }
  128. #endregion
  129. #region Static Methods
  130. /// <summary>
  131. /// Deletes an instance of SurveyResponse. Returns true on success.
  132. /// </summary>
  133. /// <param name="responseGuid"> responseGuid </param>
  134. /// <returns>bool</returns>
  135. public static bool Delete(Guid responseGuid)
  136. {
  137. return DBSurveyResponse.Delete(
  138. responseGuid);
  139. }
  140. /// <summary>
  141. /// Gets an IList with all instances of SurveyResponse.
  142. /// </summary>
  143. public static List<SurveyResponse> GetAll(Guid surveyGuid)
  144. {
  145. IDataReader reader = DBSurveyResponse.GetAll(surveyGuid);
  146. return LoadFromReader(reader);
  147. }
  148. private static List<SurveyResponse> LoadFromReader(IDataReader reader)
  149. {
  150. List<SurveyResponse> surveyResponseList = new List<SurveyResponse>();
  151. try
  152. {
  153. while (reader.Read())
  154. {
  155. SurveyResponse surveyResponse = new SurveyResponse();
  156. surveyResponse.responseGuid = new Guid(reader["ResponseGuid"].ToString());
  157. surveyResponse.surveyGuid = new Guid(reader["SurveyGuid"].ToString());
  158. surveyResponse.userGuid = new Guid(reader["UserGuid"].ToString());
  159. if (reader["SubmissionDate"] != DBNull.Value)
  160. surveyResponse.submissionDate = Convert.ToDateTime(reader["SubmissionDate"], CultureInfo.InvariantCulture);
  161. surveyResponse.annonymous = Convert.ToBoolean(reader["Annonymous"], CultureInfo.InvariantCulture);
  162. surveyResponse.complete = Convert.ToBoolean(reader["Complete"], CultureInfo.InvariantCulture);
  163. surveyResponseList.Add(surveyResponse);
  164. }
  165. }
  166. finally
  167. {
  168. reader.Close();
  169. }
  170. return surveyResponseList;
  171. }
  172. private static SurveyResponse FromReader(IDataReader reader)
  173. {
  174. SurveyResponse response = null;
  175. try
  176. {
  177. if (reader.Read())
  178. {
  179. response = new SurveyResponse();
  180. response.responseGuid = new Guid(reader["ResponseGuid"].ToString());
  181. response.surveyGuid = new Guid(reader["SurveyGuid"].ToString());
  182. response.userGuid = new Guid(reader["UserGuid"].ToString());
  183. if (reader["SubmissionDate"] != DBNull.Value)
  184. response.submissionDate = Convert.ToDateTime(reader["SubmissionDate"], CultureInfo.InvariantCulture);
  185. response.annonymous = Convert.ToBoolean(reader["Annonymous"], CultureInfo.InvariantCulture);
  186. response.complete = Convert.ToBoolean(reader["Complete"], CultureInfo.InvariantCulture);
  187. }
  188. }
  189. finally
  190. {
  191. reader.Close();
  192. }
  193. return response;
  194. }
  195. public static SurveyResponse GetFirst(Guid surveyGuid)
  196. {
  197. IDataReader reader = DBSurveyResponse.GetFirst(surveyGuid);
  198. return FromReader(reader);
  199. }
  200. public static SurveyResponse GetNext(Guid responseGuid)
  201. {
  202. IDataReader reader = DBSurveyResponse.GetNext(responseGuid);
  203. return FromReader(reader);
  204. }
  205. public static SurveyResponse GetPrevious(Guid responseGuid)
  206. {
  207. IDataReader reader = DBSurveyResponse.GetPrevious(responseGuid);
  208. return FromReader(reader);
  209. }
  210. #endregion
  211. }
  212. }