@PathParam("accountId") IntegerParam accountId,
@FormParam("name") String name,
@FormParam("currency") CurrencyParam currency,
@FormParam("archived") BooleanParam archived) {
final Account account = accountDAO.findAccount(user.getAccountKey(), accountId.getValue());
boolean shouldUpdate = false;
if (account == null) {
throw new WebApplicationException(Status.NOT_FOUND);
}
if (name != null) {
account.setName(name);
shouldUpdate = true;
}
if (currency != null) {
account.setCurrency(currency.getValue());
shouldUpdate = true;
}
if (archived != null) {
if ((archived.getValue() && account.isArchived()) || (!archived.getValue() && account.isActive())) {
// already in the target state
} else if (archived.getValue() && account.isActive()) {
account.setStatus(AccountStatus.ARCHIVED);
shouldUpdate = true;
} else if (!archived.getValue() && account.isArchived()) {
account.setStatus(AccountStatus.ACTIVE);
shouldUpdate = true;
} else {
throw new WebApplicationException(
Response
.status(Status.BAD_REQUEST)
.entity("Cannot change archived status of an account with status of " + account.getStatus())
.build()
);
}
}