public EntityBase(final Entity target) { this.id = target.getId(); this.createdDate = target.getCreatedDate(); this.updatedDate = target.getUpdatedDate(); }
public DefaultPagination(final Pagination original, final Long limit, final Iterator<T> delegate) { this(original.getCurrentOffset(), limit, original.getTotalNbRecords(), original.getMaxNbRecords(), delegate); }
@Override public Pagination<M> getAll(final InternalTenantContext context) { // We usually always want to wrap our queries in an EntitySqlDaoTransactionWrapper... except here. // Since we want to stream the results out, we don't want to auto-commit when this method returns. final EntitySqlDao<M, E> sqlDao = transactionalSqlDao.onDemandForStreamingResults(realSqlDao); // Note: we need to perform the count before streaming the results, as the connection // will be busy as we stream the results out. This is also why we cannot use // SQL_CALC_FOUND_ROWS / FOUND_ROWS (which may not be faster anyways). final Long count = sqlDao.getCount(context); final Iterator<M> results = sqlDao.getAll(context); return new DefaultPagination<M>(count, results); }
private void checkPagination(final long nbRecords) { final Pagination<CustomField> foundCustomFields = customFieldUserApi.searchCustomFields("ACCOUNT", 0L, nbRecords + 1L, callContext); Assert.assertEquals(foundCustomFields.iterator().hasNext(), nbRecords > 0); Assert.assertEquals(foundCustomFields.getMaxNbRecords(), (Long) nbRecords); Assert.assertEquals(foundCustomFields.getTotalNbRecords(), (Long) nbRecords); final Pagination<CustomField> gotCustomFields = customFieldUserApi.getCustomFields(0L, nbRecords + 1L, callContext); Assert.assertEquals(gotCustomFields.iterator().hasNext(), nbRecords > 0); Assert.assertEquals(gotCustomFields.getMaxNbRecords(), (Long) nbRecords); Assert.assertEquals(gotCustomFields.getTotalNbRecords(), (Long) nbRecords); } }
public static <E extends Entity, O, T extends BillingExceptionBase> Pagination<E> getEntityPagination(final Long limit, final SourcePaginationBuilder<O, T> sourcePaginationBuilder, final Function<O, E> function) throws T { final Pagination<O> modelsDao = sourcePaginationBuilder.build(); return new DefaultPagination<E>(modelsDao, limit, Iterators.<E>filter(Iterators.<O, E>transform(modelsDao.iterator(), function), Predicates.<E>notNull())); }
public static <T> Pagination<T> build(final Long offset, final Long limit, final Collection<T> elements) { return build(offset, limit, elements.size(), elements); }
@Test(groups = "fast", description = "Test Util: Pagination builder tests") public void testDefaultPaginationBuilder() throws Exception { Assert.assertEquals(DefaultPagination.<Integer>build(0L, 0L, ImmutableList.<Integer>of()), expectedOf(0L, 0L, 0L, ImmutableList.<Integer>of())); Assert.assertEquals(DefaultPagination.<Integer>build(0L, 10L, ImmutableList.<Integer>of()), expectedOf(0L, 0L, 0L, ImmutableList.<Integer>of())); Assert.assertEquals(DefaultPagination.<Integer>build(10L, 0L, ImmutableList.<Integer>of()), expectedOf(10L, 0L, 0L, ImmutableList.<Integer>of())); Assert.assertEquals(DefaultPagination.<Integer>build(10L, 10L, ImmutableList.<Integer>of()), expectedOf(10L, 0L, 0L, ImmutableList.<Integer>of())); Assert.assertEquals(DefaultPagination.<Integer>build(0L, 0L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(0L, 0L, 5L, ImmutableList.<Integer>of())); Assert.assertEquals(DefaultPagination.<Integer>build(0L, 10L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(0L, 5L, 5L, ImmutableList.<Integer>of(1, 2, 3, 4, 5))); Assert.assertEquals(DefaultPagination.<Integer>build(4L, 10L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(4L, 1L, 5L, ImmutableList.<Integer>of(5))); Assert.assertEquals(DefaultPagination.<Integer>build(0L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(0L, 3L, 5L, ImmutableList.<Integer>of(1, 2, 3))); Assert.assertEquals(DefaultPagination.<Integer>build(1L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(1L, 3L, 5L, ImmutableList.<Integer>of(2, 3, 4))); Assert.assertEquals(DefaultPagination.<Integer>build(2L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(2L, 3L, 5L, ImmutableList.<Integer>of(3, 4, 5))); Assert.assertEquals(DefaultPagination.<Integer>build(3L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(3L, 2L, 5L, ImmutableList.<Integer>of(4, 5))); Assert.assertEquals(DefaultPagination.<Integer>build(4L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(4L, 1L, 5L, ImmutableList.<Integer>of(5))); Assert.assertEquals(DefaultPagination.<Integer>build(5L, 3L, ImmutableList.<Integer>of(1, 2, 3, 4, 5)), expectedOf(5L, 0L, 5L, ImmutableList.<Integer>of())); }
public static <T> Pagination<T> build(final Long offset, final Long limit, final Integer maxNbRecords, final Collection<T> elements) { final List<T> allResults = ImmutableList.<T>copyOf(elements); final List<T> results; if (offset >= allResults.size()) { results = ImmutableList.<T>of(); } else if (offset + limit > allResults.size()) { results = allResults.subList(offset.intValue(), allResults.size()); } else { results = allResults.subList(offset.intValue(), offset.intValue() + limit.intValue()); } return new DefaultPagination<T>(offset, limit, (long) results.size(), (long) maxNbRecords, results.iterator()); }
@Override public Pagination<M> getAll(final InternalTenantContext context) { final List<M> result = new ArrayList<M>(); for (final Map<Long, M> cur : entities.values()) { result.add(cur.values().iterator().next()); } return new DefaultPagination<M>(getCount(context), result.iterator()); }
private DefaultSubscriptionBase getDefaultSubscriptionBase(final Entity subscriptionBase, final Catalog catalog, final InternalTenantContext context) throws CatalogApiException { if (subscriptionBase instanceof DefaultSubscriptionBase) { return (DefaultSubscriptionBase) subscriptionBase; } else { // Safe cast, see above return (DefaultSubscriptionBase) dao.getSubscriptionFromId(subscriptionBase.getId(), catalog, context); } } }
@Override public String toString() { final StringBuilder sb = new StringBuilder("DefaultPagination{"); sb.append("currentOffset=").append(currentOffset); sb.append(", nextOffset=").append(getNextOffset()); sb.append(", totalNbRecords=").append(totalNbRecords); sb.append(", maxNbRecords=").append(maxNbRecords); sb.append('}'); return sb.toString(); }
private void checkPagination(final long nbRecords) { final Pagination<Tag> foundTags = tagUserApi.searchTags("ACCOUNT", 0L, nbRecords + 1L, callContext); Assert.assertEquals(foundTags.iterator().hasNext(), nbRecords > 0); Assert.assertEquals(foundTags.getMaxNbRecords(), (Long) nbRecords); Assert.assertEquals(foundTags.getTotalNbRecords(), (Long) nbRecords); final Pagination<Tag> gotTags = tagUserApi.getTags(0L, nbRecords + 1L, callContext); Assert.assertEquals(gotTags.iterator().hasNext(), nbRecords > 0); Assert.assertEquals(gotTags.getMaxNbRecords(), (Long) nbRecords); Assert.assertEquals(gotTags.getTotalNbRecords(), (Long) nbRecords); } }
@Override public Pagination<Account> getAccounts(final Long offset, final Long limit, final TenantContext context) { return DefaultPagination.<Account>build(offset, limit, accounts); }
private Pagination<Integer> expectedOf(final Long currentOffset, final Long totalNbRecords, final Long maxNbRecords, final List<Integer> delegate) { return new DefaultPagination<Integer>(currentOffset, Long.MAX_VALUE, totalNbRecords, maxNbRecords, delegate.iterator()); } }
private Builder<String> extractEntityIdsFromBatchArgument(final Iterable arg) { final Iterator iterator = arg.iterator(); final Builder<String> entityIds = new Builder<String>(); while (iterator.hasNext()) { final Object object = iterator.next(); if (!(object instanceof Entity)) { // No good - ignore return null; } else { entityIds.add(((Entity) object).getId().toString()); } } return entityIds; }
@Override public Pagination<SubscriptionBundleModelDao> searchSubscriptionBundles(final String searchKey, final Long offset, final Long limit, final InternalTenantContext context) { final List<SubscriptionBundleModelDao> results = new LinkedList<SubscriptionBundleModelDao>(); int maxNbRecords = 0; for (final SubscriptionBundleModelDao bundleModelDao : getAll(context)) { maxNbRecords++; if (bundleModelDao.getId().toString().equals(searchKey) || bundleModelDao.getExternalKey().equals(searchKey) || bundleModelDao.getAccountId().toString().equals(searchKey)) { results.add(bundleModelDao); } } return DefaultPagination.<SubscriptionBundleModelDao>build(offset, limit, maxNbRecords, results); }
@Override public Pagination<Account> searchAccounts(final String searchKey, final Long offset, final Long limit, final TenantContext tenantContext) { final List<Account> results = new LinkedList<Account>(); for (final Account account : accounts) { if ((account.getName() != null && account.getName().contains(searchKey)) || (account.getEmail() != null && account.getEmail().contains(searchKey)) || (account.getExternalKey() != null && account.getExternalKey().contains(searchKey)) || (account.getCompanyName() != null && account.getCompanyName().contains(searchKey))) { results.add(account); } } return DefaultPagination.<Account>build(offset, limit, accounts.size(), results); }
@Override public Pagination<AccountModelDao> searchAccounts(final String searchKey, final Long offset, final Long limit, final InternalTenantContext context) { final Collection<AccountModelDao> results = new LinkedList<AccountModelDao>(); int maxNbRecords = 0; for (final AccountModelDao account : getAll(context)) { maxNbRecords++; if ((account.getName() != null && account.getName().contains(searchKey)) || (account.getEmail() != null && account.getEmail().contains(searchKey)) || (account.getExternalKey() != null && account.getExternalKey().contains(searchKey)) || (account.getCompanyName() != null && account.getCompanyName().contains(searchKey))) { results.add(account); } } return DefaultPagination.<AccountModelDao>build(offset, limit, maxNbRecords, results); }