001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys.document.service.impl;
017    
018    import java.util.HashSet;
019    import java.util.List;
020    import java.util.Set;
021    
022    import org.kuali.kfs.sys.document.service.AccountingLineTableTransformation;
023    import org.kuali.kfs.sys.document.web.AccountingLineTableCell;
024    import org.kuali.kfs.sys.document.web.AccountingLineTableRow;
025    
026    /**
027     * Rendering transformation which removes any empty cells and rows from a table
028     */
029    public class RemoveEmptyCellsAndRowsRenderingTransformationImpl implements AccountingLineTableTransformation {
030    
031        /**
032         * Removes any empty cells and rows from this table
033         * @param rows the table rows to render
034         */
035        public void transformRows(List<AccountingLineTableRow> rows) {
036            Set<AccountingLineTableRow> rowsToRemove = new HashSet<AccountingLineTableRow>();
037            for (AccountingLineTableRow row : rows) {
038                removeEmptyCells(row);
039                if (row.isEmpty()) {
040                    rowsToRemove.add(row);
041                }
042            }
043            rows.removeAll(rowsToRemove);
044        }
045        
046        /**
047         * Removes all empty cells from a given row
048         * @param row the row to remove empty cells from
049         */
050        public void removeEmptyCells(AccountingLineTableRow row) {
051            Set<AccountingLineTableCell> cellsToRemove = new HashSet<AccountingLineTableCell>();
052            for (AccountingLineTableCell cell : row.getCells()) {
053                if (cell.isEmpty()) {
054                    cellsToRemove.add(cell);
055                }
056            }
057            row.getCells().removeAll(cellsToRemove);
058        }
059    }