5 Replies Latest reply on Apr 2, 2009 8:46 AM by kukeltje

    Queries to search the database for process instances  based

      Hi,

      I need to make an API in jBPM 3.0.2 able to make simple queries to search the database for process instances based on process variables.

      I try a little test and I develloped that :

      on ContextSession :

       nomAtt : process variable name
       o : value of the process variable to search
      
       public List findProcessInstance(String nomAtt,Object o)
       {
      
      
       JbpmType jbpmType = this.getJbpmType(o.getClass());
      
       Object objetConverti = jbpmType.converter.convert(o);
      
      
       String req = "select vi.processInstance " +
       " from " +
       jbpmType.variableInstanceClass.getName() +
       " vi" +
       " where vi.value = :value and vi.name = :name ";
      
      
       List result = null;
       try {
       Query query = session.createQuery(req);
       query.setParameter("value", objetConverti);
       query.setString("name", nomAtt);
       result = query.list();
       } catch (Exception e) {
       jbpmSession.handleException();
       throw new RuntimeException("oulahlah ", e);
       }
       return result;
      
      
       }


      and the code of the method getJbpmType the idea come from VariableInstance.createVariableInstance(Class javaType)

      public JbpmType getJbpmType(Class javaType) {
      
       Iterator iter = JbpmType.getJbpmTypes().iterator();
       while (iter.hasNext())
       {
       JbpmType jbpmType = (JbpmType) iter.next();
      
       // isMatch indicates wether the given javaType matches this jbpmType
       boolean isMatch = false;
       if ( (jbpmType.variableClass!=null)
       && (jbpmType.variableClass.isAssignableFrom(javaType)) ) {
       isMatch = true;
       } else {
       if (javaType.getName().equals(jbpmType.variableClassName)) {
       isMatch = true;
       } else if ("{serializable-classes}".equals(jbpmType.variableClassName)) {
       isMatch = (Serializable.class.isAssignableFrom(javaType));
       } else if ("{hibernateable-long-id-classes}".equals(jbpmType.variableClassName)) {
       JbpmSession currentJbpmSession = JbpmSession.getCurrentJbpmSession();
       isMatch = ( (currentJbpmSession!=null)
       && (currentJbpmSession.getJbpmSessionFactory().isHibernatableWithLongId(javaType)));
       } else if ("{hibernateable-string-id-classes}".equals(jbpmType.variableClassName)) {
       JbpmSession currentJbpmSession = JbpmSession.getCurrentJbpmSession();
       isMatch = ( (currentJbpmSession!=null)
       && (currentJbpmSession.getJbpmSessionFactory().isHibernatableWithStringId(javaType)));
       }
       }
      
       if (isMatch) {
       return jbpmType;
       }
       }
      
       return null;
      
       }





      Does someone have an idea/suggestion on this code. Is it a good way to do that ?
      Can I use easily this method with the futur versions (3.1, ...) of jBPM


      Thanks in advance for your answer.





        • 1. Re: Queries to search the database for process instances  ba
          aguizar

           

          "" wrote:
          Does someone have an idea/suggestion on this code. Is it a good way to do that ?

          The only problem I see is that the JbpmSession has been deprecated in the branch 3.1. In exchange, the class JbpmType is more useful now that it exposes a method that does exactly what you need:
          public class JbpmType {
           ...
           public boolean matches(Class valueClass) {
           ...
           }
          }

          I'd suggest you moved to 3.1 if at all possible.

          • 2. Re: Queries to search the database for process instances  ba
            andixt

            Hi,

            I have the similar problem. I have some process instances which stores some variables of type serializable. I mean, variables are not simple String or long objects but custom beans which implements serializable. So, is it possible to make search query for such cases based on some property of such serializable beans?

            Example: each process instance stores variable named "registrationInfo" which is of type RegistrationInfo:

            public class RegistrationInfo implements Serializable{
             private String username;
             private String email;
             private String password;
            ...
            }
            


            Is it possible to make query for all process instances which store variable named "registrationInfo" and this variable has property "username" set to 'admin' for example? The purpose of this query to is check if there is a running process which stores registration information about user who has chosen some username. Just to check if such user name is not taken yet.
            Is username whould stored as String variable I think there wouldn't problem to check that. But this username is stored inside of serializable bean, so I cannot guess how to fetch such records.
            Thanks.

            • 3. Re: Queries to search the database for process instances  ba

              If you store these three variables as strings instead of RegistrationInfo this task would be really simple. (You could create a RegistrationInfo from the strings where needed.)

              In table jbpm_variableinstance you see that date, long, double, and strings are handled specially, very simple to access. All other serialized variables are put into jbpm_bytearray/byteblock i.e. essentially stored as a blob.

              Volker

              • 4. Re: Queries to search the database for process instances  ba
                andixt

                Thank you for reply.
                Unfortunately my bean is much bigger, and has more than 20 properties, I've just listed only few to show structure somehow.

                So, there is no way to make query with blobs, right?

                • 5. Re: Queries to search the database for process instances  ba
                  kukeltje

                  or store them in your own domain model and not *in* jBPM