protected void BuildSetterCommands(Object entity, IObjRef[] newORIs, RelationMember member, IReader reader) { if (!member.IsToMany) { if (newORIs.Length == 0) { return; } else if (newORIs.Length == 1) { IObjectFuture objectFuture = new ObjRefFuture(newORIs[0]); IObjectCommand command = CommandBuilder.Build(reader.CommandTypeRegistry, objectFuture, entity, member); reader.AddObjectCommand(command); } else { throw new ArgumentException("Multiple values for to-one relation"); } } else { Object coll = ListUtil.CreateCollectionOfType(member.RealType, newORIs.Length); MethodInfo addMethod = coll.GetType().GetMethod("Add"); Object[] parameters = new Object[1]; bool useObjectFuture = false; ICommandBuilder commandBuilder = CommandBuilder; ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry; foreach (IObjRef ori in newORIs) { if (!(ori is IDirectObjRef)) { IObjectFuture objectFuture = new ObjRefFuture(ori);; IObjectCommand command = commandBuilder.Build(commandTypeRegistry, objectFuture, coll, addMethod); reader.AddObjectCommand(command); useObjectFuture = true; continue; } Object item = ((IDirectObjRef)ori).Direct; if (useObjectFuture) { IObjectCommand command = commandBuilder.Build(commandTypeRegistry, null, coll, addMethod, item); reader.AddObjectCommand(command); } else { parameters[0] = item; addMethod.Invoke(coll, parameters); } } } }
protected IBeanRuntime <C> BuildIntern <C>(ICommandTypeRegistry commandTypeRegistry, IObjectFuture objectFuture, Object parent) where C : IObjectCommand { Type commandType = typeof(C); Type overridingCommandType = commandTypeRegistry.GetOverridingCommandType(commandType); if (overridingCommandType != null) { commandType = overridingCommandType; } IBeanRuntime <C> beanRuntime = BeanContext.RegisterBean <C>(commandType).PropertyValue("ObjectFuture", objectFuture).PropertyValue("Parent", parent); return(beanRuntime); }
public void ProcessRead(IPostProcessReader reader) { reader.NextTag(); ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry; ICommandTypeExtendable commandTypeExtendable = reader.CommandTypeExtendable; commandTypeExtendable.RegisterOverridingCommandType(typeof(MergeArraySetterCommand), typeof(ArraySetterCommand)); Object result = reader.ReadObject(); commandTypeExtendable.UnregisterOverridingCommandType(typeof(MergeArraySetterCommand), typeof(ArraySetterCommand)); if (!(result is CUDResult)) { throw new Exception("Can only handle results of type '" + typeof(CUDResult).Name + "'. Result of type '" + result.GetType().Name + "' given."); } ICommandBuilder commandBuilder = CommandBuilder; Member directObjRefDirectMember = this.directObjRefDirectMember; CUDResult cudResult = (CUDResult)result; IList <IChangeContainer> changes = cudResult.AllChanges; for (int i = 0, size = changes.Count; i < size; i++) { IChangeContainer changeContainer = changes[i]; if (!(changeContainer is CreateContainer)) { continue; } IObjRef ori = changeContainer.Reference; if (ori == null) { continue; } else if (ori is DirectObjRef) { IObjectFuture objectFuture = new ObjRefFuture(ori); IObjectCommand setterCommand = commandBuilder.Build(commandTypeRegistry, objectFuture, ori, directObjRefDirectMember); reader.AddObjectCommand(setterCommand); IObjectCommand mergeCommand = commandBuilder.Build(commandTypeRegistry, objectFuture, changeContainer); reader.AddObjectCommand(mergeCommand); } else { throw new Exception("Not implemented yet"); } } }
protected virtual Object PostProcess(Object obj, IPostProcessReader postProcessReader) { if (obj is IObjectFuture) { IObjectFuture objectFuture = (IObjectFuture)obj; ICommandTypeRegistry commandTypeRegistry = postProcessReader.CommandTypeRegistry; IObjectCommand objectCommand = CommandBuilder.Build(commandTypeRegistry, objectFuture, null); postProcessReader.AddObjectCommand(objectCommand); postProcessReader.ExecuteObjectCommands(); obj = objectFuture.Value; } else { postProcessReader.ExecuteObjectCommands(); } return(obj); }
public virtual Object ReadObject(Type returnType, Type objType, int id, IReader reader) { Object obj = Activator.CreateInstance(objType); if (id > 0) { reader.PutObjectWithId(obj, id); } reader.NextTag(); ITypeInfoItem[] members = reader.GetMembersOfType(objType); int index = 0; String valueAttribute = XmlDictionary.ValueAttribute; String primitiveElement = XmlDictionary.PrimitiveElement; ICommandBuilder commandBuilder = CommandBuilder; ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry; IConversionHelper conversionHelper = ConversionHelper; while (reader.IsStartTag()) { ITypeInfoItem member = members[index++]; Object memberValue; if (primitiveElement.Equals(reader.GetElementName())) { String value = reader.GetAttributeValue(valueAttribute); memberValue = conversionHelper.ConvertValueToType(member.RealType, value); reader.MoveOverElementEnd(); } else { memberValue = reader.ReadObject(member.RealType); } if (memberValue is IObjectFuture) { IObjectFuture objectFuture = (IObjectFuture)memberValue; IObjectCommand command = commandBuilder.Build(commandTypeRegistry, objectFuture, obj, member); reader.AddObjectCommand(command); } else { member.SetValue(obj, memberValue); } } return(obj); }
protected Object PostProcess(Object obj, IPostProcessReader pullParserReader) { HandlePostProcessingTag(pullParserReader); if (obj is IObjectFuture) { IObjectFuture objectFuture = (IObjectFuture)obj; ICommandTypeRegistry commandTypeRegistry = pullParserReader.CommandTypeRegistry; IObjectCommand objectCommand = CommandBuilder.Build(commandTypeRegistry, objectFuture, null); pullParserReader.AddObjectCommand(objectCommand); pullParserReader.ExecuteObjectCommands(); obj = objectFuture.Value; } else { pullParserReader.ExecuteObjectCommands(); } return(obj); }
public IObjectCommand Build(ICommandTypeRegistry commandTypeRegistry, IObjectFuture objectFuture, Object parent, params Object[] optionals) { IObjectCommand command; if (parent == null) { command = BuildIntern <ResolveObjectCommand>(commandTypeRegistry, objectFuture, parent).Finish(); } else if (parent.GetType().IsArray) { command = BuildIntern <ArraySetterCommand>(commandTypeRegistry, objectFuture, parent).PropertyValue("Index", optionals[0]).Finish(); } else if (parent is IEnumerable && !(parent is String)) { IBeanRuntime <CollectionSetterCommand> beanRuntime = BuildIntern <CollectionSetterCommand>(commandTypeRegistry, objectFuture, parent); if (optionals.Length > 0) { beanRuntime.PropertyValue("AddMethod", optionals[0]); } if (optionals.Length > 1) { beanRuntime.PropertyValue("Obj", optionals[1]); } command = beanRuntime.Finish(); } else if (parent is CreateContainer || parent is UpdateContainer) { command = BuildIntern <MergeCommand>(commandTypeRegistry, objectFuture, parent).Finish(); } else { command = BuildIntern <ObjectSetterCommand>(commandTypeRegistry, objectFuture, parent).PropertyValue("Member", optionals[0]).Finish(); } return(command); }
public virtual Object ReadObject(Type returnType, String elementName, int id, IReader reader) { if (!XmlDictionary.ArrayElement.Equals(elementName)) { throw new Exception("Element '" + elementName + "' not supported"); } int length = Int32.Parse(reader.GetAttributeValue(XmlDictionary.SizeAttribute)); Type componentType = ClassElementHandler.ReadFromAttribute(reader); Array targetArray; if (!reader.IsEmptyElement()) { reader.NextTag(); } if ("values".Equals(reader.GetElementName())) { String listOfValuesString = reader.GetAttributeValue("v"); if (typeof(char).Equals(componentType) || typeof(byte).Equals(componentType) || typeof(sbyte).Equals(componentType) || typeof(bool).Equals(componentType)) { targetArray = (Array)ConversionHelper.ConvertValueToType(componentType.MakeArrayType(), listOfValuesString, EncodingInformation.SOURCE_BASE64 | EncodingInformation.TARGET_PLAIN); reader.PutObjectWithId(targetArray, id); } else { targetArray = Array.CreateInstance(componentType, length); reader.PutObjectWithId(targetArray, id); String[] items = splitPattern.Split(listOfValuesString); for (int a = 0, size = items.Length; a < size; a++) { String item = items[a]; if (item == null || item.Length == 0) { continue; } Object convertedValue = ConversionHelper.ConvertValueToType(componentType, items[a]); targetArray.SetValue(convertedValue, a); } } reader.MoveOverElementEnd(); } else { if (returnType.IsGenericType) { componentType = returnType.GetGenericArguments()[0]; } targetArray = Array.CreateInstance(componentType, length); reader.PutObjectWithId(targetArray, id); ICommandBuilder commandBuilder = CommandBuilder; ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry; for (int index = 0; index < length; index++) { Object item = reader.ReadObject(componentType); if (item is IObjectFuture) { IObjectFuture objectFuture = (IObjectFuture)item; IObjectCommand command = commandBuilder.Build(commandTypeRegistry, objectFuture, targetArray, index); reader.AddObjectCommand(command); } else { targetArray.SetValue(item, index); } } } return(targetArray); }
public virtual Object ReadObject(Type returnType, String elementName, int id, IReader reader) { if (!XmlDictionary.SetElement.Equals(elementName) && !XmlDictionary.ListElement.Equals(elementName)) { throw new Exception("Element '" + elementName + "' not supported"); } String lengthValue = reader.GetAttributeValue(XmlDictionary.SizeAttribute); int length = lengthValue != null && lengthValue.Length > 0 ? Int32.Parse(lengthValue) : 0; // Do not remove although in Java it is not necessary to extract the generic type information of a collection. // This code is important for environments like C# Type componentType = ClassElementHandler.ReadFromAttribute(reader); if (returnType.IsGenericType) { componentType = returnType.GetGenericArguments()[0]; } MethodInfo addMethod = null; Object[] parameters = new Object[1]; IEnumerable coll; if (XmlDictionary.SetElement.Equals(elementName)) { Type setType = typeof(HashSet <>).MakeGenericType(componentType); coll = (IEnumerable)Activator.CreateInstance(setType); addMethod = setType.GetMethod("Add"); } else { Type listType = typeof(List <>).MakeGenericType(componentType); coll = (IEnumerable)(length > 0 ? Activator.CreateInstance(listType, length) : Activator.CreateInstance(listType)); addMethod = listType.GetMethod("Add"); } reader.PutObjectWithId(coll, id); reader.NextTag(); bool useObjectFuture = false; ICommandBuilder commandBuilder = CommandBuilder; ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry; while (reader.IsStartTag()) { Object item = reader.ReadObject(componentType); if (item is IObjectFuture) { IObjectFuture objectFuture = (IObjectFuture)item; IObjectCommand command = commandBuilder.Build(commandTypeRegistry, objectFuture, coll, addMethod); reader.AddObjectCommand(command); useObjectFuture = true; } else if (useObjectFuture) { IObjectCommand command = commandBuilder.Build(commandTypeRegistry, null, coll, addMethod, item); reader.AddObjectCommand(command); } else { parameters[0] = item; addMethod.Invoke(coll, parameters); } } return(coll); }