private CompositeMap sqlQuery(String sql) throws Exception {
   ResultSet resultSet = null;
   SqlServiceContext sql_context = null;
   CompositeMap result = new CompositeMap("result");
   try {
     Connection conn = dataSource.getConnection();
     sql_context = SqlServiceContext.createSqlServiceContext(conn);
     ParsedSql stmt = createStatement(sql);
     SqlRunner runner = new SqlRunner(sql_context, stmt);
     resultSet = runner.query(null);
     ResultSetLoader mRsLoader = new ResultSetLoader();
     mRsLoader.setFieldNameCase(Character.LOWERCASE_LETTER);
     FetchDescriptor desc = FetchDescriptor.fetchAll();
     CompositeMapCreator compositeCreator = new CompositeMapCreator(result);
     mRsLoader.loadByResultSet(resultSet, desc, compositeCreator);
   } finally {
     DBUtil.closeResultSet(resultSet);
     if (sql_context != null) sql_context.freeConnection();
   }
   return result;
 }
  @Override
  public CompositeMap getCustomizationData(
      BusinessModel model, String function_code, CompositeMap context) {
    if (custDimensionsRecords == null || custDimensionsRecords.getChilds() == null) return null;
    String bm_name = model.getName();
    SqlServiceContext ssc = null;
    ResultSet rs_exists = null;
    ResultSet rs_details = null;
    CompositeMap result = new CompositeMap("result");
    try {
      ssc = databaseServiceFactory.createContextWithConnection();
      String exits_sql =
          "select 1   from dual  where exists  (select 1 from sys_bm_config_customization t "
              + " where t.bm_code='"
              + bm_name
              + "' and t.function_code = '"
              + function_code
              + "' and t.enable_flag='Y')";
      ParsedSql exits_stmt = createStatement(exits_sql);
      SqlRunner exits_runner = new SqlRunner(ssc, exits_stmt);
      rs_exists = exits_runner.query(null);
      if (!rs_exists.next()) {
        return null;
      }

      String bm_tag = model.getTag();
      if (bm_tag != null) {
        executeDimensionProc(bm_tag, custDimensionsRecords, context);
      }
      String dimenson_sql_template =
          "select d.order_num,record_id,head_id,function_id,function_code,bm_code,dimension_type,dimension_value,"
              + " bm_select_value,bm_data_source,bm_where_clause,bm_order_by,bm_query_condition"
              + " from sys_bm_config_customization t,sys_bm_config_dimension d "
              + " where t.dimension_type=d.dimension_code and t.enable_flag = 'Y'"
              + " and t.bm_code='"
              + bm_name
              + "' and t.function_code = '"
              + function_code
              + "' and t.dimension_type=";
      StringBuffer sb = new StringBuffer("");
      boolean firstRecord = true;
      for (Iterator it = custDimensionsRecords.getChildIterator(); it.hasNext(); ) {
        CompositeMap record = (CompositeMap) it.next();
        String dimensionCode = record.getString("dimension_code");
        String data_query_sql = record.getString("data_query_sql");
        String dimension_tag = record.getString("dimension_tag");
        if (dimension_tag != null) {
          if (bm_tag == null) continue;
          boolean is_tag_match = bm_tag.contains(dimension_tag);
          if (!is_tag_match) continue;
        }
        if (firstRecord) firstRecord = false;
        else sb.append(" union all ");
        sb.append(dimenson_sql_template).append("'").append(dimensionCode).append("'");
        if (data_query_sql != null) sb.append(" and ").append(data_query_sql);
      }
      if (!firstRecord) sb.append(" order by 1 ");
      if ("".equals(sb.toString())) return null;
      String custDetailRecordsSql = sb.toString();
      LoggingContext.getLogger(context, this.getClass().getCanonicalName())
          .config("custDetailRecordsSql:" + custDetailRecordsSql);
      ParsedSql stmt = createStatement(custDetailRecordsSql);
      SqlRunner runner = new SqlRunner(ssc, stmt);
      rs_details = runner.query(context);
      ResultSetLoader mRsLoader = new ResultSetLoader();
      mRsLoader.setFieldNameCase(Character.LOWERCASE_LETTER);
      FetchDescriptor desc = FetchDescriptor.fetchAll();
      CompositeMapCreator compositeCreator = new CompositeMapCreator(result);
      mRsLoader.loadByResultSet(rs_details, desc, compositeCreator);
      if (result.getChilds() == null) return null;
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      DBUtil.closeResultSet(rs_exists);
      DBUtil.closeResultSet(rs_details);
      if (ssc != null)
        try {
          ssc.freeConnection();
        } catch (SQLException e) {
          throw new RuntimeException(e);
        }
    }
    context.putObject(DEFAULT_CUSTOM_DATA, result);
    return result;
  }
Пример #3
0
  public void query(CompositeMap context_map) throws Exception {
    super.doPopulate();
    ILogger logger =
        LoggingContext.getLogger(context_map, DatabaseConstant.AURORA_DATABASE_LOGGING_TOPIC);
    SqlServiceContext context =
        (SqlServiceContext) DynamicObject.cast(context_map, SqlServiceContext.class);

    String cache_key = null;
    boolean is_cache = false;
    ICache cache_for_data = null;
    if (cacheKey != null) {
      is_cache = true;
      cache_for_data = CacheUtil.getNamedCache(mObjectReg, KEY_DATA_CACHE_NAME);
      cache_key = TextParser.parse(cacheKey, context_map);
      CompositeMap data = (CompositeMap) cache_for_data.getValue(cache_key);
      if (data != null) {
        logger.config("Cached data found with key " + cache_key);
        // write cache to context
        CompositeMap root_node =
            getMapFromRootPath(
                context.getModel(), TextParser.parse(this.rootPath, context.getObjectContext()));
        root_node.copy(data);
        return;
      } else {
        logger.config(
            "Cached data not found with key " + cache_key + ", doing normal database query");
      }
    }

    prepare(context_map);

    // context.setTrace(trace);
    ServiceOption option = ServiceOption.createInstance();
    option.setQueryMode(mode);
    option.setAutoCount(autoCount);
    option.setConnectionName(connectionName);
    option.setFieldCase(fieldNameCaseValue);
    option.setRecordName(recordName);
    transferServiceOption(option, ServiceOption.KEY_DEFAULT_WHERE_CLAUSE);
    transferServiceOption(option, ServiceOption.KEY_QUERY_ORDER_BY);
    context.setServiceOption(option);

    IResultSetConsumer consumer = null;
    //        CompositeMapCreator compositeCreator = null;
    try {
      // get parameter
      CompositeMap param = context.getCurrentParameter();
      if (parameter != null) {
        Object obj = param.getObject(parameter);
        if (obj != null) {
          if (!(obj instanceof CompositeMap))
            throw new IllegalArgumentException(
                "query parameter should be instance of CompositeMap, but actually got "
                    + obj.getClass().getName());
          param = (CompositeMap) obj;
        }
      }

      // page settings
      FetchDescriptor desc = FetchDescriptor.createFromParameter(context.getParameter());
      desc.setFetchAll(fetchAll);
      if (pageSize != null) desc.setPageSize(pageSize.intValue());

      // ResultSet consumer
      if (this.rsConsummer != null) consumer = this.rsConsummer;
      else {
        consumer = (IResultSetConsumer) context.getInstanceOfType(IResultSetConsumer.class);
        if (consumer == null) {
          consumer = new CompositeMapCreator();
          mOCManager.populateObject(mEntryConfig, consumer);
        }
      }
      if (consumer instanceof IContextAcceptable)
        ((IContextAcceptable) consumer).setContext(context_map);
      // set root path
      if (consumer instanceof IRootMapAcceptable) {
        CompositeMap result =
            getMapFromRootPath(
                context.getModel(), TextParser.parse(this.rootPath, context.getObjectContext()));
        if (result != null) ((IRootMapAcceptable) consumer).setRoot(result);
      }
      context.setResultsetConsumer(consumer);
      doQuery(param, consumer, desc);

      // write data to cache
      if (is_cache) {
        CompositeMap d = (CompositeMap) consumer.getResult();
        CompositeMap copied_data = (CompositeMap) d.clone();
        copied_data.setParent(null);
        cache_for_data.setValue(cache_key, copied_data);
      }
      /*
      if( transform_list != null && consumer instanceof  IRootMapAcceptable){
          CompositeMap root = ((IRootMapAcceptable)consumer).getRoot();
          Transformer.doBatchTransform( root, transform_list );
      }
      */
    } finally {
      context.setServiceOption(null);
      context.setResultsetConsumer(null);
      cleanUp(context_map);
    }
  }