public bool Extract(Expression tree, IQueryable<DSResource> resourceRoot, ResourceType resourceType, EntityMetadata entityMetadata) { this.resourceRoot = resourceRoot; this.entityMetadata = entityMetadata; this.navigationProperty = null; this.referredEntityKeys = new Dictionary<string, object>(); this.referringEntityKeys = new Dictionary<string, object>(); this.currentState = ReferredResourceExtractor.ExtractionState.ExtractingReferredEntityInfo; this.Visit(tree); if (this.currentState == ReferredResourceExtractor.ExtractionState.ExtractingReferringEntityInfo) { DSResource dSResource = ResourceTypeExtensions.CreateKeyOnlyResource(resourceType, this.referringEntityKeys); if (dSResource != null) { this.ReferredResource = ResourceTypeExtensions.CreateKeyOnlyResource(this.navigationProperty.ResourceType, this.referredEntityKeys); if (this.ReferredResource != null) { this.currentState = ReferredResourceExtractor.ExtractionState.ExtractionDone; } } } if (this.currentState != ReferredResourceExtractor.ExtractionState.ExtractionDone) { this.currentState = ReferredResourceExtractor.ExtractionState.ExtractionFailed; } return this.currentState == ReferredResourceExtractor.ExtractionState.ExtractionDone; }
public ReferredEntityInstance(DSResource resource, UserContext userContext, ResourceType type, EntityMetadata metadata, string membershipId) { this.userContext = userContext; this.resourceType = type; this.metadata = metadata; this.membershipId = membershipId; this.resource = resource; }
public override object Serialize(object clrObject, int depth) { object value; if (depth != 10) { ResourceType resourceType = base.ResourceType; if (clrObject != null) { resourceType = base.ResourceType.FindResourceType(clrObject.GetType()); } DSResource dSResource = new DSResource(resourceType, false); foreach (ResourceProperty property in resourceType.Properties) { if (clrObject != null) { value = SerializerBase.GetValue(property, clrObject); } else { value = null; } object obj = value; if (obj != null || !property.ResourceType.IsPrimitive() || property.ResourceType.IsNullable()) { if (obj != null || (property.Kind & (ResourcePropertyKind.Primitive | ResourcePropertyKind.ResourceReference)) == 0) { if (clrObject != null || (property.Kind & ResourcePropertyKind.ComplexType) == 0) { dSResource.SetValue(property.Name, SerializerBase.SerializeResourceProperty(obj, base.ResourceType, property, depth + 1)); } else { TraceHelper.Current.DebugMessage(string.Concat(property.Name, " setting null to ComplexType")); dSResource.SetValue(property.Name, null); } } else { TraceHelper.Current.DebugMessage(string.Concat(property.Name, " is null; skipping")); } } else { object[] name = new object[1]; name[0] = property.Name; throw new PSObjectSerializationFailedException(string.Format(CultureInfo.CurrentCulture, Resources.PropertyNotFoundInPSObject, name)); } } return dSResource; } else { TraceHelper.Current.SerializationMaximumObjectDepthReached(); return null; } }
public EntityUpdate(UserContext userContext, ResourceType type, EntityMetadata metadata, string membershipId) { this.userContext = userContext; this.resourceType = type; this.metadata = metadata; this.commandType = CommandType.Create; this.query = null; this.membershipId = membershipId; this.propertyUpdates = new SortedDictionary<string, object>(); this.updatedResource = null; this.resolveResource = null; }
public static DSResource CreateKeyOnlyResource(ResourceType resourceType, Dictionary<string, object> inputKeys) { DSResource dSResource; ReadOnlyCollection<ResourceProperty> properties = resourceType.Properties; IEnumerable<ResourceProperty> resourceProperties = properties.Where<ResourceProperty>((ResourceProperty it) => (it.Kind & ResourcePropertyKind.Key) == ResourcePropertyKind.Key); IEnumerable<string> strs = resourceProperties.Select<ResourceProperty, string>((ResourceProperty it) => it.Name); if (inputKeys.Count == strs.Count<string>()) { DSResource dSResource1 = new DSResource(resourceType, true); Dictionary<string, object>.Enumerator enumerator = inputKeys.GetEnumerator(); try { while (enumerator.MoveNext()) { KeyValuePair<string, object> current = enumerator.Current; if (strs.Contains<string>(current.Key)) { dSResource1.SetValue(current.Key, current.Value); } else { TraceHelper.Current.DebugMessage(string.Concat("CreateKeyOnlyResource: Returning null. Property ", current.Key, " is not key in the resource ", resourceType.Name)); dSResource = null; return dSResource; } } return dSResource1; } finally { enumerator.Dispose(); } return dSResource; } else { object[] name = new object[4]; name[0] = "CreateKeyOnlyResource: Number of keys of ResourceType and inside properties does not match. Returning null. \nResource type name "; name[1] = resourceType.Name; name[2] = "\nInput Properties count: "; name[3] = inputKeys.Count; TraceHelper.Current.DebugMessage(string.Concat(name)); return null; } }
public override object Serialize(object clrObject, int depth) { object obj; if (clrObject != null) { DSResource dSResource = new DSResource(base.ResourceType, true); if (this.referencePropertyType != PSEntityMetadata.ReferenceSetCmdlets.ReferencePropertyType.KeyOnly) { EntityTypeSerializer entityTypeSerializer = new EntityTypeSerializer(base.ResourceType, true); dSResource = entityTypeSerializer.Serialize(clrObject, depth + 1) as DSResource; } else { IEnumerator<ResourceProperty> enumerator = base.ResourceType.Properties.GetEnumerator(); using (enumerator) { while (enumerator.MoveNext()) { ResourceProperty current = enumerator.Current; if ((current.Kind & ResourcePropertyKind.Key) != ResourcePropertyKind.Key) { continue; } if (clrObject != current.GetCustomState().DefaultValue) { object obj1 = clrObject; dSResource.SetValue(current.Name, SerializerBase.SerializeResourceProperty(obj1, base.ResourceType, current, depth + 1)); } else { obj = null; return obj; } } return dSResource; } return obj; } return dSResource; } else { return null; } }
public EntityUpdate(CommandType commandType, UserContext userContext, ResourceType type, EntityMetadata metadata, IQueryable query, string membershipId) { ExceptionHelpers.ThrowArgumentExceptionIf("commandType", commandType != CommandType.Update, Resources.InternalErrorOccurred, new object[0]); this.query = query; this.userContext = userContext; this.membershipId = membershipId; this.resourceType = type; this.metadata = metadata; this.commandType = commandType; this.propertyUpdates = new SortedDictionary<string, object>(); this.updatedResource = null; this.resolveResource = null; CommandArgumentVisitor commandArgumentVisitor = new CommandArgumentVisitor(this); commandArgumentVisitor.Visit(query.Expression); if (this.AreAllKeyFieldsSpecified()) { return; } else { throw new ArgumentException(ExceptionHelpers.GetExceptionMessage(Resources.KeysMissingInQuery, new object[0])); } }
public object Resolve() { if (this.commandType != CommandType.Delete) { if (this.updatedResource == null) { if (this.resolveResource == null) { this.resolveResource = new DSResource(this.resourceType, true); } return this.resolveResource; } else { return this.updatedResource; } } else { throw new NotImplementedException(ExceptionHelpers.GetExceptionMessage(Resources.ResolveResourceAfterDelete, new object[0])); } }
public void InvokeCommand() { if (PSCommandManager.IsReferenceCmdlet(this.commandType)) { IReferenceSetCommand referenceSetCommand = DataServiceController.Current.GetReferenceSetCommand(this.commandType, this.userContext, this.referringProperty, this.metadata, this.membershipId, null); using (referenceSetCommand) { UriParametersHelper.AddParametersToCommand(referenceSetCommand, DataServiceController.Current.GetCurrentResourceUri()); this.AddPropertyUpdates(referenceSetCommand); referenceSetCommand.AddReferredObject(this.referredInstance.GetKeyValues()); referenceSetCommand.AddReferringObject(this.GetKeyValues()); List<DSResource> dSResources = new List<DSResource>(); DataServiceController.Current.QuotaSystem.CheckCmdletExecutionQuota(this.userContext); IEnumerator<DSResource> enumerator = referenceSetCommand.InvokeAsync(dSResources.AsQueryable<DSResource>().Expression, true); while (enumerator.MoveNext()) { } } } else { ICommand command = DataServiceController.Current.GetCommand(this.commandType, this.userContext, this.resourceType, this.metadata, this.membershipId); using (command) { UriParametersHelper.AddParametersToCommand(command, DataServiceController.Current.GetCurrentResourceUri()); this.AddPropertyUpdates(command); List<DSResource> dSResources1 = new List<DSResource>(); IEnumerator<DSResource> enumerator1 = command.InvokeAsync(dSResources1.AsQueryable<DSResource>().Expression, true); while (enumerator1.MoveNext()) { dSResources1.Add(enumerator1.Current); } if (this.commandType == CommandType.Delete || dSResources1.Count < 1) { if (this.commandType != CommandType.Create || dSResources1.Count > 0) { this.updatedResource = null; } else { throw new DataServiceException(string.Format(Resources.CreateCommandNotReturnedInstance, this.resourceType.Name)); } } else { this.updatedResource = dSResources1.First<DSResource>(); } } } }
public override object Serialize(object clrObject, int depth) { object value; object[] name = new object[1]; name[0] = base.ResourceType.Name; clrObject.ThrowIfNull("clrObject", new ParameterExtensions.MessageLoader(SerializerBase.GetNullPassedForSerializingEntityMessage), name); ResourceType resourceType = base.ResourceType; if (clrObject as PSObject == null) { resourceType = base.ResourceType.FindResourceType(clrObject.GetType()); } else { PSObject pSObject = clrObject as PSObject; if (pSObject != null && pSObject.BaseObject != null) { resourceType = base.ResourceType.FindResourceType(pSObject.BaseObject.GetType()); } } DSResource dSResource = new DSResource(resourceType, this.serializeKeyOnly); foreach (ResourceProperty property in resourceType.Properties) { if (this.serializeKeyOnly && (property.Kind & ResourcePropertyKind.Key) != ResourcePropertyKind.Key) { continue; } if ((property.Kind & ResourcePropertyKind.ResourceSetReference) == ResourcePropertyKind.ResourceSetReference) { PSEntityMetadata testHookEntityMetadata = this.TestHookEntityMetadata; if (testHookEntityMetadata == null) { DataContext currentContext = DataServiceController.Current.GetCurrentContext(); if (currentContext != null) { testHookEntityMetadata = currentContext.UserSchema.GetEntityMetadata(base.ResourceType) as PSEntityMetadata; } } if (testHookEntityMetadata != null) { PSEntityMetadata.ReferenceSetCmdlets referenceSetCmdlet = null; if (testHookEntityMetadata.CmdletsForReferenceSets.TryGetValue(property.Name, out referenceSetCmdlet) && referenceSetCmdlet.Cmdlets.ContainsKey(CommandType.GetReference)) { if (referenceSetCmdlet.GetRefHidden) { dSResource.SetValue(property.Name, null); continue; } else { PSReferencedResourceSet pSReferencedResourceSet = new PSReferencedResourceSet(property, base.ResourceType); dSResource.SetValue(property.Name, pSReferencedResourceSet); continue; } } } } if (clrObject != null) { value = SerializerBase.GetValue(property, clrObject); } else { value = null; } object obj = value; if (obj == null) { if (!property.ResourceType.IsPrimitive() || property.ResourceType.IsNullable()) { if ((property.Kind & (ResourcePropertyKind.Primitive | ResourcePropertyKind.ResourceReference)) != 0) { Tracer tracer = new Tracer(); tracer.DebugMessage(string.Concat(property.Name, " is null; skipping")); continue; } } else { object[] objArray = new object[1]; objArray[0] = property.Name; throw new PSObjectSerializationFailedException(string.Format(CultureInfo.CurrentCulture, Resources.PropertyNotFoundInPSObject, objArray)); } } dSResource.SetValue(property.Name, SerializerBase.SerializeResourceProperty(obj, base.ResourceType, property, depth)); } return dSResource; }