Esempio n. 1
0
        /// <summary>
        /// Saves the contact.
        /// </summary>
        /// <param name="contact">The contact to be saved.</param>
        /// <returns>
        /// A true or false result of the successful saving this contact
        /// </returns>
        public bool SaveContact(Contact contact)
        {
            List <ReplaceableAttribute> attributeList = new List <ReplaceableAttribute>
            {
                new ReplaceableAttribute().WithName("Email").WithValue(contact.Email),
                new ReplaceableAttribute().WithName("Name").WithValue(contact.Name),
                new ReplaceableAttribute().WithName("Phone").WithValue(contact.Phone)
            };

            contact.ID = Guid.NewGuid().ToString();
            bool success = false;

            try
            {
                if (!SimpleDBProxy.Domains.Contains(DomainName))
                {
                    SimpleDBProxy.AddDomain(DomainName);
                }
                PutAttributesRequest action = new PutAttributesRequest
                {
                    ItemName   = contact.ID,
                    Attribute  = attributeList,
                    DomainName = DomainName
                };
                PutAttributesResponse response = SimpleDBProxy.Service.PutAttributes(action);
                success = true;
            }
            catch (Exception requestException)
            {
                success = false;
            }

            return(success);
        }
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            PutAttributesResponse response = new PutAttributesResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.IsStartElement)
                {
                    if (context.TestExpression("PutAttributesResult", 2))
                    {
                        UnmarshallResult(context, response);
                        continue;
                    }

                    if (context.TestExpression("ResponseMetadata", 2))
                    {
                        response.ResponseMetadata = ResponseMetadataUnmarshaller.Instance.Unmarshall(context);
                    }
                }
            }

            return(response);
        }
Esempio n. 3
0
        internal async Task <int> GetNextId()
        {
            var client = GetClient();
            int nextId = -1;

            GetAttributesRequest request = new GetAttributesRequest();

            request.DomainName     = _domain;
            request.ItemName       = "NextId";
            request.AttributeNames = new List <string> {
                "Id"
            };
            request.ConsistentRead = true;

            try
            {
                GetAttributesResponse response = await client.GetAttributesAsync(request);

                if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
                {
                    if (response.Attributes.Count > 0)
                    {
                        nextId = Convert.ToInt32(response.Attributes[0].Value);
                    }

                    PutAttributesRequest putRequest = new PutAttributesRequest();
                    putRequest.DomainName = _domain;
                    putRequest.ItemName   = "NextId";
                    putRequest.Attributes.Add(
                        new ReplaceableAttribute {
                        Name = "Id", Value = numberToString(++nextId), Replace = true
                    }
                        );

                    try
                    {
                        PutAttributesResponse putResponse = await client.PutAttributesAsync(putRequest);
                    }
                    catch (AmazonSimpleDBException ex)
                    {
                        _logger.LogError(ex, $"Error Code: {ex.ErrorCode}, Error Type: {ex.ErrorType}");
                        throw;
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }

            return(nextId);
        }
Esempio n. 4
0
        private void btnPutAttributes_Click(object sender, RoutedEventArgs e)
        {
            SimpleDBResponseEventHandler <object, ResponseEventArgs> handler = null;

            this.SomeMessage = "Please wait...";
            handler          = delegate(object senderAmazon, ResponseEventArgs args)
            {
                //Unhook from event.
                SimpleDB.Client.OnSimpleDBResponse -= handler;
                PutAttributesResponse response = args.Response as PutAttributesResponse;
                this.Dispatcher.BeginInvoke(() =>
                {
                    if (null != response)
                    {
                        this.SomeMessage = "Attribute put successfully.";
                    }
                    else
                    {
                        AmazonSimpleDBException exception = args.Response as AmazonSimpleDBException;
                        if (null != exception)
                        {
                            this.SomeMessage = "Error: " + exception.Message;
                        }
                    }
                });
            };

            SimpleDB.Client.OnSimpleDBResponse += handler;

            PutAttributesRequest putAttributesRequest = new PutAttributesRequest {
                DomainName = this.DomainName, ItemName = this.ItemName
            };
            List <ReplaceableAttribute> attributesOne = putAttributesRequest.Attribute;

            //Calculate the attributes and their values to put.
            foreach (var item in GetListAttributeAndValueFromString(this.AttributesAndValuesToPut))
            {
                attributesOne.Add(new ReplaceableAttribute().WithName(item.Attribute).WithValue(item.Value));
            }


            //attributesOne.Add(new ReplaceableAttribute().WithName("Category").WithValue("Clothes"));
            //attributesOne.Add(new ReplaceableAttribute().WithName("Subcategory").WithValue("Sweater"));
            //attributesOne.Add(new ReplaceableAttribute().WithName("Name").WithValue("Cathair Sweater"));
            //attributesOne.Add(new ReplaceableAttribute().WithName("Color").WithValue("Siamese"));
            //attributesOne.Add(new ReplaceableAttribute().WithName("Size").WithValue("Small"));
            //attributesOne.Add(new ReplaceableAttribute().WithName("Size").WithValue("Medium"));
            //attributesOne.Add(new ReplaceableAttribute().WithName("Size").WithValue("Large"));
            SimpleDB.Client.PutAttributes(putAttributesRequest);
        }
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            PutAttributesResponse response = new PutAttributesResponse();

            while (context.Read())
            {
                if (context.IsStartElement)
                {
                    if (context.TestExpression("ResponseMetadata", 2))
                    {
                        response.ResponseMetadata = ResponseMetadataUnmarshaller.GetInstance().Unmarshall(context);
                    }
                }
            }


            return(response);
        }
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            PutAttributesResponse response = new PutAttributesResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("attributes", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <Attribute, AttributeUnmarshaller>(AttributeUnmarshaller.Instance);
                    response.Attributes = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, PutAttributesResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.ReadAtDepth(originalDepth))
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                }
            }

            return;
        }
Esempio n. 8
0
        private T Upsert(T asset)
        {
            PutAttributesRequest request = new PutAttributesRequest();

            request.DomainName = m_objectMapper.TableName;
            request.ItemName   = asset.Id.ToString();
            request.Attribute  = new List <ReplaceableAttribute>();
            Dictionary <MetaDataMember, object> allPropertyValues = m_objectMapper.GetAllValues(asset);

            foreach (KeyValuePair <MetaDataMember, object> propertyValue in allPropertyValues)
            {
                if (!propertyValue.Key.IsPrimaryKey)
                {
                    if (propertyValue.Value != null)
                    {
                        request.Attribute.Add(new ReplaceableAttribute().WithReplace(true).WithName(propertyValue.Key.MappedName.ToLower()).WithValue(GetValue(propertyValue.Value)));

                        /*if (propertyValue.Key.Type == typeof(DateTime) || propertyValue.Key.Type == typeof(Nullable<DateTime>)) {
                         *  //logger.Debug("Upsert adding attribute name=" + propertyValue.Key.Name.ToLower() + ", value=" + ((DateTime)propertyValue.Value).ToString("o") + ".");
                         *  request.Attribute.Add(new ReplaceableAttribute().WithReplace(true).WithName(propertyValue.Key.MappedName.ToLower()).WithValue(((DateTime)propertyValue.Value).ToString("o")));
                         * }
                         * else {
                         *  //logger.Debug("Upsert adding attribute name=" + propertyValue.Key.Name + ", value=" + propertyValue.Value.ToString() + ".");
                         *  request.Attribute.Add(new ReplaceableAttribute().WithReplace(true).WithName(propertyValue.Key.MappedName.ToLower()).WithValue(propertyValue.Value.ToString()));
                         * }*/
                    }
                    //else {
                    //     request.Attribute.Add(new ReplaceableAttribute().WithReplace(true).WithName(propertyValue.Key.Name).WithValue(null));
                    // }
                }
            }

            PutAttributesResponse response = m_simpleDBClient.PutAttributes(request);

            if (response.IsSetResponseMetadata())
            {
                ResponseMetadata responseMetadata = response.ResponseMetadata;
                //logger.Debug("Upsert response for " + request.DomainName + ", id=" + request.ItemName + ", attributes=" + request.Attribute.Count + ": " + responseMetadata.RequestId);
            }

            return(asset);
        }
        public bool AddUpdateRoute(Route route)
        {
            bool success = true;

            using (AmazonSimpleDBClient client = new AmazonSimpleDBClient(_publicKey, _secretKey))
            {
                PutAttributesRequest request = new PutAttributesRequest
                {
                    DomainName = DomainName,
                    ItemName   = route.Id.ToString()
                };
                request.Attribute.Add(new ReplaceableAttribute()
                {
                    Name = "Name", Replace = true, Value = route.Name
                });
                request.Attribute.Add(new ReplaceableAttribute()
                {
                    Name = "Distance", Replace = true, Value = route.Distance.ToString()
                });
                request.Attribute.Add(new ReplaceableAttribute()
                {
                    Name = "Id", Replace = true, Value = route.Id.ToString()
                });
                request.Attribute.Add(new ReplaceableAttribute()
                {
                    Name = "LastTimeRidden", Replace = true, Value = route.LastTimeRidden.ToShortDateString()
                });
                request.Attribute.Add(new ReplaceableAttribute()
                {
                    Name = "Location", Replace = true, Value = route.Location
                });
                try
                {
                    PutAttributesResponse response = client.PutAttributes(request);
                }
                catch (Exception repositoryError)
                {
                    success = false;
                }
            }
            return(success);
        }
Esempio n. 10
0
        public string Request(Domain.Insert pInsert)
        {
            #region Attributes and values validation

            if (pInsert.Attributes.Count != pInsert.Values.Count)
            {
                throw new Exception("SimpleSQL: insert command has different attributes and values.");
            }

            #endregion

            PutAttributesRequest mPutAction = new PutAttributesRequest().WithDomainName(this.GetDomainName(pInsert.Table)).WithItemName(pInsert.ItemID.ToString());

            List <ReplaceableAttribute> mPutAttributes = mPutAction.Attribute;
            ReplaceableAttribute        mAttribute;

            #region Add attributes to the SimpleDB object

            int mTotalAttributes = pInsert.Attributes.Count;
            for (int mIndex = 0; mIndex < mTotalAttributes; mIndex++)
            {
                mAttribute = new ReplaceableAttribute()
                {
                    Name  = pInsert.Attributes[mIndex],
                    Value = pInsert.Values[mIndex].ToString()
                };

                mPutAttributes.Add(mAttribute);
            }

            #endregion

            mPutAttributes.Add(new ReplaceableAttribute().WithName(this.aSimpleSQLTableName).WithValue(pInsert.Table));

            PutAttributesResponse mResponse = this.aSimpleDBClient.PutAttributes(mPutAction);

            return(mResponse.ToString());
        }
Esempio n. 11
0
        /// <summary>
        /// Put the user's reco into the ZigMeRecos domain in SimpleDB
        /// </summary>
        /// <returns></returns>
        private bool SaveRecoToSimpleDB(string myFBId)
        {
            AmazonSimpleDB sdb = GetSDB();

            // Creating a domain
            String domainName = "ZigMeRecos";
            CreateDomainRequest createDomain = (new CreateDomainRequest()).WithDomainName(domainName);

            sdb.CreateDomain(createDomain);

            // Check to see how many recos this FB user id has stored in our domain
            String         selectExpression    = "Select * From ZigMeRecos Where FBId = '" + myFBId + "'";
            SelectRequest  selectRequestAction = new SelectRequest().WithSelectExpression(selectExpression);
            SelectResponse selectResponse      = sdb.Select(selectRequestAction);

            int cRecos = 0;

            // Now store the actual recommendation item
            if (selectResponse.IsSetSelectResult())
            {
                SelectResult selectResult = selectResponse.SelectResult;
                cRecos = selectResult.Item.Count;
            }
            cRecos++;
            String recoItem = "Reco_" + myFBId + "_" + cRecos;
            PutAttributesRequest        putAttributesRecoItem = new PutAttributesRequest().WithDomainName(domainName).WithItemName(recoItem);
            List <ReplaceableAttribute> attributesRecoItem    = putAttributesRecoItem.Attribute;

            attributesRecoItem.Add(new ReplaceableAttribute().WithName("FBId").WithValue(myFBId));
            attributesRecoItem.Add(new ReplaceableAttribute().WithName("Name").WithValue(RecoName.Text));
            attributesRecoItem.Add(new ReplaceableAttribute().WithName("Email").WithValue(ContactEmail.Text));
            attributesRecoItem.Add(new ReplaceableAttribute().WithName("City").WithValue(RecoCity.Text));
            attributesRecoItem.Add(new ReplaceableAttribute().WithName("Service").WithValue(RecoService.SelectedValue));
            PutAttributesResponse putAttributesResponse = sdb.PutAttributes(putAttributesRecoItem);

            return(putAttributesResponse.IsSetResponseMetadata());
        }
Esempio n. 12
0
        public override void UpdateProperty(Guid id, string propertyName, object value)
        {
            try
            {
                PutAttributesRequest request = new PutAttributesRequest();
                request.DomainName = m_objectMapper.TableName;
                request.ItemName   = id.ToString();
                request.Attribute  = new List <ReplaceableAttribute>();
                request.Attribute.Add(new ReplaceableAttribute().WithReplace(true).WithName(propertyName.ToLower()).WithValue(GetValue(value)));

                PutAttributesResponse response = m_simpleDBClient.PutAttributes(request);

                if (response.IsSetResponseMetadata())
                {
                    ResponseMetadata responseMetadata = response.ResponseMetadata;
                    logger.Debug("UpdateProperty response: " + responseMetadata.RequestId);
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception SimpleDBAssetPersistor UpdateProperty (for " + typeof(T).Name + "). " + excp.Message);
                throw;
            }
        }