Search in sources :

Example 16 with Wrapped

use of org.kie.server.api.model.Wrapped in project droolsjbpm-integration by kiegroup.

the class UserTaskAdminServicesClientImpl method reassignWhenNotCompleted.

@Override
public Long reassignWhenNotCompleted(String containerId, Long taskId, String expiresAt, OrgEntities orgEntities) {
    Object result = null;
    if (config.isRest()) {
        Map<String, Object> valuesMap = new HashMap<String, Object>();
        valuesMap.put(CONTAINER_ID, containerId);
        valuesMap.put(TASK_INSTANCE_ID, taskId);
        Map<String, String> headers = new HashMap<String, String>();
        String queryString = "?expiresAt=" + expiresAt + "&whenNotCompleted=true";
        result = makeHttpPostRequestAndCreateCustomResponse(build(loadBalancer.getUrl(), ADMIN_TASK_URI + "/" + TASK_INSTANCE_REASSIGNMENTS_URI, valuesMap) + queryString, orgEntities, Object.class, headers);
    } else {
        CommandScript script = new CommandScript(Collections.singletonList((KieServerCommand) new DescriptorCommand("UserTaskAdminService", "reassignWhenNotCompleted", serialize(orgEntities), marshaller.getFormat().getType(), new Object[] { containerId, taskId, expiresAt })));
        ServiceResponse<String> response = (ServiceResponse<String>) executeJmsCommand(script, DescriptorCommand.class.getName(), "BPM", containerId).getResponses().get(0);
        throwExceptionOnFailure(response);
        if (shouldReturnWithNullResponse(response)) {
            return null;
        }
        result = deserialize(response.getResult(), Object.class);
    }
    if (result instanceof Wrapped) {
        return (Long) ((Wrapped) result).unwrap();
    }
    return ((Number) result).longValue();
}
Also used : DescriptorCommand(org.kie.server.api.commands.DescriptorCommand) KieServerCommand(org.kie.server.api.model.KieServerCommand) HashMap(java.util.HashMap) CommandScript(org.kie.server.api.commands.CommandScript) ServiceResponse(org.kie.server.api.model.ServiceResponse) Wrapped(org.kie.server.api.model.Wrapped)

Example 17 with Wrapped

use of org.kie.server.api.model.Wrapped in project droolsjbpm-integration by kiegroup.

the class ControllerUtils method unmarshal.

public static <T> T unmarshal(String data, String marshallingFormat, Class<T> unmarshalType) {
    if (data == null || data.isEmpty()) {
        return null;
    }
    MarshallingFormat format = getFormat(marshallingFormat);
    Marshaller marshaller = null;
    switch(format) {
        case JAXB:
            {
                marshaller = jaxbMarshaller;
                break;
            }
        case JSON:
            {
                marshaller = jsonMarshaller;
                break;
            }
        default:
            {
                marshaller = jsonMarshaller;
                break;
            }
    }
    Object instance = marshaller.unmarshall(data, unmarshalType);
    if (instance instanceof Wrapped) {
        return (T) ((Wrapped) instance).unwrap();
    }
    return (T) instance;
}
Also used : Marshaller(org.kie.server.api.marshalling.Marshaller) MarshallingFormat(org.kie.server.api.marshalling.MarshallingFormat) Wrapped(org.kie.server.api.model.Wrapped)

Example 18 with Wrapped

use of org.kie.server.api.model.Wrapped in project droolsjbpm-integration by kiegroup.

the class DMNKieContainerCommandServiceImpl method executeScript.

@Override
public ServiceResponsesList executeScript(CommandScript commands, MarshallingFormat marshallingFormat, String classType) {
    List<ServiceResponse<? extends Object>> responses = new ArrayList<ServiceResponse<? extends Object>>();
    for (KieServerCommand command : commands.getCommands()) {
        if (!(command instanceof DescriptorCommand)) {
            LOG.warn("Unsupported command '{}' given, will not process it", command.getClass().getName());
            continue;
        }
        try {
            ServiceResponse<?> result = null;
            Object handler = null;
            DescriptorCommand descriptorCommand = (DescriptorCommand) command;
            // find out the handler to call to process given command
            if ("DMNService".equals(descriptorCommand.getService())) {
                handler = modelEvaluatorServiceBase;
            } else {
                throw new IllegalStateException("Unable to find handler for " + descriptorCommand.getService() + " service");
            }
            List<Object> arguments = new ArrayList();
            // process and unwrap arguments
            for (Object arg : descriptorCommand.getArguments()) {
                LOG.debug("Before :: Argument with type {} and value {}", arg.getClass(), arg);
                if (arg instanceof Wrapped) {
                    arg = ((Wrapped) arg).unwrap();
                }
                LOG.debug("After :: Argument with type {} and value {}", arg.getClass(), arg);
                arguments.add(arg);
            }
            if (descriptorCommand.getPayload() != null && !descriptorCommand.getPayload().isEmpty()) {
                arguments.add(descriptorCommand.getPayload());
            }
            if (descriptorCommand.getMarshallerFormat() != null && !descriptorCommand.getMarshallerFormat().isEmpty()) {
                arguments.add(descriptorCommand.getMarshallerFormat());
            }
            LOG.debug("About to execute {} operation on {} with args {}", descriptorCommand.getMethod(), handler, arguments);
            // process command via reflection and handler
            result = (ServiceResponse<?>) MethodUtils.invokeMethod(handler, descriptorCommand.getMethod(), arguments.toArray());
            LOG.debug("Handler {} returned response {}", handler, result);
            // return successful result
            responses.add(result);
        } catch (InvocationTargetException e) {
            responses.add(new ServiceResponse(ServiceResponse.ResponseType.FAILURE, e.getTargetException().getMessage()));
        } catch (Throwable e) {
            LOG.error("Error while processing {} command", command, e);
            // return failure result
            responses.add(new ServiceResponse(ServiceResponse.ResponseType.FAILURE, e.getMessage()));
        }
    }
    LOG.debug("About to return responses '{}'", responses);
    return new ServiceResponsesList(responses);
}
Also used : DescriptorCommand(org.kie.server.api.commands.DescriptorCommand) ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) KieServerCommand(org.kie.server.api.model.KieServerCommand) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceResponse(org.kie.server.api.model.ServiceResponse) Wrapped(org.kie.server.api.model.Wrapped)

Example 19 with Wrapped

use of org.kie.server.api.model.Wrapped in project droolsjbpm-integration by kiegroup.

the class JBPMKieContainerCommandServiceImpl method executeScript.

@Override
public ServiceResponsesList executeScript(CommandScript commands, MarshallingFormat marshallingFormat, String classType) {
    List<ServiceResponse<? extends Object>> responses = new ArrayList<ServiceResponse<? extends Object>>();
    for (KieServerCommand command : commands.getCommands()) {
        if (!(command instanceof DescriptorCommand)) {
            logger.warn("Unsupported command '{}' given, will not process it", command.getClass().getName());
            continue;
        }
        boolean wrapResults = false;
        try {
            Object result = null;
            Object handler = null;
            DescriptorCommand descriptorCommand = (DescriptorCommand) command;
            // find out the handler to call to process given command
            if ("DefinitionService".equals(descriptorCommand.getService())) {
                handler = definitionServiceBase;
            } else if ("ProcessService".equals(descriptorCommand.getService())) {
                handler = processServiceBase;
            } else if ("UserTaskService".equals(descriptorCommand.getService())) {
                handler = userTaskServiceBase;
            } else if ("QueryService".equals(descriptorCommand.getService())) {
                handler = runtimeDataServiceBase;
            } else if ("JobService".equals(descriptorCommand.getService())) {
                handler = executorServiceBase;
            } else if ("QueryDataService".equals(descriptorCommand.getService())) {
                handler = queryDataServiceBase;
                // enable wrapping as in case of embedded objects jaxb does not properly parse it due to possible unknown types (List<?> etc)
                if (marshallingFormat.equals(MarshallingFormat.JAXB)) {
                    wrapResults = true;
                }
            } else if ("DocumentService".equals(descriptorCommand.getService())) {
                handler = documentServiceBase;
            } else if ("ProcessAdminService".equals(descriptorCommand.getService())) {
                handler = processAdminServiceBase;
            } else if ("UserTaskAdminService".equals(descriptorCommand.getService())) {
                handler = userTaskAdminServiceBase;
            } else {
                throw new IllegalStateException("Unable to find handler for " + descriptorCommand.getService() + " service");
            }
            List<Object> arguments = new ArrayList();
            // process and unwrap arguments
            for (Object arg : descriptorCommand.getArguments()) {
                logger.debug("Before :: Argument with type {} and value {}", arg.getClass(), arg);
                if (arg instanceof Wrapped) {
                    arg = ((Wrapped) arg).unwrap();
                }
                logger.debug("After :: Argument with type {} and value {}", arg.getClass(), arg);
                arguments.add(arg);
            }
            if (descriptorCommand.getPayload() != null && !descriptorCommand.getPayload().isEmpty()) {
                arguments.add(descriptorCommand.getPayload());
            }
            if (descriptorCommand.getMarshallerFormat() != null && !descriptorCommand.getMarshallerFormat().isEmpty()) {
                arguments.add(descriptorCommand.getMarshallerFormat());
            }
            logger.debug("About to execute {} operation on {} with args {}", descriptorCommand.getMethod(), handler, arguments);
            // process command via reflection and handler
            result = MethodUtils.invokeMethod(handler, descriptorCommand.getMethod(), arguments.toArray());
            logger.debug("Handler {} returned response {}", handler, result);
            if (wrapResults) {
                result = ModelWrapper.wrap(result);
                logger.debug("Wrapped response is {}", result);
            }
            // return successful result
            responses.add(new ServiceResponse(ServiceResponse.ResponseType.SUCCESS, "", result));
        } catch (InvocationTargetException e) {
            logger.error("Error while processing {} command", command, e);
            responses.add(new ServiceResponse(ServiceResponse.ResponseType.FAILURE, e.getTargetException().getMessage()));
        } catch (Throwable e) {
            logger.error("Error while processing {} command", command, e);
            // return failure result
            responses.add(new ServiceResponse(ServiceResponse.ResponseType.FAILURE, e.getMessage()));
        }
    }
    logger.debug("About to return responses '{}'", responses);
    return new ServiceResponsesList(responses);
}
Also used : DescriptorCommand(org.kie.server.api.commands.DescriptorCommand) ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) KieServerCommand(org.kie.server.api.model.KieServerCommand) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceResponse(org.kie.server.api.model.ServiceResponse) Wrapped(org.kie.server.api.model.Wrapped)

Example 20 with Wrapped

use of org.kie.server.api.model.Wrapped in project droolsjbpm-integration by kiegroup.

the class ProcessServicesClientImpl method startProcessFromNodeIds.

@Override
public Long startProcessFromNodeIds(String containerId, String processId, CorrelationKey correlationKey, Map<String, Object> variables, String... nodes) {
    Object result = null;
    ProcessStartSpec spec = new ProcessStartSpec();
    spec.setVariables(variables);
    spec.setNodeIds(Arrays.asList(nodes));
    if (config.isRest()) {
        Map<String, Object> valuesMap = new HashMap<>();
        valuesMap.put(CONTAINER_ID, containerId);
        valuesMap.put(PROCESS_ID, processId);
        valuesMap.put(CORRELATION_KEY, correlationKey.toExternalForm());
        result = makeHttpPostRequestAndCreateCustomResponse(build(loadBalancer.getUrl(), PROCESS_URI + "/" + START_PROCESS_FROM_NODES_WITH_CORRELATION_KEY_POST_URI, valuesMap), spec, Object.class);
    } else {
        CommandScript script = new CommandScript(singletonList((KieServerCommand) new DescriptorCommand("ProcessService", "startProcessWithCorrelationKeyFromNodeIds", serialize(spec), marshaller.getFormat().getType(), containerId, processId, correlationKey.toExternalForm())));
        ServiceResponse<String> response = (ServiceResponse<String>) executeJmsCommand(script, DescriptorCommand.class.getName(), "BPM", containerId).getResponses().get(0);
        throwExceptionOnFailure(response);
        if (shouldReturnWithNullResponse(response)) {
            return null;
        }
        result = deserialize(response.getResult(), Object.class);
    }
    if (result instanceof Wrapped) {
        return (Long) ((Wrapped) result).unwrap();
    }
    return ((Number) result).longValue();
}
Also used : DescriptorCommand(org.kie.server.api.commands.DescriptorCommand) KieServerCommand(org.kie.server.api.model.KieServerCommand) HashMap(java.util.HashMap) CommandScript(org.kie.server.api.commands.CommandScript) ServiceResponse(org.kie.server.api.model.ServiceResponse) ProcessStartSpec(org.kie.server.api.model.definition.ProcessStartSpec) Wrapped(org.kie.server.api.model.Wrapped)

Aggregations

Wrapped (org.kie.server.api.model.Wrapped)36 ServiceResponse (org.kie.server.api.model.ServiceResponse)32 DescriptorCommand (org.kie.server.api.commands.DescriptorCommand)31 KieServerCommand (org.kie.server.api.model.KieServerCommand)31 HashMap (java.util.HashMap)27 CommandScript (org.kie.server.api.commands.CommandScript)27 ArrayList (java.util.ArrayList)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 ServiceResponsesList (org.kie.server.api.model.ServiceResponsesList)5 List (java.util.List)4 Map (java.util.Map)4 NodeInstanceList (org.kie.server.api.model.instance.NodeInstanceList)4 ProcessInstanceList (org.kie.server.api.model.instance.ProcessInstanceList)4 VariableInstanceList (org.kie.server.api.model.instance.VariableInstanceList)4 Marshaller (org.kie.server.api.marshalling.Marshaller)3 MarshallingFormat (org.kie.server.api.marshalling.MarshallingFormat)3 ItemList (org.kie.server.api.model.ItemList)3 ExecutionErrorInstanceList (org.kie.server.api.model.admin.ExecutionErrorInstanceList)3 ProcessDefinitionList (org.kie.server.api.model.definition.ProcessDefinitionList)3 QueryDefinitionList (org.kie.server.api.model.definition.QueryDefinitionList)3