class BrowseServiceImpl extends ComponentSupport implements BrowseService {

  @Override
  public PageResult<Asset> previewAssets(final RepositorySelector repositorySelector,
                                           final List<Repository> repositories,
                                           final String jexlExpression,
                                           final QueryOptions queryOptions)
  {
    checkNotNull(repositories);
    checkNotNull(jexlExpression);
    final Repository repository = repositories.get(0);
    try (StorageTx storageTx = repository.facet(StorageFacet.class).txSupplier().get()) {
      storageTx.begin();
      List<Repository> previewRepositories;
      if (repositories.size() == 1 && groupType.equals(repository.getType())) {
        previewRepositories = repository.facet(GroupFacet.class).leafMembers();
      }
      else {
        previewRepositories = repositories;
      }

      PreviewAssetsSqlBuilder builder = new PreviewAssetsSqlBuilder(
          repositorySelector,
          jexlExpression,
          queryOptions,
          getRepoToContainedGroupMap(repositories));

      String whereClause = String.format("and (%s)", builder.buildWhereClause());

      //The whereClause is passed in as the querySuffix so that contentExpression will run after repository filtering
      return new PageResult<>(
          storageTx.countAssets(null, builder.buildSqlParams(), previewRepositories, whereClause),
          Lists.newArrayList(storageTx.findAssets(null, builder.buildSqlParams(),
              previewRepositories, whereClause + builder.buildQuerySuffix()))
      );
    }
  }

  @Inject public  BrowseServiceImpl(@Named(GroupType.NAME) final Type groupType,
                           final ComponentEntityAdapter componentEntityAdapter,
                           final VariableResolverAdapterManager variableResolverAdapterManager,
                           final ContentPermissionChecker contentPermissionChecker,
                           final AssetEntityAdapter assetEntityAdapter,
                           final BrowseAssetIterableFactory browseAssetIterableFactory,
                           final BrowseAssetsSqlBuilder browseAssetsSqlBuilder,
                           final BrowseComponentsSqlBuilder browseComponentsSqlBuilder,
                           final BucketStore bucketStore,
                           final RepositoryManager repositoryManager);

}