UserFormAdapter.cs
上传用户:husern
上传日期:2022-03-24
资源大小:534k
文件大小:6k
源码类别:

编辑器/阅读器

开发平台:

C#

  1. // -- FILE ------------------------------------------------------------------
  2. // name       : UserFormAdapter.cs
  3. // project    : Itenso Web User Forms
  4. // created    : Jani Giannoudis - 2008.10.30
  5. // language   : c#
  6. // environment: .NET 2.0
  7. // copyright  : (c) 2008 by Itenso GmbH, Switzerland
  8. // --------------------------------------------------------------------------
  9. using System;
  10. using System.Web.UI;
  11. using Itenso.WebUserForms.Data.Form;
  12. using Itenso.WebUserForms.Data.Variable;
  13. using Itenso.WebUserForms.Controls;
  14. namespace Itenso.WebUserForms.Runtime
  15. {
  16. // ------------------------------------------------------------------------
  17. public sealed class UserFormAdapter
  18. {
  19. // ----------------------------------------------------------------------
  20. private UserFormAdapter()
  21. {
  22. } // UserFormAdapter
  23. // ----------------------------------------------------------------------
  24. public static IForm ExtractForm( UserControl userControl )
  25. {
  26. if ( userControl == null )
  27. {
  28. throw new ArgumentNullException( "userControl" );
  29. }
  30. IUserForm userForm = UserFormCollector.CollectUserForm( userControl );
  31. return ExtractForm( userForm );
  32. } // ExtractForm
  33. // ----------------------------------------------------------------------
  34. public static void ApplyForm( UserControl userControl, IForm form )
  35. {
  36. ApplyForm( userControl, form, false );
  37. } // ApplyForm
  38. // ----------------------------------------------------------------------
  39. public static void ApplyForm( UserControl userControl, IForm form, bool versionCheck )
  40. {
  41. if ( userControl == null )
  42. {
  43. throw new ArgumentNullException( "userControl" );
  44. }
  45. if ( form == null )
  46. {
  47. throw new ArgumentNullException( "form" );
  48. }
  49. IUserForm userForm = UserFormCollector.CollectUserForm( userControl );
  50. ApplyForm( userForm, form, versionCheck );
  51. } // ApplyForm
  52. // ----------------------------------------------------------------------
  53. private static void ApplyForm( IUserForm userForm, IForm form, bool versionCheck )
  54. {
  55. if ( userForm == null )
  56. {
  57. throw new ArgumentNullException( "userForm" );
  58. }
  59. if ( form == null )
  60. {
  61. throw new ArgumentNullException( "form" );
  62. }
  63. if ( !form.FormType.Equals( userForm.Header.Type ) )
  64. {
  65. throw new UserFormAdapterException( "incompatible user form name", userForm, form );
  66. }
  67. if ( !form.Name.Equals( userForm.Header.Name ) )
  68. {
  69. throw new UserFormAdapterException( "incompatible user form name", userForm, form );
  70. }
  71. if ( versionCheck )
  72. {
  73. if ( !form.FormVersion.Equals( userForm.Header.FormVersion ) )
  74. {
  75. throw new UserFormAdapterException( "incompatible user form version", userForm, form );
  76. }
  77. }
  78. // header
  79. userForm.Header.FormType = form.FormType;
  80. userForm.Header.FormName = form.Name;
  81. userForm.Header.FormId = form.FormId;
  82. userForm.Header.FormVersion = form.FormVersion;
  83. userForm.Header.FormCreated = form.Created;
  84. userForm.Header.FormCreatedByUser = form.CreatedByUser;
  85. userForm.Header.FormLastUpdated = form.LastUpdated;
  86. userForm.Header.FormLastUpdatedByUser = form.LastUpdatedByUser;
  87. ApplyFormItems( userForm, form );
  88. } // ApplyForm
  89. // ----------------------------------------------------------------------
  90. private static void ApplyFormItems( IUserForm userForm, IFormGroup formGroup )
  91. {
  92. if ( !formGroup.Name.Equals( userForm.Header.Name ) )
  93. {
  94. return;
  95. }
  96. foreach ( IFormEntity formEntity in formGroup.Entities )
  97. {
  98. switch ( formEntity.Kind )
  99. {
  100. case FormEntityType.Form:
  101. IFormGroup subFormGroup = formEntity as IFormGroup;
  102. if ( subFormGroup != null )
  103. {
  104. IUserForm webSubForm = userForm.Subforms.FindByFormName( subFormGroup.Name );
  105. if ( webSubForm != null )
  106. {
  107. ApplyFormItems( webSubForm, subFormGroup );
  108. }
  109. }
  110. break;
  111. case FormEntityType.Field:
  112. IFormField formField = formEntity as IFormField;
  113. if ( formField != null )
  114. {
  115. IUserFormField userFormField = userForm.Fields.FindByFieldName( formField.Name );
  116. if ( userFormField != null )
  117. {
  118. userFormField.FieldValue = formField.Content;
  119. }
  120. }
  121. break;
  122. }
  123. }
  124. } // ApplyFormItems
  125. // ----------------------------------------------------------------------
  126. private static IForm ExtractForm( IUserForm userForm )
  127. {
  128. if ( userForm == null )
  129. {
  130. throw new ArgumentNullException( "userForm" );
  131. }
  132. Form form = new Form();
  133. form.FormType = userForm.Header.Type;
  134. form.Name = userForm.Header.Name;
  135. form.FormVersion = userForm.Header.FormVersion;
  136. form.FormId = userForm.Header.FormId;
  137. if ( userForm.Header.FormCreated.HasValue )
  138. {
  139. form.Created = userForm.Header.FormCreated.Value;
  140. }
  141. form.CreatedByUser = userForm.Header.FormCreatedByUser;
  142. if ( userForm.Header.FormLastUpdated.HasValue )
  143. {
  144. form.LastUpdated = userForm.Header.FormLastUpdated.Value;
  145. }
  146. form.LastUpdatedByUser = userForm.Header.FormLastUpdatedByUser;
  147. ExtractFormItems( userForm, form );
  148. return form;
  149. } // ExtractForm
  150. // ----------------------------------------------------------------------
  151. private static void ExtractFormItems( IUserForm userForm, IForm form )
  152. {
  153. foreach ( IUserFormField userFormField in userForm.Fields )
  154. {
  155. FormField formField = new FormField( userFormField.FieldName, userFormField.FieldValue );
  156. form.Entities.Add( formField );
  157. }
  158. foreach ( IUserForm webSubform in userForm.Subforms )
  159. {
  160. Form subform = new Form( webSubform.Header.Name );
  161. form.Entities.Add( subform );
  162. ExtractFormItems( webSubform, subform );
  163. }
  164. } // ExtractFormItems
  165. } // class UserFormAdapter
  166. } // namespace Itenso.WebUserForms.Runtime
  167. // -- EOF -------------------------------------------------------------------