PageRenderTime 987ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/activewriter/trunk/src/Dsl/CodeGeneration/ModelClass.cs

https://github.com/castleprojectcontrib/Projects
C# | 230 lines | 185 code | 27 blank | 18 comment | 51 complexity | 095d007bd1cb6fc1d0f3e302bc5c6c02 MD5 | raw file
  1. #region License
  2. // Copyright 2004-2010 Castle Project - http:www.castleproject.org/
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http:www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #endregion
  17. namespace Castle.ActiveWriter
  18. {
  19. using System;
  20. using System.CodeDom;
  21. using System.Collections.ObjectModel;
  22. using CodeGeneration;
  23. using Microsoft.VisualStudio.Modeling;
  24. public partial class ModelClass
  25. {
  26. #region Public Code Generation Methods
  27. public bool IsGeneric()
  28. {
  29. return
  30. (Model.UseGenerics && UseGenerics == InheritableBoolean.Inherit) ||
  31. UseGenerics == InheritableBoolean.True;
  32. }
  33. public bool AreRelationsGeneric()
  34. {
  35. return
  36. (Model.UseGenericRelations && UseGenericRelations == InheritableBoolean.Inherit) ||
  37. UseGenericRelations == InheritableBoolean.True;
  38. }
  39. public bool DoesImplementINotifyPropertyChanged()
  40. {
  41. return (Model.ImplementINotifyPropertyChanged && ImplementINotifyPropertyChanged == InheritableBoolean.Inherit) ||
  42. ImplementINotifyPropertyChanged == InheritableBoolean.True;
  43. }
  44. public bool DoesImplementINotifyPropertyChanging()
  45. {
  46. return (Model.ImplementINotifyPropertyChanging && ImplementINotifyPropertyChanging == InheritableBoolean.Inherit) ||
  47. ImplementINotifyPropertyChanging == InheritableBoolean.True;
  48. }
  49. public bool HasPropertyWithValidators()
  50. {
  51. return Properties.Find(property => property.IsValidatorSet()) != null;
  52. }
  53. public CodeAttributeDeclaration GetActiveRecordAttribute()
  54. {
  55. CodeAttributeDeclaration attribute = new CodeAttributeDeclaration("ActiveRecord");
  56. if (!string.IsNullOrEmpty(Table))
  57. attribute.Arguments.Add(AttributeHelper.GetPrimitiveAttributeArgument(Table));
  58. if (Cache != CacheEnum.Undefined)
  59. attribute.Arguments.Add(AttributeHelper.GetNamedEnumAttributeArgument("Cache", "CacheEnum", Cache));
  60. if (!string.IsNullOrEmpty(CustomAccess))
  61. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("CustomAccess", CustomAccess));
  62. if (!string.IsNullOrEmpty(DiscriminatorColumn))
  63. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("DiscriminatorColumn", DiscriminatorColumn));
  64. if (!string.IsNullOrEmpty(DiscriminatorType))
  65. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("DiscriminatorType", DiscriminatorType));
  66. if (!string.IsNullOrEmpty(DiscriminatorValue))
  67. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("DiscriminatorValue", DiscriminatorValue));
  68. if (Lazy)
  69. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("Lazy", Lazy));
  70. if (!string.IsNullOrEmpty(Proxy))
  71. attribute.Arguments.Add(AttributeHelper.GetNamedTypeAttributeArgument("Proxy", Proxy));
  72. if (!string.IsNullOrEmpty(Schema))
  73. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("Schema", Schema));
  74. if (!string.IsNullOrEmpty(Where))
  75. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("Where", Where));
  76. if (DynamicInsert)
  77. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("DynamicInsert", DynamicInsert));
  78. if (DynamicUpdate)
  79. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("DynamicUpdate", DynamicUpdate));
  80. if (!string.IsNullOrEmpty(Persister))
  81. attribute.Arguments.Add(AttributeHelper.GetNamedTypeAttributeArgument("Persister", Persister));
  82. if (SelectBeforeUpdate)
  83. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("SelectBeforeUpdate", SelectBeforeUpdate));
  84. if (Polymorphism != Polymorphism.Implicit)
  85. attribute.Arguments.Add(AttributeHelper.GetNamedEnumAttributeArgument("Polymorphism", "Polymorphism", Polymorphism));
  86. if (!Mutable)
  87. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("Mutable", Mutable));
  88. if (BatchSize != 1)
  89. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("BatchSize", BatchSize));
  90. if (Locking != OptimisticLocking.Version)
  91. attribute.Arguments.Add(AttributeHelper.GetNamedEnumAttributeArgument("Locking", "OptimisticLocking", Locking));
  92. if (!UseAutoImport)
  93. attribute.Arguments.Add(AttributeHelper.GetNamedAttributeArgument("UseAutoImport", UseAutoImport));
  94. return attribute;
  95. }
  96. public void AddActiveRecordAttributes(CodeTypeDeclaration classDeclaration)
  97. {
  98. classDeclaration.CustomAttributes.Add(GetActiveRecordAttribute());
  99. AddJoinedBaseAttribute(classDeclaration);
  100. }
  101. public void AddJoinedBaseAttribute(CodeTypeDeclaration classDeclaration)
  102. {
  103. if (IsJoinedBase)
  104. {
  105. classDeclaration.CustomAttributes.Add(new CodeAttributeDeclaration("JoinedBase"));
  106. }
  107. }
  108. public string EffectiveTable
  109. {
  110. get
  111. {
  112. return string.IsNullOrEmpty(Table)
  113. ? Name
  114. : Table;
  115. }
  116. }
  117. public ModelClass ClassParent
  118. {
  119. get
  120. {
  121. LinkedElementCollection<ModelClass> parentClasses = InheritanceRelation.GetTargetModelClasses(this);
  122. if (parentClasses.Count == 1)
  123. return parentClasses[0];
  124. return null;
  125. }
  126. }
  127. public ReadOnlyCollection<ModelClass> ClassChildren
  128. {
  129. get
  130. {
  131. return new ReadOnlyCollection<ModelClass>(InheritanceRelation.GetSourceModelClasses(this));
  132. }
  133. }
  134. public bool IsJoinedBase
  135. {
  136. get
  137. {
  138. // We need the JoinedBase attribute on the top class and also any subclasses with further children.
  139. return ClassChildren.Count > 0;
  140. }
  141. }
  142. public bool IsJoinedSubclass
  143. {
  144. get
  145. {
  146. return ClassParent != null;
  147. }
  148. }
  149. public bool IsSubclass
  150. {
  151. get
  152. {
  153. return ClassParent != null;
  154. }
  155. }
  156. public bool PrimaryKeySpecifiedByUser
  157. {
  158. get
  159. {
  160. foreach (ModelProperty modelProperty in Properties)
  161. if (modelProperty.KeyType != KeyType.None)
  162. return true;
  163. return false;
  164. }
  165. }
  166. public PropertyData PrimaryKey
  167. {
  168. get
  169. {
  170. foreach (ModelProperty modelProperty in Properties)
  171. if (modelProperty.KeyType != KeyType.None)
  172. return new PropertyData(modelProperty);
  173. // If the user didn't specify a key, see if we can generate one from the common
  174. // primary key information.
  175. if (!String.IsNullOrEmpty(Model.CommonPrimaryKeyPropertyFormat))
  176. {
  177. PropertyData propertyData = new PropertyData(String.Format(Model.CommonPrimaryKeyPropertyFormat, Name), this);
  178. if (!String.IsNullOrEmpty(Model.CommonPrimaryKeyColumnFormat))
  179. propertyData.Column = String.Format(Model.CommonPrimaryKeyColumnFormat, EffectiveTable);
  180. propertyData.ColumnType = Model.CommonPrimaryKeyColumnType;
  181. propertyData.Generator = Model.CommonPrimaryKeyGenerator;
  182. propertyData.KeyType = KeyType.PrimaryKey;
  183. propertyData.NotNull = true;
  184. // The column name is implicitly the same as the property name. This removes the extra argument if possible.
  185. if (propertyData.Name == propertyData.Column)
  186. propertyData.Column = null;
  187. return propertyData;
  188. }
  189. return null;
  190. }
  191. }
  192. public PropertyAccess EffectiveAccess
  193. {
  194. get
  195. {
  196. if (Access == InheritablePropertyAccess.Inherit)
  197. return Model.Access;
  198. return Access.GetMatchingPropertyAccess();
  199. }
  200. }
  201. #endregion
  202. }
  203. }