public static JToken GetApiReturnFormat(this DataGraphProperty property, DataGraphSchema schema)
        {
            if (property.IsCustomType())
            {
                var type = schema.CustomTypes.First(i => i.ClassName == property.Type);

                if (property.IsArray)
                {
                    var array = new JArray();
                    array.Add(type.GetApiReturnFormat(schema));
                    return(array);
                }
                else
                {
                    return(type.GetApiReturnFormat(schema));
                }
            }
            else
            {
                if (!property.IsArray)
                {
                    switch (property.Type)
                    {
                    case "string":
                        return("Sample string");

                    case "int":
                        return(3);

                    case "decimal":
                        return(4.99);
                    }
                }
                else
                {
                    switch (property.Type)
                    {
                    case "string":
                        return(new JArray("Sample string one", "Sample string two"));

                    case "int":
                        return(new JArray(0, 1));

                    case "decimal":
                        return(new JArray(3.52, 4.99));
                    }
                }
            }

            throw new NotImplementedException();
        }
示例#2
0
        private static void AssertTypeMatches(JToken jtoken, DataGraphProperty property)
        {
            // Note that we ignore arrays since the PUT operations don't accept arrays, they accept single values
            if (property.IsCustomType())
            {
                switch (jtoken.Type)
                {
                // Both objects and integers (reference to the object ID) are allowed
                case JTokenType.Object:
                case JTokenType.Integer:
                    return;

                default:
                    throw new InvalidOperationException();
                }
            }
            else
            {
                switch (property.Type)
                {
                case "string":
                    if (jtoken.Type != JTokenType.String)
                    {
                        throw new InvalidOperationException();
                    }
                    break;

                case "int":
                    if (jtoken.Type != JTokenType.Integer)
                    {
                        throw new InvalidOperationException();
                    }
                    break;

                case "decimal":
                    if (jtoken.Type != JTokenType.Float)
                    {
                        throw new InvalidOperationException();
                    }
                    break;

                default:
                    throw new NotImplementedException();
                }
            }
        }
示例#3
0
        private void PutIntoObject(string customerId, int graphId, string userId, int objectId, IEnumerable <DataGraphClass> customTypes, DataGraphProperty prop, JToken bodyToken)
        {
            // Validate that type matches (note that array vs non array doesn't matter, put only allows adding a single item, not an array)
            AssertTypeMatches(bodyToken, prop);

            if (!prop.IsArray)
            {
                if (prop.IsCustomType())
                {
                    var customType = customTypes.First(i => i.ClassName == prop.Type);

                    DataGraphReferencePropertyValue literalReference = _context.ReferencePropertyValues.FirstOrDefault(i =>
                                                                                                                       i.CustomerId == customerId &&
                                                                                                                       i.GraphId == graphId &&
                                                                                                                       i.ObjectId == objectId &&
                                                                                                                       i.PropertyName == prop.Name);

                    if (bodyToken.Type == JTokenType.Integer)
                    {
                        int objIdToReference = bodyToken.Value <int>();

                        AssertReferencedObject(customerId, graphId, userId, objIdToReference, customType.ClassName);

                        if (literalReference == null)
                        {
                            literalReference = new DataGraphReferencePropertyValue
                            {
                                CustomerId         = customerId,
                                GraphId            = graphId,
                                ObjectId           = objectId,
                                PropertyName       = prop.Name,
                                ReferencedObjectId = objIdToReference
                            };

                            _context.ReferencePropertyValues.Add(literalReference);
                        }
                        else
                        {
                            literalReference.ReferencedObjectId = objIdToReference;
                        }
                    }

                    else
                    {
                        // Nuke the old value
                        if (literalReference != null)
                        {
                            var toDelete = _context.Objects.FirstOrDefault(i =>
                                                                           i.CustomerId == customerId &&
                                                                           i.GraphId == graphId &&
                                                                           i.ObjectId == literalReference.ObjectId);

                            if (toDelete != null)
                            {
                                _context.Objects.Remove(toDelete);
                            }
                        }
                        else
                        {
                            literalReference = new DataGraphReferencePropertyValue
                            {
                                CustomerId   = customerId,
                                GraphId      = graphId,
                                ObjectId     = objectId,
                                PropertyName = prop.Name
                            };

                            _context.ReferencePropertyValues.Add(literalReference);
                        }

                        literalReference.ReferencedObject = AddObject(customerId, graphId, "", customType, bodyToken as JObject);
                    }
                }

                else
                {
                    var existing = _context.LiteralPropertyValues.FirstOrDefault(i =>
                                                                                 i.CustomerId == customerId &&
                                                                                 i.GraphId == graphId &&
                                                                                 i.ObjectId == objectId &&
                                                                                 i.PropertyName == prop.Name);
                    if (existing != null)
                    {
                        // Strings require Formatting.None to output using the "" quotes around the string
                        existing.ProperyValueJson = bodyToken.ToString(Newtonsoft.Json.Formatting.None);
                    }
                    else
                    {
                        _context.LiteralPropertyValues.Add(new DataGraphLiteralPropertyValue()
                        {
                            CustomerId       = customerId,
                            GraphId          = graphId,
                            ObjectId         = objectId,
                            PropertyName     = prop.Name,
                            ProperyValueJson = bodyToken.ToString(Newtonsoft.Json.Formatting.None)
                        });
                    }
                }

                _context.SaveChanges();
            }
            else
            {
                // Arrays

                if (!prop.IsCustomType())
                {
                    _context.ListOfLiterals.Add(new DataGraphListOfLiteralsPropertyValue
                    {
                        CustomerId        = customerId,
                        GraphId           = graphId,
                        ObjectId          = objectId,
                        PropertyName      = prop.Name,
                        ListItemValueJson = bodyToken.ToString(Newtonsoft.Json.Formatting.None)
                    });
                }
                else
                {
                    var customType = customTypes.First(i => i.ClassName == prop.Type);

                    if (bodyToken.Type == JTokenType.Integer)
                    {
                        int objIdToAdd = bodyToken.Value <int>();

                        // Check object exists and is correct type and user has permission to reference it
                        AssertReferencedObject(customerId, graphId, userId, objIdToAdd, customType.ClassName);

                        _context.ListOfReferences.Add(new DataGraphListOfReferencesPropertyValue
                        {
                            CustomerId         = customerId,
                            GraphId            = graphId,
                            ObjectId           = objectId,
                            PropertyName       = prop.Name,
                            ReferencedObjectId = objIdToAdd
                        });
                    }

                    else
                    {
                        var dbObj = AddObject(customerId, graphId, "", customType, bodyToken as JObject);
                        _context.SaveChanges();

                        _context.ListOfReferences.Add(new DataGraphListOfReferencesPropertyValue
                        {
                            CustomerId       = customerId,
                            GraphId          = graphId,
                            ObjectId         = objectId,
                            PropertyName     = prop.Name,
                            ReferencedObject = dbObj
                        });
                    }
                }

                _context.SaveChanges();
            }
        }
示例#4
0
        public JToken GetPropertyValueJson(string customerId, int graphId, int objectId, DataGraphProperty prop)
        {
            if (!prop.IsCustomType())
            {
                if (!prop.IsArray)
                {
                    var json = _context.LiteralPropertyValues.First(i =>
                                                                    i.CustomerId == customerId &&
                                                                    i.GraphId == graphId &&
                                                                    i.ObjectId == objectId &&
                                                                    i.PropertyName == prop.Name).ProperyValueJson;

                    var token = JToken.Parse(json);
                    return(token);
                }
                else
                {
                    JArray array = new JArray();

                    foreach (var itemValueJson in _context.ListOfLiterals.Where(i =>
                                                                                i.CustomerId == customerId &&
                                                                                i.GraphId == graphId &&
                                                                                i.ObjectId == objectId &&
                                                                                i.PropertyName == prop.Name).Select(i => i.ListItemValueJson))
                    {
                        array.Add(JToken.Parse(itemValueJson));
                    }

                    return(array);
                }
            }
            else
            {
                if (!prop.IsArray)
                {
                    int refObjId = _context.ReferencePropertyValues.Where(i =>
                                                                          i.CustomerId == customerId &&
                                                                          i.GraphId == graphId &&
                                                                          i.ObjectId == objectId &&
                                                                          i.PropertyName == prop.Name).Select(i => i.ReferencedObjectId).First();

                    return(GetObjectJson(customerId, graphId, refObjId));
                }
                else
                {
                    JArray array = new JArray();

                    foreach (var refObjId in _context.ListOfReferences.Where(i =>
                                                                             i.CustomerId == customerId &&
                                                                             i.GraphId == graphId &&
                                                                             i.ObjectId == objectId &&
                                                                             i.PropertyName == prop.Name).Select(i => i.ReferencedObjectId).ToArray())
                    {
                        array.Add(GetObjectJson(customerId, graphId, refObjId));
                    }

                    return(array);
                }
            }
        }