1 package org.tigris.scarab.reports; 2 3 48 49 import java.util.Date ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 import com.workingdogs.village.Record; 54 55 import org.apache.torque.util.Criteria; 57 58 import org.tigris.scarab.om.ModuleManager; 59 import org.tigris.scarab.om.MITList; 60 import org.tigris.scarab.om.MITListItem; 61 import org.tigris.scarab.om.ScarabUser; 62 import org.tigris.scarab.om.ActivityPeer; 63 import org.tigris.scarab.om.ActivitySetPeer; 64 import org.tigris.scarab.om.ActivitySetTypePeer; 65 import org.tigris.scarab.om.IssuePeer; 66 import org.tigris.scarab.util.TableModel; 67 import org.tigris.scarab.services.security.ScarabSecurity; 68 69 75 public class ReportTableModel 76 extends TableModel 77 { 78 private static final String ACT_ATTRIBUTE_ID = 79 ActivityPeer.ATTRIBUTE_ID.substring( 80 ActivityPeer.ATTRIBUTE_ID.indexOf('.')+1); 81 private static final String ACT_NEW_USER_ID = 82 ActivityPeer.NEW_USER_ID.substring( 83 ActivityPeer.NEW_USER_ID.indexOf('.')+1); 84 private static final String ACT_NEW_OPTION_ID = 85 ActivityPeer.NEW_OPTION_ID.substring( 86 ActivityPeer.NEW_OPTION_ID.indexOf('.')+1); 87 private static final String ACT_ISSUE_ID = 88 ActivityPeer.ISSUE_ID.substring( 89 ActivityPeer.ISSUE_ID.indexOf('.')+1); 90 private static final String ACT_TRANSACTION_ID = 91 ActivityPeer.TRANSACTION_ID.substring( 92 ActivityPeer.TRANSACTION_ID.indexOf('.')+1); 93 private static final String ACT_END_DATE = 94 ActivityPeer.END_DATE.substring( 95 ActivityPeer.END_DATE.indexOf('.')+1); 96 private static final String TRAN_TRANSACTION_ID = 97 ActivitySetPeer.TRANSACTION_ID.substring( 98 ActivitySetPeer.TRANSACTION_ID.indexOf('.')+1); 99 private static final String TRAN_CREATED_DATE = 100 ActivitySetPeer.CREATED_DATE.substring( 101 ActivitySetPeer.CREATED_DATE.indexOf('.')+1); 102 private static final String TRAN_CREATED_BY = 103 ActivitySetPeer.CREATED_BY.substring( 104 ActivitySetPeer.CREATED_BY.indexOf('.')+1); 105 private static final String TRAN_TYPE_ID = 106 ActivitySetPeer.TYPE_ID.substring( 107 ActivitySetPeer.TYPE_ID.indexOf('.')+1); 108 109 private ReportDefinition reportDefn; 110 private List rowHeadings; 111 private List columnHeadings; 112 private Date date; 113 private Integer moduleId; 114 private Integer issueTypeId; 115 private MITList mitList; 116 117 private int[] colspan; 118 private int[] rowspan; 119 private boolean isSearchAllowed; 120 121 ReportTableModel(ReportBridge report, Date date, ScarabUser searcher) 122 throws Exception 123 { 124 this.reportDefn = report.getReportDefinition(); 125 ReportAxis axis = null; 126 List axes = reportDefn.getReportAxisList(); 127 if (axes != null && axes.size() >= 2) 128 { 129 axis = (ReportAxis)axes.get(1); 130 } 131 if (axis != null) 132 { 133 columnHeadings = axis.getReportHeadings(); 134 } 135 136 if (axes != null && axes.size() >= 1) 137 { 138 axis = (ReportAxis)axes.get(0); 139 } 140 if (axis != null) 141 { 142 rowHeadings = axis.getReportHeadings(); 143 } 144 145 this.date = date; 146 147 List xmits = reportDefn.getModuleIssueTypes(); 148 if (xmits.size() == 1) 149 { 150 ModuleIssueType mit = (ModuleIssueType)xmits.get(0); 151 this.moduleId = mit.getModuleId(); 152 this.issueTypeId = mit.getIssueTypeId(); 153 isSearchAllowed = searcher.hasPermission( 154 ScarabSecurity.ISSUE__SEARCH, ModuleManager.getInstance(moduleId)); 155 } 156 else 157 { 158 String [] perms = {ScarabSecurity.ISSUE__SEARCH}; 159 MITList searchableList = report.getMITList() 160 .getPermittedSublist(perms, searcher); 161 isSearchAllowed = searchableList.size() > 0; 162 if (searchableList.size() == 1) 163 { 164 MITListItem item = searchableList.getFirstItem(); 165 this.moduleId = item.getModuleId(); 166 this.issueTypeId = item.getIssueTypeId(); 167 } 168 else 169 { 170 this.mitList = searchableList; 171 } 172 } 173 } 174 175 179 public List getRowHeadings() 180 { 181 return rowHeadings; 182 } 183 184 188 public List getColumnHeadings() 189 { 190 return columnHeadings; 191 } 192 193 public int getColspan(int index) 194 { 195 int result = 1; 196 if (columnHeadings != null) 197 { 198 if (colspan == null) 199 { 200 int numLevels = columnHeadings.size(); 201 colspan = new int[numLevels - 1]; 202 for (int j=0; j<numLevels-1; j++) 203 { 204 colspan[j] = 1; 205 for (int i=numLevels-1; i>j; i--) 206 { 207 colspan[j] *= 208 ((ReportHeading)columnHeadings.get(i)).size(); 209 } 210 } 211 } 212 213 if (index < colspan.length) 214 { 215 result = colspan[index]; 216 } 217 } 218 219 return result; 220 } 221 222 public int getRowspan(int index) 223 { 224 int result = 1; 225 if (rowHeadings != null) 226 { 227 if (rowspan == null) 228 { 229 int numLevels = rowHeadings.size(); 230 rowspan = new int[numLevels - 1]; 231 for (int j=0; j<numLevels-1; j++) 232 { 233 rowspan[j] = 1; 234 for (int i=numLevels-1; i>j; i--) 235 { 236 rowspan[j] *= 237 ((ReportHeading)rowHeadings.get(i)).size(); 238 } 239 } 240 } 241 242 if (index < rowspan.length) 243 { 244 result = rowspan[index]; 245 } 246 } 247 248 return result; 249 } 250 251 public int getColumnCount() 252 { 253 return ((ReportHeading)columnHeadings.get(0)).size() * getColspan(0); 254 } 255 256 public int getRowCount() 257 { 258 return ((ReportHeading)rowHeadings.get(0)).size() * getRowspan(0); 259 } 260 261 262 private Object [] getColumnDataArray(int column) 263 { 264 Object [] dataArray; 265 if (columnHeadings == null) 266 { 267 dataArray = new Object [0]; 268 } 269 else 270 { 271 int numLevels = columnHeadings.size(); 272 dataArray = new Object [numLevels]; 273 int index = 0; 274 for (Iterator i=columnHeadings.iterator(); i.hasNext(); index++) 275 { 276 ReportHeading heading = (ReportHeading)i.next(); 277 dataArray[index] = heading.get((column/getColspan(index)) % heading.size()); 278 } 279 } 280 281 return dataArray; 282 } 283 284 private Object [] getRowDataArray(int row) 285 { 286 Object [] dataArray; 287 if (rowHeadings == null) 288 { 289 dataArray = new Object [0]; 290 } 291 else 292 { 293 int numLevels = rowHeadings.size(); 294 dataArray = new Object [numLevels]; 295 int index = 0; 296 for (Iterator i=rowHeadings.iterator(); i.hasNext(); index++) 297 { 298 ReportHeading heading = (ReportHeading)i.next(); 299 dataArray[index] = heading.get((row/getRowspan(index)) % heading.size()); 300 } 301 } 302 return dataArray; 303 } 304 305 public Object getValueAt(int row, int column) 306 throws Exception 307 { 308 if (row < 0 || row >= getRowCount()) 309 { 310 throw new IndexOutOfBoundsException ("Row index was " + row); } 312 313 if (column < 0 || column >= getColumnCount()) 314 { 315 throw new IndexOutOfBoundsException ("Column index was " + column); } 317 318 Object contents = null; 319 if (columnHeadings != null && columnHeadings.size() == 1 && 321 ((ReportHeading)columnHeadings.get(0)).get(0) instanceof ReportDate) 322 { 323 Date date = ((ReportDate) ((ReportHeading) columnHeadings.get(0)) 324 .get(column)).dateValue(); 325 if (date.getTime() <= System.currentTimeMillis()) 326 { 327 contents = new Integer (getIssueCount(getRowDataArray(row), 328 date)); 329 } 330 else 331 { 332 contents = ""; 334 } 335 } 336 else if (rowHeadings != null && rowHeadings.size() == 1 && 337 ((ReportHeading)rowHeadings.get(0)).get(0) instanceof ReportDate) 338 { 339 Date date = ((ReportDate)((ReportHeading)rowHeadings.get(0)) 340 .get(row)).dateValue(); 341 if (date.getTime() <= System.currentTimeMillis()) 342 { 343 contents = new Integer (getIssueCount( 344 getColumnDataArray(column), date)); 345 } 346 else 347 { 348 contents = ""; 350 } 351 } 352 else 353 { 354 contents = new Integer (getIssueCount( 355 getRowDataArray(row), getColumnDataArray(column), date)); 356 } 357 358 return contents; 359 } 360 361 private int getIssueCount(Object [] rowData, Object [] columnData, Date date) 362 throws Exception 363 { 364 Criteria crit = new Criteria(); 365 crit.addSelectColumn("count(DISTINCT a0." + ACT_ISSUE_ID + ')'); 367 int rowLength = rowData.length; 368 for (int i=0; i<rowLength; i++) 369 { 370 addOptionOrGroup(i, rowData[i], date, crit); 371 372 } 373 for (int i=0; i<columnData.length; i++) 374 { 375 addOptionOrGroup(i+rowLength, columnData[i], date, crit); 376 } 377 return getCountAndCleanUp(crit); 378 } 379 380 public int getIssueCount(Object [] dataArray, Date date) 381 throws Exception 382 { 383 Criteria crit = new Criteria(); 384 crit.addSelectColumn("count(DISTINCT a0." + ACT_ISSUE_ID + ')'); 385 for (int i=0; i<dataArray.length; i++) 386 { 387 addOptionOrGroup(i, dataArray[i], date, crit); 388 } 389 return getCountAndCleanUp(crit); 390 } 391 392 private void addOptionOrGroup(int alias, Object optionOrGroup, 393 Date date, Criteria crit) 394 { 395 if (optionOrGroup == null) 396 { 397 throw new NullPointerException ("cell definition cannot contain nulls"); } 399 400 String a = "a"+alias; 401 String t = "t"+alias; 402 crit.addJoin(a + '.' + ACT_ISSUE_ID, IssuePeer.ISSUE_ID); 405 for (int i=alias-1; i>=0; i--) 406 { 407 crit.addJoin("a"+i+'.'+ACT_ISSUE_ID, a+'.'+ACT_ISSUE_ID); 408 } 409 crit.addAlias("a"+alias, ActivityPeer.TABLE_NAME); 410 crit.addAlias("t"+alias, ActivitySetPeer.TABLE_NAME); 411 412 crit.addJoin(a+"."+ACT_TRANSACTION_ID, t+'.'+TRAN_TRANSACTION_ID); 413 crit.add(t, TRAN_CREATED_DATE, date, Criteria.LESS_THAN); 414 Criteria.Criterion c1 = crit 416 .getNewCriterion(a, ACT_END_DATE, date, Criteria.GREATER_THAN); 417 c1.or(crit.getNewCriterion(a, ACT_END_DATE, null, Criteria.EQUAL)); 418 crit.add(c1); 419 420 if (optionOrGroup instanceof ReportGroup) 421 { 422 List options = ((ReportGroup)optionOrGroup) 423 .getReportOptionAttributes(); 424 if (options != null && options.size() > 0) 425 { 426 Integer [] nks = new Integer [options.size()]; 427 for (int i=0; i<nks.length; i++) 428 { 429 nks[i] = ((ReportOptionAttribute)options.get(i)) 430 .getOptionId(); 431 } 432 433 crit.addIn(a+'.'+ACT_NEW_OPTION_ID, nks); 434 } 435 else 436 { 437 List users = ((ReportGroup)optionOrGroup) 438 .getReportUserAttributes(); 439 if (users != null && users.size() > 0) 440 { 441 Integer [] nks = new Integer [users.size()]; 442 for (int i=0; i<nks.length; i++) 443 { 444 nks[i] = ((ReportUserAttribute)users.get(i)) 445 .getUserId(); 446 } 447 448 crit.addIn(a+'.'+ACT_NEW_USER_ID, nks); 449 } 450 else 451 { 452 crit.add(a+'.'+ACT_NEW_OPTION_ID, -1); 454 } 455 } 456 } 457 else if (optionOrGroup instanceof ReportOptionAttribute) 458 { 459 crit.add(a, ACT_NEW_OPTION_ID, 460 ((ReportOptionAttribute)optionOrGroup).getOptionId()); 461 } 462 else if (optionOrGroup instanceof ReportUserAttribute) 463 { 464 ReportUserAttribute rua = (ReportUserAttribute)optionOrGroup; 465 Integer attributeId = rua.getAttributeId(); 466 if (attributeId.intValue() == 0) 467 { 468 crit.add(t, TRAN_TYPE_ID, 470 ActivitySetTypePeer.CREATE_ISSUE__PK); 471 crit.add(t, TRAN_CREATED_BY, rua.getUserId()); 472 } 473 else 474 { 475 crit.add(a, ACT_ATTRIBUTE_ID, rua.getAttributeId()); 476 crit.add(a, ACT_NEW_USER_ID, rua.getUserId()); 477 } 478 } 479 } 480 481 private boolean isXMITSearch() 482 { 483 return mitList != null && !mitList.isSingleModuleIssueType(); 484 } 485 486 private int getCountAndCleanUp(Criteria crit) 487 throws Exception 488 { 489 int result = 0; 490 if (isSearchAllowed) 491 { 492 if (isXMITSearch()) 493 { 494 mitList.addToCriteria(crit); 495 } 496 else 497 { 498 crit.add(IssuePeer.MODULE_ID, moduleId); 499 crit.add(IssuePeer.TYPE_ID, issueTypeId); 500 } 501 crit.add(IssuePeer.DELETED, false); 502 result = ((Record)ActivityPeer.doSelectVillageRecords(crit).get(0)) 503 .getValue(1).asInt(); 504 } 505 506 return result; 507 } 508 509 public boolean isOption(Object obj) 510 { 511 return obj instanceof ReportOptionAttribute; 512 } 513 public boolean isOptionGroup(Object obj) 514 { 515 return obj instanceof ReportGroup; 516 } 517 public boolean isAttributeAndUser(Object obj) 518 { 519 return obj instanceof ReportUserAttribute; 520 } 521 public boolean isUser(Object obj) 522 { 523 return obj instanceof ScarabUser; 524 } 525 public boolean isReportDate(Object obj) 526 { 527 return obj instanceof ReportDate; 528 } 529 530 public String displayAttribute(Object cell) 531 { 532 return reportDefn.displayAttribute(cell); 533 } 534 535 public String displayOption(ReportOptionAttribute cell) 536 { 537 return reportDefn.displayOption(cell); 538 } 539 540 public String displayUser(ReportUserAttribute cell) 541 { 542 return reportDefn.displayUser(cell); 543 } 544 } 545 | Popular Tags |