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 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");
                }
            }
        }
        private static void HandleRequest(string[] split)
        {
            var request    = split.First();
            var parameters = split.Skip(1).ToArray();

            if (Environment.IsSystemCommand(request))
            {
                var command = Environment.GetSystemCommand(request);
                command.Execute(parameters);
            }
            else if (Environment.IsView(request))
            {
                var view = Environment.GetView(request);
                view.Print(parameters);
            }
            else
            {
                ICommandBuilder commandBuilder = Environment.GetShellCommand(request);
                ICommand        command        = commandBuilder.Build(parameters);

                Recorder.AddCommand(command);

                IPublishCommands commandPublisher = Environment.GetCommandPublisher();
                commandPublisher.Publish(command);
            }
        }
Example #4
0
        public async Task <int> ExecuteNonQueryAsync(ICommandBuilder commandBuilder, CancellationToken cancellationToken)
        {
            using var connection = await _database.GetOpenConnectionAsync(cancellationToken);

            using var command = commandBuilder.Build(connection);
            return(await command.ExecuteNonQueryAsync(cancellationToken));
        }
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, HttpMethods.Post, Route = "application")] HttpRequest req,
            [DurableClient] IDurableOrchestrationClient processStarter,
            [NotificationSubscriber] string subscriber,
            ILogger log)
        {
            var instanceId = _guidProvider.GenerateGuid();
            _correlationInitializer.Initialize(instanceId);

            var mappingResult = await _fromFormToApplicationAddDtoRequestMapper.MapRequest(req);

            log.LogProgress(OperationStatus.Started, "Application process started", instanceId);

            if (!mappingResult.Success)
            {
                log.LogInformation($"Invalid data provided to the application process with instanceId: {instanceId}");
                return new BadRequestObjectResult(mappingResult.Errors);
            }

            await _subscriber.Register(instanceId, subscriber);

            var command = await _commandBuilder.Build(mappingResult.Value);

            await processStarter.StartNewAsync(nameof(ApplicationProcessOrchestrator), instanceId, command);

            log.LogProgress(OperationStatus.InProgress, "Started processing of application process", instanceId);
            return new AcceptedWithCorrelationIdHeaderResult(instanceId);
        }
Example #6
0
        private static void HandleRequest(string[] split)
        {
            ICommandBuilder commandBuilder = Environment.GetShellCommand(split.First());
            ICommand        command        = commandBuilder.Build(split.Skip(1).ToArray());

            command.Execute();
        }
        public void Register(ICommandBuilder command, IExtension extension)
        {
            Assert.IsNotNull(command);
            Assert.IsNotNull(extension);
            Command builded = command.Build();

            Assert.IsNotNull(builded);

            Builders.Add(command, builded);
            RBuilders.Add(builded, command);
            Commands.Add(builded, extension);
        }
Example #8
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);
        }
        private static void HandleRequest(string[] split)
        {
            if (Environment.IsSystemCommand(split.First()))
            {
                var command = Environment.GetSystemCommand(split.First());
                command.Execute(split.Skip(1).ToArray());
            }
            else
            {
                ICommandBuilder commandBuilder = Environment.GetShellCommand(split.First());
                ICommand        command        = commandBuilder.Build(split.Skip(1).ToArray());

                Recorder.AddCommand(command);

                IPublishCommands commandPublisher = Environment.GetCommandPublisher();
                commandPublisher.Publish(command);
            }
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, HttpMethods.Post, Route = "application")] HttpRequest req,
            [DurableClient] IDurableOrchestrationClient processStarter,
            ILogger log)
        {
            var mappingResult = await _fromFormToApplicationAddDtoRequestMapper.MapRequest(req);

            var instanceId = _guidProvider.GenerateGuid().ToString("N");

            if (!mappingResult.Success)
            {
                log.LogInformation($"Invalid data provided to the application process with instanceId: {instanceId}");
                return(new BadRequestObjectResult(mappingResult.Errors));
            }

            var command = await _commandBuilder.Build(mappingResult.Value);

            await processStarter.StartNewAsync(nameof(ApplicationProcessOrchestrator), instanceId, command);

            log.LogInformation($"Started orchestration of application process with instanceId: {instanceId}");

            return(new AcceptedWithCorrelationIdHeaderResult(instanceId));
        }
Example #11
0
        public async Task <IEnumerable <T> > ExecuteReaderAsync <T>(ICommandBuilder commandBuilder, CancellationToken cancellationToken)
        {
            IConnection  connection = null;
            DbCommand    command    = null;
            DbDataReader reader     = null;

            try
            {
                connection = await _database.GetOpenConnectionAsync(cancellationToken);

                command = commandBuilder.Build(connection);
                reader  = await command.ExecuteReaderAsync(cancellationToken);

                return(ToEnumerable <T>(connection, command, reader));
            }
            catch
            {
                reader?.Dispose();
                command?.Dispose();
                connection?.Dispose();
                throw;
            }
        }
Example #12
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);
        }
Example #13
0
 /// <summary>
 /// Executes the command builder
 /// </summary>
 /// <param name="args"></param>
 /// <param name="sb"></param>
 /// <param name="cmd"></param>
 private void ExecuteCommandBuilder(ICommandBuilder cmd, GenerationArgs args, StringBuilder sb)
 {
     cmd.Build(args, sb);
 }
Example #14
0
        public async Task ExecuteAsync(ICommandBuilder builder, CancellationToken cancellationToken)
        {
            var commands = builder.Build();

            await this.Executer.ExecuteAsync(commands, cancellationToken);
        }
Example #15
0
        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 #16
0
        public void Process()
        {
            NetworkStream stream = null;

            try
            {
                stream = _tcpClient.GetStream();
                var data = new byte[MessageLength];

                while (true)
                {
                    var builder = new StringBuilder();

                    do
                    {
                        var bytes = stream.Read(data, 0, data.Length);

                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    }while (stream.DataAvailable);

                    var message = builder.ToString();
                    if (string.IsNullOrEmpty(message))
                    {
                        // idle
                        continue;
                    }

                    var cmd = _commandBuilder.Build(message);
                    if (cmd != null)
                    {
                        if (cmd.IsValid())
                        {
//#if DEBUG
//                            _logger.Log($"request: command:{cmd.GetType().Name}, params:{cmd}");
//#endif

                            var result = cmd.Execute(_wordDictionary);

//#if DEBUG
//                            _logger.Log($"response:{result}");
//#endif

                            data = Encoding.Unicode.GetBytes(result);
                            stream.Write(data, 0, data.Length);
                        }
                        else
                        {
                            var debugMessage = $"Command \"{cmd.GetType().Name}\" is invalid!";
                            _logger.Log(debugMessage);

                            data = Encoding.Unicode.GetBytes(debugMessage);
                            stream.Write(data, 0, data.Length);
                        }
                    }
                    else
                    {
                        var debugMessage = $"Can't parse command by presented args";
                        _logger.Log(debugMessage);
                        data = Encoding.Unicode.GetBytes(debugMessage);
                        stream.Write(data, 0, data.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Log("Error:", ex);
            }
            finally
            {
                _tcpClient?.Close();
                stream?.Close();
            }
        }
Example #17
0
        public void Execute(ICommandBuilder builder)
        {
            var commands = builder.Build();

            this.Executer.Execute(commands);
        }