/// <summary> /// Finds the matching data type and update /// </summary> /// <param name="clientDataType"></param> /// <returns></returns> private DataType FindMatchingDataTypeAndUpdate(DataType clientDataType) { // type with same name must exists only once var existingDataType = this.FromName(clientDataType.NormalizedName); if (existingDataType != null) { // if structure does not change, use it if (existingDataType.Equals(clientDataType)) { return existingDataType; } else { // otherwise - update clientDataType.Id = existingDataType.Id; clientDataType.OriginalName = existingDataType.OriginalName; // when client is sending data, some fields can be omitted by JSON standards // only allow fields to be added automatically but not removed clientDataType.CombineProperties(existingDataType); // since it will be costly operation - try to ensure that client // really have new property before attempting to update if (clientDataType.Equals(existingDataType) == false) { _db.InsertOrReplace(clientDataType); // update our mappings // remaps the table _db.CreateTable(clientDataType.GetCompiledType()); // update the cached type this.Types[clientDataType.NormalizedName] = clientDataType; } } } else { // this is a new data type this.Register(clientDataType); } return clientDataType; }
/// <summary> /// Scaffolds the specified input JObject /// </summary> /// <param name="inputJson">The input json.</param> /// <returns></returns> public DataType Scaffold(JObject sourceObject) { var clientDataType = new DataType(); clientDataType.OriginalName = "Scaffoled"; clientDataType.Properties = (from KeyValuePair<string, JToken> property in sourceObject select new DataProperty(property.Key, property.Value.Type)).ToList(); clientDataType.EnsureHasNeccessaryProperties(); return clientDataType; }
/// <summary> /// Scaffolds the specified input json into DataType /// </summary> /// <param name="inputJson">The input json.</param> /// <returns></returns> public DataType Scaffold(string inputJson) { var sourceObject = JsonConvert.DeserializeObject(inputJson) as JObject; var clientDataType = new DataType(); clientDataType.OriginalName = "Scaffoled"; clientDataType.Properties = (from KeyValuePair<string, JToken> property in sourceObject select new DataProperty(property.Key, property.Value.Type)).ToList(); clientDataType.EnsureHasNeccessaryProperties(); return clientDataType; }
/// <summary> /// Registers the specified type. /// </summary> /// <param name="toRegister">To register.</param> /// <returns></returns> public DataType Register(DataType toRegister) { if (toRegister.Id == int.MaxValue) { throw new InvalidOperationException("Cannot Update StaticType"); } if (toRegister.Id == 0) { if (this.RegisteredTypes.Where(t => t.NormalizedName == toRegister.NormalizedName).FirstOrDefault() != null) { throw new InvalidOperationException("Duplicate Structure Name"); } toRegister.EnsureHasNeccessaryProperties(); _db.Insert(toRegister); } else { toRegister.EnsureHasNeccessaryProperties(); _db.Update(toRegister); } _CachedDataType = null; var finalType = this.RegisteredTypes.Where(t => t.NormalizedName == toRegister.NormalizedName).FirstOrDefault(); return finalType; }
/// <summary> /// Combine new fields from other data type /// </summary> /// <param name="other"></param> public virtual void CombineProperties( DataType other ) { var properties = this.Properties.ToList(); Action<string, string> addOrReplaceProperties = (name, type) => { var prop = (from p in properties where p.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) select p).FirstOrDefault(); if (prop == null) { properties.Add(new DataProperty() { Name = name, Type = type }); } else { prop.Name = name; prop.Type = type; } }; foreach (var item in other.Properties) { addOrReplaceProperties(item.Name, item.Type); } properties.RemoveAll(p => p.Name == "AttachmentBase64" || p.Name == "AttachmentExtension"); this.Properties = properties.ToList(); }
/// <summary> /// Not permitted /// </summary> public override void CombineProperties(DataType other) { return; }