Example usage for com.vaadin.data Binder writeBeanIfValid

List of usage examples for com.vaadin.data Binder writeBeanIfValid

Introduction

In this page you can find the example usage for com.vaadin.data Binder writeBeanIfValid.

Prototype

public boolean writeBeanIfValid(BEAN bean) 

Source Link

Document

Writes changes from the bound fields to the given bean if all validators (binding and bean level) pass.

Usage

From source file:org.jpos.qi.eeuser.UsersHelper.java

License:Open Source License

public boolean updateUser(Binder binder, String currentPass, String newClearPass) throws BLException {
    boolean userUpdated;
    try {/*from   w  w  w .j a v a  2s . c  om*/
        userUpdated = (boolean) DB.execWithTransaction((db) -> {
            UserManager mgr = new UserManager(db);
            User oldUser = (User) ((User) getOriginalEntity()).clone();
            if (binder.writeBeanIfValid(getOriginalEntity())) {
                User user = (User) getOriginalEntity();
                user = (User) db.session().merge(user);
                boolean updated = false;
                if (!newClearPass.isEmpty()) {
                    boolean passwordOK = false;
                    boolean newPasswordOK = false;
                    user.getPasswordhistory(); //to avoid lazy
                    passwordOK = mgr.checkPassword(user, currentPass);
                    newPasswordOK = mgr.checkNewPassword(user, newClearPass);
                    if (passwordOK && newPasswordOK) {
                        mgr.setPassword(user, newClearPass);
                        updated = true;
                    } else if (!newPasswordOK) {
                        throw new BLException("This password has already been used");
                    }
                }
                updated = updated || addRevisionUpdated(db, getEntityName(), String.valueOf(user.getId()),
                        oldUser, user, new String[] { "nick", "name", "email", "active", "roles", "password" });
                return updated;
            } else {
                throw new BLException(getApp().getMessage("errorMessage.invalidFields"));
            }
        });
    } catch (BLException e) {
        throw e;
    } catch (Exception e) {
        getApp().getLog().error(e);
        return false;
    }
    if (userUpdated && getOriginalEntity().equals(getApp().getUser())) {
        try {
            DB.exec((db) -> {
                db.session().refresh(getApp().getUser());
                return null;
            });
        } catch (Exception e) {
            getApp().getLog().error(e);
        }
    }
    return userUpdated;
}

From source file:org.jpos.qi.eeuser.UsersHelper.java

License:Open Source License

public boolean saveUser(Binder binder, String clearPass) throws BLException {
    User u = (User) getOriginalEntity();
    if (binder.writeBeanIfValid(getOriginalEntity())) {
        try {// w ww.  ja  v a  2  s .co  m
            return (boolean) DB.execWithTransaction((db) -> {
                db.save(u);
                if (clearPass != null && !clearPass.isEmpty()) {
                    UserManager mgr = new UserManager(db);
                    try {
                        mgr.setPassword(u, clearPass);
                    } catch (BLException e) {
                        return false;
                    }
                    addRevisionCreated(db, getEntityName(), u.getId().toString());
                    u.setForcePasswordChange(true);
                    db.session().update(u);
                    return true;
                }
                return false;
            });
        } catch (Exception e) {
            getApp().getLog().error(e);
            return false;
        }
    } else {
        throw new BLException("Invalid user");
    }
}

From source file:org.jpos.qi.minigl.NewEntryForm.java

License:Open Source License

private Button.ClickListener addEntryClickListener(Binder binder) {
    return event -> {
        if (binder.validate().isOk()) {
            if (binder.writeBeanIfValid(binder.getBean())) {
                GLEntry entry = (GLEntry) binder.getBean();
                int count = 0;
                List<GLEntry> newEntriesList = new ArrayList<>();
                for (GLEntry e : transaction.getEntries()) {
                    e.setId(count);//from   ww w  .  j a  v a2 s.com
                    newEntriesList.add(e);
                    count++;
                }
                if (entry.isCredit()) {
                    GLCredit credit = new GLCredit();
                    credit.setAccount(entry.getAccount());
                    credit.setAmount(entry.getAmount());
                    credit.setLayer(entry.getLayer());
                    credit.setDetail(entry.getDetail());
                    credit.setTags(entry.getTags());
                    credit.setTransaction(entry.getTransaction());
                    credit.setId(count);
                    newEntriesList.add(credit);
                } else if (entry.isDebit()) {
                    GLDebit debit = new GLDebit();
                    debit.setAccount(entry.getAccount());
                    debit.setAmount(entry.getAmount());
                    debit.setLayer(entry.getLayer());
                    debit.setDetail(entry.getDetail());
                    debit.setTags(entry.getTags());
                    debit.setTransaction(entry.getTransaction());
                    debit.setId(count);
                    newEntriesList.add(debit);
                }
                transaction.setEntries(newEntriesList);
                entryGrid.setValue(transaction);
                clearFields();
                shouldReverse = true;
                entryGrid.setComponentError(null);
            }
        } else {
            BindingValidationStatus<?> result = (BindingValidationStatus<?>) binder.validate()
                    .getFieldValidationErrors().get(0);
            app.displayNotification(result.getResult().get().getErrorMessage());
        }
        focusOnFirstField();
    };
}

From source file:org.jpos.qi.minigl.TransactionsHelper.java

License:Open Source License

protected boolean saveEntity(Binder binder, EntryGrid entryGrid) throws BLException {
    try {/*from ww w .ja v a2  s . c  om*/
        return (boolean) DB.execWithTransaction(db -> {
            if (binder.writeBeanIfValid(getOriginalEntity())) {
                GLTransaction txn = (GLTransaction) getOriginalEntity();
                List<GLEntry> entries = entryGrid.getValue().getEntries();
                //Reset ids to 0
                for (GLEntry e : entries)
                    e.setId(0);
                txn.setEntries(entries);
                txn.setTimestamp(new Date());
                GLSession glSession = new GLSession(db);
                glSession.post(txn.getJournal(), txn);
                addRevisionCreated(db, getEntityName(), getItemId(getOriginalEntity()));
                return true;
            }
            return false;
        });
    } catch (Exception e) {
        throw new BLException(e.getMessage());
    }
}

From source file:org.jpos.qi.QIHelper.java

License:Open Source License

public boolean saveEntity(Binder binder) throws BLException {
    try {/*from  ww w . j a  v a  2s. co m*/
        return (boolean) DB.execWithTransaction(db -> {
            if (binder.writeBeanIfValid(getOriginalEntity())) {
                db.save(getOriginalEntity());
                addRevisionCreated(db, getEntityName(), getItemId(getOriginalEntity()));
                return true;
            }
            return false;
        });
    } catch (Exception e) {
        getApp().getLog().error(e);
        getApp().displayNotification(getApp().getMessage("errorMessage.unexpected"));
        return false;
    }
}

From source file:org.jpos.qi.sysconfig.SysConfigHelper.java

License:Open Source License

@Override
public boolean saveEntity(Binder binder) throws BLException {
    SysConfig entity = (SysConfig) getOriginalEntity();
    if (binder.writeBeanIfValid(getOriginalEntity())) {
        String id = entity.getId();
        id = prefix != null ? prefix + id : id;
        if (getSysConfig(id) == null) {
            final String finalId = id;
            try {
                return (boolean) DB.execWithTransaction((db) -> {
                    SysConfigManager mgr = new SysConfigManager(db, prefix);
                    mgr.put(entity.getId(), entity.getValue());
                    addRevisionCreated(db, "SYSCONFIG", finalId);
                    return true;
                });/*from  w w  w. j  ava 2s.  co m*/
            } catch (Exception e) {
                QI.getQI().getLog().error(e);
                return false;
            }
        } else {
            throw new BLException("SysConfig " + id + " already exists.");
        }
    } else {
        throw new BLException("Invalid SysConfig");
    }
}