/// <summary>
        ///     Construct representation of locked ExternalTask from corresponding entity.
        ///     During mapping variables will be collected,during collection variables will not be deserialized
        ///     and scope will not be set to local.
        /// </summary>
        /// <seealso cref=
        /// <seealso
        ///     cref="AbstractVariableScope#collectVariables(VariableMapImpl, Collection, boolean, boolean)" />
        /// </seealso>
        /// <param name="externalTaskEntity"> - source persistent entity to use for fields </param>
        /// <param name="variablesToFetch">
        ///     - list of variable names to fetch, if null then all variables will be fetched
        /// </param>
        /// <returns>
        ///     object with all fields copied from the ExternalTaskEntity, error details fetched from the
        ///     database and variables attached
        /// </returns>
        public static LockedExternalTaskImpl FromEntity(ExternalTaskEntity externalTaskEntity,
                                                        IList <string> variablesToFetch, bool deserializeVariables)
        {
            var result = new LockedExternalTaskImpl();

            result.id                 = externalTaskEntity.Id;
            result.topicName          = externalTaskEntity.TopicName;
            result.workerId           = externalTaskEntity.WorkerId;
            result.lockExpirationTime = externalTaskEntity.LockExpirationTime;
            result.retries            = externalTaskEntity.Retries;
            result.errorMessage       = externalTaskEntity.ErrorMessage;
            result.errorDetails       = externalTaskEntity.ErrorDetails;

            result.processInstanceId    = externalTaskEntity.ProcessInstanceId;
            result.executionId          = externalTaskEntity.ExecutionId;
            result.activityId           = externalTaskEntity.ActivityId;
            result.activityInstanceId   = externalTaskEntity.ActivityInstanceId;
            result.processDefinitionId  = externalTaskEntity.ProcessDefinitionId;
            result.processDefinitionKey = externalTaskEntity.ProcessDefinitionKey;
            result.tenantId             = externalTaskEntity.TenantId;
            result.priority             = externalTaskEntity.Priority;

            ExecutionEntity execution = externalTaskEntity.Execution;

            result.variables = new VariableMapImpl();
            execution.CollectVariables(result.variables, variablesToFetch, false, deserializeVariables);

            return(result);
        }
        public virtual IVariableMap Execute(CommandContext commandContext)
        {
            EnsureUtil.EnsureNotNull("executionId", ExecutionId);

            ExecutionEntity execution = commandContext.ExecutionManager.FindExecutionById(ExecutionId);

            EnsureUtil.EnsureNotNull("execution " + ExecutionId + " doesn't exist", "execution", execution);

            CheckGetExecutionVariables(execution, commandContext);

            var executionVariables = new VariableMapImpl();

            //collect variables from execution
            execution.CollectVariables(executionVariables, VariableNames, IsLocal, DeserializeValues);

            return(executionVariables);
        }