Package siena.core.options

Examples of siena.core.options.QueryOptionState


    QueryOptionState opt = (QueryOptionState)(options.get(QueryOptionState.ID)).activate();
    opt.lifeCycle = QueryOptionState.LifeCycle.STATEFUL;
  }
 
  protected void optionStateless() {
    QueryOptionState opt = (QueryOptionState)(options.get(QueryOptionState.ID)).activate();
    opt.lifeCycle = QueryOptionState.LifeCycle.STATELESS;
  }
View Full Code Here


    if(gaeCtx==null){
      gaeCtx = new QueryOptionGaeContext();
      query.customize(gaeCtx);
    }
   
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
    QueryOptionFetchType fetchType = (QueryOptionFetchType)query.option(QueryOptionFetchType.ID);
    FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();

    QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
    if(!pag.isPaginating()){
      // no pagination but pageOption active
      if(pag.isActive()){
        // if local limit is set, it overrides the pageOption.pageSize
        if(limit!=Integer.MAX_VALUE){
          gaeCtx.realPageSize = limit;
          fetchOptions.limit(gaeCtx.realPageSize);
          // pageOption is passivated to be sure it is not reused
          pag.passivate();
        }
        // using pageOption.pageSize
        else {
          gaeCtx.realPageSize = pag.pageSize;
          fetchOptions.limit(gaeCtx.realPageSize);
          // passivates the pageOption in stateless mode not to keep anything between 2 requests
          if(state.isStateless()){
            pag.passivate();
          }           
        }
      }
      else {
        if(limit != Integer.MAX_VALUE){
          gaeCtx.realPageSize = limit;
          fetchOptions.limit(gaeCtx.realPageSize);
        }
      }
    }else {
      // paginating so use the pagesize and don't passivate pageOption
      // local limit is not taken into account
      gaeCtx.realPageSize = pag.pageSize;
      fetchOptions.limit(gaeCtx.realPageSize);
    }

    QueryOptionOffset off = (QueryOptionOffset)query.option(QueryOptionOffset.ID);
    // if local offset has been set, uses it
    if(offset!=0){
      off.activate();
      off.offset = offset;
    }
           
    // if previousPage has detected there is no more data, simply returns an empty list
    if(gaeCtx.noMoreDataBefore){
      return new ArrayList<T>();
    }
           
    if(state.isStateless()) {
      if(pag.isPaginating()){
        if(off.isActive()){
          gaeCtx.realOffset+=off.offset;
          fetchOptions.offset(gaeCtx.realOffset);
          off.passivate();
View Full Code Here

  }
 
 
  private <T> Iterable<T> doFetchIterable(Query<T> query, int limit, int offset) {
    QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext)query.option(QueryOptionGaeContext.ID);
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
    QueryOptionFetchType fetchType = (QueryOptionFetchType)query.option(QueryOptionFetchType.ID);
       
    if(gaeCtx==null){
      gaeCtx = new QueryOptionGaeContext();
      query.customize(gaeCtx);
    }

    FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();

    QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
    if(!pag.isPaginating()){
      // no pagination but pageOption active
      if(pag.isActive()){
        // if local limit is set, it overrides the pageOption.pageSize
        if(limit!=Integer.MAX_VALUE){
          gaeCtx.realPageSize = limit;
          fetchOptions.limit(gaeCtx.realPageSize);
          // pageOption is passivated to be sure it is not reused
          pag.passivate();
        }
        // using pageOption.pageSize
        else {
          gaeCtx.realPageSize = pag.pageSize;
          fetchOptions.limit(gaeCtx.realPageSize);
          // passivates the pageOption in stateless mode not to keep anything between 2 requests
          if(state.isStateless()){
            pag.passivate();
          }           
        }
      }
      else {
        if(limit != Integer.MAX_VALUE){
          gaeCtx.realPageSize = limit;
          fetchOptions.limit(gaeCtx.realPageSize);
        }
      }
    }else {
      // paginating so use the pagesize and don't passivate pageOption
      // local limit is not taken into account
      gaeCtx.realPageSize = pag.pageSize;
      fetchOptions.limit(gaeCtx.realPageSize);
    }

    QueryOptionOffset off = (QueryOptionOffset)query.option(QueryOptionOffset.ID);
    // if local offset has been set, uses it
    if(offset!=0){
      off.activate();
      off.offset = offset;
    }
   
    // if previousPage has detected there is no more data, simply returns an empty list
    if(gaeCtx.noMoreDataBefore){
      return new ArrayList<T>();
    }
           
    if(state.isStateless()) {
      if(pag.isPaginating()){     
        if(off.isActive()){
          gaeCtx.realOffset+=off.offset;
          fetchOptions.offset(gaeCtx.realOffset);
          off.passivate();
View Full Code Here

    return fieldMap;
  }

  public static <T> void paginate(QueryData<T> query) {
    QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext)query.option(QueryOptionGaeContext.ID);
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
    if(gaeCtx==null){
      gaeCtx = new QueryOptionGaeContext();
      query.options().put(gaeCtx.type, gaeCtx);
    }
   
    // resets the realoffset to 0 if stateless
    if(state.isStateless()){
      gaeCtx.realOffset = 0;
    }
  }
View Full Code Here

    }
  }
 
  public static <T> void nextPage(QueryData<T> query) {
    QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
    QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext)query.option(QueryOptionGaeContext.ID);
    if(gaeCtx==null){
      gaeCtx = new QueryOptionGaeContext();
      query.options().put(gaeCtx.type, gaeCtx);
    }
   
    // if no more data after, doesn't try to go after
    if(gaeCtx.noMoreDataAfter){
      return;
    }
   
    // if no more data before, removes flag to be able and stay there
    if(gaeCtx.noMoreDataBefore){
      gaeCtx.noMoreDataBefore = false;
      return;
    }
   
    if(pag.isPaginating()){
      gaeCtx.realPageSize = pag.pageSize;
      if(state.isStateless()) {
        //QueryOptionOffset offset = (QueryOptionOffset)query.option(QueryOptionOffset.ID);
        //if(offset.isActive()){
        gaeCtx.realOffset+=pag.pageSize;
        //}
      }     
View Full Code Here

    }
  }

  public static <T> void previousPage(QueryData<T> query) {
    QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
    QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext)query.option(QueryOptionGaeContext.ID);
    if(gaeCtx==null){
      gaeCtx = new QueryOptionGaeContext();
      query.options().put(gaeCtx.type, gaeCtx);
    }
   
    // if no more data before, doesn't try to go before
    if(gaeCtx.noMoreDataBefore){
      return;
    }
   
    // if no more data after, removes flag to be able to go before
    if(gaeCtx.noMoreDataAfter){
      gaeCtx.noMoreDataAfter = false;
    }
   
    if(pag.isPaginating()){
      gaeCtx.realPageSize = pag.pageSize;
      if(state.isStateless()) {
        //QueryOptionOffset offset = (QueryOptionOffset)query.option(QueryOptionOffset.ID);
        //if(offset.isActive()){
          if(gaeCtx.realOffset>=pag.pageSize) {
            gaeCtx.realOffset-=pag.pageSize;
          }
View Full Code Here

    // if local offset has been set, uses it
    if(offset!=0){
      offsetOpt.activate();
      offsetOpt.offset = offset;
    }
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
   
    // if previousPage has detected there is no more data, simply returns an empty list
    if(jdbcCtx.noMoreDataBefore){
      return new ArrayList<T>();
    }
       
    if(state.isStateless()
        || (state.isStateful() && !jdbcCtx.isActive())
        || (state.isStateful() && jdbcCtx.isActive() && jdbcCtx.isClosed())) {
      if(state.isStateless()){
        if(pag.isPaginating()){
          if(offsetOpt.isActive()){
            jdbcCtx.realOffset+=offsetOpt.offset;
            offsetOpt.passivate();
          }else {
            // keeps realOffset
          }
        }else {
          // if page is active, immediately passivates it not to keep is active
          if(pag.isActive()) {
            pag.passivate();
          }
          if(offsetOpt.isActive()){
            jdbcCtx.realOffset=offsetOpt.offset;
            offsetOpt.passivate();
          }else{
            jdbcCtx.realOffset = 0;
          }
        }
      } else {
        if(offsetOpt.isActive()){
          jdbcCtx.realOffset+=offsetOpt.offset;
          offsetOpt.passivate();
        }else {
          // keeps realOffset
        }
      }
      Class<T> clazz = query.getQueriedClass();
      List<Object> parameters = new ArrayList<Object>();
      StringBuilder sql = JdbcDBUtils.buildSqlSelect(query);
      appendSqlWhere(query, sql, parameters);
      JdbcDBUtils.appendSqlOrder(query, sql);
      JdbcDBUtils.appendSqlLimitOffset(query, sql, parameters);
      //sql.append(suffix);
      PreparedStatement statement = null;
      ResultSet rs = null;
      try {
        statement = createStatement(sql.toString(), parameters);
        if(pag.isPaginating()) {
          // this is just a hint to the DB so wonder if it should be used
          statement.setFetchSize(jdbcCtx.realPageSize);
        }
        rs = statement.executeQuery();
        List<T> result = JdbcMappingUtils.mapList(clazz, rs, ClassInfo.getClassInfo(clazz).tableName,
            JdbcMappingUtils.getJoinFields(query), jdbcCtx.realPageSize);
       
        if(pag.isPaginating()){
          if(result.size() == 0){
            jdbcCtx.noMoreDataAfter = true;
          }
          else {
            jdbcCtx.noMoreDataAfter = false;
          }
        }else {
          if(state.isStateful()){
            jdbcCtx.realOffset += result.size();
          }
        }
       
        if(state.isStateless()){
          JdbcDBUtils.closeResultSet(rs);
          JdbcDBUtils.closeStatementAndConnection(this, statement);
        }else {
          Integer offsetParamIdx = parameters.size();
          Integer limitParamIdx = offsetParamIdx - 1;
View Full Code Here

    // if local offset has been set, uses it
    if(offset!=0){
      offsetOpt.activate();
      offsetOpt.offset = offset;
    }
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
   
   
    // if previousPage has detected there is no more data, simply returns an empty list
    if(jdbcCtx.noMoreDataBefore){
      return new ArrayList<T>();
    }
       
    if(state.isStateless() || (state.isStateful() && !jdbcCtx.isActive())) {
      if(state.isStateless()){
        if(pag.isPaginating()){
          if(offsetOpt.isActive()){
            jdbcCtx.realOffset+=offsetOpt.offset;
            offsetOpt.passivate();
          }else {
            // keeps realOffset
          }
        }else {
          // if page is active, immediately passivates it not to keep is active
          if(pag.isActive()) {
            pag.passivate();
          }
          if(offsetOpt.isActive()){
            jdbcCtx.realOffset=offsetOpt.offset;
            offsetOpt.passivate();
          }else{
            jdbcCtx.realOffset = 0;
          }
        }
      } else {
        if(offsetOpt.isActive()){
          jdbcCtx.realOffset+=offsetOpt.offset;
          offsetOpt.passivate();
        }else {
          // keeps realOffset
        }
      }
     
      Class<T> clazz = query.getQueriedClass();
      List<Object> parameters = new ArrayList<Object>();
      StringBuilder sql = JdbcDBUtils.buildSqlSelect(query);
      appendSqlWhere(query, sql, parameters);
      JdbcDBUtils.appendSqlOrder(query, sql);
      JdbcDBUtils.appendSqlLimitOffset(query, sql, parameters);
      //sql.append(suffix);
      PreparedStatement statement = null;
      ResultSet rs = null;
      try {
        statement = createStatement(sql.toString(), parameters);
        if(pag.isActive()) {
          // this is just a hint to the DB so wonder if it should be used
          statement.setFetchSize(jdbcCtx.realPageSize);
        }
        rs = statement.executeQuery();
        List<T> result = JdbcMappingUtils.mapListKeys(clazz, rs, ClassInfo.getClassInfo(clazz).tableName,
            JdbcMappingUtils.getJoinFields(query), jdbcCtx.realPageSize);
       
        if(pag.isPaginating()){
          if(result.size() == 0){
            jdbcCtx.noMoreDataAfter = true;
          }
          else {
            jdbcCtx.noMoreDataAfter = false;
          }
        }else {
          if(state.isStateful()){
            jdbcCtx.realOffset += result.size();
          }
        }
       
        if(state.isStateless()){
          JdbcDBUtils.closeResultSet(rs);
          JdbcDBUtils.closeStatementAndConnection(this, statement);
        }else {
          Integer offsetParamIdx = parameters.size();
          Integer limitParamIdx = offsetParamIdx - 1;
View Full Code Here

    // if local offset has been set, uses it
    if(offset!=0){
      offsetOpt.activate();
      offsetOpt.offset = offset;
    }
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);

    // if previousPage has detected there is no more data, simply returns an empty list
    if(jdbcCtx.noMoreDataBefore){
      return new ArrayList<T>();
    }
   
    // forces the reusable option since iteration requires it!!!
    //query.stateful();   
   
    if(state.isStateless() || (state.isStateful() && !jdbcCtx.isActive())) {
      if(state.isStateless()){
        if(pag.isPaginating()){
          if(offsetOpt.isActive()){
            jdbcCtx.realOffset+=offsetOpt.offset;
            offsetOpt.passivate();
          }else {
            // keeps realOffset
          }
        }else {
          // if page is active, immediately passivates it not to keep is active
          if(pag.isActive()) {
            pag.passivate();
          }
          if(offsetOpt.isActive()){
            jdbcCtx.realOffset=offsetOpt.offset;
            offsetOpt.passivate();
          }else{
            jdbcCtx.realOffset = 0;
          }
        }
      } else {
        if(offsetOpt.isActive()){
          jdbcCtx.realOffset+=offsetOpt.offset;
          offsetOpt.passivate();
        }else {
          // keeps realOffset
        }
      }
     
      List<Object> parameters = new ArrayList<Object>();
      StringBuilder sql = JdbcDBUtils.buildSqlSelect(query);
      appendSqlWhere(query, sql, parameters);
      JdbcDBUtils.appendSqlOrder(query, sql);
      JdbcDBUtils.appendSqlLimitOffset(query, sql, parameters);
      //sql.append(suffix);
      PreparedStatement statement = null;
      ResultSet rs = null;
      try {
        statement = createStatement(sql.toString(), parameters);
        if(pag.isActive()) {
          // this is just a hint to the DB so wonder if it should be used
          statement.setFetchSize(jdbcCtx.realPageSize);
        }
        rs = statement.executeQuery();
       
        if(state.isStateless()){
          //in iteration, doesn't close the resultset to reuse it
          //JdbcDBUtils.closeResultSet(rs);
          //JdbcDBUtils.closeStatement(statement);
        }else {
          Integer offsetParamIdx = parameters.size();
View Full Code Here

  }

  @Override
  public <T> void paginate(Query<T> query) {
    QueryOptionJdbcContext jdbcCtx = (QueryOptionJdbcContext)query.option(QueryOptionJdbcContext.ID);
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
    if(jdbcCtx==null){
      jdbcCtx = new QueryOptionJdbcContext();
      query.customize(jdbcCtx);
    }
   
    // resets the realoffset to 0 if stateless
    if(state.isStateless()){
      jdbcCtx.realOffset = 0;
    }
  }
View Full Code Here

TOP

Related Classes of siena.core.options.QueryOptionState

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.