/**
   * Returns an ordered range of all the foos where field2 = ?.
   *
   * <p>Useful when paginating results. Returns a maximum of <code>end - start</code> instances.
   * <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result
   * set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start
   * </code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}
   * will return the full result set. If <code>orderByComparator</code> is specified, then the query
   * will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and
   * pagination is required (<code>start</code> and <code>end</code> are not {@link
   * com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default
   * ORDER BY logic from {@link com.liferay.testpacl.model.impl.FooModelImpl}. If both <code>
   * orderByComparator</code> and pagination are absent, for performance reasons, the query will not
   * have an ORDER BY clause and the returned result set will be sorted on by the primary key in an
   * ascending order.
   *
   * @param field2 the field2
   * @param start the lower bound of the range of foos
   * @param end the upper bound of the range of foos (not inclusive)
   * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
   * @return the ordered range of matching foos
   */
  @Override
  public List<Foo> findByField2(
      boolean field2, int start, int end, OrderByComparator<Foo> orderByComparator) {
    boolean pagination = true;
    FinderPath finderPath = null;
    Object[] finderArgs = null;

    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
      pagination = false;
      finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_FIELD2;
      finderArgs = new Object[] {field2};
    } else {
      finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_FIELD2;
      finderArgs = new Object[] {field2, start, end, orderByComparator};
    }

    List<Foo> list = (List<Foo>) FinderCacheUtil.getResult(finderPath, finderArgs, this);

    if ((list != null) && !list.isEmpty()) {
      for (Foo foo : list) {
        if ((field2 != foo.getField2())) {
          list = null;

          break;
        }
      }
    }

    if (list == null) {
      StringBundler query = null;

      if (orderByComparator != null) {
        query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 3));
      } else {
        query = new StringBundler(3);
      }

      query.append(_SQL_SELECT_FOO_WHERE);

      query.append(_FINDER_COLUMN_FIELD2_FIELD2_2);

      if (orderByComparator != null) {
        appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
      } else if (pagination) {
        query.append(FooModelImpl.ORDER_BY_JPQL);
      }

      String sql = query.toString();

      Session session = null;

      try {
        session = openSession();

        Query q = session.createQuery(sql);

        QueryPos qPos = QueryPos.getInstance(q);

        qPos.add(field2);

        if (!pagination) {
          list = (List<Foo>) QueryUtil.list(q, getDialect(), start, end, false);

          Collections.sort(list);

          list = Collections.unmodifiableList(list);
        } else {
          list = (List<Foo>) QueryUtil.list(q, getDialect(), start, end);
        }

        cacheResult(list);

        FinderCacheUtil.putResult(finderPath, finderArgs, list);
      } catch (Exception e) {
        FinderCacheUtil.removeResult(finderPath, finderArgs);

        throw processException(e);
      } finally {
        closeSession(session);
      }
    }

    return list;
  }