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.

344 lines
9.1 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 Name
  11. {
  12. //
  13. // Attribute: xml:lang
  14. //
  15. private string isoLangCode;
  16. [XmlAttribute( "xml:lang" )]
  17. public string IsoLangCode
  18. {
  19. get { return isoLangCode; }
  20. set { isoLangCode = value; }
  21. }
  22. //
  23. // Element: Value
  24. //
  25. [XmlText]
  26. public string Value;
  27. public Name()
  28. {
  29. }
  30. //
  31. // 741019 - use the UDDI site language if one is not specified.
  32. //
  33. public Name( string name ) : this( Config.GetString( "Setup.WebServer.ProductLanguage", "en" ), name )
  34. {
  35. }
  36. public Name( string isoLangCode, string name )
  37. {
  38. this.IsoLangCode = isoLangCode;
  39. this.Value = name;
  40. }
  41. public void Save( string parentKey, EntityType parentType )
  42. {
  43. Debug.Enter();
  44. SqlStoredProcedureAccessor sp = new SqlStoredProcedureAccessor();
  45. switch( parentType )
  46. {
  47. case EntityType.BusinessEntity:
  48. sp.ProcedureName = "net_businessEntity_name_save";
  49. sp.Parameters.Add( "@businessKey", SqlDbType.UniqueIdentifier );
  50. sp.Parameters.SetGuidFromString( "@businessKey", parentKey );
  51. break;
  52. case EntityType.BusinessService:
  53. sp.ProcedureName = "net_businessService_name_save";
  54. sp.Parameters.Add( "@serviceKey", SqlDbType.UniqueIdentifier );
  55. sp.Parameters.SetGuidFromString( "@serviceKey", parentKey );
  56. break;
  57. default:
  58. //throw new UDDIException( ErrorType.E_fatalError, "Unexpected parent entity type '" + parentType.ToString() + "'" );
  59. throw new UDDIException( ErrorType.E_fatalError, "UDDI_ERROR_UNEXPECTED_PARENT_ENTITY_TYPE", parentType.ToString() );
  60. }
  61. sp.Parameters.Add( "@isoLangCode", SqlDbType.VarChar, UDDI.Constants.Lengths.IsoLangCode );
  62. sp.Parameters.Add( "@name", SqlDbType.NVarChar, UDDI.Constants.Lengths.Name );
  63. sp.Parameters.SetString( "@isoLangCode", ( 1 == Context.ApiVersionMajor ? Context.User.IsoLangCode : IsoLangCode ) );
  64. sp.Parameters.SetString( "@name", Value );
  65. sp.ExecuteNonQuery();
  66. Debug.Leave();
  67. }
  68. }
  69. /// ********************************************************************
  70. /// class NameCollection
  71. /// --------------------------------------------------------------------
  72. /// <summary>
  73. /// </summary>
  74. /// ********************************************************************
  75. ///
  76. public class NameCollection : CollectionBase
  77. {
  78. public NameCollection()
  79. {
  80. }
  81. public void Get( string parentKey, EntityType parentType )
  82. {
  83. SqlStoredProcedureAccessor sp = new SqlStoredProcedureAccessor();
  84. switch( parentType )
  85. {
  86. case EntityType.BusinessEntity:
  87. sp.ProcedureName = "net_businessEntity_names_get";
  88. sp.Parameters.Add( "@businessKey", SqlDbType.UniqueIdentifier );
  89. sp.Parameters.SetGuidFromString( "@businessKey", parentKey );
  90. break;
  91. case EntityType.BusinessService:
  92. sp.ProcedureName = "net_businessService_names_get";
  93. sp.Parameters.Add( "@serviceKey", SqlDbType.UniqueIdentifier );
  94. sp.Parameters.SetGuidFromString( "@serviceKey", parentKey );
  95. break;
  96. default:
  97. // throw new UDDIException( ErrorType.E_fatalError, "Unexpected parent entity type '" + parentType.ToString() + "'" );
  98. throw new UDDIException( ErrorType.E_fatalError, "UDDI_ERROR_UNEXPECTED_PARENT_ENTITY_TYPE", parentType.ToString() );
  99. }
  100. SqlDataReaderAccessor reader = sp.ExecuteReader();
  101. try
  102. {
  103. Read( reader );
  104. }
  105. finally
  106. {
  107. reader.Close();
  108. }
  109. }
  110. public void Read( SqlDataReaderAccessor reader )
  111. {
  112. if( 1 == Context.ApiVersionMajor )
  113. {
  114. if( reader.Read() )
  115. Add( null, reader.GetString( "name" ) );
  116. }
  117. else
  118. {
  119. while( reader.Read() )
  120. Add( reader.GetString( "isoLangCode" ), reader.GetString( "name" ) );
  121. }
  122. }
  123. internal void ValidateForFind()
  124. {
  125. //
  126. // For v1 messages, we need to throw an exception. But for v2, errata 3
  127. // says that we need to just truncate.
  128. //
  129. if( 1 == Context.ApiVersionMajor )
  130. {
  131. foreach( Name name in this )
  132. {
  133. if( null != name.Value && name.Value.Trim().Length > UDDI.Constants.Lengths.Name )
  134. {
  135. // throw new UDDIException( ErrorType.E_nameTooLong, "A name specified in the search exceed the allowable length" );
  136. throw new UDDIException( ErrorType.E_nameTooLong, "UDDI_ERROR_NAME_TOO_LONG" );
  137. }
  138. }
  139. }
  140. else
  141. {
  142. foreach( Name name in this )
  143. Utility.ValidateLength( ref name.Value, "name", UDDI.Constants.Lengths.Name );
  144. }
  145. }
  146. internal void Validate()
  147. {
  148. int minLength = 1;
  149. int maxLength = UDDI.Constants.Lengths.Name;
  150. int count = this.Count;
  151. //
  152. // We have to make sure we have a names since the schema has made <name> optional to accomodate
  153. // service projections.
  154. //
  155. if( 0 == count )
  156. {
  157. //throw new UDDIException( ErrorType.E_fatalError, "Name is a required element." );
  158. throw new UDDIException( ErrorType.E_fatalError, "UDDI_ERROR_NAME_IS_A_REQUIRED_ELEMENT" );
  159. }
  160. if( 1 == Context.ApiVersionMajor )
  161. {
  162. //
  163. // Validate the name string length.
  164. //
  165. Utility.ValidateLength( ref this[ 0 ].Value, "name", maxLength, minLength );
  166. return;
  167. }
  168. for( int i = 0; i < count; i ++ )
  169. {
  170. //
  171. // The language code should be lower-case characters.
  172. //
  173. string isoLangCode = this[ i ].IsoLangCode;
  174. if( null != isoLangCode )
  175. this[ i ].IsoLangCode = isoLangCode.ToLower();
  176. }
  177. bool languageAssigned = false;
  178. for( int i = 0; i < count; i ++ )
  179. {
  180. //
  181. // Validate the name string length.
  182. //
  183. Utility.ValidateLength( ref this[ i ].Value, "name", maxLength, minLength );
  184. //
  185. // Validate the language code. If one was not specified,
  186. // we'll use the publisher's default language.
  187. //
  188. if( Utility.StringEmpty( this[ i ].IsoLangCode ) )
  189. {
  190. //
  191. // Only one name can have an unassigned language.
  192. //
  193. if( languageAssigned )
  194. {
  195. // throw new UDDIException(
  196. // ErrorType.E_languageError,
  197. //"More than one name was found to have an unassigned language" );
  198. throw new UDDIException( ErrorType.E_languageError, "UDDI_ERROR_MORE_THAN_ONE_NAME_UNASSIGNED" );
  199. }
  200. languageAssigned = true;
  201. //
  202. // Fix: Bug 2340 9/9/2002, creeves
  203. //
  204. // if( i > 0 )
  205. // {
  206. // throw new UDDIException(
  207. // ErrorType.E_languageError,
  208. // "Only the first name can have a blank or missing xml:lang attribute. All other names must have a valid xml:lang attribute." );
  209. // }
  210. this[ i ].IsoLangCode = Context.User.IsoLangCode;
  211. }
  212. }
  213. // Split loops and fill in default IsoLangCode first (if needed),
  214. // then look for repeated languages
  215. //
  216. for( int i = 0; i < count; i ++ )
  217. {
  218. //
  219. // Check to make sure there is only one name
  220. // per language.
  221. //
  222. string isoLangCode = this[ i ].IsoLangCode;
  223. Debug.Write( SeverityType.Info, CategoryType.Data, "Name[" + i + "]: " + this[ i ].Value + ", IsoLangCode: " + isoLangCode );
  224. for( int j = i + 1; j < count; j ++ )
  225. {
  226. if( false == Utility.StringEmpty(this[ j ].IsoLangCode)
  227. && isoLangCode.ToLower() == this[ j ].IsoLangCode.ToLower() )
  228. {
  229. Debug.Write( SeverityType.Info, CategoryType.Data, "Error: Name[" + j + "]: " + this[ j ].Value + ", IsoLangCode " + this[ j ].IsoLangCode + " matches IsoLangCode[" + i + "]: " + isoLangCode );
  230. //throw new UDDIException(
  231. //ErrorType.E_languageError,
  232. //"More than one name found for language '" + isoLangCode + "'" );
  233. throw new UDDIException( ErrorType.E_languageError, "UDDI_ERROR_MORE_THAN_ONE_NAME_FOR_LANGUAGE", isoLangCode );
  234. }
  235. }
  236. }
  237. }
  238. public void Save( string parentKey, EntityType parentType )
  239. {
  240. Debug.Enter();
  241. foreach( Name name in this )
  242. name.Save( parentKey, parentType );
  243. Debug.Leave();
  244. }
  245. public Name this[ int index ]
  246. {
  247. get { return (Name)List[ index ]; }
  248. set { List[ index ] = value; }
  249. }
  250. public int Add( Name name )
  251. {
  252. return List.Add( name );
  253. }
  254. public int Add( string name )
  255. {
  256. return List.Add( new Name( name ) );
  257. }
  258. public int Add( string isoLangCode, string name )
  259. {
  260. return List.Add( new Name( isoLangCode, name ) );
  261. }
  262. public void Insert( int index, Name name )
  263. {
  264. List.Insert( index, name );
  265. }
  266. public int IndexOf( Name name )
  267. {
  268. return List.IndexOf( name );
  269. }
  270. public bool Contains( Name name )
  271. {
  272. return List.Contains( name );
  273. }
  274. public void Remove( Name name )
  275. {
  276. List.Remove( name );
  277. }
  278. public void CopyTo( Name[] names, int index )
  279. {
  280. List.CopyTo( names, index );
  281. }
  282. }
  283. }