OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | 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;

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

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

    public SQLField getField() {
        return field;
    }

    public SQLRowAccessor getForeignLinkRow() {
        return foreignLinkRow;
    }

    public String getComboName() {
        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);
    }

}