public ScopeDAO getScope(Long siidl) {
   for (ProcessDaoImpl process : _store.values()) {
     for (ProcessInstanceDAO instance : process._instances.values()) {
       if (instance.getScope(siidl) != null) return instance.getScope(siidl);
     }
   }
   return null;
 }
 public Map<Long, Collection<CorrelationSetDAO>> getCorrelationSets(
     Collection<ProcessInstanceDAO> instances) {
   Map<Long, Collection<CorrelationSetDAO>> map =
       new HashMap<Long, Collection<CorrelationSetDAO>>();
   for (ProcessInstanceDAO instance : instances) {
     Long id = instance.getInstanceId();
     Collection<CorrelationSetDAO> existing = map.get(id);
     if (existing == null) {
       existing = new ArrayList<CorrelationSetDAO>();
       map.put(id, existing);
     }
     existing.addAll(instance.getCorrelationSets());
   }
   return map;
 }
  public void invokeNewInstance(MyRoleMessageExchangeImpl mex, RoutingInfo routing) {
    Operation operation = getMyRoleOperation(mex.getOperationName());

    if (__log.isDebugEnabled()) {
      __log.debug(
          "INPUTMSG: "
              + routing.correlator.getCorrelatorId()
              + ": routing failed, CREATING NEW INSTANCE");
    }
    ProcessDAO processDAO = _process.getProcessDAO();

    if (_process._pconf.getState() == ProcessState.RETIRED) {
      throw new InvalidProcessException(
          "Process is retired.", InvalidProcessException.RETIRED_CAUSE_CODE);
    }

    if (!_process.processInterceptors(mex, InterceptorInvoker.__onNewInstanceInvoked)) {
      __log.debug("Not creating a new instance for mex " + mex + "; interceptor prevented!");
      throw new InvalidProcessException(
          "Cannot instantiate process '" + _process.getPID() + "' any more.",
          InvalidProcessException.TOO_MANY_INSTANCES_CAUSE_CODE);
    }

    ProcessInstanceDAO newInstance = processDAO.createInstance(routing.correlator);

    BpelRuntimeContextImpl instance =
        _process.createRuntimeContext(newInstance, new PROCESS(_process.getOProcess()), mex);

    // send process instance event
    NewProcessInstanceEvent evt =
        new NewProcessInstanceEvent(
            new QName(_process.getOProcess().targetNamespace, _process.getOProcess().getName()),
            _process.getProcessDAO().getProcessId(),
            newInstance.getInstanceId());
    evt.setPortType(mex.getPortType().getQName());
    evt.setOperation(operation.getName());
    evt.setMexId(mex.getMessageExchangeId());
    _process._debugger.onEvent(evt);
    _process.saveEvent(evt, newInstance);
    mex.setCorrelationStatus(MyRoleMessageExchange.CorrelationStatus.CREATE_INSTANCE);
    mex.getDAO().setInstance(newInstance);
    if (mex.getDAO().getCreateTime() == null)
      mex.getDAO().setCreateTime(instance.getCurrentEventDateTime());

    _process._engine.acquireInstanceLock(newInstance.getInstanceId());
    instance.execute();
  }
  public void invokeInstance(MyRoleMessageExchangeImpl mex, RoutingInfo routing) {
    Operation operation = getMyRoleOperation(mex.getOperationName());
    if (__log.isDebugEnabled()) {
      __log.debug(
          "INPUTMSG: "
              + routing.correlator.getCorrelatorId()
              + ": ROUTING to existing instance "
              + routing.messageRoute.getTargetInstance().getInstanceId());
    }

    ProcessInstanceDAO instanceDao = routing.messageRoute.getTargetInstance();
    BpelProcess process2 =
        _process._engine._activeProcesses.get(instanceDao.getProcess().getProcessId());

    // Reload process instance for DAO.
    BpelRuntimeContextImpl instance = process2.createRuntimeContext(instanceDao, null, null);
    instance.inputMsgMatch(routing.messageRoute.getGroupId(), routing.messageRoute.getIndex(), mex);

    // Kill the route so some new message does not get routed to
    // same process instance.
    routing.correlator.removeRoutes(routing.messageRoute.getGroupId(), instanceDao);

    // send process instance event
    CorrelationMatchEvent evt =
        new CorrelationMatchEvent(
            new QName(process2.getOProcess().targetNamespace, process2.getOProcess().getName()),
            process2.getProcessDAO().getProcessId(),
            instanceDao.getInstanceId(),
            routing.matchedKeySet);
    evt.setPortType(mex.getPortType().getQName());
    evt.setOperation(operation.getName());
    evt.setMexId(mex.getMessageExchangeId());

    process2._debugger.onEvent(evt);
    // store event
    process2.saveEvent(evt, instanceDao);

    mex.setCorrelationStatus(MyRoleMessageExchange.CorrelationStatus.MATCHED);
    mex.getDAO().setInstance(routing.messageRoute.getTargetInstance());
    if (mex.getDAO().getCreateTime() == null)
      mex.getDAO().setCreateTime(instance.getCurrentEventDateTime());

    instance.execute();
  }
 private int compareInstanceUsingKey(
     String key, ProcessInstanceDAO instanceDAO1, ProcessInstanceDAO instanceDAO2) {
   String s1 = null;
   String s2 = null;
   boolean ascending = true;
   String orderKey = key;
   if (key.startsWith("+") || key.startsWith("-")) {
     orderKey = key.substring(1, key.length());
     if (key.startsWith("-")) ascending = false;
   }
   ProcessDAO process1 = getProcess(instanceDAO1.getProcess().getProcessId());
   ProcessDAO process2 = getProcess(instanceDAO2.getProcess().getProcessId());
   if ("pid".equals(orderKey)) {
     s1 = process1.getProcessId().toString();
     s2 = process2.getProcessId().toString();
   } else if ("name".equals(orderKey)) {
     s1 = process1.getProcessId().getLocalPart();
     s2 = process2.getProcessId().getLocalPart();
   } else if ("namespace".equals(orderKey)) {
     s1 = process1.getProcessId().getNamespaceURI();
     s2 = process2.getProcessId().getNamespaceURI();
   } else if ("version".equals(orderKey)) {
     s1 = "" + process1.getVersion();
     s2 = "" + process2.getVersion();
   } else if ("status".equals(orderKey)) {
     s1 = "" + instanceDAO1.getState();
     s2 = "" + instanceDAO2.getState();
   } else if ("started".equals(orderKey)) {
     s1 = ISO8601DateParser.format(instanceDAO1.getCreateTime());
     s2 = ISO8601DateParser.format(instanceDAO2.getCreateTime());
   } else if ("last-active".equals(orderKey)) {
     s1 = ISO8601DateParser.format(instanceDAO1.getLastActiveTime());
     s2 = ISO8601DateParser.format(instanceDAO2.getLastActiveTime());
   }
   if (ascending) return s1.compareTo(s2);
   else return s2.compareTo(s1);
 }
  @SuppressWarnings("unchecked")
  public Collection<ProcessInstanceDAO> instanceQuery(InstanceFilter filter) {
    if (filter.getLimit() == 0) {
      return Collections.EMPTY_LIST;
    }
    List<ProcessInstanceDAO> matched = new ArrayList<ProcessInstanceDAO>();
    // Selecting
    selectionCompleted:
    for (ProcessDaoImpl proc : _store.values()) {
      boolean pmatch = true;
      if (filter.getNameFilter() != null
          && !equalsOrWildcardMatch(filter.getNameFilter(), proc.getProcessId().getLocalPart()))
        pmatch = false;
      if (filter.getNamespaceFilter() != null
          && !equalsOrWildcardMatch(
              filter.getNamespaceFilter(), proc.getProcessId().getNamespaceURI())) pmatch = false;

      if (pmatch) {
        for (ProcessInstanceDAO inst : proc._instances.values()) {
          boolean match = true;

          if (filter.getStatusFilter() != null) {
            boolean statusMatch = false;
            for (Short status : filter.convertFilterState()) {
              if (inst.getState() == status.byteValue()) statusMatch = true;
            }
            if (!statusMatch) match = false;
          }
          if (filter.getStartedDateFilter() != null
              && !dateMatch(filter.getStartedDateFilter(), inst.getCreateTime(), filter))
            match = false;
          if (filter.getLastActiveDateFilter() != null
              && !dateMatch(filter.getLastActiveDateFilter(), inst.getLastActiveTime(), filter))
            match = false;

          // if (filter.getPropertyValuesFilter() != null) {
          // for (Map.Entry propEntry :
          // filter.getPropertyValuesFilter().entrySet()) {
          // boolean entryMatched = false;
          // for (ProcessPropertyDAO prop : proc.getProperties()) {
          // if (prop.getName().equals(propEntry.getKey())
          // && (propEntry.getValue().equals(prop.getMixedContent())
          // || propEntry.getValue().equals(prop.getSimpleContent())))
          // {
          // entryMatched = true;
          // }
          // }
          // if (!entryMatched) {
          // match = false;
          // }
          // }
          // }

          if (match) {
            matched.add(inst);
            if (matched.size() == filter.getLimit()) {
              break selectionCompleted;
            }
          }
        }
      }
    }
    // And ordering
    if (filter.getOrders() != null) {
      final List<String> orders = filter.getOrders();

      Collections.sort(
          matched,
          new Comparator<ProcessInstanceDAO>() {
            public int compare(ProcessInstanceDAO o1, ProcessInstanceDAO o2) {
              for (String orderKey : orders) {
                int result = compareInstanceUsingKey(orderKey, o1, o2);
                if (result != 0) return result;
              }
              return 0;
            }
          });
    }

    return matched;
  }