jeudi 19 octobre 2017

avoiding many if statements, code improvement

I have oracle database that I can't change. In this db I have a user table with authority fields.

For example:

book_read = 1;  - means that user has authority 
book_upd  = 0;  - means that user he does not
report_read = 1;

Right now I am checking like this:

List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
if (user.getBook_read() == 1) {
    authorities.add(new SimpleGrantedAuthority("ROLE_BOOK_READ"));
}
if (user.getBook_upd() == 1) {
    authorities.add(new SimpleGrantedAuthority("ROLE_BOOK_UPD"));
}
if (user.getReport_read() == 1) {
    authorities.add(new SimpleGrantedAuthority("ROLE_REPORT_READ"));
}

I have a lot of Authorities (about 20, they all have a separate column in the table). Сan someone suggest how I can improve the code?

Aucun commentaire:

Enregistrer un commentaire