/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2018 jPOS Software SRL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.qi.minigl;

import com.vaadin.data.Converter;
import com.vaadin.data.Result;
import com.vaadin.data.ValueContext;
import org.jpos.ee.BLException;
import org.jpos.ee.DB;
import org.jpos.gl.Account;
import org.jpos.gl.FinalAccount;
import org.jpos.gl.GLSession;
import org.jpos.qi.QI;


/**
 * Created by alcarraz on 15/09/15.
 */
public class AccountConverter implements Converter<String, Account> {

    private boolean createNew;
    private boolean required;

    public AccountConverter() {
        //By default we allow to leave the field empty but if there is a value we require that an account exists
        this(false, false);
    }

    public AccountConverter (boolean required, boolean createNew) {
        super();
        this.required = required;
        this.createNew = createNew;
    }

    @Override
    public Result<Account> convertToModel(String value, ValueContext valueContext) {
        if (value != null && !value.isEmpty()) {
            try {
                Account acct = (Account) DB.exec(db -> {
                    GLSession session = new GLSession(db);
                    Account res = session.getAccount("jcard",value);
                    if (res == null && createNew) {
                        res = new FinalAccount();
                        res.setCode(value);
                    } else if (res == null) {
                        throw new BLException("Invalid Account Code");
                    }
                    return res;
                });
                return Result.ok(acct);
            } catch (Exception e) {
                return Result.error(e.getMessage());
            }
        }
        if (required)
            return Result.error(QI.getQI().getMessage("errorMessage.req", QI.getQI().getMessage("account")));
        else
            return Result.ok(null);
    }

    @Override
    public String convertToPresentation(Account value, ValueContext context) {
        return (value == null) ? "" : value.getCode();
    }
}

@@ -956,32 +956,69 @@
 pos.gl.Account;%0A
+import org.jpos.gl.CompositeAccount;%0A
 import org.jpos.
@@ -1026,32 +1026,32 @@
 l.FinalAccount;%0A
-
 import org.jpos.
@@ -1231,24 +1231,57 @@
  createNew;%0A
+    private boolean createFinal;%0A
     private 
@@ -1539,32 +1539,282 @@
 an createNew) %7B%0A
+        //By default we allow to leave the field empty but if there is a value we require that an account exists%0A        this(required, createNew, true);%0A    %7D%0A%0A    public AccountConverter (boolean required, boolean createNew, boolean createFinal) %7B%0A
         super();
@@ -1880,24 +1880,64 @@
  createNew;%0A
+        this.createFinal = createFinal;%0A
     %7D%0A%0A    @
@@ -2362,23 +2362,155 @@
     
-res = new Final
+if (createFinal)%0A                            res = new FinalAccount();%0A                        else%0A                            res = new Composite
 Acco

Allow converter to create a CompositeAccount