/**
   * Perform operation for the given request using business class(ejb) and return response
   *
   * @throws I2B2Exception
   * @see edu.harvard.i2b2.crc.delegate.RequestHandler#execute()
   */
  public BodyType execute() throws I2B2Exception {
    QueryProcessorUtil qpUtil = QueryProcessorUtil.getInstance();
    String response = null;
    BodyType bodyType = new BodyType();
    MasterInstanceResultResponseType masterInstanceResponse = null;
    try {
      StartAnalysisLocal startAnalysisLocal = qpUtil.getStartAnalysisLocal();

      DAOFactoryHelper daoFactoryHelper =
          new DAOFactoryHelper(
              this.getDataSourceLookup().getDomainId(),
              getDataSourceLookup().getProjectPath(),
              getDataSourceLookup().getOwnerId());
      IDAOFactory daoFactory = daoFactoryHelper.getDAOFactory();

      // response = queryManagerLocal.processQuery(requestXml);
      masterInstanceResponse = startAnalysisLocal.start(daoFactory, requestXml);
      // processQuery(this.getDataSourceLookup(), requestXml);
      // masterInstanceResponse.setStatus(this.buildCRCStausType(
      // RequestHandlerDelegate.DONE_TYPE, "DONE"));

      // response = this.buildResponseMessage(requestXml, bodyType);
    } catch (I2B2Exception e) {
      masterInstanceResponse = new MasterInstanceResultResponseType();
      masterInstanceResponse.setStatus(
          this.buildCRCStausType(RequestHandlerDelegate.ERROR_TYPE, e.getMessage()));

    } finally {
      edu.harvard.i2b2.crc.datavo.setfinder.query.ObjectFactory psmObjFactory =
          new edu.harvard.i2b2.crc.datavo.setfinder.query.ObjectFactory();
      bodyType.getAny().add(psmObjFactory.createResponse(masterInstanceResponse));
    }

    return bodyType;
  }
  private boolean checkDataObscRole(DataSourceLookup originalDataSourceLookup, List<String> roles)
      throws I2B2Exception {
    boolean noDataAggFlag = false, noDataObfscFlag = false;

    String domainId = originalDataSourceLookup.getDomainId();
    String projectId = originalDataSourceLookup.getProjectPath();
    String userId = originalDataSourceLookup.getOwnerId();

    DAOFactoryHelper helper = new DAOFactoryHelper(domainId, projectId, userId);

    IDAOFactory daoFactory = helper.getDAOFactory();
    AuthrizationHelper authHelper = new AuthrizationHelper(domainId, projectId, userId, daoFactory);

    try {
      authHelper.checkRoleForProtectionLabel("SETFINDER_QRY_WITHOUT_DATAOBFSC", roles);
    } catch (MissingRoleException noRoleEx) {
      noDataAggFlag = true;
    } catch (I2B2Exception e) {
      throw e;
    }
    try {
      authHelper.checkRoleForProtectionLabel("SETFINDER_QRY_WITH_DATAOBFSC", roles);
    } catch (MissingRoleException noRoleEx) {
      noDataObfscFlag = true;
    } catch (I2B2Exception e) {
      throw e;
    }
    if (noDataAggFlag && !noDataObfscFlag) {
      return true;
    } else {
      return false;
    }
  }
  public long getTotal(int maxInputList) throws SQLException, I2B2Exception {

    IInputOptionListHandler inputOptionListHandler =
        PDOFactory.buildInputListHandler(inputList, dataSourceLookup);
    // inputList.getPatientList().setMax(maxInputList);
    int minIndex = inputOptionListHandler.getMinIndex();
    inputOptionListHandler.setMaxIndex(minIndex + maxInputList);

    // iterate the panel and call total
    IFactRelatedQueryHandler factRelatedHandler = null;
    String countSqlFrom = " ";
    String countClause = " COUNT(*) ";
    if (dataSourceLookup.getServerType().equalsIgnoreCase(DAOFactoryHelper.ORACLE)
        || dataSourceLookup.getServerType().equalsIgnoreCase(DAOFactoryHelper.POSTGRESQL)) {
      factRelatedHandler =
          new FactRelatedQueryHandler(dataSourceLookup, inputList, filterList, outputOptionList);
    } else if (dataSourceLookup.getServerType().equalsIgnoreCase(DAOFactoryHelper.SQLSERVER)) {
      countSqlFrom = "as";
      countClause = " COUNT_BIG(*) ";
      factRelatedHandler =
          new SQLServerFactRelatedQueryHandler(
              dataSourceLookup, inputList, filterList, outputOptionList);
    }
    factRelatedHandler.setProjectParamMap(this.projectParamMap);
    factRelatedHandler.setModifierMetadataXmlMap(this.modifierMetadataXmlMap);

    FactOutputOptionType factOutputOptionType = new FactOutputOptionType();
    factOutputOptionType.setOnlykeys(true);
    outputOptionList.setObservationSet(factOutputOptionType);
    // outputOptionList.getObservationSet().setOnlykeys(true);

    // build sql for all the panel
    // DAOFactoryHelper daoFactoryHelper = new DAOFactoryHelper(
    // dataSourceLookup);

    IPageDao pageTotalDao =
        daoFactoryHelper.getDAOFactory().getPatientDataDAOFactory().getPageDAO();
    int panelCount = filterList.getPanel().size();
    List<String> panelSqlList = new ArrayList<String>(panelCount);
    List<Integer> sqlCountList = new ArrayList<Integer>(panelCount);
    long totalObservations = 0;
    for (PanelType singlePanel : filterList.getPanel()) {
      int sqlParamCount = singlePanel.getItem().size();
      if (singlePanel.getInvert() == 1) {
        sqlParamCount++;
      }

      sqlCountList.add(sqlParamCount);
      String totalSql = pageTotalDao.buildTotalSql(factRelatedHandler, singlePanel);
      if (totalSql.trim().length() == 0) {
        continue;
      }
      panelSqlList.add(
          "SELECT " + countClause + " from ( " + totalSql + " ) " + countSqlFrom + " totalsql");
    }

    totalObservations =
        pageTotalDao.getTotalForAllPanel(panelSqlList, sqlCountList, inputOptionListHandler);
    return totalObservations;
  }
 public PagingHandler(
     DAOFactoryHelper helper,
     final InputOptionListType inputList,
     final FilterListType filterList,
     String pageMethodName)
     throws I2B2DAOException {
   this.daoFactoryHelper = helper;
   this.dataSourceLookup = helper.getDataSourceLookup();
   this.inputList = inputList;
   this.filterList = filterList;
   this.outputOptionList = new OutputOptionListType();
   pageMethod = PageMethodFactory.buildPageMethod(pageMethodName);
 }
  public HashMap getMinPatientIndexAndTheTotal(int maxInputList)
      throws SQLException, I2B2Exception {

    IInputOptionListHandler inputOptionListHandler =
        PDOFactory.buildInputListHandler(inputList, dataSourceLookup);

    // inputList.getPatientList().setMax(maxInputList);
    inputOptionListHandler.setMaxIndex(maxInputList);

    // iterate the panel and call total
    IFactRelatedQueryHandler factRelatedHandler = null;
    String countSqlFrom = " ";
    if (dataSourceLookup.getServerType().equalsIgnoreCase(DAOFactoryHelper.ORACLE)
        || dataSourceLookup.getServerType().equalsIgnoreCase(DAOFactoryHelper.POSTGRESQL)) {
      factRelatedHandler =
          new FactRelatedQueryHandler(dataSourceLookup, inputList, filterList, outputOptionList);
    } else if (dataSourceLookup.getServerType().equalsIgnoreCase(DAOFactoryHelper.SQLSERVER)) {
      countSqlFrom = "as";
      factRelatedHandler =
          new SQLServerFactRelatedQueryHandler(
              dataSourceLookup, inputList, filterList, outputOptionList);
    }
    // set project param
    factRelatedHandler.setProjectParamMap(this.projectParamMap);
    factRelatedHandler.setModifierMetadataXmlMap(this.modifierMetadataXmlMap);

    FactOutputOptionType factOutputOptionType = new FactOutputOptionType();
    factOutputOptionType.setOnlykeys(true);
    outputOptionList.setObservationSet(factOutputOptionType);
    // outputOptionList.getObservationSet().setOnlykeys(true);

    // build sql for all the panel
    // DAOFactoryHelper daoFactoryHelper = new DAOFactoryHelper(
    // dataSourceLookup);

    IPageDao pageTotalDao =
        daoFactoryHelper.getDAOFactory().getPatientDataDAOFactory().getPageDAO();
    int panelCount = filterList.getPanel().size();
    List<String> panelMinSqlList = new ArrayList<String>(panelCount);
    List<Integer> sqlCountList = new ArrayList<Integer>(panelCount);
    long totalObservations = 0;
    // things to consider to find the minimum
    // a)How to handle the different input list (i.e. enumeration list,table
    // or patient_dimension)

    String panelSql = "";
    for (PanelType singlePanel : filterList.getPanel()) {
      int sqlParamCount = singlePanel.getItem().size();
      if (singlePanel.getInvert() == 1) {
        sqlParamCount++;
      }
      if (inputOptionListHandler.isCollectionId()) {
        sqlParamCount++;
      }

      sqlCountList.add(sqlParamCount);

      panelSql = pageTotalDao.buildTotalSql(factRelatedHandler, singlePanel);
      if (panelSql.length() == 0) {
        continue;
      }
      String minSql = inputOptionListHandler.generateMinIndexSql(panelSql);
      System.out.println("min sql for panel " + minSql);
      panelMinSqlList.add(minSql);
    }

    return pageTotalDao.getMinIndexAndCountAllPanel(
        panelMinSqlList, sqlCountList, inputOptionListHandler);
  }