OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 181 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.openconcerto.modules.common.batchprocessing;

import java.util.List;

import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.element.SQLElementDirectory;
import org.openconcerto.sql.model.SQLField;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.request.SQLFieldTranslator;

public class BatchField {

    private final SQLField field;
    private final SQLRowAccessor foreignLinkRow;
    private final SQLFieldTranslator translator;
    private final SQLElement elementLink;
    private final SQLElementDirectory dir;

    public BatchField(SQLElementDirectory dir, SQLField field, SQLRowAccessor foreignLinkRow) {
        this.field = field;
        this.dir = dir;
        this.foreignLinkRow = foreignLinkRow;

        this.translator = dir.getTranslator();
        if (foreignLinkRow == null) {
            this.elementLink = null;
        } else {
            this.elementLink = dir.getElement(foreignLinkRow.getTable());
        }
    }

    private SQLField fieldToMatch;
    private Object matchingValue;

    public void setDefaultMatchingValue(SQLField fieldToMatch, Object value) {
        this.fieldToMatch = fieldToMatch;
        this.matchingValue = value;
    }

    public SQLField getField() {
        return field;
    }

    public SQLRowAccessor getForeignLinkRow() {
        return foreignLinkRow;
    }
    
    public SQLField getFieldToMatch() {
        return fieldToMatch;
    }
    
    public Object getMatchingValue() {
        return matchingValue;
    }

    public String getComboName() {
        if (this.fieldToMatch != null) {
            return this.dir.getElement(this.fieldToMatch.getTable()).getPluralName() + " " + this.translator.getLabelFor(this.field) + " " + this.matchingValue;
        } else if (this.foreignLinkRow == null) {
            return this.translator.getLabelFor(this.field);
        } else {
            return this.elementLink.getPluralName() + " " + this.foreignLinkRow.getString("NOM") + " " + this.translator.getLabelFor(this.field);
        }
    }

    public List<SQLRow> getReferentRows(SQLRowAccessor rowOrigin) {
        SQLSelect sel = new SQLSelect();
        sel.addSelectStar(this.field.getTable());
        final Where w = new Where(this.field.getTable().getField("ID_" + rowOrigin.getTable().getName()), "=", rowOrigin.getID());
        sel.setWhere(w.and(new Where(this.field.getTable().getField("ID_" + foreignLinkRow.getTable().getName()), "=", foreignLinkRow.getID())));
        return SQLRowListRSH.execute(sel);
    }

}