Exemplo n.º 1
0
        public static DSResource CreateResourceWithKeyAndReferenceSetCmdlets(ResourceType resourceType, Dictionary <string, object> keyProperties, EntityMetadata entityMetadata)
        {
            DSResource dSResource = ResourceTypeExtensions.CreateKeyOnlyResource(resourceType, keyProperties);

            if (dSResource != null)
            {
                PSEntityMetadata pSEntityMetadatum = entityMetadata as PSEntityMetadata;
                ReadOnlyCollection <ResourceProperty> properties = resourceType.Properties;
                foreach (ResourceProperty resourceProperty in properties.Where <ResourceProperty>((ResourceProperty it) => (it.Kind & ResourcePropertyKind.ResourceSetReference) == ResourcePropertyKind.ResourceSetReference))
                {
                    PSEntityMetadata.ReferenceSetCmdlets referenceSetCmdlet = null;
                    if (!pSEntityMetadatum.CmdletsForReferenceSets.TryGetValue(resourceProperty.Name, out referenceSetCmdlet) || !referenceSetCmdlet.Cmdlets.ContainsKey(CommandType.GetReference))
                    {
                        continue;
                    }
                    if (referenceSetCmdlet.GetRefHidden)
                    {
                        dSResource.SetValue(resourceProperty.Name, null);
                    }
                    else
                    {
                        PSReferencedResourceSet pSReferencedResourceSet = new PSReferencedResourceSet(resourceProperty, resourceType);
                        dSResource.SetValue(resourceProperty.Name, pSReferencedResourceSet);
                    }
                }
                return(dSResource);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
		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;
			}
		}
Exemplo n.º 4
0
        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);
            }
        }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
		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;
			}
		}
Exemplo n.º 7
0
		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;
			}
		}
Exemplo n.º 8
0
		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;
		}
Exemplo n.º 9
0
        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);
        }