PageRenderTime 40ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

/FHSSpirit/FHSSpiritDataControl/DataControlNews.cs

https://github.com/spirit-fhs/wp7
C# | 281 lines | 204 code | 56 blank | 21 comment | 4 complexity | 410615d8be6f03b5c0ca082ecf6e68e1 MD5 | raw file
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using System.Windows.Threading;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. //from Models
  15. //using WindowsPhoneSpiritApplication;
  16. using Newtonsoft.Json.Linq;
  17. using FHSSpiritDataModels;
  18. using FHSSpiritDataModelsFS;
  19. using System.IO;
  20. using System.Text;
  21. namespace FHSSpiritDataControl
  22. {
  23. public class DataControlNews
  24. {
  25. //note: DataControlNews is a Singleton
  26. private static DataControlNews instance;
  27. public static DataControlNews Instance
  28. {
  29. get
  30. {
  31. if (instance == null)
  32. {
  33. instance = new DataControlNews();
  34. }
  35. return instance;
  36. }
  37. }
  38. public delegate void ErrorMessage(string message);
  39. public delegate string GetURI();
  40. public delegate string GetFHSID();
  41. public delegate void ResponseNews(ItemMessageModel[] messages);
  42. public ErrorMessage m_del_ErrorMessage;
  43. public GetURI m_del_GetUIR;
  44. public ResponseNews m_del_ResponseNews;
  45. public GetFHSID m_del_GetFHSID;
  46. System.Uri m_UriDataSource;
  47. string m_strNewsPostFix = "/fhs-spirit/news";
  48. string m_strCommentPostFix = "/fhs-spirit/news/comment";
  49. //Members to add a new Comment
  50. string m_strNewComment = "";
  51. int m_nNewMessageID = -1;
  52. string m_strFHSID = "";
  53. private DataControlNews()
  54. {
  55. }
  56. public void requestAllNews()
  57. {
  58. try
  59. {
  60. this.m_UriDataSource = new Uri(this.m_del_GetUIR() + this.m_strNewsPostFix);
  61. HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(this.m_UriDataSource);
  62. httpWebRequest.Method = "GET";
  63. // Set the ContentType property of the WebRequest.
  64. //httpWebRequest.Accept = "application/xml";
  65. httpWebRequest.Accept = "application/json";
  66. //httpWebRequest.Headers = "";
  67. var result = (IAsyncResult)httpWebRequest.BeginGetResponse(ResponseCallback, httpWebRequest);
  68. }
  69. catch (Exception ex)
  70. {
  71. this.m_del_ErrorMessage(ex.Message);
  72. }
  73. }
  74. private void ResponseCallback(IAsyncResult result)
  75. {
  76. try
  77. {
  78. HttpWebRequest httpWebRequest = (HttpWebRequest)result.AsyncState;
  79. WebResponse response = httpWebRequest.EndGetResponse(result);
  80. System.IO.Stream stream = response.GetResponseStream();
  81. System.IO.StreamReader sr = new System.IO.StreamReader(stream);
  82. string strJSON = sr.ReadToEnd();
  83. processJSONString(strJSON);
  84. }
  85. catch (Exception ex)
  86. {
  87. this.m_del_ErrorMessage(ex.Message);
  88. }
  89. }
  90. public void addComment(string strComments, int nCurrentMessageID)
  91. {
  92. try
  93. {
  94. this.m_UriDataSource = new Uri(this.m_del_GetUIR() + this.m_strCommentPostFix);
  95. HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(this.m_UriDataSource);
  96. httpWebRequest.Method = "PUT";
  97. httpWebRequest.Accept = "application/json";
  98. httpWebRequest.ContentType = "application/json";
  99. this.m_strNewComment = strComments;
  100. this.m_nNewMessageID = nCurrentMessageID;
  101. this.m_strFHSID = this.m_del_GetFHSID();
  102. var result = (IAsyncResult)httpWebRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), httpWebRequest);
  103. }
  104. catch (Exception ex)
  105. {
  106. this.m_del_ErrorMessage(ex.Message);
  107. }
  108. }
  109. private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
  110. {
  111. HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
  112. // End the operation
  113. Stream postStream = request.EndGetRequestStream(asynchronousResult);
  114. //sample
  115. string postData = "{\"newsComment\":{\"content\":\"" + this.m_strNewComment + "\",\"news\":{\"news_id\":" + this.m_nNewMessageID.ToString() + "},\"owner\":{\"fhs_id\":\"" + this.m_strFHSID + "\"}}}";
  116. // Convert the string into a byte array.
  117. byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  118. //Latin 1
  119. //byte[] byteArray = System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(postData);
  120. // Write to the request stream.
  121. postStream.Write(byteArray, 0, byteArray.Length);
  122. postStream.Close();
  123. // Start the asynchronous operation to get the response
  124. request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
  125. }
  126. private void GetResponseCallback(IAsyncResult asynchronousResult)
  127. {
  128. try
  129. {
  130. HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
  131. // End the operation
  132. HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
  133. Stream streamResponse = response.GetResponseStream();
  134. StreamReader streamRead = new StreamReader(streamResponse);
  135. string responseString = streamRead.ReadToEnd();
  136. Console.WriteLine(responseString);
  137. // Close the stream object
  138. streamResponse.Close();
  139. streamRead.Close();
  140. // Release the HttpWebResponse
  141. response.Close();
  142. }
  143. catch (Exception ex)
  144. {
  145. this.m_del_ErrorMessage(ex.Message);
  146. }
  147. }
  148. private void processJSONString(string strJSON)
  149. {
  150. JObject jObject = JObject.Parse(strJSON);
  151. var query = from resultArray in (JArray)jObject["news"]
  152. let resultObject = resultArray as JObject
  153. orderby (string)resultObject.SelectToken("creationDate") descending
  154. //let resultObject2 = resultArray as JArray
  155. //let degreeClasses = "MAI3, MAI7"
  156. let flat = flatDegreeClasses(resultObject)
  157. let comments = addComments(resultObject)
  158. select new ItemMessageModel
  159. {
  160. Content = (string)resultObject.SelectToken("content"),
  161. Course = flat,
  162. CreationDateTime = (string)resultObject.SelectToken("creationDate"),
  163. ExpireDate = (string)resultObject.SelectToken("expireDate"),
  164. ID = (int)resultObject.SelectToken("news_id"),
  165. Owner = (string)resultObject["owner"].SelectToken("displayedName"),
  166. Title = (string)resultObject.SelectToken("title"),
  167. CountComments = (int)(resultObject["newsComment"] as JArray).Count(),
  168. NewsComments = comments
  169. };
  170. ItemMessageModel[] messages = query.ToArray();
  171. this.m_del_ResponseNews(messages);
  172. }
  173. static private string flatDegreeClasses(JObject jObject)
  174. {
  175. var query = from resultArray in (JArray)jObject["degreeClass"]
  176. let resultObject = resultArray as JObject
  177. select new DegreeClass
  178. {
  179. DegClass = (string)resultObject.SelectToken("title")
  180. };
  181. DegreeClass[] degreeClasses = query.ToArray();
  182. string outputString = "";
  183. foreach (DegreeClass d in degreeClasses)
  184. {
  185. outputString += d.DegClass + " ";
  186. }
  187. return outputString;
  188. }
  189. static private ItemMessageCommentModel[] addComments(JObject jObject)
  190. {
  191. var query = from resultArray in (JArray)jObject["newsComment"]
  192. let resultObject = resultArray as JObject
  193. //orderby (string)(resultObject["creationDate"] as JObject).SelectToken("creationDate") descending
  194. orderby (string)resultObject.SelectToken("creationDate") descending
  195. select new ItemMessageCommentModel
  196. {
  197. Content = (string)resultObject.SelectToken("content"),
  198. //CreationDateTime = (string)(resultObject["creationDate"] as JObject).SelectToken("creationDate"),
  199. CreationDateTime = (string)resultObject.SelectToken("creationDate"),
  200. ID = (int)resultObject.SelectToken("comment_id"),
  201. Owner = (string)resultObject["owner"].SelectToken("displayedName")
  202. };
  203. ItemMessageCommentModel[] arrItemMessageCommentModel = query.ToArray();
  204. return arrItemMessageCommentModel;
  205. }
  206. }
  207. public class DegreeClass
  208. {
  209. private string m_strDegreeClass;
  210. public string DegClass
  211. {
  212. get
  213. {
  214. return m_strDegreeClass;
  215. }
  216. set
  217. {
  218. if (value != m_strDegreeClass)
  219. {
  220. m_strDegreeClass = value;
  221. }
  222. }
  223. }
  224. };
  225. }