PageRenderTime 59ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/ResourceManagement.Client/RmRequestFactory.cs

#
C# | 198 lines | 159 code | 22 blank | 17 comment | 24 complexity | 811aa188612c51c46f7c198f04997b63 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using System.Xml.Schema;
  5. using System.Globalization;
  6. using Microsoft.ResourceManagement.Client.WsTransfer;
  7. using Microsoft.ResourceManagement.Client.WsEnumeration;
  8. using Microsoft.ResourceManagement.ObjectModel;
  9. using System.Text;
  10. namespace Microsoft.ResourceManagement.Client
  11. {
  12. /// <summary>
  13. /// This class constructs web service requests based on RmResource and RmResourceTransaction objects
  14. /// </summary>
  15. public class RmRequestFactory : RmFactory
  16. {
  17. private Dictionary<String, bool> ProhibitedAttributes;
  18. public RmRequestFactory()
  19. : this(new XmlSchemaSet())
  20. {
  21. }
  22. public RmRequestFactory(XmlSchemaSet rmSchema)
  23. : base(rmSchema)
  24. {
  25. this.ProhibitedAttributes = new Dictionary<string, bool>();
  26. // These are attributes which cannot be set by the client ever.
  27. this.ProhibitedAttributes.Add(@"ObjectID", true);
  28. // Need ObjectType for create and a client which changes it will get permission denied
  29. //this.ProhibitedAttributes.Add(@"ObjectType", true);
  30. this.ProhibitedAttributes.Add(@"Creator", true);
  31. this.ProhibitedAttributes.Add(@"CreatedTime", true);
  32. this.ProhibitedAttributes.Add(@"ExpectedRulesList", true);
  33. this.ProhibitedAttributes.Add(@"DetectedRulesList", true);
  34. this.ProhibitedAttributes.Add(@"DeletedTime", true);
  35. this.ProhibitedAttributes.Add(@"ResourceTime", true);
  36. this.ProhibitedAttributes.Add(@"ComputedMember", true);
  37. this.ProhibitedAttributes.Add(@"ComputedActor", true);
  38. }
  39. #region WS-Transfer
  40. /// <summary>
  41. /// Constructs a put request based on the changes tracked in the transaction.
  42. /// </summary>
  43. /// <param name="transaction">The transaction object which tracked the changes made to an object.</param>
  44. /// <returns></returns>
  45. public virtual PutRequest CreatePutRequest(RmResourceChanges transaction)
  46. {
  47. if (transaction == null)
  48. {
  49. throw new ArgumentNullException("transaction");
  50. }
  51. RmResource rmObject = transaction.RmObject;
  52. if(rmObject == null)
  53. {
  54. throw new InvalidOperationException("transaction does not have rmObject");
  55. }
  56. lock (rmObject)
  57. {
  58. PutRequest putRequest = new PutRequest();
  59. putRequest.ResourceReferenceProperty = new ResourceReferenceProperty(rmObject.ObjectID.ToString());
  60. if (String.IsNullOrEmpty(rmObject.Locale) == false)
  61. {
  62. putRequest.ResourceLocaleProperty = new ResourceLocaleProperty(new System.Globalization.CultureInfo(rmObject.Locale));
  63. }
  64. putRequest.ModifyRequest = new ModifyRequest();
  65. IList<RmAttributeChange> changes = transaction.GetChanges();
  66. foreach (RmAttributeChange attributeChange in changes)
  67. {
  68. if (this.ProhibitedAttributes.ContainsKey(attributeChange.Name.Name))
  69. continue;
  70. DirectoryAccessChange putReqChange = BuildDirectoryAccessChange(attributeChange);
  71. if (base.IsMultiValued(attributeChange.Name))
  72. {
  73. putReqChange.Operation = attributeChange.Operation.ToString();
  74. }
  75. else
  76. {
  77. if (attributeChange.Operation == RmAttributeChangeOperation.Add)
  78. {
  79. putReqChange.Operation = RmAttributeChangeOperation.Replace.ToString();
  80. }
  81. else if (attributeChange.Operation == RmAttributeChangeOperation.Delete)
  82. {
  83. putReqChange.Operation = RmAttributeChangeOperation.Replace.ToString();
  84. putReqChange.AttributeValue = null;
  85. }
  86. }
  87. putRequest.ModifyRequest.Changes.Add(putReqChange);
  88. }
  89. return putRequest;
  90. }
  91. }
  92. /// <summary>
  93. /// Constructs a create request based on the provided object.
  94. /// </summary>
  95. /// <param name="newResource">The RmResource object for which to construct a create request.</param>
  96. /// <returns></returns>
  97. public virtual CreateRequest CreateCreateRequest(RmResource newResource)
  98. {
  99. if (newResource == null)
  100. {
  101. throw new ArgumentNullException("newResource");
  102. }
  103. lock (newResource)
  104. {
  105. CreateRequest createRequest = new CreateRequest();
  106. createRequest.AddRequest = new AddRequest();
  107. createRequest.AddRequest.AttributeTypeAndValues = new List<DirectoryAccessChange>();
  108. foreach (KeyValuePair<RmAttributeName, RmAttributeValue> attribute in newResource.Attributes)
  109. {
  110. if (this.ProhibitedAttributes.ContainsKey(attribute.Key.Name))
  111. continue;
  112. foreach (IComparable value in attribute.Value.Values)
  113. {
  114. DirectoryAccessChange createReqChange = BuildDirectoryAccessChange(attribute.Key, value);
  115. // cannot specify the operation on create
  116. createReqChange.Operation = null;
  117. createRequest.AddRequest.AttributeTypeAndValues.Add(createReqChange);
  118. }
  119. }
  120. return createRequest;
  121. }
  122. }
  123. public virtual DeleteRequest CreateDeleteRequest(RmReference objectId)
  124. {
  125. if (objectId == null)
  126. {
  127. throw new ArgumentNullException("objectId");
  128. }
  129. DeleteRequest deleteRequest = new DeleteRequest();
  130. deleteRequest.ResourceReferenceProperty = new ResourceReferenceProperty(objectId.Value);
  131. return deleteRequest;
  132. }
  133. public virtual GetRequest CreateGetRequest(RmReference objectId, CultureInfo culture, String[] attributes)
  134. {
  135. GetRequest request = new GetRequest();
  136. request.ResourceReferenceProperty = new ResourceReferenceProperty(objectId.Value);
  137. if (culture != null)
  138. {
  139. request.ResourceLocaleProperty = new ResourceLocaleProperty(culture);
  140. }
  141. if (attributes != null)
  142. {
  143. request.BaseObjectSearchRequest = new BaseObjectSearchRequest(attributes);
  144. }
  145. return request;
  146. }
  147. #endregion
  148. #region WS-Enumeration
  149. public virtual IEnumerable<RmResource> CreateEnumeration(WsEnumerationClient client, RmResourceFactory factory, String filter, String[] attributes)
  150. {
  151. return new EnumerationResultEnumerator(client, factory, filter, attributes);
  152. }
  153. #endregion
  154. DirectoryAccessChange BuildDirectoryAccessChange(RmAttributeChange attribute)
  155. {
  156. DirectoryAccessChange retReqChange = new DirectoryAccessChange();
  157. retReqChange.AttributeType = attribute.Name.Name;
  158. XmlElement attributeValueElem = base.RmDoc.CreateElement(retReqChange.AttributeType, RmNamespace);
  159. if (attribute.Value != null)
  160. {
  161. attributeValueElem.InnerText = attribute.Value.ToString();
  162. }
  163. retReqChange.AttributeValue.Values.Add(attributeValueElem);
  164. return retReqChange;
  165. }
  166. DirectoryAccessChange BuildDirectoryAccessChange(RmAttributeName name, IComparable value)
  167. {
  168. DirectoryAccessChange retReqChange = new DirectoryAccessChange();
  169. retReqChange.AttributeType = name.Name;
  170. XmlElement attributeValueElem = base.RmDoc.CreateElement(retReqChange.AttributeType, RmNamespace);
  171. attributeValueElem.InnerText = value.ToString();
  172. retReqChange.AttributeValue.Values.Add(attributeValueElem);
  173. return retReqChange;
  174. }
  175. }
  176. }