Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

475 lines
10 KiB

  1. using System;
  2. using System.Data;
  3. using System.Collections;
  4. using System.Data.SqlClient;
  5. using System.Xml.Serialization;
  6. using UDDI;
  7. using UDDI.Diagnostics;
  8. namespace UDDI.API.Business
  9. {
  10. public class Contact
  11. {
  12. //
  13. // Attribute: useType
  14. //
  15. [ XmlAttribute( "useType" ) ]
  16. public string UseType;
  17. //
  18. // Element: description
  19. //
  20. private DescriptionCollection descriptions;
  21. [ XmlElement( "description" ) ]
  22. public DescriptionCollection Descriptions
  23. {
  24. get
  25. {
  26. if( null == descriptions )
  27. descriptions = new DescriptionCollection();
  28. return descriptions;
  29. }
  30. set { descriptions = value; }
  31. }
  32. //
  33. // Element: personName
  34. //
  35. [ XmlElement( "personName" ) ]
  36. public string PersonName
  37. {
  38. get{ return personName; }
  39. set{ personName = value; }
  40. }
  41. private string personName;
  42. //
  43. // Element: phone
  44. //
  45. private PhoneCollection phones;
  46. [ XmlElement( "phone" ) ]
  47. public PhoneCollection Phones
  48. {
  49. get
  50. {
  51. if( null == phones )
  52. phones = new PhoneCollection();
  53. return phones;
  54. }
  55. set { phones = value; }
  56. }
  57. //
  58. // Element: email
  59. //
  60. private EmailCollection emails;
  61. [ XmlElement( "email" ) ]
  62. public EmailCollection Emails
  63. {
  64. get
  65. {
  66. if( null == emails )
  67. emails = new EmailCollection();
  68. return emails;
  69. }
  70. set { emails = value; }
  71. }
  72. //
  73. // Element: address
  74. //
  75. private AddressCollection addresses;
  76. [ XmlElement( "address" ) ]
  77. public AddressCollection Addresses
  78. {
  79. get
  80. {
  81. if( null == addresses )
  82. addresses = new AddressCollection();
  83. return addresses;
  84. }
  85. set { addresses = value; }
  86. }
  87. public Contact()
  88. {
  89. }
  90. public Contact( string personName, string useType )
  91. {
  92. PersonName = personName;
  93. UseType = useType;
  94. }
  95. internal void Validate()
  96. {
  97. Debug.Enter();
  98. Utility.ValidateLength( ref UseType, "useType", UDDI.Constants.Lengths.UseType );
  99. Utility.ValidateLength( ref personName, "personName", UDDI.Constants.Lengths.PersonName );
  100. Descriptions.Validate();
  101. Emails.Validate();
  102. Phones.Validate();
  103. Addresses.Validate();
  104. Debug.Leave();
  105. }
  106. public void Save( string businessKey )
  107. {
  108. //
  109. // Create a command object to invoke the stored procedure
  110. //
  111. SqlCommand cmd = new SqlCommand( "net_businessEntity_contact_save", ConnectionManager.GetConnection() );
  112. cmd.Transaction = ConnectionManager.GetTransaction();
  113. cmd.CommandType = CommandType.StoredProcedure;
  114. //
  115. // Parameters
  116. //
  117. cmd.Parameters.Add( new SqlParameter( "@businessKey", SqlDbType.UniqueIdentifier ) ).Direction = ParameterDirection.Input;
  118. cmd.Parameters.Add( new SqlParameter( "@useType", SqlDbType.NVarChar, UDDI.Constants.Lengths.UseType ) ).Direction = ParameterDirection.Input;
  119. cmd.Parameters.Add( new SqlParameter( "@personName", SqlDbType.NVarChar, UDDI.Constants.Lengths.PersonName ) ).Direction = ParameterDirection.Input;
  120. cmd.Parameters.Add( new SqlParameter( "@contactID", SqlDbType.BigInt ) ).Direction = ParameterDirection.Output;
  121. //
  122. // Set parameter values and execute query
  123. //
  124. SqlParameterAccessor parmacc = new SqlParameterAccessor( cmd.Parameters );
  125. parmacc.SetGuidFromString( "@businessKey", businessKey );
  126. parmacc.SetString( "@personName", PersonName );
  127. parmacc.SetString( "@useType", UseType );
  128. cmd.ExecuteScalar();
  129. //
  130. // Move out parameters into local variables
  131. //
  132. long ContactID = parmacc.GetLong( "@contactID" );
  133. //
  134. // Save sub-objects
  135. //
  136. Descriptions.Save( ContactID, EntityType.Contact );
  137. Phones.Save( ContactID );
  138. Emails.Save( ContactID );
  139. Addresses.Save( ContactID );
  140. }
  141. public void Get( int contactId )
  142. {
  143. SqlStoredProcedureAccessor sp = new SqlStoredProcedureAccessor( "net_businessEntity_contact_get_batch" );
  144. sp.Parameters.Add( "@contactId", SqlDbType.BigInt );
  145. sp.Parameters.SetLong( "@contactId", contactId );
  146. SqlDataReaderAccessor reader = null;
  147. ArrayList addressIDs = new ArrayList();
  148. try
  149. {
  150. //
  151. // net_businessEntity_contact_get_batch will return the objects contained in a business in the following order:
  152. //
  153. // - descriptions
  154. // - phones
  155. // - emails
  156. // - addresses
  157. reader = sp.ExecuteReader();
  158. //
  159. // Read the descriptions
  160. //
  161. Descriptions.Read( reader );
  162. //
  163. // Read the phones
  164. //
  165. if ( true == reader.NextResult() )
  166. {
  167. Phones.Read( reader );
  168. }
  169. //
  170. // Read the emails
  171. //
  172. if ( true == reader.NextResult() )
  173. {
  174. Emails.Read( reader );
  175. }
  176. //
  177. // Read the addresses
  178. //
  179. if ( true == reader.NextResult() )
  180. {
  181. addressIDs = Addresses.Read( reader );
  182. }
  183. }
  184. finally
  185. {
  186. if( reader != null )
  187. {
  188. reader.Close();
  189. }
  190. }
  191. //
  192. // These calls will make separate sproc calls, so we have to close our reader first.
  193. //
  194. Addresses.Populate( addressIDs );
  195. #if never
  196. //
  197. // Call get method on sub-objects personName and UseType
  198. // should have been populate by contacts.get() method;
  199. //
  200. Descriptions.Get( contactId, EntityType.Contact );
  201. Phones.Get( contactId );
  202. Emails.Get( contactId );
  203. Addresses.Get( contactId );
  204. #endif
  205. }
  206. }
  207. public class ContactCollection : CollectionBase
  208. {
  209. internal void Validate()
  210. {
  211. foreach( Contact c in this )
  212. {
  213. c.Validate();
  214. }
  215. }
  216. public void Save( string businessKey )
  217. {
  218. //
  219. // Walk collection and call save on individual contact instances
  220. //
  221. foreach( Contact c in this )
  222. {
  223. c.Save( businessKey );
  224. }
  225. }
  226. public void Get( string businessKey )
  227. {
  228. //
  229. // Create a command object to invoke the stored procedure net_get_contacts
  230. //
  231. SqlStoredProcedureAccessor cmd = new SqlStoredProcedureAccessor( "net_businessEntity_contacts_get" );
  232. //
  233. // Input parameters
  234. //
  235. cmd.Parameters.Add( "@businessKey", SqlDbType.UniqueIdentifier, ParameterDirection.Input );
  236. cmd.Parameters.SetGuidFromString( "@businessKey", businessKey );
  237. //
  238. // Run the stored procedure
  239. //
  240. SqlDataReaderAccessor reader = cmd.ExecuteReader();
  241. ArrayList contactIds = null;
  242. try
  243. {
  244. contactIds = Read( reader );
  245. }
  246. finally
  247. {
  248. reader.Close();
  249. }
  250. Populate( contactIds );
  251. #if never
  252. const int ContactIdIndex = 0;
  253. const int UseTypeIndex = 1;
  254. const int PersonNameIndex = 2;
  255. ArrayList contactIds = new ArrayList();
  256. //
  257. // Create a command object to invoke the stored procedure net_get_contacts
  258. //
  259. SqlCommand cmd = new SqlCommand( "net_businessEntity_contacts_get", ConnectionManager.GetConnection() );
  260. cmd.Transaction = ConnectionManager.GetTransaction();
  261. cmd.CommandType = CommandType.StoredProcedure;
  262. //
  263. // Input parameters
  264. //
  265. cmd.Parameters.Add( new SqlParameter( "@businessKey", SqlDbType.UniqueIdentifier ) ).Direction = ParameterDirection.Input;
  266. //
  267. // Set parameter values
  268. //
  269. SqlParameterAccessor populator = new SqlParameterAccessor( cmd.Parameters );
  270. populator.SetGuidFromString( "@businessKey", businessKey );
  271. //
  272. // Run the stored procedure
  273. //
  274. SqlDataReader rdr = cmd.ExecuteReader();
  275. try
  276. {
  277. SqlDataReaderAccessor dracc = new SqlDataReaderAccessor( rdr );
  278. //
  279. // The contacts will be contained in the result set
  280. //
  281. while( rdr.Read() )
  282. {
  283. //
  284. // construct a new contact from the data in this row, fully populate contact and add to collection
  285. //
  286. Add( new Contact( dracc.GetString( PersonNameIndex ), dracc.GetString( UseTypeIndex ) ) );
  287. contactIds.Add( dracc.GetInt( ContactIdIndex ) );
  288. }
  289. }
  290. finally
  291. {
  292. rdr.Close();
  293. }
  294. int i = 0;
  295. foreach( Contact contact in this )
  296. {
  297. contact.Get( (int) contactIds[ i++ ] );
  298. }
  299. #endif
  300. }
  301. public ArrayList Read( SqlDataReaderAccessor reader )
  302. {
  303. const int ContactIdIndex = 0;
  304. const int UseTypeIndex = 1;
  305. const int PersonNameIndex = 2;
  306. ArrayList contactIds = new ArrayList();
  307. //
  308. // The contacts will be contained in the result set
  309. //
  310. while( reader.Read() )
  311. {
  312. //
  313. // construct a new contact from the data in this row, fully populate contact and add to collection
  314. //
  315. contactIds.Add( reader.GetInt( ContactIdIndex ) );
  316. Add( new Contact( reader.GetString( PersonNameIndex ), reader.GetString( UseTypeIndex ) ) );
  317. }
  318. return contactIds;
  319. }
  320. public void Populate( ArrayList contactIds )
  321. {
  322. int i = 0;
  323. foreach( Contact contact in this )
  324. {
  325. contact.Get( (int) contactIds[ i++ ] );
  326. }
  327. }
  328. public Contact this[ int index ]
  329. {
  330. get { return ( Contact)List[index]; }
  331. set { List[ index ] = value; }
  332. }
  333. public int Add()
  334. {
  335. return List.Add( new Contact() );
  336. }
  337. public int Add( string personName )
  338. {
  339. return List.Add( new Contact( personName, null ) );
  340. }
  341. public int Add( string personName, string useType )
  342. {
  343. return List.Add( new Contact( personName, useType ) );
  344. }
  345. public int Add( Contact value )
  346. {
  347. return List.Add( value );
  348. }
  349. public void Insert( int index, Contact value )
  350. {
  351. List.Insert( index, value );
  352. }
  353. public int IndexOf( Contact value )
  354. {
  355. return List.IndexOf( value );
  356. }
  357. public bool Contains( Contact value )
  358. {
  359. return List.Contains( value );
  360. }
  361. public void Remove( Contact value )
  362. {
  363. List.Remove( value );
  364. }
  365. public void CopyTo( Contact[] array )
  366. {
  367. foreach( Contact contact in array )
  368. Add( contact );
  369. }
  370. public Contact[] ToArray()
  371. {
  372. return (Contact[])InnerList.ToArray( typeof( Contact ) );
  373. }
  374. public void Sort()
  375. {
  376. InnerList.Sort( new ContactComparer() );
  377. }
  378. internal class ContactComparer : IComparer
  379. {
  380. public int Compare( object x, object y )
  381. {
  382. Contact entity1 = (Contact)x;
  383. Contact entity2 = (Contact)y;
  384. //
  385. // Check for null PersonName.
  386. //
  387. if( null==entity1.PersonName && null==entity2.PersonName )
  388. return 0;
  389. else if( null==entity1.PersonName )
  390. return -1;
  391. else if( null==entity2.PersonName )
  392. return 1;
  393. else
  394. return string.Compare( entity1.PersonName, entity2.PersonName, true );
  395. }
  396. }
  397. }
  398. }