How to retrieve in java the value of the data process ?

1
0
-1

Ji !

The question is : how to get in java the value of a variable set during the process ? (for an external use of course)

Which API ? I don't find any clue in the javadoc !

Thanks for helping !

Comments

Submitted by romain.lataye on Tue, 11/18/2014 - 10:52

I had to ask in order maybe to find the solution :

Using this from ProcessRuntimeAPI

DataInstance getProcessDataInstance(String dataName, long processInstanceId) throws DataNotFoundException Get the value of named data item from a specified process instance. The value is returned in a DataInstance object.

http://documentation.bonitasoft.com/javadoc/api/6.3/org/bonitasoft/engin...(java.lang.String, long)

Submitted by antoine.mottier on Tue, 11/18/2014 - 12:05

When calling the getProcessDataInstance, are you sure that you set the "dataName" with exactly the same name as process data defined in the Studio?

Also can you share your code that you write in order to get the "processInstanceId" that you use?

Thanks.

Submitted by Smoltok on Wed, 11/19/2014 - 12:19

I answer instead of Romain :)

Here is what we use :

LoginAPI loginAPI = TenantAPIAccessor.getLoginAPI(); APISession apiSession = loginAPI.login(bonitaLogin, bonitaPassword); ProcessAPI processAPI = TenantAPIAccessor.getProcessAPI(apiSession);

SearchOptions searchOptions = new SearchOptionsBuilder(0, 250).filter(ArchivedProcessInstancesSearchDescriptor.NAME,"process_name").done(); SearchResult archivedProcessInstanceResults = processAPI.searchArchivedProcessInstances(searchOptions);

List archives = archivedProcessInstanceResults.getResult();

for (int i=0;i<archives.size();i++) { ArchivedProcessInstance api = (ArchivedProcessInstance) archives.get(i); ArchivedDataInstance di = processAPI.getArchivedProcessDataInstance("data_name",api.getRootProcessInstanceId());

String value_to_retrieve = di.getValue().toString(); }

1 answer

1
0
-1

Answer edited:

First, a simple recommendation: instead of using the getRootProcessInstanceId() method, you should use the getSourceObjectId() method. In fact, getArchivedProcessDataInstance expected to receive the process instance id as it was when the instance was "live" (i.e. not yet archived). Using getRootProcessInstanceId() should not be an issue as searchArchivedProcessInstances() will not return any sub-processes (and so root process instance should be the same as source object id).

I didn't did any important modification and it seems "to work just fine for me".

You can find below my version of the code and the associated Maven pom.xml file to build the project. Maybe you can try to run it and share with me log output (or your existing log output / stack trace).

package org.bonitasoft.democlientv6;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bonitasoft.engine.api.ApiAccessType;
import org.bonitasoft.engine.api.LoginAPI;
import org.bonitasoft.engine.api.ProcessAPI;
import org.bonitasoft.engine.api.TenantAPIAccessor;
import org.bonitasoft.engine.bpm.data.ArchivedDataInstance;
import org.bonitasoft.engine.bpm.process.ArchivedProcessInstance;
import org.bonitasoft.engine.bpm.process.ArchivedProcessInstancesSearchDescriptor;
import org.bonitasoft.engine.search.SearchOptions;
import org.bonitasoft.engine.search.SearchOptionsBuilder;
import org.bonitasoft.engine.search.SearchResult;
import org.bonitasoft.engine.session.APISession;
import org.bonitasoft.engine.util.APITypeManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

        private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

        private static final String BONITA_SERVER_HOSTNAME = "localhost";

        private static final String BONITA_SERVER_PORT = "8080";

        private static final String BONITA_WEBAPP_NAME = "bonita";

        private static final String PROCESS_NAME = "process_name";

        public static void main(String[] args) throws Exception {

                Map<String, String> apiTypeManagerParams = new HashMap<>();
                apiTypeManagerParams.put("server.url", "http://"
                                + BONITA_SERVER_HOSTNAME + ":" + BONITA_SERVER_PORT);
                apiTypeManagerParams.put("application.name", BONITA_WEBAPP_NAME);
                APITypeManager.setAPITypeAndParams(ApiAccessType.HTTP,
                                apiTypeManagerParams);

                LoginAPI loginAPI = TenantAPIAccessor.getLoginAPI();
                APISession session = loginAPI.login("walter.bates", "bpm");
                ProcessAPI processAPI = TenantAPIAccessor.getProcessAPI(session);

                SearchOptions searchOptions = new SearchOptionsBuilder(0, 250).filter(
                                ArchivedProcessInstancesSearchDescriptor.NAME, PROCESS_NAME)
                                .done();
                SearchResult<ArchivedProcessInstance> archivedProcessInstanceResults = processAPI
                                .searchArchivedProcessInstances(searchOptions);

                List<ArchivedProcessInstance> archives = archivedProcessInstanceResults
                                .getResult();

                LOGGER.info("Found {} archived process instances for process {}",
                                archives.size(), PROCESS_NAME);

                for (ArchivedProcessInstance archivedProcessInstance : archives) {
                        long sourceObjectId = archivedProcessInstance.getSourceObjectId();
                        long rootProcessInstanceId = archivedProcessInstance
                                        .getRootProcessInstanceId();

                        LOGGER.info("Root process instance: {}", rootProcessInstanceId);
                        LOGGER.info("Source object id: {}", sourceObjectId);
                        LOGGER.info("Process name: {}", archivedProcessInstance.getName());

                        ArchivedDataInstance di = processAPI
                                        .getArchivedProcessDataInstance("data_name",
                                                        sourceObjectId);
                        String valueToRetrieve = di.getValue().toString();
                        LOGGER.info("value: {}", valueToRetrieve);
                }
        }
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>org.bonitasoft</groupId>
        <artifactId>demo-client-v6</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <description>A standalone Java application that call Bonita BPM Execution Engine API using Bonita Client library.</description>

        <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

        <build>
                <plugins>
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-compiler-plugin</artifactId>
                                <version>3.1</version>
                                <configuration>
                                        <encoding>UTF-8</encoding>
                                        <source>1.7</source>
                                        <target>1.7</target>
                                </configuration>
                        </plugin>
                </plugins>
                <pluginManagement>
                        <plugins>
                                <!--This plugin's configuration is used to store Eclipse m2e settings
                                        only. It has no influence on the Maven build itself. -->
                                <plugin>
                                        <groupId>org.eclipse.m2e</groupId>
                                        <artifactId>lifecycle-mapping</artifactId>
                                        <version>1.0.0</version>
                                        <configuration>
                                                <lifecycleMappingMetadata>
                                                        <pluginExecutions>
                                                                <pluginExecution>
                                                                        <pluginExecutionFilter>
                                                                                <groupId>
                                                                                        org.apache.maven.plugins
                                                                                </groupId>
                                                                                <artifactId>
                                                                                        maven-dependency-plugin
                                                                                </artifactId>
                                                                                <versionRange>
                                                                                        [2.1,)
                                                                                </versionRange>
                                                                                <goals>
                                                                                        <goal>
                                                                                                copy-dependencies
                                                                                        </goal>
                                                                                </goals>
                                                                        </pluginExecutionFilter>
                                                                        <action>
                                                                                <ignore></ignore>
                                                                        </action>
                                                                </pluginExecution>
                                                        </pluginExecutions>
                                                </lifecycleMappingMetadata>
                                        </configuration>
                                </plugin>
                        </plugins>
                </pluginManagement>
        </build>

        <dependencies>
                <dependency>
                        <groupId>org.bonitasoft.engine</groupId>
                        <artifactId>bonita-client</artifactId>
                        <version>6.3.8</version>
                </dependency>
                <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-api</artifactId>
                        <version>1.7.7</version>
                </dependency>
                <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-simple</artifactId>
                        <version>1.7.7</version>
                </dependency>
        </dependencies>

</project>

Comments

Submitted by Smoltok on Wed, 11/19/2014 - 14:16

Currently, there is no sub process but if I call getId instead of getRoot..() it didn't work !

Submitted by antoine.mottier on Wed, 11/19/2014 - 20:11

I edit my answer: your code seems to work as it is. So I would be interested to know if you get any stack trace / log message in order for me to find the root cause of the issue?

Submitted by Smoltok on Thu, 11/20/2014 - 11:05

Well, here are the logs : (don't think it is very useful)

11:02:46.791 [main] INFO - Found 129 archived process instances for process XXX 11:02:46.795 [main] INFO - Root process instance: 41008 11:02:46.795 [main] INFO - Source object id: 41008 11:02:46.795 [main] INFO - Process name: XXX 11:02:46.811 [main] INFO - value: yyyyy 11:02:46.811 [main] INFO - Root process instance: 42002 11:02:46.811 [main] INFO - Source object id: 42002 11:02:46.811 [main] INFO - Process name: XXX 11:02:46.822 [main] INFO - value: eeeee .....

Submitted by antoine.mottier on Thu, 11/20/2014 - 11:15

Ok so from the log file it seems to successfully retrieve archived process instance data value. So I don't understand what goes wrong for you here?

Maybe you want to get the data of currently running instance. In such case, source code should be pretty much the same but without the "Archived" keyword in API methods and Java classes.

Notifications