public void constructEngineFlow(Integer siteId, RuleType rt) {
    String flowName = siteId + rt.getPrefixUrl();
    Collection flowRules = null;
    if (ConsequenceType.NOTIFIER.equals(rt.getConsequenceType())) {
      flowRules = new ArrayList();
      List ruleList = ruleManager.getEnabledRuleListForRuleType(siteId, rt);
      int i = 2;
      for (Iterator itorRule = ruleList.iterator(); itorRule.hasNext(); i++) {
        Rule r = (Rule) itorRule.next();
        FlowRule fr = new FlowRule();
        fr.setRule(r);
        if (itorRule.hasNext()) {
          fr.setNextSeqWhenFail(i);
          fr.setNextSeqWhenPass(i);
        } else {
          fr.setNextSeqWhenFail(0);
          fr.setNextSeqWhenPass(0);
        }
        flowRules.add(fr);
      }
    } else {
      Flow f = getSiteEnabledFlow(siteId, rt);
      if (f != null) {
        flowRules = dao.getRulesForFlow(f.getId());
      }
    }

    if (flowRules == null || flowRules.isEmpty()) {
      workspace.removeFlow(flowName);
      return;
    }
    EngineFlow ef = workspace.getFlow(flowName);
    if (ef == null) {
      ef = workspace.createFlow(flowName);
    }
    ef.lockForUpdate();
    try {
      ef.clearRules();
      for (Iterator itor = flowRules.iterator(); itor.hasNext(); ) {
        ef.addRule(createEngineRuleForFlowRule((FlowRule) itor.next()));
      }
    } finally {
      ef.releaseLock();
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see com.aof.service.business.rule.FlowManager#saveFlow(com.aof.model.business.rule.Flow,
   *      java.util.Set)
   */
  public Flow[] saveFlow(Flow f, Set flowRules) {
    Flow[] result = null;
    boolean needUpdateWorkspace = false;

    Flow f2 = getSiteEnabledFlow(f.getSite().getId(), f.getType());
    if (f.equals(f2)) {
      f2.setDescription(f.getDescription());
      f2.setEnabled(f.getEnabled());
      f = f2;
      needUpdateWorkspace = true;
    } else {
      if (EnabledDisabled.ENABLED.equals(f.getEnabled())) {
        if (f2 != null) {
          result = new Flow[2];
          f2.setEnabled(EnabledDisabled.DISABLED);
          result[1] = dao.saveFlow(f2);
        }
        needUpdateWorkspace = true;
      }
    }

    if (result == null) result = new Flow[1];
    result[0] = dao.saveFlow(f);
    if (flowRules != null) {
      for (Iterator itor = dao.getRulesForFlow(f.getId()).iterator(); itor.hasNext(); ) {
        dao.removeFlowRule((FlowRule) itor.next());
      }
      for (Iterator itor = flowRules.iterator(); itor.hasNext(); ) {
        dao.saveFlowRule((FlowRule) itor.next());
      }
    }
    if (needUpdateWorkspace) {
      constructEngineFlow(f.getSite().getId(), f.getType());
    }
    return result;
  }