Search in sources :

Example 1 with ContentMarshallerContext

use of org.kie.internal.task.api.ContentMarshallerContext in project jbpm by kiegroup.

the class UserTaskServiceImpl method getTaskOutputContentByTaskId.

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> getTaskOutputContentByTaskId(String deploymentId, Long taskId) {
    UserTaskInstanceDesc task = dataService.getTaskById(taskId);
    validateTask(deploymentId, taskId, task);
    RuntimeManager manager = getRuntimeManager(task);
    if (manager == null) {
        logger.warn("Cannot find runtime manager for task {}", taskId);
        return null;
    }
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(task.getProcessInstanceId()));
    try {
        TaskService taskService = engine.getTaskService();
        // perform actual operation
        Task taskInstanceById = taskService.getTaskById(taskId);
        long documentContentId = taskInstanceById.getTaskData().getOutputContentId();
        if (documentContentId > 0) {
            Content contentById = taskService.getContentById(documentContentId);
            if (contentById == null) {
                return new HashMap<String, Object>();
            }
            ContentMarshallerContext ctx = TaskContentRegistry.get().getMarshallerContext(task.getDeploymentId());
            Object unmarshall = ContentMarshallerHelper.unmarshall(contentById.getContent(), ctx.getEnvironment(), ctx.getClassloader());
            Map<String, Object> data = (Map<String, Object>) unmarshall;
            for (Object variable : data.values()) {
                if (variable instanceof LazyLoaded<?>) {
                    ((LazyLoaded<?>) variable).load();
                }
            }
            return data;
        }
        return new HashMap<String, Object>();
    } finally {
        disposeRuntimeEngine(manager, engine);
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) Task(org.kie.api.task.model.Task) HashMap(java.util.HashMap) InternalTaskService(org.kie.internal.task.api.InternalTaskService) UserTaskService(org.jbpm.services.api.UserTaskService) TaskService(org.kie.api.task.TaskService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) LazyLoaded(org.kie.internal.utils.LazyLoaded) Content(org.kie.api.task.model.Content) UserTaskInstanceDesc(org.jbpm.services.api.model.UserTaskInstanceDesc) HashMap(java.util.HashMap) Map(java.util.Map) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Example 2 with ContentMarshallerContext

use of org.kie.internal.task.api.ContentMarshallerContext in project jbpm by kiegroup.

the class SingletonRuntimeManager method init.

public void init() {
    // TODO should we proxy/wrap the ksession so we capture dispose.destroy method calls?
    String location = getLocation();
    Long knownSessionId = getPersistedSessionId(location, identifier);
    InternalTaskService internalTaskService = newTaskService(taskServiceFactory);
    boolean owner = false;
    TransactionManager tm = null;
    if (environment.usePersistence()) {
        tm = getTransactionManagerInternal(environment.getEnvironment());
        owner = tm.begin();
    }
    try {
        if (knownSessionId > 0) {
            try {
                this.singleton = new SynchronizedRuntimeImpl(factory.findKieSessionById(knownSessionId), internalTaskService);
            } catch (RuntimeException e) {
            // in case session with known id was found
            }
        }
        if (this.singleton == null) {
            this.singleton = new SynchronizedRuntimeImpl(factory.newKieSession(), internalTaskService);
            persistSessionId(location, identifier, singleton.getKieSession().getIdentifier());
        }
        ((RuntimeEngineImpl) singleton).setManager(this);
        TaskContentRegistry.get().addMarshallerContext(getIdentifier(), new ContentMarshallerContext(environment.getEnvironment(), environment.getClassLoader()));
        configureRuntimeOnTaskService(internalTaskService, singleton);
        registerItems(this.singleton);
        attachManager(this.singleton);
        this.registry.register(this);
        if (tm != null) {
            tm.commit(owner);
        }
    } catch (Exception e) {
        if (tm != null) {
            tm.rollback(owner);
        }
        throw new RuntimeException("Exception while initializing runtime manager " + this.identifier, e);
    }
}
Also used : TransactionManager(org.drools.persistence.api.TransactionManager) InternalTaskService(org.kie.internal.task.api.InternalTaskService) IOException(java.io.IOException) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Example 3 with ContentMarshallerContext

use of org.kie.internal.task.api.ContentMarshallerContext in project jbpm by kiegroup.

the class PerProcessInstanceRuntimeManager method init.

@Override
public void init() {
    TaskContentRegistry.get().addMarshallerContext(getIdentifier(), new ContentMarshallerContext(environment.getEnvironment(), environment.getClassLoader()));
    boolean owner = false;
    TransactionManager tm = null;
    if (environment.usePersistence()) {
        tm = getTransactionManagerInternal(environment.getEnvironment());
        owner = tm.begin();
    }
    try {
        // need to init one session to bootstrap all case - such as start timers
        KieSession initialKsession = factory.newKieSession();
        // there is a need to call getProcessRuntime otherwise the start listeners are not registered
        initialKsession.execute(new ExecutableCommand<Void>() {

            private static final long serialVersionUID = 1L;

            @Override
            public Void execute(org.kie.api.runtime.Context context) {
                KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
                ((InternalKnowledgeRuntime) ksession).getProcessRuntime();
                return null;
            }
        });
        factory.onDispose(initialKsession.getIdentifier());
        initialKsession.execute(new DestroyKSessionCommand(initialKsession, this));
        if (!"false".equalsIgnoreCase(System.getProperty("org.jbpm.rm.init.timer"))) {
            if (mapper instanceof JPAMapper) {
                List<Long> ksessionsToInit = ((JPAMapper) mapper).findKSessionToInit(this.identifier);
                for (Long id : ksessionsToInit) {
                    initialKsession = factory.findKieSessionById(id);
                    initialKsession.execute(new DisposeKSessionCommand(initialKsession, this));
                }
            }
        }
        if (tm != null) {
            tm.commit(owner);
        }
    } catch (Exception e) {
        if (tm != null) {
            tm.rollback(owner);
        }
        throw new RuntimeException("Exception while initializing runtime manager " + this.identifier, e);
    }
}
Also used : SessionNotFoundException(org.kie.internal.runtime.manager.SessionNotFoundException) TransactionManager(org.drools.persistence.api.TransactionManager) JPAMapper(org.jbpm.runtime.manager.impl.mapper.JPAMapper) KieSession(org.kie.api.runtime.KieSession) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Example 4 with ContentMarshallerContext

use of org.kie.internal.task.api.ContentMarshallerContext in project jbpm by kiegroup.

the class TaskInstanceServiceImpl method getContentMapForUser.

@Override
public Map<String, Object> getContentMapForUser(Long taskId, String userId) {
    // check permissions
    this.lifeCycleManager.taskOperation(Operation.View, taskId, userId, null, null, toGroups(null));
    Task task = this.persistenceContext.findTask(taskId);
    if (task.getTaskData() != null && task.getTaskData().getOutputContentId() != null) {
        Content content = this.persistenceContext.findContent(task.getTaskData().getOutputContentId());
        ContentMarshallerContext mContext = TaskContentRegistry.get().getMarshallerContext(task);
        Object outputContent = ContentMarshallerHelper.unmarshall(content.getContent(), mContext.getEnvironment(), mContext.getClassloader());
        if (outputContent instanceof Map) {
            return (Map<String, Object>) outputContent;
        } else {
            throw new IllegalStateException("Output content for task " + taskId + " is not a Map<String, Object>!");
        }
    }
    return null;
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) Content(org.kie.api.task.model.Content) InternalContent(org.kie.internal.task.api.model.InternalContent) HashMap(java.util.HashMap) Map(java.util.Map) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Example 5 with ContentMarshallerContext

use of org.kie.internal.task.api.ContentMarshallerContext in project jbpm by kiegroup.

the class TaskContentServiceImpl method loadContentData.

@SuppressWarnings("unchecked")
protected Map<String, Object> loadContentData(Long contentId, Task task) {
    if (contentId != null) {
        Map<String, Object> data = null;
        Content contentById = getContentById(contentId);
        if (contentById != null) {
            ContentMarshallerContext mContext = getMarshallerContext(task);
            Object unmarshalledObject = ContentMarshallerHelper.unmarshall(contentById.getContent(), mContext.getEnvironment(), mContext.getClassloader());
            if (!(unmarshalledObject instanceof Map)) {
                data = new HashMap<String, Object>();
                data.put("Content", unmarshalledObject);
            } else {
                data = (Map<String, Object>) unmarshalledObject;
            }
            return data;
        }
    }
    return null;
}
Also used : Content(org.kie.api.task.model.Content) InternalContent(org.kie.internal.task.api.model.InternalContent) HashMap(java.util.HashMap) Map(java.util.Map) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Aggregations

ContentMarshallerContext (org.kie.internal.task.api.ContentMarshallerContext)21 Content (org.kie.api.task.model.Content)15 Map (java.util.Map)14 HashMap (java.util.HashMap)12 Task (org.kie.api.task.model.Task)12 InternalContent (org.kie.internal.task.api.model.InternalContent)8 ContentData (org.kie.internal.task.api.model.ContentData)6 InternalTaskData (org.kie.internal.task.api.model.InternalTaskData)5 TaskEventSupport (org.jbpm.services.task.events.TaskEventSupport)4 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)4 InternalTaskService (org.kie.internal.task.api.InternalTaskService)4 TaskPersistenceContext (org.kie.internal.task.api.TaskPersistenceContext)4 TransactionManager (org.drools.persistence.api.TransactionManager)3 UserTaskService (org.jbpm.services.api.UserTaskService)3 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)3 TaskContext (org.jbpm.services.task.commands.TaskContext)3 PermissionDeniedException (org.jbpm.services.task.exception.PermissionDeniedException)3 KieSession (org.kie.api.runtime.KieSession)3 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)3 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)3