package com.fp.base.persistence.util.formula;

import java.util.List;

import javax.persistence.Query;

import org.nfunk.jep.function.PostfixMathCommand;

import com.fp.common.formula.FormulaEvaluator;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.pgeneral.gene.TgeneFunctions;

public class FormulaManager {
    private FormulaEvaluator fe;

    public FormulaManager() throws Exception {
        this.fe = new FormulaEvaluator();
        this.addMaiaFunctions();
    }

    private void addMaiaFunctions() throws Exception {
        Query qry = PersistenceHelper.createQuery("from com.fp.persistence.pgeneral.gene.TgeneFunctions p where p.active='Y'");
        List<TgeneFunctions> lF = qry.getResultList();
        for (TgeneFunctions f : lF) {
            this.fe.setFunction(f.getPk(), (PostfixMathCommand) Class.forName(f.getClassname()).newInstance());
        }
    }

    public FormulaEvaluator getEvaluator() {
        return this.fe;
    }
}
