1 package com.calipso.reportgenerator.userinterface; 2 3 import com.calipso.reportgenerator.reportdefinitions.types.*; 4 import com.calipso.reportgenerator.reportdefinitions.ReportView; 5 import com.calipso.reportgenerator.reportdefinitions.DimensionProperty; 6 import com.calipso.reportgenerator.reportdefinitions.*; 7 import com.calipso.reportgenerator.reportcalculator.expression.*; 8 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 9 import com.calipso.reportgenerator.reportcalculator.SharedInteger; 10 import com.calipso.reportgenerator.common.*; 11 12 import java.util.*; 13 import java.awt.*; 14 import java.text.DateFormat ; 15 import java.text.SimpleDateFormat ; 16 import java.text.ParseException ; 17 18 import com.calipso.reportgenerator.common.InfoException; 19 20 25 28 public class PivotTableProperties { 29 private ArrayList columnProperties; 30 private boolean withTotal; 31 private ArrayList visibleMetrics; 32 private ColorConditionManager conditionManager; 33 private ReportResult reportResult; 34 private Map params; 35 private Map metricStates; 36 private final int DEFAULT_COLUMN_WIDTH = 75; 37 38 41 public PivotTableProperties() { 42 columnProperties = new ArrayList(); 43 visibleMetrics = new ArrayList(); 44 } 45 46 public PivotTableProperties(ReportResult reportResult) throws InfoException { 47 columnProperties = new ArrayList(); 48 visibleMetrics = new ArrayList(); 49 this.reportResult = reportResult; 50 initTableProperties(); 51 } 52 53 58 public void assignFromView(ReportView reportView) throws InfoException { 59 overrideDimensionProperties(reportView); 60 overrideMetricsProperties(reportView); 61 overrideLightBoxerProperties(reportView); 62 this.withTotal = reportView.getVisibleTotals(); 63 } 64 65 69 private void initTableProperties() throws InfoException { 70 try { 71 ArrayList rowList = (ArrayList) reportResult.getReportQuery().getDimensionsByLocation(DimensionDefinitionLocationType.ROW); 72 fillColumnsProperties(rowList, DimensionDefinitionLocationType.ROW.toString()); 73 ArrayList pageList = (ArrayList) reportResult.getReportQuery().getDimensionsByLocation(DimensionDefinitionLocationType.PAGE); 74 fillColumnsProperties(pageList, DimensionDefinitionLocationType.PAGE.toString()); 75 ArrayList columnList = (ArrayList) reportResult.getReportQuery().getDimensionsByLocation(DimensionDefinitionLocationType.COLUMN); 76 fillColumnsProperties(columnList, DimensionDefinitionLocationType.COLUMN.toString()); 77 } 78 catch (Exception e) { 79 throw new InfoException(LanguageTraslator.traslate("227")); 80 } 81 getMetricStates(); 82 } 83 84 88 public Map getMetricStates() { 89 if (metricStates == null) { 90 metricStates = new HashMap(); 91 loadAllMetrics(metricStates); 92 } 93 return metricStates; 94 } 95 96 100 private void loadAllMetrics(Map states) { 101 java.util.List list = reportResult.getReportQuery().getMetrics(); 102 for (int index = 0; index < list.size(); index++) { 103 QueryMetric queryMetric = (QueryMetric) list.get(index); 104 ReportMetricSpec metricSpec = reportResult.getMetricFromName(queryMetric.getName()); 105 MetricState metricState = new MetricState(metricSpec); 106 metricState.setVisible(queryMetric.getVisible()); 107 states.put(metricSpec.getName(), metricState); 108 setVisibleMetrics(metricState); 109 } 110 } 111 112 118 private void fillColumnsProperties(ArrayList list, String location) throws InfoException { 119 Iterator iterator = list.iterator(); 120 while (iterator.hasNext()) { 121 QueryDimension dimension = (QueryDimension) iterator.next(); 122 ColumnProperties properties = new ColumnProperties(); 123 properties.setColumnName(dimension.getName()); 124 properties.setWidth(DEFAULT_COLUMN_WIDTH); 125 int indexDimension = reportResult.getReportQuery().getQueryDimensionFromName(properties.getColumnName()).getIndex(); 126 if(reportResult.getReportQuery().getExcludedValues(indexDimension) != null){ 127 properties.setExcludeValue(reportResult.getReportQuery().getExcludedValues(indexDimension)); 128 } else { 129 properties.setExcludeValue(new TreeSet()); 130 } 131 properties.setOrder(reportResult.getReportQuery().getQueryDimensionFromName(properties.getColumnName()).getOrder()); 132 properties.setLocation(location); 133 properties.setRankMetricName(reportResult.getReportQuery().getQueryDimensionFromName(properties.getColumnName()).getRankMetricName()); 134 properties.setOrder(reportResult.getReportQuery().getQueryDimensionFromName(properties.getColumnName()).getOrder()); 135 getColumnProperties().add(properties); 136 } 137 } 138 139 144 private void overrideExcludeValues(DimensionProperty dimensionProperty,ColumnProperties columnProperty) { 145 if (( dimensionProperty != null )&& (columnProperty != null) && (columnProperty.getExcludeValue()!=null)){ 146 columnProperty.getExcludeValue().clear(); 147 ReportDimensionSpec dimensionSpec = reportResult.getReportSpec().getDimensionFromName(dimensionProperty.getDimensionName()); 148 switch(dimensionSpec.getDataType().getType()){ 149 case ReportDataType.DATETIME_TYPE: 150 case ReportDataType.DATE_TYPE: 151 overrideDateExcludeValues(dimensionProperty, columnProperty); 152 break; 153 case ReportDataType.STRING_TYPE: 154 overrideStringExludeValues(dimensionProperty, columnProperty); 155 break; 156 case ReportDataType.FLOAT_TYPE: 157 overrideFloatExcludeValues(dimensionProperty, columnProperty); 158 case ReportDataType.INTEGER_TYPE: 159 overrideIntegerExcludeValues(dimensionProperty, columnProperty); 160 break; 161 case ReportDataType.BOOLEAN_TYPE: 162 overrideBooleanExcludeValues(dimensionProperty, columnProperty); 163 break; 164 } 165 } 166 } 167 168 private void overrideBooleanExcludeValues(DimensionProperty dimensionProperty, ColumnProperties columnProperty) { 169 for (int i=0;i<dimensionProperty.getExcludeValueCount();i++){ 170 String value = dimensionProperty.getExcludeValue()[i].getValue(); 171 columnProperty.getExcludeValue().add(Boolean.valueOf(value)); 172 } 173 } 174 175 private void overrideIntegerExcludeValues(DimensionProperty dimensionProperty, ColumnProperties columnProperty) { 176 for (int i=0;i<dimensionProperty.getExcludeValueCount();i++){ 177 String value = dimensionProperty.getExcludeValue()[i].getValue(); 178 columnProperty.getExcludeValue().add(SharedInteger.newFrom(new Integer (value))); 179 } 180 } 181 182 private void overrideFloatExcludeValues(DimensionProperty dimensionProperty, ColumnProperties columnProperty) { 183 for (int i=0;i<dimensionProperty.getExcludeValueCount();i++){ 184 String value = dimensionProperty.getExcludeValue()[i].getValue(); 185 columnProperty.getExcludeValue().add(SharedFloat.newFrom(new Float (value))); 186 } 187 } 188 189 private void overrideStringExludeValues(DimensionProperty dimensionProperty, ColumnProperties columnProperty) { 190 for (int i=0;i<dimensionProperty.getExcludeValueCount();i++){ 191 columnProperty.getExcludeValue().add(dimensionProperty.getExcludeValue()[i].getValue()); 192 } 193 } 194 195 private void overrideDateExcludeValues(DimensionProperty dimensionProperty,ColumnProperties columnProperty) { 196 for (int i=0;i<dimensionProperty.getExcludeValueCount();i++){ 197 try { 198 String value = dimensionProperty.getExcludeValue()[i].getValue(); 199 DateFormat dateFormat = SimpleDateFormat.getDateInstance(DateFormat.SHORT, LanguageTraslator.getLocale()); 200 columnProperty.getExcludeValue().add(dateFormat.parse(value)); 201 } catch (ParseException e) { 202 e.printStackTrace(); 203 } 204 } 205 } 206 207 212 private void overrideLightBoxerProperties(ReportView reportView) { 213 LightBoxDefinition lightBoxDefinition; 214 Expression expresion=null; 215 boolean andExpresion; 216 217 boolean isRange=false; 218 conditionManager.clearValues(); 219 int lightBoxDefinitionCount = reportView.getLightBoxer()==null ? 0 : reportView.getLightBoxer().getLightBoxDefinitionCount(); 220 for (int i = 0; i < lightBoxDefinitionCount; i++){ 221 andExpresion = false; 222 lightBoxDefinition = reportView.getLightBoxer().getLightBoxDefinition()[i]; 223 if (isRange) { 224 isRange = false; 225 expresion = getEndRangeExpresion(expresion,lightBoxDefinition); 226 andExpresion = true; 227 }else { 228 if (lightBoxDefinition.getType().getType() == LightBoxDefinitionTypeType.RANGE.getType()){ 229 isRange = true; 230 expresion = getStartRangeExpresion(lightBoxDefinition); 231 andExpresion = false; 232 }else{ 233 expresion = getSimpleExpresion(lightBoxDefinition); 234 andExpresion = true; 235 } 236 } 237 if (andExpresion){ 238 Color color = new Color(lightBoxDefinition.getColorRed(),lightBoxDefinition.getColorGreen(),lightBoxDefinition.getColorBlue()); 239 MetricState metricState = new MetricState(getReportResult().getReportSpec().getMetricFromName(lightBoxDefinition.getMetricName())); 240 metricState.setVisible(true); 241 ColorCondition colorCondition = new ColorCondition(expresion,color,metricState); 242 conditionManager.put(colorCondition); 243 } 244 } 245 } 246 247 248 private void overrideMetricsProperties(ReportView reportView) throws InfoException { 249 if (reportView.getMetricProperties()!= null){ 250 MetricProperty metricProperty; 251 for (int i= 0;i<visibleMetrics.size();i++){ 252 MetricState metricsState = (MetricState) visibleMetrics.get(i); 253 metricProperty = getMetricPropertyFromName(metricsState.getName(),reportView); 254 if (metricProperty!=null){ 255 metricsState.setVisible(metricProperty.getVisible()); 256 } 257 } 258 } 259 } 260 261 262 private MetricProperty getMetricPropertyFromName(String metricName, ReportView reportView) throws InfoException { 263 if (reportView.getMetricProperties()!=null){ 264 int size = reportView.getMetricProperties().getMetricPropertyCount(); 265 MetricProperty metricProperty; 266 for (int i = 0;i<size;i++){ 267 metricProperty = reportView.getMetricProperties().getMetricProperty()[i]; 268 if (metricProperty.getMetricName().equalsIgnoreCase(metricName)){ 269 return metricProperty; 270 } 271 } 272 } 273 throw new InfoException(LanguageTraslator.traslate("107")); 274 } 275 276 private DimensionProperty getDimensionPropertyFromName(String DimensionName, ReportView reportView) { 277 if (reportView.getDimensionProperties()!=null){ 278 int size = reportView.getDimensionProperties().getDimensionPropertyCount(); 279 DimensionProperty DimensionProperty; 280 for (int i = 0;i<size;i++){ 281 DimensionProperty = reportView.getDimensionProperties().getDimensionProperty()[i]; 282 if (DimensionProperty.getDimensionName().equalsIgnoreCase(DimensionName)){ 283 return DimensionProperty; 284 } 285 } 286 } 287 return null; 288 } 290 291 292 private void overrideDimensionProperties(ReportView reportView) { 293 if (reportView.getDimensionProperties()!=null){ 294 DimensionProperty dimensionProperty; 295 ColumnProperties columnProperty; 296 for (int i= 0;i<columnProperties.size();i++){ 297 columnProperty = (ColumnProperties) columnProperties.get(i); 298 dimensionProperty = getDimensionPropertyFromName(columnProperty.getColumnName(),reportView); 299 if (dimensionProperty !=null){ 300 columnProperty.setWidth(dimensionProperty.getWidth()); 301 } 302 overrideExcludeValues(dimensionProperty,columnProperty); 303 columnProperty.setLocation(dimensionProperty.getLocation().toString()); 304 columnProperty.setUbication(dimensionProperty.getUbication()); 305 columnProperty.setOrder(getDimensionDefinitionOrderType(dimensionProperty.getOrder())); 306 columnProperty.setRankMetricName(dimensionProperty.getRankMetricName()); 307 } 308 } 309 } 310 311 316 private DimensionDefinitionOrderType getDimensionDefinitionOrderType(DimensionPropertyOrderType order) { 317 if (order.toString().equalsIgnoreCase(DimensionDefinitionOrderType.A.toString())){ 318 return DimensionDefinitionOrderType.A; 319 } else if (order.toString().equalsIgnoreCase(DimensionDefinitionOrderType.D.toString())){ 320 return DimensionDefinitionOrderType.D; 321 } 322 return null; 323 } 324 325 329 public ArrayList getColumnProperties() { 330 return columnProperties; 331 } 332 333 public void setColumnProperties(ColumnProperties properties) { 334 this.columnProperties.add(properties); 335 } 336 337 341 public boolean isWithTotal() { 342 return withTotal; 343 } 344 345 349 public void setWithTotal(boolean withTotal) { 350 this.withTotal = withTotal; 351 } 352 353 public ArrayList getMetricProperies() { 354 return visibleMetrics; 355 } 356 357 public void setVisibleMetrics(MetricState state) { 358 this.visibleMetrics.add(state); 359 } 360 361 public ColorConditionManager getConditionManager() { 362 return conditionManager; 363 } 364 365 public void setConditionManager(ColorConditionManager conditionManager) { 366 this.conditionManager = conditionManager; 367 } 368 369 376 public ReportView getReportView(String ID, String userID, String description){ 377 ReportView reportView = ReportViewBuilder.getDefaultReportViewHeader(ID, userID, description, reportResult); 378 buildDimensionsProperties(reportView); 379 buildMetricsProperties(reportView); 380 buildLigthBox(reportView); 381 return reportView; 383 } 384 385 389 private void buildLigthBox(ReportView reportView) { 390 LightBoxer ligthBoxer = new LightBoxer(); 391 reportView.setLightBoxer(ligthBoxer); 392 LightBoxDefinition lightBox; 393 ColorCondition colorCondition; 394 Expression expression; 395 Vector conditions; 396 for (int i = 0;i<getConditionManagerSize();i++){ 397 conditions = (Vector) conditionManager.getValuesMetricConditions().values().toArray()[i]; 398 for (int cond=0;cond<conditions.size();cond++){ 399 colorCondition = (ColorCondition) conditions.toArray()[cond]; 400 lightBox = new LightBoxDefinition(); 401 ligthBoxer.addLightBoxDefinition(lightBox); 402 lightBox.setMetricName(colorCondition.getMetricName()); 403 lightBox.setColorBlue(colorCondition.getColor().getBlue()); 404 lightBox.setColorGreen(colorCondition.getColor().getGreen()); 405 lightBox.setColorRed(colorCondition.getColor().getRed()); 406 expression = colorCondition.getExpression(); 407 lightBox.setType(getTypeLightBox(expression)); 408 if (expression instanceof AndExp){ 409 lightBox.setParameter(LightBoxDefinitionParameterType.FROM); 410 lightBox.setValue(getArgumentConstant(expression.getArguments()[0].getArguments())); 411 412 lightBox = new LightBoxDefinition(); 413 ligthBoxer.addLightBoxDefinition(lightBox); 414 lightBox.setMetricName(colorCondition.getMetricName()); 415 lightBox.setColorBlue(colorCondition.getColor().getBlue()); 416 lightBox.setColorGreen(colorCondition.getColor().getGreen()); 417 lightBox.setColorRed(colorCondition.getColor().getRed()); 418 lightBox.setType(getTypeLightBox(expression)); 419 lightBox.setValue(getArgumentConstant(expression.getArguments()[1].getArguments())); 420 lightBox.setParameter(LightBoxDefinitionParameterType.TO); 421 } else { 422 lightBox.setValue(getArgumentConstant(expression.getArguments())); 423 lightBox.setParameter(LightBoxDefinitionParameterType.VALUE); 424 } 425 } 426 } 427 428 } 429 430 435 private String getArgumentConstant(Expression[] arguments) { 436 if (arguments[0].isConstant()) { 437 return arguments[0].toString(); 438 } else if (arguments[1].isConstant()){ 439 return arguments[1].toString(); 440 } 441 return ""; 442 } 443 444 448 private int getConditionManagerSize(){ 449 if(conditionManager ==null){ 450 return 0; 451 } 452 else{ 453 return conditionManager.getValuesMetricConditions().values().size(); 454 } 455 } 456 457 462 private LightBoxDefinitionTypeType getTypeLightBox(Expression expression) { 463 if (expression instanceof AndExp){ 464 return LightBoxDefinitionTypeType.RANGE; 465 } else if (expression instanceof EqualTo){ 466 return LightBoxDefinitionTypeType.EQUAL; 467 } else if (expression instanceof GreaterOrEqualTo){ 468 return LightBoxDefinitionTypeType.GREATEREQUALTHAN; 469 } else if (expression instanceof GreaterThan){ 470 return LightBoxDefinitionTypeType.GREATERTHAN; 471 } else if (expression instanceof LessOrEqualTo){ 472 return LightBoxDefinitionTypeType.LESSEQUALTHAN; 473 } else if (expression instanceof LessThan){ 474 return LightBoxDefinitionTypeType.LESSTHAN; 475 } 476 return null; 477 } 478 479 483 private void buildMetricsProperties(ReportView reportView) { 484 MetricProperties metricProperties = new MetricProperties(); 485 reportView.setMetricProperties(metricProperties); 486 MetricProperty metricProperty; 487 MetricState metricState; 488 for (int i = 0;i<visibleMetrics.size();i++){ 489 metricState = (MetricState) visibleMetrics.get(i); 490 metricProperty = new MetricProperty(); 491 metricProperties.addMetricProperty(metricProperty); 492 metricProperty.setMetricName(metricState.getName()); 493 metricProperty.setVisible(metricState.getVisible()); 494 } 495 } 496 497 501 private void buildDimensionsProperties(ReportView reportView) { 502 DimensionProperties dimensionsProperties = new DimensionProperties(); 503 reportView.setDimensionProperties(dimensionsProperties); 504 DimensionProperty dimensionProperty; 505 ColumnProperties columnProperty; 506 for (int i = 0;i<columnProperties.size();i++){ 507 columnProperty = (ColumnProperties) columnProperties.get(i); 508 dimensionProperty = new DimensionProperty(); 509 dimensionsProperties.addDimensionProperty(dimensionProperty); 510 dimensionProperty.setDimensionName(columnProperty.getColumnName()); 511 dimensionProperty.setWidth(columnProperty.getWidth()); 512 dimensionProperty.setOrder(ReportViewBuilder.getDimensionPropertyOrder(columnProperty.getOrder())); 513 dimensionProperty.setLocation(ReportViewBuilder.getDimensionPropertyLocation(columnProperty.getLocation())); 514 dimensionProperty.setUbication(columnProperty.getUbication()); 515 dimensionProperty.setRankMetricName(columnProperty.getRankMetricName()); 516 buildExcludeValues(dimensionProperty,columnProperty); 517 } 518 519 } 520 521 526 private void buildExcludeValues(DimensionProperty dimensionProperty, ColumnProperties columnProperty) { 527 Iterator excludedValues = columnProperty.getExcludeValue().iterator(); 528 while (excludedValues.hasNext()) { 529 Object value = excludedValues.next(); 530 ExcludeValue excludeValue = new ExcludeValue(); 531 excludeValue.setValue(value.toString()); 532 dimensionProperty.addExcludeValue(excludeValue); 533 } 534 } 535 536 public ReportResult getReportResult() { 537 return reportResult; 538 } 539 540 public void setReportResult(ReportResult reportResult) throws InfoException { 541 this.reportResult = reportResult; 542 updateColumnsProperties(); 543 } 544 545 public void updateColumnsProperties() throws InfoException{ 546 Iterator iterator = this.getColumnProperties().iterator(); 547 while(iterator.hasNext()){ 548 ColumnProperties properties = (ColumnProperties) iterator.next(); 549 Set set = reportResult.getDimensionUnCheckedValues(properties.getColumnName()); 550 if(set != null){ 551 properties.setExcludeValue(set); 552 } 553 else{ 554 properties.setExcludeValue(new TreeSet()); 555 } 556 } 557 } 558 private Expression getSimpleExpresion(LightBoxDefinition lightBoxDefinition) { 559 if (lightBoxDefinition.getType().toString().equalsIgnoreCase(LightBoxDefinitionTypeType.EQUAL.toString())){ 560 return (new VariableExp("VALUE").newEqualTo(new ConstantExp(new Float (lightBoxDefinition.getValue())))); 561 } else if (lightBoxDefinition.getType().toString().equalsIgnoreCase(LightBoxDefinitionTypeType.GREATEREQUALTHAN.toString())){ 562 return (new VariableExp("VALUE").newGreaterOrEqualTo(new ConstantExp(new Float (lightBoxDefinition.getValue())))); 563 } else if (lightBoxDefinition.getType().toString().equalsIgnoreCase(LightBoxDefinitionTypeType.GREATERTHAN.toString())){ 564 return (new VariableExp("VALUE").newGreaterThan(new ConstantExp(new Float (lightBoxDefinition.getValue())))); 565 } else if (lightBoxDefinition.getType().toString().equalsIgnoreCase(LightBoxDefinitionTypeType.LESSEQUALTHAN.toString())){ 566 return (new VariableExp("VALUE").newLessOrEquealTo(new ConstantExp(new Float (lightBoxDefinition.getValue())))); 567 } else if (lightBoxDefinition.getType().toString().equalsIgnoreCase(LightBoxDefinitionTypeType.LESSTHAN.toString())){ 568 return (new VariableExp("VALUE").newLessThan(new ConstantExp(new Float (lightBoxDefinition.getValue())))); 569 } 570 return null; 571 } 572 574 private Expression getStartRangeExpresion(LightBoxDefinition lightBoxDefinition) { 575 return (new VariableExp("VALUE").newGreaterThan(new ConstantExp(new Float (lightBoxDefinition.getValue())))); 576 } 577 private Expression getEndRangeExpresion(Expression expresion, LightBoxDefinition lightBoxDefinition) { 578 return new AndExp(expresion,(new VariableExp("VALUE").newLessThan(new ConstantExp(new Float (lightBoxDefinition.getValue()))))); 579 } 580 581 585 public ColumnProperties getRowColumn(String columnName) { 586 ColumnProperties currentColumnProperties; 587 for (int i=0;i<columnProperties.size();i++){ 588 currentColumnProperties = (ColumnProperties)columnProperties.get(i); 589 if (currentColumnProperties.getColumnName().equalsIgnoreCase(columnName)){ 590 return currentColumnProperties; 591 } 592 } 593 return null; 594 } 595 596 600 public Map getParams() { 601 if (params==null){ 602 params = new Hashtable(); 603 } 604 return params; 605 } 606 public void newColumnPropertiesArrayList(ArrayList list){ 607 columnProperties = null; 608 columnProperties = list; 609 } 610 } | Popular Tags |