static private void ConvertUnitCodeToIds(List <KeyValuePair <BizStructure, string> > changedProperties, Dictionary <string, string> unitCodeIdTable) { foreach (KeyValuePair <BizStructure, string> pair in changedProperties) { BizStructure obj = pair.Key; string name = pair.Value; ItemSchema p = obj.Schema.GetItem(name); object v = obj[p.Name]; if (v == null) { continue; } if (p.DataType == BizDataType.Unit) { if (!unitCodeIdTable.ContainsKey((string)v)) { obj[p.Name] = null; } else { obj[p.Name] = unitCodeIdTable[(string)v]; } } else if (p.DataType == BizDataType.UnitArray) { string[] vs = (string[])v; for (int i = 0; i < vs.Length; i++) { string _v = vs[i]; if (string.IsNullOrEmpty(_v) || !unitCodeIdTable.ContainsKey(_v)) { vs[i] = null; } else { vs[i] = unitCodeIdTable[_v]; } } obj[p.Name] = vs; } } }
private static bool JObjectToSchema(JObject jobject, out BizStructureSchema schema, out string errorMessage) { errorMessage = null; schema = new BizStructureSchema(); foreach (KeyValuePair <string, JToken> keyValuePair in jobject) { if (keyValuePair.Value == null) { continue; } if (string.Compare(keyValuePair.Key, "code", true) == 0) { schema.Code = keyValuePair.Value.ToString(); continue; } if (!(keyValuePair.Value is JArray)) { errorMessage = keyValuePair.Value.ToString() + "不合法"; return(false); } JArray itemObject = (JArray)keyValuePair.Value; string name = keyValuePair.Key; string displayName = null; string strDataType = null; BizDataType dataType = BizDataType.ShortString; BizStructureSchema childSchema = null; foreach (JObject childObject in itemObject) { foreach (KeyValuePair <string, JToken> itemKeyValuePair in childObject) { if (string.Compare(itemKeyValuePair.Key, "name", true) == 0) { name = itemKeyValuePair.Value.ToString(); } else if (string.Compare(itemKeyValuePair.Key, "displayname", true) == 0) { displayName = itemKeyValuePair.Value.ToString(); } else if (string.Compare(itemKeyValuePair.Key, "datatype", true) == 0) { strDataType = itemKeyValuePair.Value.ToString(); try { dataType = (BizDataType)Enum.Parse(typeof(BizDataType), strDataType, true); } catch { errorMessage = string.Format("字段类型{0}不合法", strDataType); return(false); } } else if (string.Compare(itemKeyValuePair.Key, "childschema", true) == 0) { if (itemKeyValuePair.Value != null && !string.IsNullOrEmpty(itemKeyValuePair.Value.ToString())) { if (!(itemKeyValuePair.Value is JObject)) { errorMessage = string.Format("字段{0}不合法", itemKeyValuePair.Value.ToString()); return(false); } JObject childJObject = (JObject)itemKeyValuePair.Value; if (!JObjectToSchema(childJObject, out childSchema, out errorMessage)) { return(false); } } } } if (string.IsNullOrEmpty(displayName)) { displayName = name; } ItemSchema itemSchema = new ItemSchema(name, displayName, dataType, childSchema); schema.Add(itemSchema); } } return(true); }
private static bool JObjectToStructure(JObject jobject, BizStructure obj, out string errorMessage) { errorMessage = null; // 赋值进去 foreach (KeyValuePair <string, JToken> keyValuePair in jobject) { ItemSchema property = obj.Schema.GetItem(keyValuePair.Key); if (keyValuePair.Value == null) { continue; } else if (property == null) { continue; } else if (property.DataType == BizDataType.BizStructure) { BizStructure childObject = null; if (keyValuePair.Value.ToString() != "") { if (!(keyValuePair.Value is JObject)) { errorMessage = string.Format("属性{0}是一个结构,但是传过来的Json字符串与该类型不一致", property.Name); return(false); } JObject childJObject = (JObject)keyValuePair.Value; BizStructureSchema childSchema = property.ChildSchema; childObject = new BizStructure(childSchema); if (!JObjectToStructure(childJObject, childObject, out errorMessage)) { return(false); } } obj[property.Name] = childObject; } else if (property.DataType == BizDataType.BizStructureArray) { List <BizStructure> boList = new List <BizStructure>(); if (keyValuePair.Value.ToString() != "") { if (!(keyValuePair.Value is JArray)) { errorMessage = string.Format("属性{0}是一个业务对象数组,但是传过来的Json字符串与该类型不一致", property.Name); return(false); } BizStructureSchema childSchema = property.ChildSchema; if (childSchema == null) { continue; } int count = keyValuePair.Value.Count(); if (count == 0) { continue; } for (int i = 0; i < count; i++) { object p_v = keyValuePair.Value[i]; if (!(p_v is JObject)) { errorMessage = string.Format("属性{0}是一个业务对象数组,但是传过来的Json字符串与该类型不一致", property.Name); return(false); } JObject childJObject = (JObject)p_v; BizStructure childBo = new BizStructure(childSchema); if (!JObjectToStructure(childJObject, childBo, out errorMessage)) { return(false); } boList.Add(childBo); } } obj[keyValuePair.Key] = boList.ToArray(); } else if (property.DataType == BizDataType.UnitArray) { List <string> vs = new List <string>(); if (keyValuePair.Value.ToString() != "") { if (!(keyValuePair.Value is JArray)) { errorMessage = string.Format("属性{0}是一个多人字段,但是传过来的Json字符串与该类型不一致", property.Name); return(false); } int count = keyValuePair.Value.Count(); for (int i = 0; i < count; i++) { string unit = keyValuePair.Value[i].ToString(); vs.Add(unit); } } obj[keyValuePair.Key] = vs.ToArray(); } else if (property.DataType == BizDataType.Unit) { obj[keyValuePair.Key] = keyValuePair.Value.ToString(); } else { object v = Utility.Convert(keyValuePair.Value.ToString(), property.RealType, true, null); obj[property.Name] = v; } } return(true); }