package org.jpos.qi.minigl;

import com.vaadin.data.Binder;
import org.jpos.ee.BLException;
import org.jpos.ee.DB;
import org.jpos.gl.Journal;
import org.jpos.gl.Layer;
import org.jpos.qi.QI;
import org.jpos.qi.QIHelper;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

public class LayersHelper extends QIHelper {
    private Journal selectedJournal;

    protected LayersHelper() {
        super(Layer.class);
    }

    @Override
    public Stream getAll(int offset, int limit, Map<String, Boolean> orders) throws Exception {
        List<Layer> all = DB.exec(db -> {
            LayerManager mgr = new LayerManager(db);
            return mgr.getAll(offset,limit,orders);
        });
        return all.stream();
    }

    @Override
    public int getItemCount() throws Exception {
        return DB.exec(db -> {
            LayerManager mgr = new LayerManager(db);
            return mgr.getItemCount();
        });
    }

    @Override
    public String getItemId(Object item) {
        return String.valueOf(((Layer)item).getId());
    }

    @Override
    public boolean updateEntity (Binder binder) throws BLException {
        System.out.println("----------> Will update entity");
        try {
            return DB.execWithTransaction((db) -> {
                Layer oldLayer = (Layer) ((Layer) getOriginalEntity()).clone();
                binder.writeBean(getOriginalEntity());
                Layer l = (Layer) getOriginalEntity();
                db.session().merge(l);
                return addRevisionUpdated(db, getEntityName(),
                        String.valueOf(l.getId()),
                        oldLayer,
                        l,
                        new String[]{"id", "journal", "name"});
            });
        } catch (Exception e) {
            QI.getQI().getLog().error(e);
            return false;
        }
    }

    public Object getEntityByParam (String param)  {
        //most id are longs, if id is of other type this method must be overridden
        Journal j = selectedJournal;
        if (j != null) {
            try {
                return DB.exec(db -> {
                    db.session().refresh(j);
                    for (Layer layer : (Set<Layer>)j.getLayers()) {
                        if (layer.getId() == Short.parseShort(param))
                            return layer;
                    }
                    return null;
                });
            } catch (Exception e) {
                getApp().getLog().error(e);
                return null;
            }
        }
        return null;
    }

    public List<Journal> getAllJournals() {
        try {
            return DB.exec(db -> {
                JournalManager mgr = new JournalManager(db);
                return mgr.getAll();
            });
        } catch (Exception e) {
            getApp().getLog().error(e);
            return null;
        }
    }

    public Journal getJournal (String journalId) throws Exception {
        if (selectedJournal != null)
            return selectedJournal;
        selectedJournal = DB.exec(db -> {
            JournalManager mgr = new JournalManager(db);
            return mgr.getItemByParam("id",journalId,false);
        });
        return selectedJournal;
    }

    public void setSelectedJournal(Journal selectedJournal) {
        this.selectedJournal = selectedJournal;
    }
}

@@ -1364,70 +1364,8 @@
 n %7B%0A
-        System.out.println(%22----------%3E Will update entity%22);%0A
     

Remove system.out