Exemplo n.º 1
0
  /**
   * Build a Hibernate {@link Criteria} from an instance filter.
   *
   * @param crit target (destination) criteria
   * @param filter filter
   */
  void buildCriteria(Criteria crit, InstanceFilter filter) {
    Criteria processCrit = crit.createCriteria("process");

    // Filtering on PID
    if (filter.getPidFilter() != null)
      processCrit.add(Restrictions.like("processId", filter.getPidFilter().replaceAll("\\*", "%")));

    if (filter.getIidFilter() != null)
      crit.add(Restrictions.eq("id", new Long(filter.getIidFilter())));

    // Filtering on name and namespace
    if (filter.getNameFilter() != null) {
      processCrit.add(Restrictions.like("typeName", filter.getNameFilter().replaceAll("\\*", "%")));
    }
    if (filter.getNamespaceFilter() != null) {
      processCrit.add(
          Restrictions.like("typeNamespace", filter.getNamespaceFilter().replaceAll("\\*", "%")));
    }

    // Specific filter for status (using a disjunction between possible statuses)
    if (filter.getStatusFilter() != null) {
      List<Short> statuses = filter.convertFilterState();
      Disjunction disj = Restrictions.disjunction();
      for (short status : statuses) {
        disj.add(Restrictions.eq("state", status));
      }
      crit.add(disj);
    }

    // Specific filter for started and last active dates.
    if (filter.getStartedDateFilter() != null) {
      for (String sdf : filter.getStartedDateFilter()) {
        addFilterOnPrefixedDate(crit, sdf, "created");
      }
    }
    if (filter.getLastActiveDateFilter() != null) {
      for (String ladf : filter.getLastActiveDateFilter()) {
        addFilterOnPrefixedDate(crit, ladf, "lastActiveTime");
      }
    }

    // Specific filter for correlation properties
    if (filter.getPropertyValuesFilter() != null) {
      Criteria propCrit = crit.createCriteria("correlationSets").createCriteria("properties");
      for (Map.Entry corValue : filter.getPropertyValuesFilter().entrySet()) {
        String propName = (String) corValue.getKey();
        if (propName.startsWith("{")) {
          String namespace = propName.substring(1, propName.lastIndexOf("}"));
          propName = propName.substring(propName.lastIndexOf("}") + 1, propName.length());
          propCrit
              .add(Restrictions.eq("name", propName))
              .add(Restrictions.eq("namespace", namespace))
              .add(Restrictions.eq("value", corValue.getValue()));
        } else {
          propCrit
              .add(Restrictions.eq("name", corValue.getKey()))
              .add(Restrictions.eq("value", corValue.getValue()));
        }
      }
    }

    // Ordering
    if (filter.orders != null) {
      for (String key : filter.orders) {
        boolean ascending = true;
        String orderKey = key;
        if (key.startsWith("+") || key.startsWith("-")) {
          orderKey = key.substring(1, key.length());
          if (key.startsWith("-")) ascending = false;
        }

        if ("name".equals(orderKey)) {
          if (ascending) processCrit.addOrder(Property.forName("typeName").asc());
          else processCrit.addOrder(Property.forName("typeName").desc());
        } else if ("namespace".equals(orderKey)) {
          if (ascending) processCrit.addOrder(Property.forName("typeNamespace").asc());
          else processCrit.addOrder(Property.forName("typeNamespace").desc());
        } else if ("pid".equals(orderKey)) {
          if (ascending) processCrit.addOrder(Property.forName("processId").asc());
          else processCrit.addOrder(Property.forName("processId").desc());
        } else if ("version".equals(orderKey)) {
          if (ascending) processCrit.addOrder(Property.forName("version").asc());
          else processCrit.addOrder(Property.forName("version").desc());
        } else if ("status".equals(orderKey)) {
          if (ascending) crit.addOrder(Property.forName("state").asc());
          else crit.addOrder(Property.forName("state").desc());
        } else if ("started".equals(orderKey)) {
          if (ascending) crit.addOrder(Property.forName("created").asc());
          else crit.addOrder(Property.forName("created").desc());
        } else if ("last-active".equals(orderKey)) {
          if (ascending) crit.addOrder(Property.forName("lastActiveTime").asc());
          else crit.addOrder(Property.forName("lastActiveTime").desc());
        }
      }
    }

    if (filter.getLimit() > 0) crit.setMaxResults(filter.getLimit());
  }
  @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;
  }