Example #1
0
        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);
                    }
                }
            }
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }
Example #5
0
        protected void ApplyRelationUpdateItems(IObjRefContainer entity, IRelationUpdateItem[] ruis, bool isUpdate, IEntityMetaData metadata, IReader reader)
        {
            List <Object> toPrefetch = new List <Object>();

            RelationMember[] relationMembers = metadata.RelationMembers;
            foreach (IRelationUpdateItem rui in ruis)
            {
                String memberName    = rui.MemberName;
                int    relationIndex = metadata.GetIndexByRelationName(memberName);
                if (ValueHolderState.INIT == entity.Get__State(relationIndex))
                {
                    throw new Exception("ValueHolder already initialized for property '" + memberName + "'");
                }

                IObjRef[] existingORIs = entity.Get__ObjRefs(relationIndex);
                IObjRef[] addedORIs    = rui.AddedORIs;
                IObjRef[] removedORIs  = rui.RemovedORIs;

                IObjRef[] newORIs;
                if (existingORIs.Length == 0)
                {
                    if (removedORIs != null && addedORIs.Length > 0)
                    {
                        throw new ArgumentException("Removing from empty member");
                    }
                    newORIs = addedORIs != null && addedORIs.Length > 0 ? addedORIs : ObjRef.EMPTY_ARRAY;
                }
                else
                {
                    // Set to efficiently remove entries
                    LinkedHashSet <IObjRef> existingORIsSet = new LinkedHashSet <IObjRef>(existingORIs);
                    if (removedORIs != null && removedORIs.Length > 0)
                    {
                        foreach (IObjRef removedORI in removedORIs)
                        {
                            if (!existingORIsSet.Remove(removedORI))
                            {
                                throw OptimisticLockUtil.ThrowModified(OriHelper.EntityToObjRef(entity), null, entity);
                            }
                        }
                    }
                    if (addedORIs != null && addedORIs.Length > 0)
                    {
                        foreach (IObjRef addedORI in addedORIs)
                        {
                            if (!existingORIsSet.Add(addedORI))
                            {
                                throw OptimisticLockUtil.ThrowModified(OriHelper.EntityToObjRef(entity), null, entity);
                            }
                        }
                    }
                    if (existingORIsSet.Count == 0)
                    {
                        newORIs = ObjRef.EMPTY_ARRAY;
                    }
                    else
                    {
                        newORIs = existingORIsSet.ToArray();
                    }
                }

                RelationMember member = relationMembers[relationIndex];
                if (isUpdate)
                {
                    entity.Set__ObjRefs(relationIndex, newORIs);
                    if (!entity.Is__Initialized(relationIndex))
                    {
                        DirectValueHolderRef dvhr = new DirectValueHolderRef(entity, member);
                        toPrefetch.Add(dvhr);
                    }
                }
                else
                {
                    BuildSetterCommands(entity, newORIs, member, reader);
                }
            }
            if (toPrefetch.Count > 0)
            {
                IObjectFuture  objectFuture = new PrefetchFuture(toPrefetch);
                IObjectCommand command      = CommandBuilder.Build(reader.CommandTypeRegistry, objectFuture, null);
                reader.AddObjectCommand(command);
            }
        }