@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()); }
@DELETE @Path("/{accountId:" + UUID_PATTERN + "}/" + TAGS) @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response deleteTags(@PathParam(ID_PARAM_NAME) final String id, @QueryParam(QUERY_TAGS) final String tagList, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagApiException, AccountApiException { final CallContext callContext = context.createContext(createdBy, reason, comment, request); // Look if there is an AUTO_PAY_OFF for that account and check if the account has a default paymentMethod // If not we can't remove the AUTO_PAY_OFF tag final Collection<UUID> tagDefinitionUUIDs = getTagDefinitionUUIDs(tagList); boolean isTagAutoPayOff = false; for (final UUID cur : tagDefinitionUUIDs) { if (cur.equals(ControlTagType.AUTO_PAY_OFF.getId())) { isTagAutoPayOff = true; break; } } final UUID accountId = UUID.fromString(id); if (isTagAutoPayOff) { 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"); } } return super.deleteTags(UUID.fromString(id), tagList, callContext); }
private TagDefinitionModelDao getTagDefinitionFromTransaction(final UUID tagDefinitionId, final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory, final InternalTenantContext context) throws TagApiException { TagDefinitionModelDao tagDefintion = null; for (final ControlTagType t : ControlTagType.values()) { if (t.getId().equals(tagDefinitionId)) { tagDefintion = new TagDefinitionModelDao(t); break; } } 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 inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> 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; } });