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.

491 lines
14 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
  9. {
  10. public class KeyedReference
  11. {
  12. [XmlAttribute("tModelKey")]
  13. public string TModelKey
  14. {
  15. get
  16. {
  17. return tmodelkey;
  18. }
  19. set
  20. {
  21. if( null == value )
  22. tmodelkey = null;
  23. else
  24. tmodelkey = value.Trim().ToLower();
  25. }
  26. }
  27. string tmodelkey;
  28. [XmlAttribute("keyName")]
  29. public string KeyName
  30. {
  31. get
  32. {
  33. return keyname;
  34. }
  35. set
  36. {
  37. if( null == value )
  38. keyname = null;
  39. else
  40. keyname = value.Trim();
  41. }
  42. }
  43. string keyname;
  44. [XmlAttribute("keyValue")]
  45. public string KeyValue
  46. {
  47. get
  48. {
  49. return keyvalue;
  50. }
  51. set
  52. {
  53. if( null == value )
  54. keyvalue = null;
  55. else
  56. keyvalue = value.Trim();
  57. }
  58. }
  59. string keyvalue;
  60. public KeyedReference()
  61. {
  62. }
  63. public KeyedReference( string name, string value )
  64. {
  65. KeyName = name;
  66. KeyValue = value;
  67. }
  68. public KeyedReference( string name, string value, string key )
  69. {
  70. KeyName = name;
  71. KeyValue = value;
  72. TModelKey = key;
  73. }
  74. public void ValidateLength()
  75. {
  76. }
  77. private void ValidateOwningBusiness( EntityType parentType, KeyedReferenceType keyedReferenceType )
  78. {
  79. //
  80. // IN87: 3. If a keyedReference contains the uddi-org:owningBusiness tModelKey, then
  81. // the following conditions must be met:
  82. // 1. The keyedReference's tModelKey is case insensitive equal to uuid:4064C064-6D14-4F35-8953-9652106476A9
  83. // 2. The keyedReference's keyValue contains a valid UUID
  84. // 3. The keyedReference is contained within a categoryBag or identifierBag
  85. // 4. The containing categoryBag or identifierBag is a child element of a tModel
  86. // 5. The UUID (from condition 2) refers to an existing businessEntity
  87. // 6. The businessEntity is owned (and published by) the same publisher that is publishing the tModel from (condition 4).
  88. //
  89. //
  90. // 1. The keyedReference's tModelKey is case insensitive equal to uuid:4064C064-6D14-4F35-8953-9652106476A9
  91. //
  92. if( Context.ContextType != ContextType.Replication &&
  93. TModelKey.ToLower().Equals( UDDI.Constants.OwningBusinessTModelKey ) )
  94. {
  95. //
  96. // 3. The keyedReference is contained within a categoryBag or identifierBag
  97. //
  98. if( keyedReferenceType != KeyedReferenceType.CategoryBag &&
  99. keyedReferenceType != KeyedReferenceType.IdentifierBag )
  100. {
  101. //
  102. // Error
  103. //
  104. throw new UDDIException(
  105. ErrorType.E_fatalError,
  106. "UDDI_ERROR_FATALERROR_KROWNINGBE" );
  107. }
  108. //
  109. // 4. The containing categoryBag or identifierBag is a child element of a tModel
  110. //
  111. if( EntityType.TModel != parentType )
  112. {
  113. //
  114. // Error
  115. //
  116. throw new UDDIException(
  117. ErrorType.E_fatalError,
  118. "UDDI_ERROR_FATALERROR_KROWNINGBETMODELCHILD" );
  119. }
  120. //
  121. // 2. The keyedReference's keyValue contains a valid UUID
  122. // 5. The UUID (from condition 2) refers to an existing businessEntity
  123. // 6. The businessEntity is owned (and published by) the same publisher that is publishing the tModel from (condition 4).
  124. //
  125. try
  126. {
  127. SqlStoredProcedureAccessor sp = new SqlStoredProcedureAccessor( "net_businessEntity_validate" );
  128. sp.Parameters.Add( "@PUID", SqlDbType.NVarChar, UDDI.Constants.Lengths.UserID );
  129. sp.Parameters.Add( "@businessKey", SqlDbType.UniqueIdentifier );
  130. sp.Parameters.Add( "@flag", SqlDbType.Int );
  131. sp.Parameters.SetString( "@PUID", Context.User.ID );
  132. sp.Parameters.SetGuidFromString( "@businessKey", KeyValue );
  133. sp.Parameters.SetInt( "@flag", 0 );
  134. sp.ExecuteNonQuery();
  135. }
  136. catch
  137. {
  138. //
  139. // Error
  140. //
  141. throw new UDDIException(
  142. ErrorType.E_fatalError,
  143. "UDDI_ERROR_FATALERROR_INVALIDEVALUEORPUBLISHER" );
  144. }
  145. }
  146. }
  147. internal void Validate( string parentKey, KeyedReferenceType keyedReferenceType )
  148. {
  149. Debug.Enter();
  150. //
  151. // IN69: 4. When a keyedReference is saved within a categoryBag without specifying
  152. // a tModelKey (that is no tModelKey attribute at all) the UDDI server MUST
  153. // assume the urn:uddi-org:general_keywords tModelKey. The resulting response
  154. // MUST add to the keyedReference the attribute
  155. // tModelKey=�uuid:A035A07C-F362-44dd-8F95-E2B134BF43B4� (case does not matter).
  156. //
  157. if( KeyedReferenceType.CategoryBag == keyedReferenceType && Utility.StringEmpty( TModelKey ) )
  158. {
  159. TModelKey = Config.GetString( "TModelKey.GeneralKeywords" );
  160. }
  161. //
  162. // IN69: 3. A UDDI server MUST reject a save_xxx request with a keyedReferences
  163. // in an identifierBag where no tModelKey attribute is specified.
  164. //
  165. if( KeyedReferenceType.IdentifierBag == keyedReferenceType && Utility.StringEmpty( TModelKey ) )
  166. {
  167. throw new UDDIException(
  168. ErrorType.E_fatalError,
  169. "UDDI_ERROR_FATALERROR_IDBAG_MISSINGTMODELKEY" );
  170. }
  171. //
  172. // IN69: 1. A UDDI server MUST reject a save_xxx request with a keyedReference
  173. // with no keyName when the urn:uddi-org:general_keywords is involved
  174. //
  175. // #1718, make sure the comparison is not case sensitive.
  176. //
  177. if( Config.GetString( "TModelKey.GeneralKeywords" ).ToLower().Equals( TModelKey ) && null == keyname )
  178. {
  179. throw new UDDIException(
  180. ErrorType.E_fatalError,
  181. "UDDI_ERROR_FATALERROR_GENERALKEYWORDS_BLANKNAME" );
  182. }
  183. //
  184. // IN69: 2. A UDDI server MUST reject a save_xxx request with a
  185. // keyedReference where only the keyValue is specified
  186. //
  187. if( Utility.StringEmpty( tmodelkey ) && Utility.StringEmpty( keyname ) )
  188. {
  189. throw new UDDIException(
  190. ErrorType.E_fatalError,
  191. "UDDI_ERROR_FATALERROR_ASSERTION_MISSINGTMODELKEYORNAME" );
  192. }
  193. //
  194. // Validate TModelKey, KeyName, and KeyValue length.
  195. //
  196. if( KeyedReferenceType.Assertion == keyedReferenceType )
  197. {
  198. if( Utility.StringEmpty( tmodelkey ) ||
  199. null == keyname ||
  200. null == keyvalue )
  201. {
  202. throw new UDDIException(
  203. ErrorType.E_fatalError,
  204. "UDDI_ERROR_FATALERROR_ASSERTION_MISSINGKEYNAMEORVALUE" );
  205. }
  206. }
  207. Utility.ValidateLength( ref tmodelkey, "tModelKey", UDDI.Constants.Lengths.TModelKey );
  208. Utility.ValidateLength( ref keyname, "keyName", UDDI.Constants.Lengths.KeyName );
  209. Utility.ValidateLength( ref keyvalue, "keyValue", UDDI.Constants.Lengths.KeyValue );
  210. Debug.VerifyKey( tmodelkey );
  211. //
  212. // TODO: We are skipping validation of this keyedreference here if the parent entity key is
  213. // the same as the tModelKey for the identifer bag or category bag. Why???
  214. //
  215. // Please insert a comment to describe why this is necessary
  216. //
  217. if( parentKey != TModelKey )
  218. {
  219. //
  220. // call net_keyedReference_validate
  221. //
  222. SqlCommand cmd = new SqlCommand( "net_keyedReference_validate", ConnectionManager.GetConnection() );
  223. cmd.CommandType = CommandType.StoredProcedure;
  224. cmd.Transaction = ConnectionManager.GetTransaction();
  225. cmd.Parameters.Add( new SqlParameter( "@PUID", SqlDbType.NVarChar, UDDI.Constants.Lengths.UserID ) ).Direction = ParameterDirection.Input;
  226. cmd.Parameters.Add( new SqlParameter( "@keyedRefType", SqlDbType.TinyInt ) ).Direction = ParameterDirection.Input;
  227. cmd.Parameters.Add( new SqlParameter( "@keyValue", SqlDbType.NVarChar, UDDI.Constants.Lengths.KeyValue ) ).Direction = ParameterDirection.Input;
  228. cmd.Parameters.Add( new SqlParameter( "@tModelKey", SqlDbType.UniqueIdentifier ) ).Direction = ParameterDirection.Input;
  229. SqlParameterAccessor paramacc = new SqlParameterAccessor( cmd.Parameters );
  230. paramacc.SetString( "@PUID", Context.User.ID );
  231. paramacc.SetShort( "@keyedRefType", (short)keyedReferenceType );
  232. paramacc.SetString( "@keyValue", KeyValue );
  233. paramacc.SetGuidFromKey( "@tModelKey", TModelKey );
  234. cmd.ExecuteNonQuery();
  235. }
  236. Debug.Leave();
  237. }
  238. public void Save( string parentKey, EntityType parentType, KeyedReferenceType keyedReferenceType )
  239. {
  240. Debug.Enter();
  241. //
  242. // IN 87 Need to validate that keyedReferences that contain the uddi-org:owningBusinessKey. We can't
  243. // do this in validate because the parentKey won't be set if the parent is a new tmodel.
  244. //
  245. ValidateOwningBusiness( parentType, keyedReferenceType );
  246. SqlStoredProcedureAccessor sp = new SqlStoredProcedureAccessor();
  247. switch( parentType )
  248. {
  249. case EntityType.BusinessEntity:
  250. if( KeyedReferenceType.CategoryBag == keyedReferenceType )
  251. sp.ProcedureName = "net_businessEntity_categoryBag_save";
  252. else
  253. sp.ProcedureName = "net_businessEntity_identifierBag_save";
  254. sp.Parameters.Add( "@businessKey", SqlDbType.UniqueIdentifier );
  255. sp.Parameters.SetGuidFromString( "@businessKey", parentKey );
  256. break;
  257. case EntityType.BusinessService:
  258. Debug.Assert(
  259. KeyedReferenceType.CategoryBag == keyedReferenceType,
  260. "Unexpected keyed reference type '" + keyedReferenceType.ToString()
  261. + "' for parent entity type '" + parentType.ToString() + "'" );
  262. sp.ProcedureName = "net_businessService_categoryBag_save";
  263. sp.Parameters.Add( "@serviceKey", SqlDbType.UniqueIdentifier );
  264. sp.Parameters.SetGuidFromString( "@serviceKey", parentKey );
  265. break;
  266. case EntityType.TModel:
  267. if( KeyedReferenceType.CategoryBag == keyedReferenceType )
  268. sp.ProcedureName = "net_tModel_categoryBag_save";
  269. else
  270. sp.ProcedureName = "net_tModel_identifierBag_save";
  271. sp.Parameters.Add( "@tModelKeyParent", SqlDbType.UniqueIdentifier );
  272. sp.Parameters.SetGuidFromKey( "@tModelKeyParent", parentKey );
  273. break;
  274. default:
  275. throw new UDDIException( ErrorType.E_fatalError, "UDDI_ERROR_UNEXPECTED_PARENT_ENTITY_TYPE" , parentType.ToString() );
  276. }
  277. sp.Parameters.Add( "@keyName", SqlDbType.NVarChar, UDDI.Constants.Lengths.KeyName );
  278. sp.Parameters.Add( "@keyValue", SqlDbType.NVarChar, UDDI.Constants.Lengths.KeyValue );
  279. sp.Parameters.Add( "@tModelKey", SqlDbType.UniqueIdentifier );
  280. sp.Parameters.SetString( "@keyName", KeyName );
  281. sp.Parameters.SetString( "@keyValue", KeyValue );
  282. sp.Parameters.SetGuidFromKey( "@tModelKey", TModelKey );
  283. sp.ExecuteNonQuery();
  284. Debug.Leave();
  285. }
  286. }
  287. public class KeyedReferenceCollection : CollectionBase
  288. {
  289. public KeyedReferenceCollection()
  290. {
  291. }
  292. internal void Validate( string parentKey, KeyedReferenceType keyedReferenceType )
  293. {
  294. foreach( KeyedReference keyRef in this )
  295. {
  296. keyRef.Validate( parentKey, keyedReferenceType );
  297. }
  298. }
  299. public void Save( string parentKey, EntityType parentType, KeyedReferenceType keyedReferenceType )
  300. {
  301. foreach( KeyedReference keyRef in this )
  302. {
  303. keyRef.Save( parentKey, parentType, keyedReferenceType );
  304. }
  305. }
  306. public KeyedReference this[int index]
  307. {
  308. get { return (KeyedReference)List[index]; }
  309. set { List[index] = value; }
  310. }
  311. public int Add(KeyedReference value)
  312. {
  313. return List.Add(value);
  314. }
  315. public int Add( string name, string value )
  316. {
  317. return List.Add( new KeyedReference( name, value ) );
  318. }
  319. public int Add( string name, string value, string key )
  320. {
  321. return List.Add( new KeyedReference( name, value, key ) );
  322. }
  323. public int Add()
  324. {
  325. return List.Add( new KeyedReference() );
  326. }
  327. public void Insert(int index, KeyedReference value)
  328. {
  329. List.Insert(index, value);
  330. }
  331. public void Get( string parentKey, EntityType parentType, KeyedReferenceType keyedReferenceType )
  332. {
  333. Debug.Enter();
  334. SqlStoredProcedureAccessor sp = new SqlStoredProcedureAccessor();
  335. switch( parentType )
  336. {
  337. case EntityType.BusinessEntity:
  338. if( KeyedReferenceType.CategoryBag == keyedReferenceType )
  339. sp.ProcedureName = "net_businessEntity_categoryBag_get";
  340. else
  341. sp.ProcedureName = "net_businessEntity_identifierBag_get";
  342. sp.Parameters.Add( "@businessKey", SqlDbType.UniqueIdentifier );
  343. sp.Parameters.SetGuidFromString( "@businessKey", parentKey );
  344. break;
  345. case EntityType.BusinessService:
  346. Debug.Assert(
  347. KeyedReferenceType.CategoryBag == keyedReferenceType,
  348. "Unexpected keyed reference type '" + keyedReferenceType.ToString()
  349. + "' for parent entity type '" + parentType.ToString() + "'" );
  350. sp.ProcedureName = "net_businessService_categoryBag_get";
  351. sp.Parameters.Add( "@serviceKey", SqlDbType.UniqueIdentifier );
  352. sp.Parameters.SetGuidFromString( "@serviceKey", parentKey );
  353. break;
  354. case EntityType.TModel:
  355. if( KeyedReferenceType.CategoryBag == keyedReferenceType )
  356. sp.ProcedureName = "net_tModel_categoryBag_get";
  357. else
  358. sp.ProcedureName = "net_tModel_identifierBag_get";
  359. sp.Parameters.Add( "@tModelKey", SqlDbType.UniqueIdentifier );
  360. sp.Parameters.SetGuidFromKey( "@tModelKey", parentKey );
  361. break;
  362. default:
  363. throw new UDDIException( ErrorType.E_fatalError, "UDDI_ERROR_UNEXPECTED_PARENT_ENTITY_TYPE" , parentType.ToString() );
  364. }
  365. SqlDataReaderAccessor reader = sp.ExecuteReader();
  366. try
  367. {
  368. Read( reader );
  369. #if never
  370. while( reader.Read() )
  371. {
  372. Add(
  373. reader.GetString( "keyName" ),
  374. reader.GetString( "keyValue" ),
  375. reader.GetKeyFromGuid( "tModelKey" ) );
  376. }
  377. #endif
  378. }
  379. finally
  380. {
  381. reader.Close();
  382. }
  383. Debug.Leave();
  384. }
  385. public void Read( SqlDataReaderAccessor reader )
  386. {
  387. while( reader.Read() )
  388. {
  389. Add( reader.GetString( "keyName" ),
  390. reader.GetString( "keyValue" ),
  391. reader.GetKeyFromGuid( "tModelKey" ) );
  392. }
  393. }
  394. public int IndexOf( KeyedReference value )
  395. {
  396. return List.IndexOf( value );
  397. }
  398. public bool Contains( KeyedReference value )
  399. {
  400. return List.Contains( value );
  401. }
  402. public void Remove( KeyedReference value )
  403. {
  404. List.Remove( value );
  405. }
  406. public void CopyTo( KeyedReference[] array )
  407. {
  408. foreach( KeyedReference keyedReference in array )
  409. Add( keyedReference );
  410. }
  411. public KeyedReference[] ToArray()
  412. {
  413. return (KeyedReference[])InnerList.ToArray( typeof( KeyedReference ) );
  414. }
  415. }
  416. }