From 62628e05cd9fe7f16e05f521cc1aec6306ad709e Mon Sep 17 00:00:00 2001 From: Santiago Revilla Date: Thu, 21 Mar 2019 17:06:10 -0300 Subject: [PATCH] Move Account validator to separate class to allow reusing it --- .../org/jpos/qi/minigl/AccountValidator.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 modules/qi-minigl/src/main/java/org/jpos/qi/minigl/AccountValidator.java diff --git a/modules/qi-minigl/src/main/java/org/jpos/qi/minigl/AccountValidator.java b/modules/qi-minigl/src/main/java/org/jpos/qi/minigl/AccountValidator.java new file mode 100644 index 0000000000..0f91c71db9 --- /dev/null +++ b/modules/qi-minigl/src/main/java/org/jpos/qi/minigl/AccountValidator.java @@ -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 { + 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(); + + } +}