PageRenderTime 23ms CodeModel.GetById 2ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/ResourceManagement.Client/RmRequestFactory.cs

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