code: private String replaceAppliedToken(String query)
      {
          final String tokenRegex = "\\btoken\\(";
          final String pattern = "#TOKENKUNDERA#"; // need to replace with this as
                                                   // pattern matcher was
                                                   // returning false.
          query = query.replaceAll(tokenRegex, pattern);
  
          if (query.indexOf(pattern) > -1) // Means token( has been present and
                                           // replaced.
          {
              CQLTranslator translator = new CQLTranslator();
  
              int closingIndex = query.indexOf(CQLTranslator.CLOSE_BRACKET, query.lastIndexOf(pattern));
  
              String object = query.substring(query.lastIndexOf(pattern) + pattern.length(), closingIndex);
  
              Object entity = results.get(results.size() - 1);
              Class idClazz = ((AbstractAttribute) entityMetadata.getIdAttribute()).getBindableJavaType();
              Object id = PropertyAccessorHelper.getId(entity, entityMetadata);
              StringBuilder builder = new StringBuilder();
  
              translator.appendValue(builder, idClazz, id, false, false);
  
              query = query.replaceAll(pattern + object, pattern + builder.toString());
              query = query.replaceAll(pattern, CQLTranslator.TOKEN);
          }
          return query;
      }
docs: Replace applied token.
  
  @param query
  the query
  @return the string