package com.awspaas.user.apps.donenow_ctt.at;

import com.actionsoft.bpms.bo.design.cache.BOCache;
import com.actionsoft.bpms.bo.design.model.BOItemModel;
import com.actionsoft.bpms.bo.design.model.BOModel;
import com.actionsoft.bpms.commons.at.AbstExpression;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.AWSExpressionException;
import org.apache.commons.lang3.StringUtils;

public class getFormField extends AbstExpression {
    public getFormField(ExpressionContext atContext, String expressionValue) {
        super(atContext, expressionValue);
    }

    /**
     *
     * @param s
     * @return
     * @throws AWSExpressionException
     */
    @Override
    public String execute(String s) throws AWSExpressionException {
        String boName = getParameter(s, 1).trim();
        String fieldName = getParameter(s, 2).trim();
        String boIdOrBindid = getParameter(s, 3).trim();

        boName = boName.toUpperCase();
        fieldName = fieldName.toUpperCase();

        if (StringUtils.isBlank(boName) || StringUtils.isBlank(fieldName) || StringUtils.isBlank(boIdOrBindid) || !boName.startsWith("BO_EU_DN"))
            return null;

        BOModel boModel = BOCache.getInstance().getModelByEntityName(boName);
        if (boModel == null) {
            return null;
        }
        boName = boModel.getName().toUpperCase();

        if (!boName.startsWith("BO_EU_"))
            return null;

        BOItemModel boItemModel = boModel.getItem(fieldName);
        if (boItemModel == null)
            return null;
        fieldName = boItemModel.getName();
        if (boName.equals("BO_EU_DND_GENERAL"))
            return DBSql.getString("select " + fieldName + " FROM " + boName + " where (ID =? OR OID=?) ", new Object[]{boIdOrBindid, boIdOrBindid});
        else
            return DBSql.getString("select " + fieldName + " FROM " + boName + " where (ID =? OR BINDID=?)", new Object[]{boIdOrBindid, boIdOrBindid});
    }
}

