partial         void ResolveRelationships(ODataOperation operation, object entity)
        {
            if(entity == null)
                return;

            System.Type entityType = entity.GetType();

            if(operation == ODataOperation.Insert)
            {
                if(entityType == typeof(Address))
                {
                    Address targetEntity = entity as Address;
                    User targetUser = Users.FirstOrDefault(a => a.UserId == targetEntity.UserId);
                    if(targetUser!= null)
                        targetUser.Addresses.Add(targetEntity);
                }
                else if(entityType == typeof(AgentId))
                {
                    AgentId targetEntity = entity as AgentId;
                    User targetUser = Users.FirstOrDefault(a => a.UserId == targetEntity.UserId);
                    if(targetUser!= null)
                        targetUser.AgentIds.Add(targetEntity);
                }
                else if(entityType == typeof(Badges))
                {
                    Badges targetEntity = entity as Badges;
                    User targetUser = Users.FirstOrDefault(a => a.UserId == targetEntity.UserId);
                    if(targetUser!= null)
                        targetUser.Badges.Add(targetEntity);
                }
                else if(entityType == typeof(ContactNumber))
                {
                    ContactNumber targetEntity = entity as ContactNumber;
                    User targetUser = Users.FirstOrDefault(a => a.UserId == targetEntity.UserId);
                    if(targetUser!= null)
                        targetUser.ContactNumbers.Add(targetEntity);
                }
                else if(entityType == typeof(EMailAddress))
                {
                    EMailAddress targetEntity = entity as EMailAddress;
                    User targetUser = Users.FirstOrDefault(a => a.UserId == targetEntity.UserId);
                    if(targetUser!= null)
                        targetUser.EMailAddresses.Add(targetEntity);
                }
                else if(entityType == typeof(Preference))
                {
                    Preference targetEntity = entity as Preference;
                    User targetUser = Users.FirstOrDefault(a => a.UserId == targetEntity.UserId);
                    if(targetUser!= null)
                        targetUser.Preferences.Add(targetEntity);
                }
                else if(entityType == typeof(Website))
                {
                    Website targetEntity = entity as Website;
                    User targetUser = Users.FirstOrDefault(a => a.UserId == targetEntity.UserId);
                    if(targetUser!= null)
                        targetUser.Websites.Add(targetEntity);
                }
            }
        }
        internal SyncWorkItem(ODataOperation op, Bookmark bookmark, int serverId)
        {
            if (bookmark == null)
                throw new ArgumentNullException("bookmark");

            this.Operation = op;
            this.Bookmark = bookmark;
            this.ServerId = serverId;
        }
        /// <summary>
        /// Validates an operation is valid.
        /// </summary>
        /// <param name="metadataDocumentUri">The metadata document uri.</param>
        /// <param name="operation">The operation to validate.</param>
        internal static void ValidateOperation(Uri metadataDocumentUri, ODataOperation operation)
        {
            Debug.Assert(operation != null, "operation != null");

            ValidationUtils.ValidateOperationMetadataNotNull(operation);
            string name = UriUtils.UriToString(operation.Metadata);

            if (metadataDocumentUri != null)
            {
                Debug.Assert(metadataDocumentUri.IsAbsoluteUri, "metadataDocumentUri.IsAbsoluteUri");
                ValidateMetadataReferencePropertyName(metadataDocumentUri, name);
                Debug.Assert(!IsOpenMetadataReferencePropertyName(metadataDocumentUri, name), "!IsOpenMetadataReferencePropertyName(metadataDocumentUri, name)");
            }
        }
Пример #4
0
        /// <summary>
        /// Gets the metadata reference fragment from the operation context uri.
        /// i.e. if the operation context uri is {absolute metadata document uri}#{container-qualified-operation-name},
        /// this method will return #{container-qualified-operation-name}.
        /// </summary>
        /// <param name="operation">Operation in question.</param>
        /// <returns>The metadata reference fragment from the operation context uri.</returns>
        private string GetOperationMetadataString(ODataOperation operation)
        {
            Debug.Assert(operation != null && operation.Metadata != null, "operation != null && operation.Metadata != null");

            string operationMetadataString = UriUtils.UriToString(operation.Metadata);

            Debug.Assert(ODataJsonLightUtils.IsMetadataReferenceProperty(operationMetadataString), "ODataJsonLightUtils.IsMetadataReferenceProperty(operationMetadataString)");

            // If we don't have a metadata document URI (which is the case with nometadata mode), just write the string form of the Uri we were given.
            if (this.MetadataDocumentBaseUri == null)
            {
                return(operation.Metadata.Fragment);
            }

            Debug.Assert(
                !ODataJsonLightValidationUtils.IsOpenMetadataReferencePropertyName(this.MetadataDocumentBaseUri, operationMetadataString),
                "Open metadata reference property is not supported, we should have thrown before this point.");

            return(ODataConstants.ContextUriFragmentIndicator + ODataJsonLightUtils.GetUriFragmentFromMetadataReferencePropertyName(this.MetadataDocumentBaseUri, operationMetadataString));
        }
Пример #5
0
        /// <summary>
        /// Creates an ODataAction or ODataFunction from a operation import.
        /// </summary>
        /// <param name="metadataDocumentUri">The metadata document uri.</param>
        /// <param name="metadataReferencePropertyName">The metadata reference property name.</param>
        /// <param name="edmOperation">The operation to create the ODataOperation for.</param>
        /// <param name="isAction">true if the created ODataOperation is an ODataAction, false otherwise.</param>
        /// <returns>The created ODataAction or ODataFunction.</returns>
        internal static ODataOperation CreateODataOperation(Uri metadataDocumentUri, string metadataReferencePropertyName, IEdmOperation edmOperation, out bool isAction)
        {
            Debug.Assert(metadataDocumentUri != null, "metadataDocumentUri != null");
            Debug.Assert(!string.IsNullOrEmpty(metadataReferencePropertyName), "!string.IsNullOrEmpty(metadataReferencePropertyName)");
            Debug.Assert(edmOperation != null, "edmOperation != null");

            isAction = edmOperation.IsAction();
            ODataOperation operation = isAction ? (ODataOperation) new ODataAction() : new ODataFunction();

            // Note that the property name can be '#name' which is not a valid Uri. We need to prepend the metadata document uri in that case.
            int parameterStartIndex = 0;

            if (isAction && (parameterStartIndex = metadataReferencePropertyName.IndexOf(JsonLightConstants.FunctionParameterStart)) > 0)
            {
                metadataReferencePropertyName = metadataReferencePropertyName.Substring(0, parameterStartIndex);
            }

            operation.Metadata = GetAbsoluteUriFromMetadataReferencePropertyName(metadataDocumentUri, metadataReferencePropertyName);
            return(operation);
        }
        /// <summary>
        /// Gets the metadata reference fragment from the operation context uri.
        /// i.e. if the operation context uri is {absolute metadata document uri}#{container-qualified-operation-name},
        /// this method will return #{container-qualified-operation-name}.
        /// </summary>
        /// <param name="operation">Operation in question.</param>
        /// <returns>The metadata reference fragment from the operation context uri.</returns>
        private string GetOperationMetadataString(ODataOperation operation)
        {
            Debug.Assert(operation != null && operation.Metadata != null, "operation != null && operation.Metadata != null");

            string operationMetadataString = UriUtils.UriToString(operation.Metadata);
            Debug.Assert(ODataJsonLightUtils.IsMetadataReferenceProperty(operationMetadataString), "ODataJsonLightUtils.IsMetadataReferenceProperty(operationMetadataString)");

            // If we don't have a metadata document URI (which is the case with nometadata mode), just write the string form of the Uri we were given.
            if (this.MetadataDocumentBaseUri == null)
            {
                return operation.Metadata.Fragment;
            }

            Debug.Assert(
                !ODataJsonLightValidationUtils.IsOpenMetadataReferencePropertyName(this.MetadataDocumentBaseUri, operationMetadataString),
                "Open metadata reference property is not supported, we should have thrown before this point.");

            return ODataConstants.ContextUriFragmentIndicator + ODataJsonLightUtils.GetUriFragmentFromMetadataReferencePropertyName(this.MetadataDocumentBaseUri, operationMetadataString);
        }
partial         void ResolveRelationships(ODataOperation operation, object entity);
        public override void SaveEntity(ODataOperation operation, object entity)
        {
            if(operation == ODataOperation.Insert)
            {
                if(entity.GetType() == typeof(User)) { Medium.GetStorage<IUserRepository, User>().Add(entity as User); }
                if(entity.GetType() == typeof(Address)) { Medium.GetStorage<IUserRepository, Address>().Add(entity as Address); }
                if(entity.GetType() == typeof(AgentId)) { Medium.GetStorage<IUserRepository, AgentId>().Add(entity as AgentId); }
                if(entity.GetType() == typeof(ContactNumber)) { Medium.GetStorage<IUserRepository, ContactNumber>().Add(entity as ContactNumber); }
                if(entity.GetType() == typeof(EMailAddress)) { Medium.GetStorage<IUserRepository, EMailAddress>().Add(entity as EMailAddress); }
                if(entity.GetType() == typeof(Preference)) { Medium.GetStorage<IUserRepository, Preference>().Add(entity as Preference); }
                if(entity.GetType() == typeof(Website)) { Medium.GetStorage<IUserRepository, Website>().Add(entity as Website); }
                if(entity.GetType() == typeof(AgentAgencyAssociation)) { Medium.GetStorage<IUserRepository, AgentAgencyAssociation>().Add(entity as AgentAgencyAssociation); }
                if(entity.GetType() == typeof(Awards)) { Medium.GetStorage<IUserRepository, Awards>().Add(entity as Awards); }
                if(entity.GetType() == typeof(Files)) { Medium.GetStorage<IUserRepository, Files>().Add(entity as Files); }
                if(entity.GetType() == typeof(FilesFileData)) { Medium.GetStorage<IUserRepository, FilesFileData>().Add(entity as FilesFileData); }
                if(entity.GetType() == typeof(FilesWizardInfo)) { Medium.GetStorage<IUserRepository, FilesWizardInfo>().Add(entity as FilesWizardInfo); }
                if(entity.GetType() == typeof(LoginEntry)) { Medium.GetStorage<IUserRepository, LoginEntry>().Add(entity as LoginEntry); }
                if(entity.GetType() == typeof(Badges)) { Medium.GetStorage<IUserRepository, Badges>().Add(entity as Badges); }
                if(entity.GetType() == typeof(MultiplyByTwoResult)) { Medium.GetStorage<IUserRepository, MultiplyByTwoResult>().Add(entity as MultiplyByTwoResult); }
            }
            else if(operation == ODataOperation.Delete)
            {
                if(entity.GetType() == typeof(User)) { Medium.GetStorage<IUserRepository, User>().Remove(entity as User); }
                if(entity.GetType() == typeof(Address)) { Medium.GetStorage<IUserRepository, Address>().Remove(entity as Address); }
                if(entity.GetType() == typeof(AgentId)) { Medium.GetStorage<IUserRepository, AgentId>().Remove(entity as AgentId); }
                if(entity.GetType() == typeof(ContactNumber)) { Medium.GetStorage<IUserRepository, ContactNumber>().Remove(entity as ContactNumber); }
                if(entity.GetType() == typeof(EMailAddress)) { Medium.GetStorage<IUserRepository, EMailAddress>().Remove(entity as EMailAddress); }
                if(entity.GetType() == typeof(Preference)) { Medium.GetStorage<IUserRepository, Preference>().Remove(entity as Preference); }
                if(entity.GetType() == typeof(Website)) { Medium.GetStorage<IUserRepository, Website>().Remove(entity as Website); }
                if(entity.GetType() == typeof(AgentAgencyAssociation)) { Medium.GetStorage<IUserRepository, AgentAgencyAssociation>().Remove(entity as AgentAgencyAssociation); }
                if(entity.GetType() == typeof(Awards)) { Medium.GetStorage<IUserRepository, Awards>().Remove(entity as Awards); }
                if(entity.GetType() == typeof(Files)) { Medium.GetStorage<IUserRepository, Files>().Remove(entity as Files); }
                if(entity.GetType() == typeof(FilesFileData)) { Medium.GetStorage<IUserRepository, FilesFileData>().Remove(entity as FilesFileData); }
                if(entity.GetType() == typeof(FilesWizardInfo)) { Medium.GetStorage<IUserRepository, FilesWizardInfo>().Remove(entity as FilesWizardInfo); }
                if(entity.GetType() == typeof(LoginEntry)) { Medium.GetStorage<IUserRepository, LoginEntry>().Remove(entity as LoginEntry); }
                if(entity.GetType() == typeof(Badges)) { Medium.GetStorage<IUserRepository, Badges>().Remove(entity as Badges); }
                if(entity.GetType() == typeof(MultiplyByTwoResult)) { Medium.GetStorage<IUserRepository, MultiplyByTwoResult>().Remove(entity as MultiplyByTwoResult); }
            }

            ResolveRelationships(operation, entity);
        }
 public void ShouldWriteOneActionWithNoTitleNoTarget()
 {
     var operations = new ODataOperation[] { new ODataAction { Metadata = new Uri("#foo", UriKind.Relative) } };
     const string expectedPayload = "{\"#foo\":{}}";
     this.WriteOperationsAndValidatePayload(operations, expectedPayload);
 }
 /// <summary>
 /// Sets the metadata builder for the operation.
 /// </summary>
 /// <param name="entryState">The state of the reader for entry to read.</param>
 /// <param name="operation">The operation to set the metadata builder on.</param>
 private void SetMetadataBuilder(IODataJsonLightReaderEntryState entryState, ODataOperation operation)
 {
     ODataEntityMetadataBuilder builder = this.MetadataContext.GetEntityMetadataBuilderForReader(entryState, this.JsonLightInputContext.MessageReaderSettings.UseKeyAsSegment);
     operation.SetMetadataBuilder(builder, this.ContextUriParseResult.MetadataDocumentUri);
 }
 public void ShouldWriteOneActionWithTwoBindingTargets()
 {
     var operations = new ODataOperation[] { new ODataAction { Metadata = new Uri("#foo", UriKind.Relative), Target = new Uri("http://www.example.com/foo") }, new ODataAction { Metadata = new Uri("#foo", UriKind.Relative), Target = new Uri("http://www.example.com/baz/foo") } };
     const string expectedPayload = "{\"#foo\":[{\"target\":\"http://www.example.com/foo\"},{\"target\":\"http://www.example.com/baz/foo\"}]}";
     this.WriteOperationsAndValidatePayload(operations, expectedPayload);
 }
 public void ShouldThrowWhenWritingTwoActionsWithSameMetadataAndSameTargets()
 {
     var operations = new ODataOperation[] { new ODataAction { Metadata = new Uri("#foo", UriKind.Relative), Target = new Uri("http://www.example.com/foo") }, new ODataAction { Metadata = new Uri("#foo", UriKind.Relative), Target = new Uri("http://www.example.com/foo") } };
     Action test = () => this.WriteOperationsAndValidatePayload(operations, null);
     test.ShouldThrow<ODataException>().WithMessage(ErrorStrings.ODataJsonLightEntryAndFeedSerializer_ActionsAndFunctionsGroupMustNotHaveDuplicateTarget("#foo", "http://www.example.com/foo"));
 }
 public void ShouldThrowWhenWritingTwoActionsWithSameMetadataAndTwoNullTargets()
 {
     var operations = new ODataOperation[] { new ODataAction { Metadata = new Uri("#foo", UriKind.Relative) }, new ODataAction { Metadata = new Uri("#foo", UriKind.Relative) } };
     Action test = () => this.WriteOperationsAndValidatePayload(operations, null);
     test.ShouldThrow<ODataException>().WithMessage(ErrorStrings.ODataJsonLightEntryAndFeedSerializer_ActionsAndFunctionsGroupMustSpecifyTarget("#foo"));
 }
 public void ShouldWriteOneActionWithTitleAndTarget()
 {
     var operations = new ODataOperation[] { new ODataAction { Metadata = new Uri("#foo", UriKind.Relative), Title = "foo", Target = new Uri("http://www.example.com/foo") } };
     const string expectedPayload = "{\"#foo\":{\"title\":\"foo\",\"target\":\"http://www.example.com/foo\"}}";
     this.WriteOperationsAndValidatePayload(operations, expectedPayload);
 }
 /// <summary>
 /// This method is called when pending changes on an entity need to be persisted to the backing store.
 /// </summary>
 /// <param name="operation">The type of operation the save is envoking.</param>
 /// <param name="entity">The entity to perform the save on.</param>
 public abstract void SaveEntity(ODataOperation operation, object entity);
 /// <summary>
 /// Returns the target uri string from the given operation.
 /// </summary>
 /// <param name="operation">Operation in question.</param>
 /// <returns>Returns the target uri string from the given operation.</returns>
 private string GetOperationTargetUriString(ODataOperation operation)
 {
     return operation.Target == null ? null : this.UriToString(operation.Target);
 }
        /// <summary>
        /// Writes an operation (an action or a function).
        /// </summary>
        /// <remarks>
        /// Expects the write to already have written the "rel value" and opened an array.
        /// </remarks>
        /// <param name="operation">The operation to write.</param>
        private void WriteOperation(ODataOperation operation)
        {
            Debug.Assert(operation != null, "operation must not be null.");
            Debug.Assert(operation.Metadata != null, "operation.Metadata != null");

            this.JsonLightOutputContext.JsonWriter.StartObjectScope();

            if (operation.Title != null)
            {
                this.JsonLightOutputContext.JsonWriter.WriteName(JsonConstants.ODataOperationTitleName);
                this.JsonLightOutputContext.JsonWriter.WriteValue(operation.Title);
            }

            if (operation.Target != null)
            {
                string targetUrlString = this.GetOperationTargetUriString(operation);
                this.JsonLightOutputContext.JsonWriter.WriteName(JsonConstants.ODataOperationTargetName);
                this.JsonLightOutputContext.JsonWriter.WriteValue(targetUrlString);
            }

            this.JsonLightOutputContext.JsonWriter.EndObjectScope();
        }
 public void ShouldWriteTwoActionsWithTwoBindingTargetsEach()
 {
     var operations = new ODataOperation[] { new ODataAction { Metadata = new Uri("#foo", UriKind.Relative), Title = "foo", Target = new Uri("http://www.example.com/1/foo") }, new ODataAction { Metadata = new Uri("#foo", UriKind.Relative), Target = new Uri("http://www.example.com/2/foo") }, new ODataAction { Metadata = new Uri("#baz", UriKind.Relative), Title = "baz", Target = new Uri("http://www.example.com/1/baz") }, new ODataAction { Metadata = new Uri("#baz", UriKind.Relative), Target = new Uri("http://www.example.com/2/baz") } };
     const string expectedPayload = "{\"#foo\":[{\"title\":\"foo\",\"target\":\"http://www.example.com/1/foo\"},{\"target\":\"http://www.example.com/2/foo\"}],\"#baz\":[{\"title\":\"baz\",\"target\":\"http://www.example.com/1/baz\"},{\"target\":\"http://www.example.com/2/baz\"}]}";
     this.WriteOperationsAndValidatePayload(operations, expectedPayload);
 }
 public void ShouldThrowWhenWritingRelativeActionsTargetWhenMetadataDocumentUriIsNotSet()
 {
     IEnumerable<ODataOperation> operations = new ODataOperation[] { new ODataAction { Metadata = new Uri("#foo", UriKind.Relative), Target = new Uri("foo", UriKind.Relative) } };
     Action test = () => this.WriteOperationsAndValidatePayload(operations, null, true, false /*setMetadataDocumentUri*/);
     test.ShouldThrow<ODataException>().WithMessage(ErrorStrings.ODataOutputContext_MetadataDocumentUriMissing);
 }
 public void ShouldWriteAbsoluteActionTargetOnWireWhenMetadataDocumentUriIsSetButMetadataAnnotationIsNotWritten()
 {
     var operations = new ODataOperation[] { new ODataAction { Metadata = new Uri("#foo", UriKind.Relative), Target = new Uri("foo", UriKind.Relative) } };
     const string expectedPayload = "{\"#foo\":{\"target\":\"http://odata.org/test/foo\"}}";
     this.WriteOperationsAndValidatePayload(operations, expectedPayload, /*isAction*/ true, true /*setMetadataDocumentUri*/, false /*writeMetadataAnnotation*/);
 }
        /// <summary>
        /// Writes an operation (an action or a function).
        /// </summary>
        /// <param name="operation">The association link to write.</param>
        internal void WriteOperation(ODataOperation operation)
        {
            DebugUtils.CheckNoExternalCallers();

            // checks for null and validates its properties
            WriterValidationUtils.ValidateOperation(operation, this.WritingResponse);

            string elementName;
            if (operation is ODataAction)
            {
                elementName = AtomConstants.ODataActionElementName;
            }
            else
            {
                Debug.Assert(operation is ODataFunction, "operation is either an ODataAction or an ODataFunction");
                elementName = AtomConstants.ODataFunctionElementName;
            }

            // <m:action ... or <m:function ...
            this.XmlWriter.WriteStartElement(
                AtomConstants.ODataMetadataNamespacePrefix,
                elementName,
                AtomConstants.ODataMetadataNamespace);

            // write the attributes of the action/function

            // The metadata URI of an ODataOperation can be relative.
            string metadataAttributeValue = this.UriToUrlAttributeValue(operation.Metadata, /*failOnRelativeUriWithoutBaseUri*/ false);

            this.XmlWriter.WriteAttributeString(AtomConstants.ODataOperationMetadataAttribute, metadataAttributeValue);

            if (operation.Title != null)
            {
                this.XmlWriter.WriteAttributeString(AtomConstants.ODataOperationTitleAttribute, operation.Title);
            }

            string targetAttribute = this.UriToUrlAttributeValue(operation.Target);
            this.XmlWriter.WriteAttributeString(AtomConstants.ODataOperationTargetAttribute, targetAttribute);

            // </m:action> or </m:function>
            this.XmlWriter.WriteEndElement();
        }
        public void PushUpdate(Entity entity, int serverId, Action callback, Failed failed)
        {
            XDocument doc = new XDocument();

            // entry...
            XElement entryElement = new XElement(XName.Get("entry", AtomNamespace));

            doc.Add(entryElement);

            // content...
            XElement contentElement = new XElement(XName.Get("content", AtomNamespace));

            contentElement.Add(new XAttribute(XName.Get("type", string.Empty), "application/xml"));
            entryElement.Add(contentElement);

            // properties...
            XElement propertiesElement = new XElement(XName.Get("properties", MsMetadataNamespace));

            contentElement.Add(propertiesElement);

            // walk the fields...
            EntityType et = entity.EntityType;

            if (et == null)
            {
                throw new InvalidOperationException("'et' is null.");
            }
            foreach (EntityField field in et.Fields)
            {
                if (!(field.IsKey) && field.IsOnServer)
                {
                    // create...
                    XElement element = new XElement(XName.Get(field.Name, MsDataNamespace));
                    object   value   = entity.GetValue(field);
                    if (value != null)
                    {
                        element.Value = value.ToString();
                    }

                    // add...
                    propertiesElement.Add(element);
                }
            }

            // run...
            String         url         = null;
            ODataOperation op          = ODataOperation.Update;
            String         xmlAsString = doc.ToString();

            if (serverId != 0)
            {
                url = GetEntityUrlForPush(entity, serverId);
            }
            else
            {
                url = this.GetServiceUrl(et);
                op  = ODataOperation.Insert;
            }

            // run...
            ExecuteODataOperation(op, url, xmlAsString, callback, failed);
        }