/neo2/trunk/Neo.MetaModel/Entity.cs

# · C# · 433 lines · 355 code · 66 blank · 12 comment · 39 complexity · b1ed2875d9fa368ed377695c8e7368c2 MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Neo.MetaModel
  6. {
  7. public enum IdMethod
  8. {
  9. IdBroker,
  10. Native,
  11. Guid,
  12. None
  13. }
  14. [Serializable]
  15. public class Entity
  16. {
  17. //--------------------------------------------------------------------------------------
  18. // constructor
  19. //--------------------------------------------------------------------------------------
  20. private Model model;
  21. private ArrayList attributes;
  22. private Hashtable attributeMap;
  23. private ArrayList relationships;
  24. private String subPackageName;
  25. private String className;
  26. private String baseClassName;
  27. private String defaultProperty;
  28. private String tableName;
  29. private IdMethod idMethod;
  30. private bool manyToMany;
  31. private bool skip;
  32. private bool forceOverwrite;
  33. private String constraint;
  34. public Entity(Model aModel)
  35. {
  36. model = aModel;
  37. attributes = new ArrayList();
  38. attributeMap = new Hashtable();
  39. relationships = new ArrayList();
  40. }
  41. //--------------------------------------------------------------------------------------
  42. // accessors
  43. //--------------------------------------------------------------------------------------
  44. public Model Model
  45. {
  46. get { return model ; }
  47. }
  48. public void AddAttribute(EntityAttribute anAttribute)
  49. {
  50. attributes.Add(anAttribute);
  51. attributeMap.Add(anAttribute.ColumnName, anAttribute);
  52. }
  53. public void RemoveAttribute(EntityAttribute anAttribute)
  54. {
  55. attributes.Remove(anAttribute);
  56. attributeMap.Remove(anAttribute.ColumnName);
  57. }
  58. public EntityAttribute GetAttributeForColumnName(string columnName)
  59. {
  60. return attributeMap[columnName] as EntityAttribute;
  61. }
  62. public ICollection Attributes
  63. {
  64. get { return attributes; }
  65. }
  66. public void AddRelationship(EntityRelationship aRelationship)
  67. {
  68. relationships.Add(aRelationship);
  69. }
  70. public void RemoveRelationship(EntityRelationship aRelationship)
  71. {
  72. relationships.Remove(aRelationship);
  73. }
  74. public ICollection Relationships
  75. {
  76. get { return relationships; }
  77. }
  78. public ICollection ParentRelationships
  79. {
  80. get
  81. {
  82. ArrayList list = new ArrayList();
  83. foreach (EntityRelationship relationship in relationships)
  84. {
  85. if (relationship.Direction == RelDirection.Parent)
  86. list.Add(relationship);
  87. }
  88. return list;
  89. }
  90. }
  91. public ICollection ChildRelationships
  92. {
  93. get
  94. {
  95. ArrayList list = new ArrayList();
  96. foreach (EntityRelationship relationship in relationships)
  97. {
  98. if (relationship.Direction == RelDirection.Child)
  99. list.Add(relationship);
  100. }
  101. return list;
  102. }
  103. }
  104. public string SubPackageName
  105. {
  106. set { subPackageName = value; }
  107. get { return subPackageName; }
  108. }
  109. public string ClassName
  110. {
  111. set { className = value; }
  112. get { return className; }
  113. }
  114. public string Constraint
  115. {
  116. get { return constraint; }
  117. set { constraint = value; }
  118. }
  119. public bool IsManyToManyTable
  120. {
  121. set { manyToMany = value; }
  122. get { return manyToMany; }
  123. }
  124. public bool Skip
  125. {
  126. set { skip = value; }
  127. get { return skip; }
  128. }
  129. public bool ForceOverwrite
  130. {
  131. set { forceOverwrite = value; }
  132. get { return forceOverwrite; }
  133. }
  134. public string BaseClassName
  135. {
  136. set { baseClassName = value; }
  137. get { return baseClassName; }
  138. }
  139. public string DefaultProperty
  140. {
  141. get { return defaultProperty; }
  142. set { defaultProperty = value; }
  143. }
  144. public string TableName
  145. {
  146. set { tableName = value; }
  147. get { return tableName; }
  148. }
  149. public IdMethod IdMethod
  150. {
  151. set { idMethod = value; }
  152. get { return idMethod; }
  153. }
  154. public int AttributeCount
  155. {
  156. get
  157. {
  158. int i = 0;
  159. foreach(EntityAttribute att in Attributes)
  160. {
  161. if (att.IsHidden || att.IsPkColumn || att.IsSQLTextColumn)
  162. {
  163. }
  164. else
  165. {
  166. i++;
  167. }
  168. }
  169. return i;
  170. }
  171. }
  172. //--------------------------------------------------------------------------------------
  173. // derived properties
  174. //--------------------------------------------------------------------------------------
  175. public string Namespace
  176. {
  177. get
  178. {
  179. string ns = model.Namespace;
  180. if(SubPackageName != null)
  181. ns += "." + SubPackageName;
  182. return ns;
  183. }
  184. set
  185. {
  186. model.Namespace = value;
  187. }
  188. }
  189. public ICollection UsedNamespaces
  190. {
  191. get
  192. {
  193. ArrayList namespaces;
  194. namespaces = new ArrayList();
  195. foreach(EntityRelationship r in Relationships)
  196. {
  197. if(namespaces.Contains(r.ForeignEntity.Namespace) == false)
  198. namespaces.Add(r.ForeignEntity.Namespace);
  199. }
  200. return namespaces;
  201. }
  202. }
  203. public ICollection PkColumns
  204. {
  205. get
  206. {
  207. ArrayList columns = new ArrayList();
  208. foreach(EntityAttribute a in attributes)
  209. {
  210. if(a.IsPkColumn)
  211. columns.Add(a);
  212. }
  213. return columns;
  214. }
  215. }
  216. public bool PrimaryKeyIsForeignKey
  217. {
  218. get
  219. {
  220. foreach(EntityAttribute attr in PkColumns)
  221. {
  222. EntityRelationship rel;
  223. if(((rel = RelationshipForAttribute(attr)) == null) || (rel.Direction == RelDirection.Parent))
  224. return false;
  225. }
  226. return true;
  227. }
  228. }
  229. public EntityRelationship RelationshipForAttribute(EntityAttribute attr)
  230. {
  231. foreach(EntityRelationship rel in Relationships)
  232. {
  233. if(attr.ColumnName == rel.LocalKey)
  234. return rel;
  235. }
  236. return null;
  237. }
  238. public IList RelationshipsForAttribute(EntityAttribute attr)
  239. {
  240. ArrayList relList;
  241. relList = new ArrayList();
  242. foreach(EntityRelationship rel in Relationships)
  243. {
  244. if(attr.ColumnName == rel.LocalKey)
  245. relList.Add(rel);
  246. }
  247. return relList;
  248. }
  249. public IList[] RelationshipSetsForColumns(IList attrList)
  250. {
  251. ArrayList[] lists;
  252. IList relList;
  253. lists = new ArrayList[attrList.Count];
  254. for(int i = 0; i < attrList.Count; i++)
  255. {
  256. lists[i] = new ArrayList();
  257. relList = RelationshipsForAttribute((EntityAttribute)attrList[i]);
  258. if(i == 0)
  259. {
  260. AddCombinations(new ArrayList(), relList, lists[i]);
  261. }
  262. else
  263. {
  264. foreach(ArrayList fixedPart in lists[i - 1])
  265. AddCombinations(fixedPart, relList, lists[i]);
  266. }
  267. }
  268. return (IList[])lists[attrList.Count - 1].ToArray(typeof(IList));
  269. }
  270. protected void AddCombinations(IList fixedPart, IList combinations, ArrayList list)
  271. {
  272. ArrayList entry;
  273. foreach(EntityRelationship r in combinations)
  274. {
  275. entry = new ArrayList(fixedPart.Count + 1);
  276. entry.AddRange(fixedPart);
  277. entry.Add(r);
  278. list.Add(entry);
  279. }
  280. }
  281. public ICollection ToOneRelationships
  282. {
  283. get
  284. {
  285. return GetRelationships(RelType.ToOne);
  286. }
  287. }
  288. public ICollection ToManyRelationships
  289. {
  290. get
  291. {
  292. return GetRelationships(RelType.ToMany);
  293. }
  294. }
  295. //--------------------------------------------------------------------------------------
  296. // helper
  297. //--------------------------------------------------------------------------------------
  298. public bool ColumnIsPrimaryKey(string columnname)
  299. {
  300. if(HasAttribute(columnname) == false)
  301. throw new ArgumentException(String.Format("Table {0} has no column named {1}.", TableName, columnname));
  302. ArrayList pkcolumns = (ArrayList)PkColumns;
  303. return ((pkcolumns.Count == 1) && (((EntityAttribute)pkcolumns[0]).ColumnName == columnname));
  304. }
  305. public bool ColumnIsForeignKey(string columnname)
  306. {
  307. if(HasAttribute(columnname) == false)
  308. throw new ArgumentException(String.Format("Table {0} has no column named {1}.", TableName, columnname));
  309. EntityRelationship rel = RelationshipForAttribute((EntityAttribute)attributeMap[columnname]);
  310. return ((rel != null) && (rel.Direction == RelDirection.Child));
  311. }
  312. public bool HasAttribute(string columnName)
  313. {
  314. return (attributeMap[columnName] != null);
  315. }
  316. protected ICollection GetRelationships(RelType types)
  317. {
  318. ArrayList list;
  319. if(types == RelType.All)
  320. {
  321. list = relationships;
  322. }
  323. else
  324. {
  325. list = new ArrayList();
  326. foreach(EntityRelationship rel in relationships)
  327. {
  328. if((rel.Type & types) != 0)
  329. list.Add(rel);
  330. }
  331. }
  332. return list;
  333. }
  334. public override string ToString()
  335. {
  336. if (String.IsNullOrEmpty(ClassName))
  337. {
  338. return base.ToString();
  339. }
  340. return ClassName;
  341. }
  342. public string ToXml()
  343. {
  344. StringBuilder sb = new StringBuilder();
  345. string table = "\t" + @"<table name=""#name#"" javaName=""#javaName#"" idMethod=""#idMethod#"" defaultProperty=""#defaultProperty#"" manyToMany=""#manyToMany#"" skipSql=""#skip#"" forceOverwrite=""#forceOverwrite#"" description="""">";
  346. table = table.Replace("#name#", this.TableName);
  347. table = table.Replace("#idMethod#", this.IdMethod.ToString().ToLower());
  348. table = table.Replace("#defaultProperty#", this.DefaultProperty);
  349. table = table.Replace("#manyToMany#", this.IsManyToManyTable.ToString().ToLower());
  350. table = table.Replace("#skip#", this.Skip.ToString().ToLower());
  351. table = table.Replace("#forceOverwrite#", this.ForceOverwrite.ToString().ToLower());
  352. table = table.Replace("#javaName#", String.IsNullOrEmpty(this.ClassName) ? this.TableName : this.ClassName);
  353. sb.AppendLine(table);
  354. foreach (EntityAttribute attribute in Attributes)
  355. {
  356. sb.Append(attribute.ToXml());
  357. }
  358. Dictionary<String, int> dups = new Dictionary<string, int>();
  359. foreach (EntityRelationship entityRelationship in this.Relationships)
  360. {
  361. sb.Append(entityRelationship.ToXml(dups));
  362. }
  363. sb.AppendLine("\t</table>");
  364. return sb.ToString();
  365. }
  366. }
  367. }