use of org.killbill.billing.util.tag.Tag in project killbill by killbill.
the class JaxRsResourceBase method createTagResponse.
protected Response createTagResponse(final UUID accountId, final List<Tag> tags, final AuditMode auditMode, final TenantContext context) throws TagDefinitionApiException {
final AccountAuditLogsForObjectType tagsAuditLogs = auditUserApi.getAccountAuditLogs(accountId, ObjectType.TAG, auditMode.getLevel(), context);
final Map<UUID, TagDefinition> tagDefinitionsCache = new HashMap<UUID, TagDefinition>();
final Collection<TagJson> result = new LinkedList<TagJson>();
for (final Tag tag : tags) {
if (tagDefinitionsCache.get(tag.getTagDefinitionId()) == null) {
tagDefinitionsCache.put(tag.getTagDefinitionId(), tagUserApi.getTagDefinition(tag.getTagDefinitionId(), context));
}
final TagDefinition tagDefinition = tagDefinitionsCache.get(tag.getTagDefinitionId());
final List<AuditLog> auditLogs = tagsAuditLogs.getAuditLogs(tag.getId());
result.add(new TagJson(tag, tagDefinition, auditLogs));
}
return Response.status(Response.Status.OK).entity(result).build();
}
use of org.killbill.billing.util.tag.Tag in project killbill by killbill.
the class TagResource method searchTags.
@TimedResource
@GET
@Path("/" + SEARCH + "/{searchKey:" + ANYTHING_PATTERN + "}")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Search tags", response = TagJson.class, responseContainer = "List")
@ApiResponses(value = {})
public Response searchTags(@PathParam("searchKey") final String searchKey, @QueryParam(QUERY_SEARCH_OFFSET) @DefaultValue("0") final Long offset, @QueryParam(QUERY_SEARCH_LIMIT) @DefaultValue("100") final Long limit, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagApiException {
final TenantContext tenantContext = context.createContext(request);
final Pagination<Tag> tags = tagUserApi.searchTags(searchKey, offset, limit, tenantContext);
final URI nextPageUri = uriBuilder.nextPage(TagResource.class, "searchTags", tags.getNextOffset(), limit, ImmutableMap.<String, String>of("searchKey", searchKey, QUERY_AUDIT, auditMode.getLevel().toString()));
final Map<UUID, TagDefinition> tagDefinitionsCache = new HashMap<UUID, TagDefinition>();
for (final TagDefinition tagDefinition : tagUserApi.getTagDefinitions(tenantContext)) {
tagDefinitionsCache.put(tagDefinition.getId(), tagDefinition);
}
return buildStreamingPaginationResponse(tags, new Function<Tag, TagJson>() {
@Override
public TagJson apply(final Tag tag) {
final TagDefinition tagDefinition = tagDefinitionsCache.get(tag.getTagDefinitionId());
// TODO Really slow - we should instead try to figure out the account id
final List<AuditLog> auditLogs = auditUserApi.getAuditLogs(tag.getId(), ObjectType.TAG, auditMode.getLevel(), tenantContext);
return new TagJson(tag, tagDefinition, auditLogs);
}
}, nextPageUri);
}
use of org.killbill.billing.util.tag.Tag in project killbill by killbill.
the class TagResource method getTags.
@TimedResource
@GET
@Path("/" + PAGINATION)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "List tags", response = TagJson.class, responseContainer = "List")
@ApiResponses(value = {})
public Response getTags(@QueryParam(QUERY_SEARCH_OFFSET) @DefaultValue("0") final Long offset, @QueryParam(QUERY_SEARCH_LIMIT) @DefaultValue("100") final Long limit, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagApiException {
final TenantContext tenantContext = context.createContext(request);
final Pagination<Tag> tags = tagUserApi.getTags(offset, limit, tenantContext);
final URI nextPageUri = uriBuilder.nextPage(TagResource.class, "getTags", tags.getNextOffset(), limit, ImmutableMap.<String, String>of(QUERY_AUDIT, auditMode.getLevel().toString()));
final Map<UUID, TagDefinition> tagDefinitionsCache = new HashMap<UUID, TagDefinition>();
for (final TagDefinition tagDefinition : tagUserApi.getTagDefinitions(tenantContext)) {
tagDefinitionsCache.put(tagDefinition.getId(), tagDefinition);
}
return buildStreamingPaginationResponse(tags, new Function<Tag, TagJson>() {
@Override
public TagJson apply(final Tag tag) {
final TagDefinition tagDefinition = tagDefinitionsCache.get(tag.getTagDefinitionId());
// TODO Really slow - we should instead try to figure out the account id
final List<AuditLog> auditLogs = auditUserApi.getAuditLogs(tag.getId(), ObjectType.TAG, auditMode.getLevel(), tenantContext);
return new TagJson(tag, tagDefinition, auditLogs);
}
}, nextPageUri);
}
use of org.killbill.billing.util.tag.Tag in project killbill by killbill.
the class TestDefaultTagUserApi method testSaveTagWithAccountRecordId.
@Test(groups = "slow")
public void testSaveTagWithAccountRecordId() throws Exception {
final UUID accountId = UUID.randomUUID();
final Long accountRecordId = 19384012L;
dbi.withHandle(new HandleCallback<Void>() {
@Override
public Void withHandle(final Handle handle) throws Exception {
// Note: we always create an accounts table, see MysqlTestingHelper
handle.execute("insert into accounts (record_id, id, email, name, first_name_length, is_notified_for_invoices, created_date, created_by, updated_date, updated_by) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", accountRecordId, accountId.toString(), "yo@t.com", "toto", 4, false, new Date(), "i", new Date(), "j");
return null;
}
});
checkPagination(0);
eventsListener.pushExpectedEvent(NextEvent.TAG);
tagUserApi.addTags(accountId, ObjectType.ACCOUNT, ImmutableList.<UUID>of(ControlTagType.WRITTEN_OFF.getId()), callContext);
assertListenerStatus();
checkPagination(1);
// Verify the tag was saved
final List<Tag> tags = tagUserApi.getTagsForObject(accountId, ObjectType.ACCOUNT, true, callContext);
Assert.assertEquals(tags.size(), 1);
Assert.assertEquals(tags.get(0).getTagDefinitionId(), ControlTagType.WRITTEN_OFF.getId());
Assert.assertEquals(tags.get(0).getObjectId(), accountId);
Assert.assertEquals(tags.get(0).getObjectType(), ObjectType.ACCOUNT);
// Verify the account_record_id was populated
dbi.withHandle(new HandleCallback<Void>() {
@Override
public Void withHandle(final Handle handle) throws Exception {
final List<Map<String, Object>> values = handle.select("select account_record_id from tags where object_id = ?", accountId.toString());
Assert.assertEquals(values.size(), 1);
Assert.assertEquals(values.get(0).keySet().size(), 1);
Assert.assertEquals(Long.valueOf(values.get(0).get("account_record_id").toString()), accountRecordId);
return null;
}
});
eventsListener.pushExpectedEvent(NextEvent.TAG);
tagUserApi.removeTags(accountId, ObjectType.ACCOUNT, ImmutableList.<UUID>of(ControlTagType.WRITTEN_OFF.getId()), callContext);
assertListenerStatus();
List<Tag> remainingTags = tagUserApi.getTagsForObject(accountId, ObjectType.ACCOUNT, false, callContext);
Assert.assertEquals(remainingTags.size(), 0);
checkPagination(0);
// Add again the tag
eventsListener.pushExpectedEvent(NextEvent.TAG);
tagUserApi.addTags(accountId, ObjectType.ACCOUNT, ImmutableList.<UUID>of(ControlTagType.WRITTEN_OFF.getId()), callContext);
assertListenerStatus();
remainingTags = tagUserApi.getTagsForObject(accountId, ObjectType.ACCOUNT, false, callContext);
Assert.assertEquals(remainingTags.size(), 1);
checkPagination(1);
// Delete again
eventsListener.pushExpectedEvent(NextEvent.TAG);
tagUserApi.removeTags(accountId, ObjectType.ACCOUNT, ImmutableList.<UUID>of(ControlTagType.WRITTEN_OFF.getId()), callContext);
assertListenerStatus();
remainingTags = tagUserApi.getTagsForObject(accountId, ObjectType.ACCOUNT, false, callContext);
Assert.assertEquals(remainingTags.size(), 0);
checkPagination(0);
}
Aggregations