Exemplo n.º 1
0
        public void Collection_Items_Can_Be_In_Different_Namespace()
        {
            var payload = new SDataPayload
            {
                ResourceName = "tradingAccount",
                Namespace    = "http://gcrm.com",
                Values       =
                {
                    {
                        "emails", new SDataPayloadCollection("email")
                        {
                            new SDataPayload
                            {
                                Namespace = "http://common.com"
                            }
                        }
                    }
                }
            };
            var nav = Utility.WritePayload(payload);
            var mgr = new XmlNamespaceManager(nav.NameTable);

            mgr.AddNamespace("g", "http://gcrm.com");
            mgr.AddNamespace("c", "http://common.com");
            var node = nav.SelectSingleNode("*/g:tradingAccount/g:emails/c:email", mgr);

            Assert.That(node, Is.Not.Null);
        }
        public void SingleResource_Verify_CanCreate()
        {
            var request = new SDataSingleResourceRequest(_service)
            {
                ResourceKind = "employees"
            };

            var payload = new SDataPayload();

            payload.Values["Title"]            = "create 1";
            payload.Values["NationalIdNumber"] = "44444";
            payload.Values["LoginId"]          = "create 4";
            payload.Values["ContactId"]        = "9999";
            payload.Values["BirthDate"]        = SyndicationDateTimeUtility.ToRfc3339DateTime(new DateTime(1970, 8, 2));
            payload.Values["HireDate"]         = SyndicationDateTimeUtility.ToRfc3339DateTime(DateTime.Now);
            payload.Values["ModifiedDate"]     = SyndicationDateTimeUtility.ToRfc3339DateTime(DateTime.Now);
            payload.Values["MaritalStatus"]    = "Single";
            payload.Values["SalariedFlag"]     = XmlConvert.ToString(true);
            payload.Values["CurrentFlag"]      = XmlConvert.ToString(true);
            payload.Values["Gender"]           = "Male";
            payload.Values["RowGuid"]          = Guid.NewGuid().ToString();

            var entry = new AtomEntry
            {
                UpdatedOn   = DateTime.Now,
                PublishedOn = DateTime.Now
            };

            entry.SetSDataPayload(payload);
            request.Entry = entry;
            _mock.Setup(s => s.CreateEntry(request, request.Entry)).Returns(TestData.Entry);

            entry = request.Create();
            Expect(entry, Is.Not.Null);
        }
Exemplo n.º 3
0
        public void Primitive_Values_Formatted_Appropriately()
        {
            var payload = new SDataPayload
            {
                ResourceName = "salesOrder",
                Namespace    = "",
                Values       =
                {
                    { "byte",           byte.MaxValue          },
                    { "sbyte",          sbyte.MaxValue         },
                    { "short",          short.MaxValue         },
                    { "ushort",         ushort.MaxValue        },
                    { "int",            int.MaxValue           },
                    { "uint",           uint.MaxValue          },
                    { "long",           long.MaxValue          },
                    { "ulong",          ulong.MaxValue         },
                    { "bool",           true                   },
                    { "char",           'z'                    },
                    { "float",          float.MaxValue         },
                    { "double",         double.MaxValue        },
                    { "decimal",        decimal.MaxValue       },
                    { "Guid",           Guid.NewGuid()         },
                    { "DateTime",       DateTime.Now           },
                    { "DateTimeOffset", DateTimeOffset.Now     },
                    { "TimeSpan",       DateTime.Now.TimeOfDay }
                }
            };
            var nav = Utility.WritePayload(payload);

            nav = nav.SelectSingleNode("*/salesOrder");

            var assertDoesNotThrow = new Action <string, Action <string> >(
                (name, action) =>
            {
                var node = nav.SelectSingleNode(name);
                Assert.That(node, Is.Not.Null);
                Assert.DoesNotThrow(() => action(node.Value));
            });

            assertDoesNotThrow("byte", x => XmlConvert.ToByte(x));
            assertDoesNotThrow("sbyte", x => XmlConvert.ToSByte(x));
            assertDoesNotThrow("short", x => XmlConvert.ToInt16(x));
            assertDoesNotThrow("ushort", x => XmlConvert.ToUInt16(x));
            assertDoesNotThrow("int", x => XmlConvert.ToInt32(x));
            assertDoesNotThrow("uint", x => XmlConvert.ToUInt32(x));
            assertDoesNotThrow("long", x => XmlConvert.ToInt64(x));
            assertDoesNotThrow("ulong", x => XmlConvert.ToUInt64(x));
            assertDoesNotThrow("bool", x => XmlConvert.ToBoolean(x));
            assertDoesNotThrow("char", x => XmlConvert.ToChar(x));
            assertDoesNotThrow("float", x => XmlConvert.ToSingle(x));
            assertDoesNotThrow("double", x => XmlConvert.ToDouble(x));
            assertDoesNotThrow("decimal", x => XmlConvert.ToDecimal(x));
            assertDoesNotThrow("Guid", x => XmlConvert.ToGuid(x));
            assertDoesNotThrow("DateTime", x => XmlConvert.ToDateTime(x, XmlDateTimeSerializationMode.RoundtripKind));
            assertDoesNotThrow("DateTimeOffset", x => XmlConvert.ToDateTimeOffset(x));
            assertDoesNotThrow("TimeSpan", x => XmlConvert.ToTimeSpan(x));
        }
Exemplo n.º 4
0
        public static XPathNavigator WritePayload(SDataPayload payload)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = XmlWriter.Create(stream))
                {
                    payload.WriteTo(writer, Client.Framework.Common.SData.Namespace);
                }

                stream.Seek(0, SeekOrigin.Begin);
                return(new XPathDocument(stream).CreateNavigator());
            }
        }
        public bool InsertRecord <T>(T dataItem)
        {
            bool returnData = false;

            // Create a SData serivce object connection.  Use the proper url and login info.
            ISDataService svc = new SDataService(GetFullSDataUrl(), _userId, _password);

            // Create the SData Payload of data to be sent
            SDataPayload myPayload = new SDataPayload();

            myPayload.Namespace    = string.Empty;
            myPayload.ResourceName = typeof(T).Name;
            myPayload.Uri          = new Uri(svc.Url);

            // Add to an ATOM feed entry b/c that's what SData uses
            AtomEntry myEntry = new AtomEntry();

            myEntry.SetSDataPayload(myPayload);

            // loop through the properties to find matching data from our query
            foreach (PropertyInfo property in dataItem.GetType().GetProperties())
            {
                // If the property value is null, don't add it
                if (property.GetValue(dataItem) == null)
                {
                    continue;
                }

                myPayload.Values.Add(property.Name, property.GetValue(dataItem));
            }

            // prepare the create request
            SDataSingleResourceRequest sendRequest = new SDataSingleResourceRequest(svc, myEntry);

            sendRequest.ResourceKind     = typeof(T).Name;
            sendRequest.ResourceSelector = "";

            try
            {
                AtomEntry response = sendRequest.Create();
                returnData = true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                returnData = false;
            }

            return(returnData);
        }
Exemplo n.º 6
0
        public static SDataPayload LoadPayload(string xml)
        {
            var payload = new SDataPayload();

            using (var strReader = new StringReader(xml))
                using (var xmlReader = XmlReader.Create(strReader))
                {
                    var doc     = new XPathDocument(xmlReader);
                    var source  = doc.CreateNavigator();
                    var manager = new XmlNamespaceManager(source.NameTable);
                    source.MoveToFirstChild();
                    payload.Load(source, manager);
                }

            return(payload);
        }
Exemplo n.º 7
0
        private void SetEntityProperty(PropertyInfo prop, SDataPayload payload, IPersistentEntity entity)
        {
            if (!prop.CanWrite)
            {
                return;
            }

            if (prop.PropertyType.FullName.StartsWith("Sage.Entity.Interfaces") ||
                prop.PropertyType.FullName.StartsWith("ICollection"))
            {
                return;
            }

            if (!payload.Values.ContainsKey(prop.Name) || payload.Values[prop.Name] == null)
            {
                return;
            }

            object convertedValue;

            if (prop.PropertyType == typeof(bool) || prop.PropertyType == typeof(Nullable <bool>))
            {
                convertedValue = Convert.ToBoolean(payload.Values[prop.Name]);
            }
            else if (prop.PropertyType == typeof(DateTime) || prop.PropertyType == typeof(Nullable <DateTime>))
            {
                convertedValue = Convert.ToDateTime(payload.Values[prop.Name]);
            }
            else if (prop.PropertyType.IsAssignableFrom(typeof(int)))
            {
                convertedValue = Convert.ToInt32(payload.Values[prop.Name]);
            }
            else if (prop.PropertyType.IsAssignableFrom(typeof(decimal)))
            {
                convertedValue = Convert.ToDecimal(payload.Values[prop.Name]);
            }
            else if (prop.PropertyType.IsAssignableFrom(typeof(double)))
            {
                convertedValue = Convert.ToDouble(payload.Values[prop.Name]);
            }
            else
            {
                convertedValue = Convert.ChangeType(payload.Values[prop.Name], prop.PropertyType);
            }

            prop.SetValue(entity, convertedValue, null);
        }
Exemplo n.º 8
0
        public void Uri_Property_Should_Be_Escaped_When_Written()
        {
            var payload = new SDataPayload
            {
                ResourceName = "person",
                Namespace    = "http://test.com",
                Uri          = new Uri("http://localhost/person('`%^ []{}<>')")
            };
            var nav = Utility.WritePayload(payload);
            var mgr = new XmlNamespaceManager(nav.NameTable);

            mgr.AddNamespace("sdata", Client.Framework.Common.SData.Namespace);
            mgr.AddNamespace("test", "http://test.com");
            var node = nav.SelectSingleNode("sdata:payload/test:person/@sdata:uri", mgr);

            Assert.That(node.Value, Is.EqualTo("http://localhost/person('%60%25%5E%20%5B%5D%7B%7D%3C%3E')"));
        }
        public bool UpdateTechnicianRecord(JT_Technician dataItem)
        {
            bool returnData = false;

            // Create a SData serivce object connection.  Use the proper url and login info.
            ISDataService svc = new SDataService(GetFullSDataUrl(), _userId, _password);

            // Create the SData Payload of data to be sent
            SDataPayload myPayload = new SDataPayload();

            myPayload.Namespace    = string.Empty;
            myPayload.ResourceName = typeof(JT_Technician).Name;
            myPayload.Uri          = new Uri(svc.Url);

            // Add to an ATOM feed entry b/c that's what SData uses
            AtomEntry myEntry = new AtomEntry();

            myEntry.SetSDataPayload(myPayload);

            // update only specific properties
            myPayload.Values.Add("CurrentStartTime", dataItem.CurrentStartTime);
            myPayload.Values.Add("CurrentStartDate", dataItem.CurrentStartDate.ToString("yyyy-MM-dd"));
            myPayload.Values.Add("CurrentSalesOrderNo", dataItem.CurrentSalesOrderNo);
            myPayload.Values.Add("CurrentWTNumber", dataItem.CurrentWTNumber);
            myPayload.Values.Add("CurrentWTStep", dataItem.CurrentWTStep);
            myPayload.Values.Add("CurrentStatus", dataItem.CurrentStatus);

            // prepare the create request
            SDataSingleResourceRequest sendRequest = new SDataSingleResourceRequest(svc, myEntry);

            sendRequest.ResourceKind     = typeof(JT_Technician).Name;
            sendRequest.ResourceSelector = dataItem.TechnicianDeptNo + ";" + dataItem.TechnicianNo;

            try
            {
                AtomEntry response = sendRequest.Update();
                returnData = true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                returnData = false;
            }

            return(returnData);
        }
Exemplo n.º 10
0
        public void throws_exception_when_ItemElementName_is_not_set()
        {
            var payload = new SDataPayload
            {
                ResourceName = "validationRule",
                Namespace    = "",
                Values       =
                {
                    { "validatorType", new SDataSimpleCollection()
                                              {
                                                  "item"
                                              } }
                }
            };

            Assert.Throws(typeof(InvalidOperationException),
                          delegate {
                var res = Utility.WritePayload(payload);
            });
        }
Exemplo n.º 11
0
        AtomEntry CopyEntityToAtomEntry(IPersistentEntity entity)
        {
            var entry   = new AtomEntry();
            var payload = new SDataPayload();

            payload.ResourceName = typeof(TEntity).Name.Substring(1);
            payload.Namespace    = "http://schemas.sage.com/dynamic/2007";

            foreach (var prop in typeof(TEntity).GetProperties())
            {
                if (!prop.PropertyType.FullName.StartsWith("Sage.Entity.Interfaces") &&
                    !prop.PropertyType.FullName.StartsWith("ICollection") &&
                    prop.CanWrite)
                {
                    payload.Values[prop.Name] = prop.GetValue(entity, null);
                }
            }
            entry.SetSDataPayload(payload);

            return(entry);
        }
Exemplo n.º 12
0
        public void Written_Collection_Uses_Item_Resource_Name()
        {
            var payload = new SDataPayload
            {
                ResourceName = "salesOrder",
                Namespace    = "",
                Values       =
                {
                    {
                        "orderLines", new SDataPayloadCollection("salesOrderLine")
                        {
                            new SDataPayload {
                                Key = "43660-1"
                            }
                        }
                    }
                }
            };
            var nav  = Utility.WritePayload(payload);
            var node = nav.SelectSingleNode("*/salesOrder/orderLines/salesOrderLine");

            Assert.That(node, Is.Not.Null);
        }
        public List <T> GetData <T>(string filterType, string filterText)
        {
            // Set up our return data object -- a list of typed objects.
            List <T> returnData = new List <T>();

            // Grab a listing of all the properties of this type of object
            PropertyInfo[] properties = typeof(T).GetProperties();

            //Create a SData serivce object connection.  Use the proper url and login info.
            string        sDataUrl = GetFullSDataUrl();
            ISDataService svc      = new SDataService(GetFullSDataUrl(), _userId, _password);

            // Now create the request, passing in the ISDataService we created above
            var req = new SDataResourceCollectionRequest(svc);

            // Tell it which kind of resource we want to access.
            // Note, this needs to match the values on the SData tab
            // of the entity in Application Architect
            // e.g., req.ResourceKind = "AR_Customer";
            Type objectType = typeof(T);

            req.ResourceKind = objectType.Name;

            // This part is optional (without it we'd be getting ALL objects of type T).
            // This is our where clause, or condition of which contacts we want.
            // In this example we want all customers whose last name starts with
            // the value 'American'. We need to use the exact property name as defined
            // in the entity (case-sensitive).
            //req.QueryValues.Add("where", @"CustomerName like 'American%'");

            req.QueryValues.Add(filterType, filterText);
            req.Count = 500; // puke - we may need to check OpenSearch numbers to see if we got it all.

            // Now read the data (or run the query)
            AtomFeed feed = null;

            try
            {
                feed = req.Read();
            }
            catch (Exception ex)
            {
                // no data... return our empty list object
                return(returnData);
            }

            // We now have the results in our AtomFeed variable, which is
            // basically a list of AtomEntry objects. To get to our data,
            // we need to read the payload from each AtomEntry and then we
            // can access the values for each field from it's Values
            // dictionary. In this example, we'll just write a few fields
            // from each contact to the console.

            foreach (AtomEntry entry in feed.Entries)
            {
                // Get the payload for this entity
                SDataPayload payload = entry.GetSDataPayload();

                // Create an instance of type T to add to our return list
                T myObj = Activator.CreateInstance <T>();

                // loop through the properties of T to find matching data from our query
                string name;
                foreach (PropertyInfo property in properties)
                {
                    name = property.Name;
                    if (!payload.Values.ContainsKey(name))
                    {
                        // the returned data doesn't contain this property at all,
                        //  so skip it.
                        continue;
                    }

                    if (payload.Values[name] == null)
                    {
                        property.SetValue(myObj, null);
                    }
                    else
                    {
                        // we found a match, so set the value of this property in this object instance
                        property.SetValue(myObj, Convert.ChangeType(payload.Values[name], property.GetMethod.ReturnType));
                    }
                }

                // add the T object instance to our return list
                returnData.Add(myObj);
            }

            // return the entire list back to the caller.
            return(returnData);
        }