1 4 package com.calipso.reportgenerator.reportcalculator; 5 6 import com.calipso.reportgenerator.common.LanguageTraslator; 7 import com.calipso.reportgenerator.common.InfoException; 8 9 import java.util.Collection ; 10 import java.util.Vector ; 11 import java.util.Iterator ; 12 13 14 23 public class DataSource implements IDataSource { 24 25 private Vector names; 26 private Vector rows; 27 private int columsCount = 0; 28 private final String COLUMNS_DEFAULT_NAME = "NONAME"; 29 30 31 37 public DataSource(int columns) throws InfoException { 38 if (columns <= 0) { 39 throw new InfoException("INFO - No se puede crear una matriz DataSource. " + columns + "<=0"); 40 } else { 41 this.columsCount = columns; 42 this.names = this.generateVector(this.COLUMNS_DEFAULT_NAME,columns); 43 this.rows = new Vector (this.columsCount); 44 } 45 } 46 47 54 public DataSource(Collection names) throws InfoException { 55 if (names == null) { 56 throw new InfoException("INFO - No se puede construir una matriz DataSource a partir de una colección de nombres nula"); 57 } else if (this.namesEmpty(names)) { 58 throw new InfoException("INFO - No se puede construir una matriz DataSource a partir de un names vacío"); 59 } else if (this.namesWithNullElements(names)) { 60 throw new InfoException("INFO - No se puede construir una matriz DataSource con elementos null en los nombres"); 61 } else if (this.namesWithNoStringElements(names)) { 62 throw new InfoException("INFO - No se puede construir una matriz DataSource con nombres que no son de tipo String"); 63 } else { 64 this.columsCount = names.size(); 65 this.names = new Vector (names); 66 this.rows = new Vector (this.columsCount); 67 } 68 69 } 70 71 75 public int getRowCount() { 76 return this.rows.size(); 77 } 78 79 83 public int getColumCount() { 84 return this.columsCount; 85 } 86 87 93 public String getColumName(int colum) throws IndexOutOfBoundsException { 94 if (!this.validateColumNumber(colum)) { 95 throw new IndexOutOfBoundsException ("INFO - La columna " + colum + " esta fuera de las coordenadas de este DataSource. Rango permitido [0.." + (this.columsCount-1) + "]"); 96 } else { 97 return this.names.elementAt(colum).toString(); 98 } 99 } 100 101 108 public void setColumName(int colum, String name) throws IndexOutOfBoundsException , InfoException { 109 if (!this.validateColumNumber(colum)) { 110 throw new IndexOutOfBoundsException ("INFO - La columna " + colum + " esta fuera de las coordenadas de este DataSource. Rango permitido [0.." + (this.columsCount-1) + "]"); 111 } else if (name == null) { 112 throw new InfoException("INFO - Las columnas no permiten valores nulos"); 113 } else { 114 this.names.setElementAt(name,colum); 115 } 116 } 117 118 125 public Object getValueAt(int row, int colum) throws IndexOutOfBoundsException { 126 if (this.validateRowNumber(row) && this.validateColumNumber(colum)) { 127 return ((Vector )this.rows.elementAt(row)).elementAt(colum); 128 } else { 129 throw new IndexOutOfBoundsException ("INFO - La fila " + row + " y la columna " + colum + " esta fuera de las coordenadas de este DataSource. Rango permitido [0.." + (this.getRowCount()-1) + "],[0.." + (this.columsCount-1) + "]"); 130 } 131 } 132 133 139 public Collection getColumValues(int colum) throws IndexOutOfBoundsException { 140 if (this.validateColumNumber(colum)) { 141 Vector data; 142 Vector values = new Vector (); 143 144 for (int i = 0; i < this.rows.size(); i++) { 145 data = (Vector )this.rows.elementAt(i); 146 values.addElement(data.elementAt(colum)); 147 } 148 149 return values; 150 } else { 151 throw new IndexOutOfBoundsException ("INFO - La columna " + colum + " esta fuera de las coordenadas de este DataSource. Rango permitido [0.." + (this.columsCount-1) + "]"); 152 } 153 } 154 155 161 public Collection getRowValues(int row) throws IndexOutOfBoundsException { 162 if (this.validateRowNumber(row)) { 163 return (Vector )this.rows.elementAt(row); 164 } else { 165 throw new IndexOutOfBoundsException ("INFO - La fila " + row + " esta fuera de las coordenadas de este DataSource. Rango permitido [0.." + (this.getRowCount()-1) + "]"); 166 } 167 } 168 169 176 public void updateValueAt(int row, int colum, Object newValue) throws IndexOutOfBoundsException { 177 if (this.validateRowNumber(row) && this.validateColumNumber(colum)) { 178 ((Vector )this.rows.elementAt(row)).setElementAt(newValue,colum); 179 } else { 180 throw new IndexOutOfBoundsException ("INFO - La fila " + row + " y la columna " + colum + " esta fuera de las coordenadas de este DataSource. Rango permitido [0.." + (this.getRowCount()-1) + "],[0.." + (this.columsCount-1) + "]"); 181 } 182 } 183 184 189 public void addRow(Collection row) throws InfoException { 190 if (row == null) { 191 throw new InfoException(LanguageTraslator.traslate("380")); 192 } else if (row.size() != this.columsCount) { 193 throw new InfoException(LanguageTraslator.traslate("381") + this.columsCount + LanguageTraslator.traslate("382") + row.size()); 194 } else { 195 this.rows.addElement(new Vector (row)); 196 } 197 } 198 199 205 private Vector generateVector(Object value, int elements) { 206 Vector vector = new Vector (); 207 208 for (int i=0; i<elements; i++) { 209 vector.addElement(value); 210 } 211 return vector; 212 } 213 214 220 private boolean validateColumNumber(int number) { 221 if ( (number < 0) || (number >= this.columsCount) ){ 222 return false; 223 } else { 224 return true; 225 } 226 } 227 228 234 private boolean validateRowNumber(int number) { 235 if ( (number < 0) || (number >= this.rows.size()) ){ 236 return false; 237 } else { 238 return true; 239 } 240 } 241 242 247 private boolean namesEmpty(Collection coll) { 248 return coll.isEmpty(); 249 } 250 251 256 private boolean namesWithNullElements(Collection coll) { 257 boolean withNulls = false; 258 259 Iterator it = coll.iterator(); 260 261 while ((it.hasNext())&&(!withNulls)) { 262 if (it.next() == null) { 263 withNulls = true; 264 } 265 } 266 return withNulls; 267 } 268 269 270 275 private boolean namesWithNoStringElements(Collection coll) { 276 boolean withNoStringElements = false; 277 278 Iterator it = coll.iterator(); 279 280 while ((it.hasNext())&&(!withNoStringElements)) { 281 if (!( it.next() instanceof String )) { 282 withNoStringElements = true; 283 } 284 } 285 return withNoStringElements; 286 } 287 288 } 289 | Popular Tags |