001package com.identityworksllc.iiq.common.table;
002
003import sailpoint.tools.GeneralException;
004import sailpoint.tools.Util;
005
006import java.util.ArrayList;
007import java.util.List;
008
009/**
010 * A class representing a row in an HTML table. Rows can be header rows or not,
011 * can have specific classes or styles applied, and contain Cells.
012 */
013public class Row extends Element implements StyleTarget {
014    /**
015     * The list of cells in this row
016     */
017    private List<Cell> cells;
018
019    /**
020     * True if this is a header row
021     */
022    private boolean header;
023
024    /**
025     * Options to be applied to any future cell added to this row
026     */
027    private List<CellOption> options;
028
029    /**
030     * Constructs a new row with all empty data
031     */
032    public Row() {
033        this.cells = new ArrayList<>();
034        this.cssClasses = new ArrayList<>();
035        this.header = false;
036        this.style = "";
037        this.options = new ArrayList<>();
038    }
039
040    /**
041     * Adds a new cell to the end of this row. THe cell will be modified according
042     * to the cell options set via setOptions if any are set.
043     * @param c The cell
044     * @throws GeneralException if any of the CellOptions specified throw an error
045     */
046    public void add(Cell c) throws GeneralException {
047        if (!Util.isEmpty(this.options)) {
048            for(CellOption option : this.options) {
049                option.accept(c);
050            }
051        }
052        this.cells.add(c);
053    }
054
055    public List<Cell> getCells() {
056        return cells;
057    }
058
059    public boolean isHeader() {
060        return header;
061    }
062
063    /**
064     * Renders this row (and all of its Cells) to HTML, writing to the provided StringBuilder
065     * @param builder The StringBuilder to write to
066     */
067    public void render(StringBuilder builder) {
068        builder.append("<tr");
069        if (!this.cssClasses.isEmpty()) {
070            builder.append(" class=\"").append(getEscapedCssClassAttr()).append("\"");
071        }
072        if (Util.isNotNullOrEmpty(style)) {
073            builder.append(" style=\"").append(getEscapedStyle()).append("\"");
074        }
075        builder.append(">");
076
077        for(Cell c : cells) {
078            if (c != null) {
079                c.render(builder, this);
080            }
081        }
082
083        builder.append("</tr>");
084    }
085
086    public void setCells(List<Cell> cells) {
087        this.cells = cells;
088    }
089
090    public void setHeader(boolean header) {
091        this.header = header;
092    }
093
094    public void setOptions(List<CellOption> options) {
095        this.options = options;
096    }
097}