/BoxSocial.Internals/ItemKey.cs

http://boxsocial.codeplex.com · C# · 413 lines · 338 code · 37 blank · 38 comment · 47 complexity · e2454f13e70a980ebdbad763b3078bfb MD5 · raw file

  1. /*
  2. * Box Social™
  3. * http://boxsocial.net/
  4. * Copyright © 2007, David Smith
  5. *
  6. * $Id:$
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Data;
  23. using System.Reflection;
  24. using System.Diagnostics;
  25. using System.Text;
  26. using System.Web;
  27. using System.Web.Caching;
  28. using BoxSocial.IO;
  29. namespace BoxSocial.Internals
  30. {
  31. public class ItemKey
  32. {
  33. private static Object itemTypeCacheLock = new object();
  34. private static Dictionary<string, ItemType> itemTypeCache = null;
  35. private static Dictionary<string, long> itemApplicationCache = null;
  36. private long itemId;
  37. private long itemTypeId;
  38. private long applicationId;
  39. private string itemType;
  40. private Type type;
  41. private ItemType typeRow;
  42. public long Id
  43. {
  44. get
  45. {
  46. return itemId;
  47. }
  48. }
  49. public long TypeId
  50. {
  51. get
  52. {
  53. return itemTypeId;
  54. }
  55. }
  56. public long ApplicationId
  57. {
  58. get
  59. {
  60. return applicationId;
  61. }
  62. }
  63. public string TypeString
  64. {
  65. get
  66. {
  67. return itemType;
  68. }
  69. }
  70. public Type Type
  71. {
  72. get
  73. {
  74. if (type == null)
  75. {
  76. type = Type.GetType(ItemKey.GetItemType(TypeId).TypeNamespace);
  77. }
  78. return type;
  79. }
  80. }
  81. public bool ImplementsLikeable
  82. {
  83. get
  84. {
  85. if (typeRow != null)
  86. {
  87. return typeRow.Likeable;
  88. }
  89. else
  90. {
  91. return false;
  92. }
  93. }
  94. }
  95. public bool ImplementsCommentable
  96. {
  97. get
  98. {
  99. if (typeRow != null)
  100. {
  101. return typeRow.Commentable;
  102. }
  103. else
  104. {
  105. return false;
  106. }
  107. }
  108. }
  109. public bool ImplementsRateable
  110. {
  111. get
  112. {
  113. if (typeRow != null)
  114. {
  115. return typeRow.Rateable;
  116. }
  117. else
  118. {
  119. return false;
  120. }
  121. }
  122. }
  123. public bool ImplementsSubscribeable
  124. {
  125. get
  126. {
  127. if (typeRow != null)
  128. {
  129. return typeRow.Subscribeable;
  130. }
  131. else
  132. {
  133. return false;
  134. }
  135. }
  136. }
  137. public bool InheritsPrimitive
  138. {
  139. get
  140. {
  141. if (typeRow != null)
  142. {
  143. return typeRow.IsPrimitive;
  144. }
  145. else
  146. {
  147. return false;
  148. }
  149. }
  150. }
  151. public ItemKey(long itemId, string itemType)
  152. {
  153. this.itemId = itemId;
  154. this.itemType = itemType;
  155. lock (itemTypeCacheLock)
  156. {
  157. if (!itemTypeCache.TryGetValue(itemType, out this.typeRow))
  158. {
  159. throw new Exception(string.Format("Cannot find key {0} in {1}", itemType, "itemTypeCache"));
  160. }
  161. if (!itemApplicationCache.TryGetValue(itemType, out this.applicationId))
  162. {
  163. throw new Exception(string.Format("Cannot find key {0} in {1}", itemType, "itemApplicationCache"));
  164. }
  165. //this.typeRow = itemTypeCache[itemType];
  166. this.itemTypeId = this.typeRow.TypeId;
  167. //this.applicationId = itemApplicationCache[itemType];
  168. }
  169. this.type = null;
  170. }
  171. public ItemKey(long itemId, long itemTypeId)
  172. {
  173. if (itemTypeId < 1)
  174. {
  175. this.itemId = itemId;
  176. this.itemTypeId = 0;
  177. this.applicationId = 0;
  178. this.type = null;
  179. this.typeRow = null;
  180. return;
  181. }
  182. this.itemId = itemId;
  183. lock (itemTypeCacheLock)
  184. {
  185. foreach (string value in itemTypeCache.Keys)
  186. {
  187. if (itemTypeCache[value].TypeId == itemTypeId)
  188. {
  189. this.itemType = value;
  190. break;
  191. }
  192. }
  193. this.typeRow = itemTypeCache[itemType];
  194. this.itemTypeId = itemTypeId;
  195. this.applicationId = itemApplicationCache[this.itemType];
  196. }
  197. this.type = null;
  198. }
  199. public ItemKey(long itemId, Type itemType)
  200. {
  201. this.itemId = itemId;
  202. long itemTypeId = GetTypeId(itemType);
  203. lock (itemTypeCacheLock)
  204. {
  205. foreach (string value in itemTypeCache.Keys)
  206. {
  207. if (itemTypeCache[value].TypeId == itemTypeId)
  208. {
  209. this.itemType = value;
  210. break;
  211. }
  212. }
  213. this.typeRow = itemTypeCache[this.itemType];
  214. this.itemTypeId = itemTypeId;
  215. this.applicationId = itemApplicationCache[this.itemType];
  216. }
  217. this.type = itemType;
  218. }
  219. public ItemKey(string key)
  220. {
  221. string[] keys = key.Split(new char[] {','});
  222. long itemId = long.Parse(keys[1]);
  223. long itemTypeId = long.Parse(keys[0]);
  224. this.itemId = itemId;
  225. lock (itemTypeCacheLock)
  226. {
  227. foreach (string value in itemTypeCache.Keys)
  228. {
  229. if (itemTypeCache[value].TypeId == itemTypeId)
  230. {
  231. this.itemType = value;
  232. break;
  233. }
  234. }
  235. this.typeRow = itemTypeCache[this.itemType];
  236. this.itemTypeId = itemTypeId;
  237. this.applicationId = itemApplicationCache[this.itemType];
  238. }
  239. this.type = null;
  240. }
  241. public static void populateItemTypeCache(Core core)
  242. {
  243. if (core == null)
  244. {
  245. throw new NullCoreException();
  246. }
  247. System.Web.Caching.Cache cache;
  248. object o = null;
  249. object b = null;
  250. if (HttpContext.Current != null && HttpContext.Current.Cache != null)
  251. {
  252. cache = HttpContext.Current.Cache;
  253. }
  254. else
  255. {
  256. cache = new Cache();
  257. }
  258. if (cache != null)
  259. {
  260. try
  261. {
  262. o = cache.Get("itemTypeIds");
  263. }
  264. catch (NullReferenceException)
  265. {
  266. }
  267. try
  268. {
  269. b = cache.Get("itemApplicationIds");
  270. }
  271. catch (NullReferenceException)
  272. {
  273. }
  274. }
  275. lock (itemTypeCacheLock)
  276. {
  277. if (o != null && o.GetType() == typeof(System.Collections.Generic.Dictionary<string, ItemType>))
  278. {
  279. itemTypeCache = (Dictionary<string, ItemType>)o;
  280. itemApplicationCache = (Dictionary<string, long>)b;
  281. }
  282. else
  283. {
  284. itemTypeCache = new Dictionary<string, ItemType>(StringComparer.Ordinal);
  285. itemApplicationCache = new Dictionary<string, long>(StringComparer.Ordinal);
  286. SelectQuery query = ItemType.GetSelectQueryStub(typeof(ItemType));
  287. DataTable typesTable;
  288. try
  289. {
  290. typesTable = core.Db.Query(query);
  291. }
  292. catch
  293. {
  294. return;
  295. }
  296. foreach (DataRow dr in typesTable.Rows)
  297. {
  298. ItemType typeItem = new ItemType(core, dr);
  299. if (!(itemTypeCache.ContainsKey(typeItem.TypeNamespace)))
  300. {
  301. itemTypeCache.Add(typeItem.TypeNamespace, typeItem);
  302. itemApplicationCache.Add(typeItem.TypeNamespace, typeItem.ApplicationId);
  303. }
  304. }
  305. if (cache != null)
  306. {
  307. cache.Add("itemTypeIds", itemTypeCache, null, Cache.NoAbsoluteExpiration, new TimeSpan(4, 0, 0), CacheItemPriority.High, null);
  308. cache.Add("itemApplicationIds", itemApplicationCache, null, Cache.NoAbsoluteExpiration, new TimeSpan(4, 0, 0), CacheItemPriority.High, null);
  309. }
  310. }
  311. }
  312. }
  313. public static long GetTypeId(Type type)
  314. {
  315. ItemType it = null;
  316. if (itemTypeCache.TryGetValue(type.FullName, out it))
  317. {
  318. return it.TypeId;
  319. }
  320. else
  321. {
  322. return 0;
  323. }
  324. }
  325. public static ItemType GetItemType(long typeId)
  326. {
  327. ItemType it = null;
  328. foreach (ItemType type in itemTypeCache.Values)
  329. {
  330. if (type.Id == typeId)
  331. {
  332. it = type;
  333. }
  334. }
  335. return it;
  336. }
  337. public override string ToString ()
  338. {
  339. return string.Format("{0},{1}", TypeId, Id);
  340. }
  341. public override bool Equals(object obj)
  342. {
  343. if (obj == null)
  344. return false;
  345. if (obj.GetType() != typeof(ItemKey))
  346. return false;
  347. ItemKey ik = (ItemKey)obj;
  348. if (TypeId != ik.TypeId)
  349. return false;
  350. if (Id != ik.Id)
  351. return false;
  352. return true;
  353. }
  354. public override int GetHashCode()
  355. {
  356. return base.GetHashCode();
  357. }
  358. /*public static bool operator ==(ItemKey ik1, ItemKey ik2)
  359. {
  360. if ((ik1.Equals(null) || ik2.Equals(null)))
  361. return false;
  362. return ik1.Equals(ik2);
  363. }
  364. public static bool operator !=(ItemKey ik1, ItemKey ik2)
  365. {
  366. if ((ik1.Equals(null) && ik2.Equals(null)))
  367. return false;
  368. if (ik1.Equals(null))
  369. return true;
  370. if (ik2.Equals(null))
  371. return true;
  372. return (!ik1.Equals(ik2));
  373. }*/
  374. }
  375. }