Package reportgen.math.condition

Source Code of reportgen.math.condition.MathExpressionConditionUnary

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package reportgen.math.condition;

import reportgen.math.reference.operator.MathExpressionOperatorRef;
import java.util.Set;
import reportgen.prototype.stream.SQLStream;
import java.util.Iterator;
import java.util.Map;
import org.jdom.Element;
import reportgen.math.ContextFilter;
import reportgen.math.MathExpressionOperand;
import reportgen.math.MathFactory;
import reportgen.prototype.context.Context;
import reportgen.math.reference.operator.variants.unary.MathOperatorUnary;
import reportgen.prototype.UsedReportableType;
import reportgen.utils.ReportException;

/**
* Условие состоит из двух операндов и одного оператора.
* Операнды могут быть составными математическими выражениями.
* Оператор является булевым оператором сравнения
* @author axe
*/

public abstract class MathExpressionConditionUnary extends MathExpressionOperand
implements ContextFilter {

    private MathExpressionOperand operand;
    private MathExpressionOperatorRef operator;

    public MathExpressionConditionUnary(Context context) {
        super(context);
    }

    public MathExpressionConditionUnary(Context context, MathExpressionOperand operand,
            MathExpressionOperatorRef operator) {
        super(context);
        this.operand = operand;
        this.operator = operator;
    }

    public MathExpressionConditionUnary(Element element, Context context) throws ReportException {
        super(element, context);

        operator = new MathExpressionOperatorRef(
                get(element, MathExpressionOperatorRef.TAG),
                getChildContext(MathExpressionOperatorRef.GROUP)
        );

        Iterator children = element.getChildren().iterator();
        while (children.hasNext() && operand == null) {
            Element iElement = (Element) children.next();
            if(iElement.getName().equals(MathExpressionOperatorRef.TAG)) {
                continue;
            }
            operand = (MathExpressionOperand) MathFactory.fromXml(iElement, getLocalContext(), this);
        }
        if(operand == null) {
            throw new ReportException("В унарном выражении отсутствует операнд");
        }
    }

    @Override
    protected void toXML(Element root)  {
        root.addContent(operator.toXML());
        root.addContent(operand.toXML());
    }

    public MathExpressionOperand getOperand() {
        return operand;
    }

    public void setOperand(MathExpressionOperand operand) {
        this.operand = operand;
    }

    final public MathExpressionOperatorRef getOperator() {
        return operator;
    }

    protected void setOperator(MathExpressionOperatorRef operator) {
        this.operator = operator;
    }

    @Override
    public void appendToQuery(SQLStream mql, Map model) throws ReportException {
        //mql.append("(");

        MathOperatorUnary opcode = (MathOperatorUnary) operator.getRef();
        if(opcode.operatorFirst()) {
            operator.appendToQuery(mql, model);
            operand.appendToQuery(mql, model);
        } else {
            operand.appendToQuery(mql, model);
            operator.appendToQuery(mql, model);
        }
        //mql.append(")");
    }

    @Override
    public Class getCls() throws ReportException {
        MathOperatorUnary unary = (MathOperatorUnary) operator.getRef();
        return unary.checkAvailiable(operand.getCls());
    }

    @Override
    public Object getValue(Map constants) throws ReportException {
        MathOperatorUnary unary = (MathOperatorUnary) operator.getRef();
        return unary.getValue(operand.getValue(constants));
    }

    @Override
    public void buildUsedSet(UsedReportableType cls, Set set) {
        operand.buildUsedSet(cls, set);
    }

    @Override
    public boolean containsMissingVariables() {
        return operand.containsMissingVariables();
    }

    @Override
    public String toString() {
        MathOperatorUnary opcode = (MathOperatorUnary) operator.getRef();
        if(opcode.operatorFirst()) {
            return operator.toString() + operand.toString();
        }
        return operand.toString() + operator.toString();
    }

    @Override
    public void validate() throws ReportException {
        super.validate();
        operator.validate();
        operand.validate();
        MathOperatorUnary un = (MathOperatorUnary) operator.getRef();
        un.checkAvailiable(operand.getCls());
    }

    @Override
    public boolean isContain(Object entity) {
        return operand.isContain(entity);
    }
}
TOP

Related Classes of reportgen.math.condition.MathExpressionConditionUnary

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.