-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Account validator to separate class to allow reusing it
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
modules/qi-minigl/src/main/java/org/jpos/qi/minigl/AccountValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} |