Package reportgen.ren.report.userinput

Source Code of reportgen.ren.report.userinput.QueryInputValue

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

package reportgen.ren.report.userinput;

import reportgen.prototype.context.group.ContextGroup;
import reportgen.utils.ReportException;
import java.util.ArrayList;
import java.util.Iterator;
import org.jdom.Element;
import reportgen.prototype.context.Context;
import reportgen.math.ContextFilter;
import reportgen.math.MathFactory;
import reportgen.math.constants.MathExpressionConstAbstract;
import reportgen.prototype.utils.ItemListRooted;
import reportgen.prototype.utils.RowCount;
import reportgen.utils.Atom;
import reportgen.utils.XML;

/**
* Входное значение, задаваемое пользователем.
* Может быть скалярное - изменяемое целое, вещественное, строка, дата
* и векторное, преобразуемое в скалярное пользователем - ОДИНОЧНЫЙ выбор из множества
* @author axe
*/
public class QueryInputValue
        extends ItemListRooted<MathExpressionConstAbstract, Context> {

    private static final String ATTR_TITLE = "seltitle";
    private static final String ATTR_DESC = "seldesc";
    private static final String ATTR_CONST = "const";
    private static final String ATTR_CANBEOMITTED = "canBeOmitted";

    public static final String TAG = "input";

    private final Atom atom;
    /**
     * Название при выборе строк
     */
    private String selectTitle;

    /**
     * Описание при выборе строк
     */
    private String selectDescription;

    /**
     * Спрашивать при построении отчета
     */
    private boolean constant;

    /**
     * Активных строк
     */
    private final RowCount activeRows = RowCount.SINGLE;

    /**
     *
     */
    private boolean canBeOmitted = false;
    private boolean omitted = false;

    public QueryInputValue() {
        selectDescription = "нет описания";
        selectTitle = "Название параметра";
        atom = new Atom();
    }

    public QueryInputValue(Element element, Context context) throws ReportException {
        super(element, context);
        atom = new Atom(element, context);
        selectTitle = XML.getStringAttribute(element, ATTR_TITLE);
        selectDescription = XML.getStringAttribute(element, ATTR_DESC, "", false);

        canBeOmitted = XML.getBoolAttribute(element, ATTR_CANBEOMITTED, false, false);
        constant = XML.getBoolAttribute(element, ATTR_CONST, false, false);

        if(size() == 0) {
            throw new ReportException("Пустой входной параметр: " + selectTitle);
        }
    }


    @Override
    protected void toXML(Element root) {
        super.toXML(root);
        atom.toXML(root);
        root.setAttribute(ATTR_TITLE, selectTitle);
        if(selectDescription.length() > 0) {
            root.setAttribute(ATTR_DESC, selectDescription);
        }
        if(constant) {
            root.setAttribute(ATTR_CONST, new Boolean(constant).toString());
        }
        if(canBeOmitted) {
            root.setAttribute(ATTR_CANBEOMITTED, new Boolean(canBeOmitted).toString());
        }
    }

    @Override
    protected MathExpressionConstAbstract loadChild(Element root, final Context context)
            throws ReportException {
        return (MathExpressionConstAbstract) MathFactory.fromXml(root, context, new ContextFilter() {

            @Override
            public Context getChildContext(ContextGroup group) {
                return context;
            }
        });
    }

    public Atom getAtom() {
        return atom;
    }

   
    @Override
    protected boolean isItemContains(MathExpressionConstAbstract item, Object obj) {
        return false;
    }

    @Override
    protected void validateItem(MathExpressionConstAbstract item) throws ReportException {
        //
    }

    @Override
    public String getRootTag() {
        return TAG;
    }

    public Class getCls() throws ReportException {
        return get(0).getCls();
    }

    @Override
    public String toString() {
        String msg = selectTitle;
        if(size() == 1) {
            return  msg + "(" + getList().toString() + ")";
        }
        if(!constant) {
            msg += "(выбор из ";
        } else {
            msg += "(";
        }
        Iterator<MathExpressionConstAbstract> it = getList().iterator();
        int i=0;
        while(it.hasNext()) {
            msg += it.next().toString();
            i++;           
            if(it.hasNext()) {
                if(i>2) {
                    msg += ", ...";
                    break;
                } else {
                    msg += ",";
                }
            }
        }
        return msg + ")";
    }

    /**
     * @return the selectTitle
     */
    public String getSelectTitle() {
        return selectTitle;
    }

    /**
     * @param selectTitle the selectTitle to set
     */
    public void setSelectTitle(String selectTitle) {
        this.selectTitle = selectTitle;
    }

    /**
     * @return the selectDescription
     */
    public String getSelectDescription() {
        return selectDescription;
    }

    /**
     * @param selectDescription the selectDescription to set
     */
    public void setSelectDescription(String selectDescription) {
        this.selectDescription = selectDescription;
    }

    /**
     * @return the askOnReport
     */
    public boolean isConstant() {
        return constant;
    }

    /**
     * @param askOnReport the askOnReport to set
     */
    public void setConstant(boolean constant) {
        this.constant = constant;
    }

    /**
     *
     * @return
     */
    public boolean isCanBeOmitted() {
        return canBeOmitted;
    }

    public void setCanBeOmitted(boolean canBeOmitted) {
        this.canBeOmitted = canBeOmitted;
    }

    public boolean isOmitted() {
        return omitted;
    }

    /**
     * @return the value
     */
    public MathExpressionConstAbstract getValue() {
        if(isOmitted()) {
            return null;
        }
        return get(0);
    }

    public MathExpressionConstAbstract[] getValues() {
        MathExpressionConstAbstract[] array = new MathExpressionConstAbstract[size()];
        getList().toArray(array);
        return array;
    }

    /**
     * @param value the value to set
     */
    public void addValue(MathExpressionConstAbstract value) throws ReportException {
        if(size() != 0) {
            if(!value.getCls().equals(get(0).getCls())) {
                throw new ReportException("Добавляемый тип и тип первого элемента не совпадают");
            }
        }
        add(value);
    }

    public void removeValue(MathExpressionConstAbstract value) throws ReportException {
        remove(value);
    }

    /**
     *
     * @return
     * @throws reportgen.ren.exception.ReportException
     */
    public UserInput getUserInput() throws ReportException {
        UserInput ui = null;
        if(size() == 1) {
            Object val = get(0).getValue(null);
            ui = new UserInputEditable(TAG, selectTitle, selectDescription, val, canBeOmitted);

        } else {
            ArrayList<Object> vals = new ArrayList<Object>();
            for(int i=0; i<size(); i++) {
                vals.add(get(i).getValue(null));
            }

            ArrayList<Integer> selected = new ArrayList<Integer>();
            selected.add(0);
            ui = new UserInputSelect(TAG, selectTitle, selectDescription,
                    selected, vals, activeRows, canBeOmitted);
        }

        return ui;
    }

    /**
     * Данные вернулись от пользователя, необходимо их обновить
     * @param ui
     * @throws reportgen.ren.exception.ReportException
     */
    public void setUserInput(UserInput ui) throws ReportException {
        if(!ui.isSelected()) {
            if(!ui.isCanBeOmitted()) {
                throw new ReportException("Пользователь не сделал выбор");
            }
            omitted = true;

        } else if(size() > 1) {
            UserInputSelect uis = (UserInputSelect) ui;
            ArrayList<Integer> selected = uis.getSelected();
            int selIndex = selected.get(0);
            if(selIndex >= size()) {
                throw new ReportException("Пользователь сделал некорректный выбор");
            }

            if(selIndex != 0) {
                while(selIndex != 0) {
                    selIndex = moveUp(selIndex);
                }
            }
            omitted = false;
        } else {
            UserInputEditable uie = (UserInputEditable) ui;
            MathExpressionConstAbstract val = get(0);
            if(!val.getCls().equals(uie.getValue().getClass())) {
                throw new ReportException("Попытка ввода данных несоответствующего типа");
            }
            Object newValue = uie.getValue();
            MathExpressionConstAbstract newVal = val.make(newValue);
            insertBefore(val, newVal);
            remove(val);
            omitted = false;
        }
    }
}
TOP

Related Classes of reportgen.ren.report.userinput.QueryInputValue

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.