001/* 002 * Copyright 2008 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 */ 016package org.kuali.ole.sys.document.web; 017 018import java.util.HashSet; 019import java.util.List; 020import java.util.Map; 021import java.util.Set; 022 023import org.kuali.ole.sys.businessobject.AccountingLine; 024import org.kuali.ole.sys.document.datadictionary.AccountingLineViewLineDefinition; 025import org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation; 026 027/** 028 * Represents a single table row to be rendered as part of an accounting line view. 029 */ 030public class AccountingLineViewLine implements ReadOnlyable, AccountingLineViewLineFillingElement { 031 private List<RenderableElement> elements; 032 private AccountingLineViewLineDefinition definition; 033 034 /** 035 * Gets the definition attribute. 036 * @return Returns the definition. 037 */ 038 public AccountingLineViewLineDefinition getDefinition() { 039 return definition; 040 } 041 /** 042 * Sets the definition attribute value. 043 * @param definition The definition to set. 044 */ 045 public void setDefinition(AccountingLineViewLineDefinition definition) { 046 this.definition = definition; 047 } 048 049 /** 050 * @see org.kuali.ole.sys.document.web.AccountingLineViewRenderableElementContainer#getName() 051 */ 052 public String getName() { 053 return definition.getElementName(); 054 } 055 /** 056 * Gets the elements attribute. 057 * @return Returns the elements. 058 */ 059 public List<RenderableElement> getElements() { 060 return elements; 061 } 062 /** 063 * Sets the elements attribute value. 064 * @param elements The elements to set. 065 */ 066 public void setElements(List<RenderableElement> fields) { 067 this.elements = fields; 068 } 069 070 /** 071 * Gets the number of actual rows requested (1) 072 * @see org.kuali.ole.sys.document.web.AccountingLineViewRenderableElement#getRequestedRowCount() 073 */ 074 public int getRequestedRowCount() { 075 return getMaxRequestedRowCount(); 076 } 077 078 /** 079 * Determines the max requested row count in the line. 080 * @return the number of rows to create for this line 081 */ 082 protected int getMaxRequestedRowCount() { 083 for (RenderableElement element : elements) { 084 if (element instanceof TableJoiningWithHeader && !element.isHidden()) { 085 return 2; 086 } 087 } 088 return 1; 089 } 090 091 /** 092 * @see org.kuali.ole.sys.document.web.TableJoining#joinRow(org.kuali.ole.sys.document.web.AccountingLineTableRow) 093 */ 094 public void joinRow(AccountingLineTableRow headerRow, AccountingLineTableRow row) { 095 for (RenderableElement element : elements) { 096 if (element instanceof TableJoining) { 097 ((TableJoining)element).joinRow(headerRow, row); 098 } else { 099 headerRow.addCell(createTableCellForNonTableJoining(element)); 100 } 101 } 102 } 103 104 /** 105 * Creates a table cell to wrap the given rendering element 106 * @param element the element to wrap 107 * @return a table cell wrapping that element 108 */ 109 protected AccountingLineTableCell createTableCellForNonTableJoining(RenderableElement element) { 110 AccountingLineTableCell cell = new AccountingLineTableCell(); 111 cell.addRenderableElement(element); 112 cell.setRowSpan(getRequestedRowCount()); 113 return cell; 114 } 115 116 /** 117 * Always throws an exception - a line may only join a table through a parent lines element 118 * @see org.kuali.ole.sys.document.web.TableJoining#joinTable(java.util.List) 119 */ 120 public void joinTable(List<AccountingLineTableRow> rows) { 121 throw new IllegalStateException("Line elements may not join a table directly; the specified rendering is incorrect"); 122 } 123 124 /** 125 * This element should be padded out 126 * @see org.kuali.ole.sys.document.web.AccountingLineViewLineFillingElement#stretchToFillLine() 127 */ 128 public boolean shouldStretchToFillLine() { 129 return false; 130 } 131 132 /** 133 * @see org.kuali.ole.sys.document.web.TableJoining#readOnlyize() 134 */ 135 public void readOnlyize() { 136 for (RenderableElement element : elements) { 137 if (element instanceof ReadOnlyable) { 138 ((ReadOnlyable)element).readOnlyize(); 139 } 140 } 141 } 142 143 /** 144 * @see org.kuali.ole.sys.document.web.ReadOnlyable#isReadOnly() 145 */ 146 public boolean isReadOnly() { 147 for (RenderableElement element : elements) { 148 if (element instanceof ReadOnlyable && !((ReadOnlyable)element).isReadOnly()) { 149 return false; 150 } 151 } 152 return true; 153 } 154 155 /** 156 * @see org.kuali.ole.sys.document.web.TableJoining#removeAllActionBlocks() 157 */ 158 public void removeAllActionBlocks() { 159 Set<RenderableElement> actionBlocksToRemove = new HashSet<RenderableElement>(); 160 for (RenderableElement element : elements) { 161 if (element.isActionBlock()) { 162 actionBlocksToRemove.add(element); 163 } 164 } 165 elements.removeAll(actionBlocksToRemove); 166 } 167 168 /** 169 * @see org.kuali.ole.sys.document.web.TableJoining#removeUnviewableBlocks() 170 */ 171 public void removeUnviewableBlocks(Set<String> unviewableBlocks) { 172 Set<RenderableElement> elementsToRemove = new HashSet<RenderableElement>(); 173 for (RenderableElement element : elements) { 174 if (element instanceof TableJoining) { 175 if (unviewableBlocks.contains(((TableJoining)element).getName())) { 176 elementsToRemove.add(element); 177 } else { 178 ((TableJoining)element).removeUnviewableBlocks(unviewableBlocks); 179 } 180 } 181 } 182 elements.removeAll(elementsToRemove); 183 } 184 185 /** 186 * Shuffles responsibility on to any TableJoining children 187 * @see org.kuali.ole.sys.document.web.TableJoining#readOnlyizeReadOnlyBlocks(java.util.Set) 188 */ 189 public void readOnlyizeReadOnlyBlocks(Set<String> readOnlyBlocks) { 190 for (RenderableElement element : elements) { 191 if (element instanceof TableJoining) { 192 ((TableJoining)element).readOnlyizeReadOnlyBlocks(readOnlyBlocks); 193 } 194 } 195 } 196 197 /** 198 * @see org.kuali.ole.sys.document.web.TableJoining#performFieldTransformation(org.kuali.ole.sys.document.service.AccountingLineFieldRenderingTransformation, org.kuali.ole.sys.businessobject.AccountingLine, java.util.Map, java.util.Map) 199 */ 200 public void performFieldTransformations(List<AccountingLineFieldRenderingTransformation> fieldTransformations, AccountingLine accountingLine, Map unconvertedValues) { 201 for (RenderableElement element : elements) { 202 if (element instanceof TableJoining) { 203 ((TableJoining)element).performFieldTransformations(fieldTransformations, accountingLine, unconvertedValues); 204 } 205 } 206 } 207 208 /** 209 * Finds the number of table cells this line expects to take up 210 * @return the number of displayed table cells this line expects to render as 211 */ 212 public int getDisplayingFieldWidth() { 213 int count = 0; 214 for (RenderableElement element : elements) { 215 if (!element.isHidden()) { 216 if (element instanceof AccountingLineViewField && ((AccountingLineViewField)element).getColSpanOverride() > 1) { 217 count += ((AccountingLineViewField)element).getColSpanOverride(); 218 } else { 219 count += 1; 220 } 221 } 222 } 223 return count; 224 } 225 226 /** 227 * @see org.kuali.ole.sys.document.web.ReadOnlyable#setEditable() 228 */ 229 public void setEditable() { 230 for (RenderableElement element : elements) { 231 if (element instanceof ReadOnlyable) { 232 ((ReadOnlyable)element).setEditable(); 233 } 234 } 235 } 236 237 /** 238 * @see org.kuali.ole.sys.document.web.TableJoining#setEditableBlocks(java.util.Set) 239 */ 240 public void setEditableBlocks(Set<String> editableBlocks) { 241 for (RenderableElement element : elements) { 242 if (element instanceof TableJoining) { 243 ((TableJoining)element).setEditableBlocks(editableBlocks); 244 } 245 } 246 } 247}