Package siena.core.options

Examples of siena.core.options.QueryOptionPage


   
    return q;
  }
 
  public static <T> void nextPage(QueryData<T> query) {
    QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext)query.option(QueryOptionSdbContext.ID);
    QueryOptionOffset off = (QueryOptionOffset)query.option(QueryOptionOffset.ID);

    if(sdbCtx==null){
      sdbCtx = new QueryOptionSdbContext();
      query.options().put(sdbCtx.type, sdbCtx);
    }
   
    // if no more data after, doesn't try to go after
    if(sdbCtx.noMoreDataAfter){
      return;
    }
   
    // if no more data before, removes flag to be able and stay there
    if(sdbCtx.noMoreDataBefore){
      sdbCtx.noMoreDataBefore = false;
      return;
    }
   
    if(pag.isPaginating()){
      if(sdbCtx.hasToken()){
        if(sdbCtx.nextToken() == null) {
          // in this case, doesn't advance to the next page
          // and stays at the offset of the beginning of the
          // last page
View Full Code Here


      throw new SienaException("Can't use nextPage after pagination has been interrupted...");
    }
  }

  public static <T> void previousPage(QueryData<T> query) {
    QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext)query.option(QueryOptionSdbContext.ID);
    if(sdbCtx==null){
      sdbCtx = new QueryOptionSdbContext();
      query.options().put(sdbCtx.type, sdbCtx);
    }
   
    // if no more data before, doesn't try to go before
    if(sdbCtx.noMoreDataBefore){
      return;
    }
   
    // if no more data after, removes flag to be able to go before
    if(sdbCtx.noMoreDataAfter){
      // here the realoffset is not at the end of current pages
      // but at the beginning of the last page
      // so need to fake that we are at the end of the last page
      sdbCtx.realOffset += pag.pageSize;
     
      sdbCtx.noMoreDataAfter = false;
    }
   
    if(pag.isPaginating()){
      if(sdbCtx.hasToken()) {
        // if tokenIdx is 0, it means at first page after beginning
        if(sdbCtx.tokenIdx == 0){
          sdbCtx.previousToken();
          // follows the real offset
View Full Code Here

   
    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 SienaFutureMock<List<T>>(new ArrayList<T>());
    }
   
    if(state.isStateless()) {
      if(pag.isPaginating()){
        if(off.isActive()){
          gaeCtx.realOffset+=off.offset;
          fetchOptions.offset(gaeCtx.realOffset);
          off.passivate();
        }else {
          fetchOptions.offset(gaeCtx.realOffset);
        }
      }else {
        // if stateless and not paginating, resets the realoffset to 0
        gaeCtx.realOffset = 0;
        if(off.isActive()){
          gaeCtx.realOffset=off.offset;
          fetchOptions.offset(gaeCtx.realOffset);
          off.passivate();
        }
      }
     
      switch(fetchType.fetchType){
      case KEYS_ONLY:
        {
          // uses iterable as it is the only async request for prepared query for the time being
          Iterable<Entity> entities = prepareKeysOnly(query).asIterable(fetchOptions);
          return new GaeSienaFutureListMapper<T>(this, entities, query, GaeSienaFutureListMapper.MapType.KEYS_ONLY);
        }
      case NORMAL:
      default:
        {
          // uses iterable as it is the only async request for prepared query for the time being
          Iterable<Entity> entities = prepare(query).asIterable(fetchOptions);
          return new GaeSienaFutureListMapper<T>(this, entities, query);
        }
      }
     
    }else {
      if(off.isActive()){
        // by default, we add the offset but it can be added with the realoffset
        // in case of cursor desactivated
        fetchOptions.offset(off.offset);
        gaeCtx.realOffset+=off.offset;
        off.passivate();
      }
     
      // manages cursor limitations for IN and != operators by using offset 
      if(!gaeCtx.isActive()){
        // cursor not yet created
        switch(fetchType.fetchType){
        case KEYS_ONLY:
          {
            PreparedQuery pq = prepareKeysOnly(query);
            if(!gaeCtx.useCursor){
              // then uses offset (in case of IN or != operators)
              //if(offset.isActive()){
              //  fetchOptions.offset(offset.offset);
              //}
              fetchOptions.offset(gaeCtx.realOffset);             
            }
           
            // we can't use real asynchronous function with cursors
            // so the page is extracted at once and wrapped into a SienaFuture
            QueryResultList<Entity> entities = pq.asQueryResultList(fetchOptions);

            // activates the GaeCtx now that it is initialised
            gaeCtx.activate();
            // sets the current cursor (in stateful mode, cursor is always kept for further use)
            if(pag.isPaginating()){
              Cursor cursor = entities.getCursor();
              if(cursor!=null){
                gaeCtx.addCursor(cursor.toWebSafeString());
              }
            }else{
              Cursor cursor = entities.getCursor();
              if(cursor!=null){
                gaeCtx.addAndMoveCursor(entities.getCursor().toWebSafeString());
              }
              // keeps track of the offset anyway if not paginating
              gaeCtx.realOffset+=entities.size();
            }                     
           
            return new GaeSienaFutureListMapper<T>(
                this, entities, query, GaeSienaFutureListMapper.MapType.KEYS_ONLY);
          }
        case NORMAL:
        default:
          {
            PreparedQuery pq = prepare(query);
            if(!gaeCtx.useCursor){
              // then uses offset (in case of IN or != operators)
              //if(offset.isActive()){
              //  fetchOptions.offset(offset.offset);
              //}
              fetchOptions.offset(gaeCtx.realOffset);             
            }
            // we can't use real asynchronous function with cursors
            // so the page is extracted at once and wrapped into a SienaFuture
            QueryResultList<Entity> entities = pq.asQueryResultList(fetchOptions);
           
            // activates the GaeCtx now that it is initialised
            gaeCtx.activate();
            // sets the current cursor (in stateful mode, cursor is always kept for further use)
            //if(gaeCtx.useCursor){
            if(pag.isPaginating()){
              Cursor cursor = entities.getCursor();
              if(cursor!=null){
                gaeCtx.addCursor(cursor.toWebSafeString());
              }
            }else{
              Cursor cursor = entities.getCursor();
              if(cursor!=null){
                gaeCtx.addAndMoveCursor(entities.getCursor().toWebSafeString());
              }
              // keeps track of the offset anyway if not paginating
              gaeCtx.realOffset+=entities.size();
            }
            //}
           
            return new GaeSienaFutureListMapper<T>(this, entities, query);
          }
        }
       
      }else {
        switch(fetchType.fetchType){
        case KEYS_ONLY:
          {
            // we prepare the query each time
            PreparedQuery pq = prepareKeysOnly(query);
            QueryResultList<Entity> entities;
            if(!gaeCtx.useCursor){
              // then uses offset (in case of IN or != operators)
              //if(offset.isActive()){
              //  fetchOptions.offset(offset.offset);
              //}
              fetchOptions.offset(gaeCtx.realOffset);
              // we can't use real asynchronous function with cursors
              // so the page is extracted at once and wrapped into a SienaFuture
              entities = pq.asQueryResultList(fetchOptions);
            }else {
              // we can't use real asynchronous function with cursors
              // so the page is extracted at once and wrapped into a SienaFuture
              String cursor = gaeCtx.currentCursor();
              if(cursor!=null){
                entities = pq.asQueryResultList(
                  fetchOptions.startCursor(Cursor.fromWebSafeString(cursor)));
              }
              else {
                entities = pq.asQueryResultList(fetchOptions);
              }
            }
           
            // sets the current cursor (in stateful mode, cursor is always kept for further use)
            //if(gaeCtx.useCursor){
            if(pag.isPaginating()){
              Cursor cursor = entities.getCursor();
              if(cursor!=null){
                gaeCtx.addCursor(cursor.toWebSafeString());
              }
            }else{
              Cursor cursor = entities.getCursor();
              if(cursor!=null){
                gaeCtx.addAndMoveCursor(entities.getCursor().toWebSafeString());
              }
              // keeps track of the offset anyway if not paginating
              gaeCtx.realOffset+=entities.size();
            }
            //}
           
            return new GaeSienaFutureListMapper<T>(
                this, entities, query, GaeSienaFutureListMapper.MapType.KEYS_ONLY);
          }
        case NORMAL:
        default:
          {
            PreparedQuery pq = prepare(query);
            QueryResultList<Entity> entities;
            if(!gaeCtx.useCursor){
              // then uses offset (in case of IN or != operators)
              //if(offset.isActive()){
              //  fetchOptions.offset(offset.offset);
              //}
              fetchOptions.offset(gaeCtx.realOffset);
              // we can't use real asynchronous function with cursors
              // so the page is extracted at once and wrapped into a SienaFuture
              entities = pq.asQueryResultList(fetchOptions);
            }else {
              // we can't use real asynchronous function with cursors
              // so the page is extracted at once and wrapped into a SienaFuture
              String cursor = gaeCtx.currentCursor();
              if(cursor!=null){
                entities = pq.asQueryResultList(
                  fetchOptions.startCursor(Cursor.fromWebSafeString(gaeCtx.currentCursor())));
              }else {
                entities = pq.asQueryResultList(fetchOptions);
              }
            }
           
            // sets the current cursor (in stateful mode, cursor is always kept for further use)
            //if(gaeCtx.useCursor){
            if(pag.isPaginating()){
              Cursor cursor = entities.getCursor();
              if(cursor!=null){
                gaeCtx.addCursor(cursor.toWebSafeString());
              }
            }else{
View Full Code Here

      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 SienaFutureMock<Iterable<T>>(new ArrayList<T>());
    }
   
    if(state.isStateless()) {
      if(pag.isPaginating()){     
        if(off.isActive()){
          gaeCtx.realOffset+=off.offset;
          fetchOptions.offset(gaeCtx.realOffset);
          off.passivate();
        }else {
          fetchOptions.offset(gaeCtx.realOffset);
        }
      }else {
               
        // if stateless and not paginating, resets the realoffset to 0
        gaeCtx.realOffset = off.offset;
        if(off.isActive()){
          fetchOptions.offset(gaeCtx.realOffset);
          off.passivate();
        }
      }
     
      switch(fetchType.fetchType){
      case ITER:
      default:
        {
          // uses iterable as it is the only async request for prepared query for the time being
          Iterable<Entity> entities = prepare(query).asIterable(fetchOptions);
          return new GaeSienaFutureIterableMapper<T>(this, entities, query);
        }
      }
     
    }else {
      if(off.isActive()){
        // by default, we add the offset but it can be added with the realoffset
        // in case of cursor desactivated
        fetchOptions.offset(off.offset);
        gaeCtx.realOffset+=off.offset;
        off.passivate();
      }
      // manages cursor limitations for IN and != operators   
      if(!gaeCtx.isActive()){
        // cursor not yet created
        switch(fetchType.fetchType){
        case ITER:
        default:
          {
            PreparedQuery pq = prepare(query);
           
            if(pag.isPaginating()){
              // in case of pagination, we need to allow asynchronous calls such as:
              // QueryAsync<MyClass> query = pm.createQuery(MyClass).paginate(5).stateful().order("name");
              // SienaFuture<Iterable<MyClass>> future1 = query.iter();
              // SienaFuture<Iterable<MyClass>> future2 = query.nextPage().iter();
              // Iterable<MyClass> it = future1.get().iterator();
              // while(it.hasNext()) { // do it }
              // it = future2.get().iterator();
              // while(it.hasNext()) { // do it }
             
              // so we can't use the asQueryResultIterable as the cursor is not moved to the end of the current page
              // but moved at each call of iterable.iterator().next()
              // thus we use the List in this case to be able to move directly to the next page with cursors
              QueryResultList<Entity> entities = pq.asQueryResultList(fetchOptions);

              // activates the GaeCtx now that it is initialised
              gaeCtx.activate();
              // sets the current cursor (in stateful mode, cursor is always kept for further use)
              //if(gaeCtx.useCursor){
              Cursor cursor = entities.getCursor();
              if(cursor!=null){
                gaeCtx.addCursor(cursor.toWebSafeString());
              }
              //}
             
              return new GaeSienaFutureIterableMapper<T>(this, entities, query);
            }else {
              // if not paginating, we simply use the queryresultiterable and moves the current cursor
              // while iterating
              QueryResultIterable<Entity> entities = pq.asQueryResultIterable(fetchOptions);
              // activates the GaeCtx now that it is initialised
              gaeCtx.activate();
              return new GaeSienaFutureIterableMapperWithCursor<T>(this, entities, query);
            }
           
          }
        }
       
      }else {
        switch(fetchType.fetchType){
        case ITER:
        default:
          {
            PreparedQuery pq = prepare(query);
            if(pag.isPaginating()){
              // in case of pagination, we need to allow asynchronous calls such as:
              // QueryAsync<MyClass> query = pm.createQuery(MyClass).paginate(5).stateful().order("name");
              // SienaFuture<Iterable<MyClass>> future1 = query.iter();
              // SienaFuture<Iterable<MyClass>> future2 = query.nextPage().iter();
              // Iterable<MyClass> it = future1.get().iterator();
View Full Code Here

      this.query = query;
      this.id = ClassInfo.getIdField(query.getQueriedClass());
      this.gaeIterator = gaeIterable.iterator();
     
      // if paginating and 0 results then no more data else resets noMoreDataAfter
      QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
      QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext)query.option(QueryOptionGaeContext.ID);
      if(pag.isPaginating()){
        if(!gaeIterator.hasNext()){
          gaeCtx.noMoreDataAfter = true;
        }else {
          gaeCtx.noMoreDataAfter = false;
        }
View Full Code Here

      this.query = query;
      this.id = ClassInfo.getIdField(query.getQueriedClass());
      this.gaeIterator = gaeIterable.iterator();
     
      // if paginating and 0 results then no more data
      QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
      QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext)query.option(QueryOptionGaeContext.ID);
      if(pag.isPaginating() && !gaeIterator.hasNext()){
        gaeCtx.noMoreDataAfter = true;
      }
    }
View Full Code Here

      this.query = query;
      this.id = ClassInfo.getIdField(query.getQueriedClass());
      this.gaeIterator = gaeIterable.iterator();
     
      // if paginating and 0 results then no more data else resets noMoreDataAfter
      QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
      QueryOptionGaeContext gaeCtx = (QueryOptionGaeContext)query.option(QueryOptionGaeContext.ID);
      if(pag.isPaginating()){
        if(!gaeIterator.hasNext()){
          gaeCtx.noMoreDataAfter = true;
        }else {
          gaeCtx.noMoreDataAfter = false;
        }
View Full Code Here

   
    return delete(l);
  }
 
  protected <T> void postMapping(Query<T> query){
    QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext)query.option(QueryOptionSdbContext.ID);
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
    QueryOptionOffset off = (QueryOptionOffset)query.option(QueryOptionOffset.ID);

    // desactivates paging & offset in stateless mode
    if(state.isStateless() && !pag.isPaginating()){
      pag.passivate();
      pag.pageSize = 0;
      sdbCtx.resetAll();
    }
    // offset if not kept as it is never reused as is even in stateful
    // mode. the stateful mode only keeps the realOffset/pagination alive
View Full Code Here

    off.passivate();
    off.offset = 0;
  }
 
  protected <T> void continueFetchNextToken(Query<T> query, List<T> results, int depth){
    QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext)query.option(QueryOptionSdbContext.ID);
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
    QueryOptionOffset off = (QueryOptionOffset)query.option(QueryOptionOffset.ID);
   
    // desactivates offset not to use if fetching more items from next token
    if(state.isStateless()){
      off.passivate();
    }
   
    if(!pag.isActive()){
      if(state.isStateless()){
        // retrieves next token
        if(sdbCtx.nextToken()!=null){
          doFetchList(query, Integer.MAX_VALUE, 0, results, depth+1);
        }
View Full Code Here

      }
    }
  }
 
  protected <T> void postFetch(Query<T> query, SelectResult res) {
    QueryOptionPage pag = (QueryOptionPage)query.option(QueryOptionPage.ID);
    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext)query.option(QueryOptionSdbContext.ID);
    QueryOptionState state = (QueryOptionState)query.option(QueryOptionState.ID);
    QueryOptionOffset off = (QueryOptionOffset)query.option(QueryOptionOffset.ID);
   
    if(sdbCtx.realPageSize == 0){
      sdbCtx.realPageSize = res.getItems().size();
    }
   
    String token = null;
    // sets the current cursor (in stateful mode, cursor is always kept for further use)
    if(pag.isPaginating()){
      token = res.getNextToken();
      if(token!=null){
        sdbCtx.addToken(token, sdbCtx.realOffset + sdbCtx.realPageSize + off.offset);
      }
      // if paginating and 0 results then no more data else resets noMoreDataAfter
View Full Code Here

TOP

Related Classes of siena.core.options.QueryOptionPage

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.