Skip to content

Commit

Permalink
Move Account validator to separate class to allow reusing it
Browse files Browse the repository at this point in the history
  • Loading branch information
sprevilla committed Mar 21, 2019
1 parent 7aba715 commit 62628e0
Showing 1 changed file with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.jpos.qi.minigl;

import com.vaadin.data.ValidationResult;
import com.vaadin.data.Validator;
import com.vaadin.data.ValueContext;
import com.vaadin.shared.ui.ErrorLevel;
import org.jpos.ee.DB;
import org.jpos.gl.Account;
import org.jpos.qi.QI;

public class AccountValidator implements Validator<Account> {
private QI app;
private Class clazz;

public AccountValidator (QI app, Class clazz) {
super();
this.app = app;
this.clazz = clazz;
}

@Override
public ValidationResult apply (Account value, ValueContext context) {
Account acct = null;
if (!context.getHasValue().isPresent())
return ValidationResult.ok();
boolean isReadOnly = context.getHasValue().get().isReadOnly();
if (isReadOnly)
return ValidationResult.ok();
if (value != null) {
try {
acct = DB.exec(db -> db.session().get(Account.class, value.getId()));
} catch (Exception e) {
app.getLog().createError(e.getMessage());
}
}
if (acct == null)
return ValidationResult.create(app.getMessage("errorMessage.willCreateAccountAndReceivables"),
ErrorLevel.INFO);
else if (clazz.isInstance(value))
return ValidationResult.create(app.getMessage("errorMessage.accountExists"), ErrorLevel.INFO);
return ValidationResult.ok();

}
}

0 comments on commit 62628e0

Please # to comment.