Example #1
0
        public void ExecuteObjectCommands()
        {
            while (objectCommands.Count != 0)
            {
                IObjectCommand[] commandSnapShot = ListUtil.ToArray(objectCommands);
                objectCommands.Clear();

                ResolveObjectFutures(commandSnapShot);

                // Commands have to be executed in-order (e.g. for CollectionSetterCommands)
                // except for MergeCommand which have to be last
                List <IObjectCommand> mergeCommands = new List <IObjectCommand>();
                foreach (IObjectCommand objectCommand in commandSnapShot)
                {
                    if (objectCommand is MergeCommand)
                    {
                        mergeCommands.Add(objectCommand);
                        continue;
                    }
                    objectCommand.Execute(this);
                }
                for (int i = 0, size = mergeCommands.Count; i < size; i++)
                {
                    IObjectCommand objectCommand = mergeCommands[i];
                    objectCommand.Execute(this);
                }
            }
        }
Example #2
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 #3
0
        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");
                }
            }
        }
Example #4
0
 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);
 }
Example #5
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 #6
0
        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);
        }
Example #7
0
        protected IDictionary <Type, ISet <IObjectFuture> > BucketSortObjectFutures(IList <IObjectCommand> objectCommands)
        {
            IDictionary <Type, ISet <IObjectFuture> > sortedObjectFutures = new Dictionary <Type, ISet <IObjectFuture> >((int)(objectCommands.Count / 0.75));

            for (int i = 0, size = objectCommands.Count; i < size; i++)
            {
                IObjectCommand objectCommand = objectCommands[i];
                IObjectFuture  objectFuture  = objectCommand.ObjectFuture;
                if (objectFuture != null)
                {
                    Type type = objectFuture.GetType();
                    ISet <IObjectFuture> objectFutures = DictionaryExtension.ValueOrDefault(sortedObjectFutures, type);
                    if (objectFutures == null)
                    {
                        objectFutures = new HashSet <IObjectFuture>();
                        sortedObjectFutures.Add(type, objectFutures);
                    }
                    objectFutures.Add(objectFuture);
                }
            }

            return(sortedObjectFutures);
        }
Example #8
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 #10
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);
            }
        }
Example #11
0
 public void AddObjectCommand(IObjectCommand objectCommand)
 {
     objectCommands.Add(objectCommand);
 }
Example #12
0
 public void DeattachCommand()
 {
     _attachedCommand = null;
 }
Example #13
0
 public void AttachCommand(IObjectCommand cmd)
 {
     _attachedCommand = cmd;
 }