4 Replies Latest reply on Aug 29, 2005 3:09 AM by Andrew Bruno

    FInd task according to its variable

    Ronald van Kuijk Master

      I'd go for


      Solution: One way to do is to externalize this information with a mapping between the process Id and the office location in an external (not in jbpm) table and first query that table, then retrieve the process with the list of ids returned.


        • 1. Re: FInd task according to its variable
          Alejandro Guizar Master

          How about this: the jbpm_variableinstance table contains a processinstance field and some other fields with either the literal value for value-typed variables or a reference ID for entity-typed variables.

          If the office location is a string, you can issue an HQL query like the following to retrieve the process instances for that location:

          select processInstance
          from StringInstance
          where name='officeLocation'
           and value='Timbouctu'


          • 2. Re: FInd task according to its variable
            Andrew Bruno Newbie

            The problem I see here is that if you have two or more "different" process definitions that use the same variableName, then it could cause some confusion.

            • 3. Re: FInd task according to its variable
              Alejandro Guizar Master

              You're right Andrew, but that is easily solved by specifying a process definition:

              select processInstance
              from StringInstance
              where name = 'officeLocation'
               and value = :location
               and processInstance.processDefinition.id = :pid


              • 4. Re: FInd task according to its variable
                Andrew Bruno Newbie

                Alex, thanks for that.

                Anyway, this is the code that I ended up implementing

                
                 private static final String findProcessInstancesByVariableNameValueQuery = "select processInstance "
                 + "from org.jbpm.context.exe.variableinstance.StringInstance "
                 + "where name= :variableName " + "and value= :variableValue ";
                
                 /**
                 * Gets a list of processInstances by variable name and value
                 *
                 * @param variableName
                 * @param variableValue
                 * @return list of ProcessInstances
                 */
                 public List findProcessInstancesByVariableNameValue(String variableName,
                 String variableValue) {
                
                 List stringInstances = null;
                 List processInstances = null;
                 try {
                 Query query = session
                 .createQuery(findProcessInstancesByVariableNameValueQuery);
                 query.setString("variableName", variableName);
                 query.setString("variableValue", variableValue);
                
                 stringInstances = query.list();
                 } catch (Exception e) {
                 log.error(e);
                 jbpmSession.handleException();
                 throw new RuntimeException("couldn't get process instances ", e);
                 }
                
                 // The result is a StringInstance, hence we need to convert the
                 // StringInstance to ProcessInstance
                
                 if ((stringInstances != null) && (stringInstances.size() != 0)) {
                
                 StringInstance stringInstance = null;
                 processInstances = new ArrayList(stringInstances.size());
                
                 Iterator i = stringInstances.iterator();
                 while (i.hasNext()) {
                 stringInstance = (StringInstance) i.next();
                 processInstances.add(stringInstance.getProcessInstance());
                 }
                 }
                
                 return processInstances;
                
                 }
                
                


                Is there a better way to implement this, rather then looping through the StringInstances in order to get process instances?

                Thanks, Andrew