Search in sources :

Example 1 with TaskServiceRequest

use of org.jbpm.services.task.rule.TaskServiceRequest in project jbpm by kiegroup.

the class TaskRuleServiceImpl method executeRules.

@Override
public void executeRules(Task task, String userId, Object params, String scope) throws TaskException {
    KieBase ruleBase = ruleContextProvider.getKieBase(scope);
    if (ruleBase != null) {
        KieSession session = ruleBase.newKieSession();
        Map<String, Object> globals = ruleContextProvider.getGlobals(scope);
        if (globals != null) {
            for (Map.Entry<String, Object> entry : globals.entrySet()) {
                session.setGlobal(entry.getKey(), entry.getValue());
            }
        }
        User user = TaskModelProvider.getFactory().newUser();
        ((InternalOrganizationalEntity) user).setId(userId);
        TaskServiceRequest request = new TaskServiceRequest(scope, user, null);
        session.setGlobal("request", request);
        session.insert(task);
        if (params != null) {
            if (params instanceof ContentData) {
                ContentMarshallerContext ctx = TaskContentRegistry.get().getMarshallerContext(task);
                params = ContentMarshallerHelper.unmarshall(((ContentData) params).getContent(), ctx.getEnvironment(), ctx.getClassloader());
            }
            session.insert(params);
        }
        session.fireAllRules();
        session.dispose();
        if (!request.isAllowed()) {
            StringBuilder error = new StringBuilder("Cannot perform operation " + scope + " :\n");
            if (request.getReasons() != null) {
                for (String reason : request.getReasons()) {
                    error.append(reason).append('\n');
                }
            }
            throw request.getException(error.toString());
        }
    }
}
Also used : User(org.kie.api.task.model.User) InternalOrganizationalEntity(org.kie.internal.task.api.model.InternalOrganizationalEntity) ContentData(org.kie.internal.task.api.model.ContentData) TaskServiceRequest(org.jbpm.services.task.rule.TaskServiceRequest) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Map(java.util.Map) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Aggregations

Map (java.util.Map)1 TaskServiceRequest (org.jbpm.services.task.rule.TaskServiceRequest)1 KieBase (org.kie.api.KieBase)1 KieSession (org.kie.api.runtime.KieSession)1 User (org.kie.api.task.model.User)1 ContentMarshallerContext (org.kie.internal.task.api.ContentMarshallerContext)1 ContentData (org.kie.internal.task.api.model.ContentData)1 InternalOrganizationalEntity (org.kie.internal.task.api.model.InternalOrganizationalEntity)1