private void remove_AUTO_INVOICE_OFF_on_clear(final UUID accountId, final InternalCallContext context) throws OverdueApiException { try { tagApi.removeTag(accountId, ObjectType.ACCOUNT, ControlTagType.AUTO_INVOICING_OFF.getId(), context); } catch (final TagApiException e) { if (e.getCode() != ErrorCode.TAG_DOES_NOT_EXIST.getCode()) { throw new OverdueApiException(e); } } }
@Override protected TagApiException generateAlreadyExistsException(final TagModelDao entity, final InternalCallContext context) { // Print the tag details, not the id here, as we throw this exception when checking if a tag already exists // by using the isSame(TagModelDao) method (see above) return new TagApiException(ErrorCode.TAG_ALREADY_EXISTS, entity.toString()); }
@Override public void addTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final InternalCallContext context) throws TagApiException { final TagModelDao tag = new TagModelDao(context.getCreatedDate(), tagDefinitionId, objectId, objectType); try { tagDao.create(tag, context); } catch (TagApiException e) { // Be lenient here and make the addTag method idempotent if (ErrorCode.TAG_ALREADY_EXISTS.getCode() != e.getCode()) { throw e; } } }
private TagDefinitionModelDao getTagDefinitionFromTransaction(final UUID tagDefinitionId, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalTenantContext context) throws TagApiException { TagDefinitionModelDao tagDefintion = SystemTags.lookup(tagDefinitionId); if (tagDefintion == null) { final TagDefinitionSqlDao transTagDefintionSqlDao = entitySqlDaoWrapperFactory.become(TagDefinitionSqlDao.class); tagDefintion = transTagDefintionSqlDao.getById(tagDefinitionId.toString(), context); } if (tagDefintion == null) { throw new TagApiException(ErrorCode.TAG_DEFINITION_DOES_NOT_EXIST, tagDefinitionId); } return tagDefintion; }
@Override public void addTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final CallContext context) throws TagApiException { if (SystemTags.isSystemTag(tagDefinitionId)) { // TODO Create a proper ErrorCode instaed throw new IllegalStateException(String.format("Failed to add tag for tagDefinitionId='%s': System tags are reserved for the system.", tagDefinitionId)); } final InternalCallContext internalContext = internalCallContextFactory.createInternalCallContext(objectId, objectType, context); final TagModelDao tag = new TagModelDao(context.getCreatedDate(), tagDefinitionId, objectId, objectType); try { tagDao.create(tag, internalContext); } catch (TagApiException e) { // Be lenient here and make the addTag method idempotent if (ErrorCode.TAG_ALREADY_EXISTS.getCode() != e.getCode()) { throw e; } } }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final TagDefinitionModelDao tagDefinition = getTagDefinitionFromTransaction(tagDefinitionId, entitySqlDaoWrapperFactory, context); final TagSqlDao transactional = entitySqlDaoWrapperFactory.become(TagSqlDao.class); final List<TagModelDao> tags = transactional.getTagsForObject(objectId, objectType, context); TagModelDao tag = null; for (final TagModelDao cur : tags) { if (cur.getTagDefinitionId().equals(tagDefinitionId)) { tag = cur; break; } } if (tag == null) { throw new TagApiException(ErrorCode.TAG_DOES_NOT_EXIST, tagDefinition.getName()); } // Delete the tag transactional.markTagAsDeleted(tag.getId().toString(), context); postBusEventFromTransaction(tag, tag, ChangeType.DELETE, entitySqlDaoWrapperFactory, context); return null; } });
Assert.fail("Deleting same tag again should fail"); } catch (final TagApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.TAG_DOES_NOT_EXIST.getCode());
@Override protected TagApiException generateAlreadyExistsException(final TagModelDao entity, final InternalCallContext context) { // Print the tag details, not the id here, as we throw this exception when checking if a tag already exists // by using the isSame(TagModelDao) method (see above) return new TagApiException(ErrorCode.TAG_ALREADY_EXISTS, entity.toString()); }
@Test(groups = "slow") public void testInsertMultipleTags() throws TagApiException { final UUID objectId = UUID.randomUUID(); final ObjectType objectType = ObjectType.ACCOUNT; eventsListener.pushExpectedEvent(NextEvent.TAG); final Tag tag = new DescriptiveTag(ControlTagType.AUTO_INVOICING_OFF.getId(), objectType, objectId, internalCallContext.getCreatedDate()); tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); try { final Tag tag2 = new DescriptiveTag(ControlTagType.AUTO_INVOICING_OFF.getId(), objectType, objectId, internalCallContext.getCreatedDate()); tagDao.create(new TagModelDao(tag2), internalCallContext); Assert.fail("Should not be able to create twice the same tag"); assertListenerStatus(); } catch (final TagApiException e) { Assert.assertEquals(ErrorCode.TAG_ALREADY_EXISTS.getCode(), e.getCode()); } }
private TagDefinitionModelDao getTagDefinitionFromTransaction(final UUID tagDefinitionId, final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final InternalTenantContext context) throws TagApiException { TagDefinitionModelDao tagDefintion = SystemTags.lookup(tagDefinitionId); if (tagDefintion == null) { final TagDefinitionSqlDao transTagDefintionSqlDao = entitySqlDaoWrapperFactory.become(TagDefinitionSqlDao.class); tagDefintion = transTagDefintionSqlDao.getById(tagDefinitionId.toString(), context); } if (tagDefintion == null) { throw new TagApiException(ErrorCode.TAG_DEFINITION_DOES_NOT_EXIST, tagDefinitionId); } return tagDefintion; }
public void parkAccount(final UUID accountId, final InternalCallContext internalCallContext) throws TagApiException { try { tagApi.addTag(accountId, ObjectType.ACCOUNT, PARK_TAG_DEFINITION_ID, internalCallContext); } catch (final TagApiException e) { if (ErrorCode.TAG_ALREADY_EXISTS.getCode() != e.getCode()) { throw e; } } }
final Account account = accountUserApi.getAccountById(accountId, callContext); if (account.getPaymentMethodId() == null) { throw new TagApiException(ErrorCode.TAG_CANNOT_BE_REMOVED, ControlTagType.AUTO_PAY_OFF, " the account does not have a default payment method");
@Override public Response toResponse(final TagApiException exception) { if (exception.getCode() == ErrorCode.TAG_DOES_NOT_EXIST.getCode()) { return buildNotFoundResponse(exception, uriInfo); } else if (exception.getCode() == ErrorCode.TAG_CANNOT_BE_REMOVED.getCode()) { return buildBadRequestResponse(exception, uriInfo); } else { return buildBadRequestResponse(exception, uriInfo); } } }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory) throws Exception { final TagDefinitionModelDao tagDefinition = getTagDefinitionFromTransaction(tagDefinitionId, entitySqlDaoWrapperFactory, context); final TagSqlDao transactional = entitySqlDaoWrapperFactory.become(TagSqlDao.class); final List<TagModelDao> tags = transactional.getTagsForObject(objectId, objectType, context); TagModelDao tag = null; for (final TagModelDao cur : tags) { if (cur.getTagDefinitionId().equals(tagDefinitionId)) { tag = cur; break; } } if (tag == null) { throw new TagApiException(ErrorCode.TAG_DOES_NOT_EXIST, tagDefinition.getName()); } // Delete the tag transactional.markTagAsDeleted(tag.getId().toString(), context); postBusEventFromTransaction(tag, tag, ChangeType.DELETE, entitySqlDaoWrapperFactory, context); return null; } });
private void remove_AUTO_INVOICE_OFF_on_clear(final UUID accountId, final InternalCallContext context) throws OverdueApiException { try { tagApi.removeTag(accountId, ObjectType.ACCOUNT, ControlTagType.AUTO_INVOICING_OFF.getId(), context); } catch (final TagApiException e) { if (e.getCode() != ErrorCode.TAG_DOES_NOT_EXIST.getCode()) { throw new OverdueApiException(e); } } }
@Override public void addTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final InternalCallContext context) throws TagApiException { final TagModelDao tag = new TagModelDao(context.getCreatedDate(), tagDefinitionId, objectId, objectType); try { tagDao.create(tag, context); } catch (TagApiException e) { // Be lenient here and make the addTag method idempotent if (ErrorCode.TAG_ALREADY_EXISTS.getCode() != e.getCode()) { throw e; } } }
@Override public void addTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final CallContext context) throws TagApiException { if (SystemTags.isSystemTag(tagDefinitionId)) { // TODO Create a proper ErrorCode instaed throw new IllegalStateException(String.format("Failed to add tag for tagDefinitionId='%s': System tags are reserved for the system.", tagDefinitionId)); } final InternalCallContext internalContext = internalCallContextFactory.createInternalCallContext(objectId, objectType, context); final TagModelDao tag = new TagModelDao(context.getCreatedDate(), tagDefinitionId, objectId, objectType); try { tagDao.create(tag, internalContext); } catch (TagApiException e) { // Be lenient here and make the addTag method idempotent if (ErrorCode.TAG_ALREADY_EXISTS.getCode() != e.getCode()) { throw e; } } }
Assert.fail("Deleting same tag again should fail"); } catch (final TagApiException e) { Assert.assertEquals(e.getCode(), ErrorCode.TAG_DOES_NOT_EXIST.getCode());
@Test(groups = "slow") public void testInsertMultipleTags() throws TagApiException { final UUID objectId = UUID.randomUUID(); final ObjectType objectType = ObjectType.ACCOUNT; eventsListener.pushExpectedEvent(NextEvent.TAG); final Tag tag = new DescriptiveTag(ControlTagType.AUTO_INVOICING_OFF.getId(), objectType, objectId, internalCallContext.getCreatedDate()); tagDao.create(new TagModelDao(tag), internalCallContext); assertListenerStatus(); try { final Tag tag2 = new DescriptiveTag(ControlTagType.AUTO_INVOICING_OFF.getId(), objectType, objectId, internalCallContext.getCreatedDate()); tagDao.create(new TagModelDao(tag2), internalCallContext); Assert.fail("Should not be able to create twice the same tag"); assertListenerStatus(); } catch (final TagApiException e) { Assert.assertEquals(ErrorCode.TAG_ALREADY_EXISTS.getCode(), e.getCode()); } }