1 package org.tigris.scarab.reports; 2 3 41 42 import java.util.List ; 43 import java.util.ArrayList ; 44 import java.util.Iterator ; 45 import java.util.Collections ; 46 import org.apache.torque.om.NumberKey; 47 import org.apache.fulcrum.intake.Retrievable; 48 import org.tigris.scarab.util.Log; 49 import org.tigris.scarab.om.AttributeOptionManager; 50 51 56 public class ReportHeading 57 implements java.io.Serializable , 58 Retrievable 59 { 60 private List reportGroups; 61 62 private List reportOptionAttributes; 63 64 private List reportDates; 65 66 private List reportUserAttributes; 67 68 private List reportDateRanges; 69 70 private String queryKey; 71 72 public int calculateType() 73 { 74 int type = 0; 75 if (getReportGroups() != null && getReportGroups().size() > 0) 76 { 77 ReportGroup firstGroup = (ReportGroup)getReportGroups().get(0); 78 if (firstGroup.getReportUserAttributes() != null) 79 { 80 type = 1; 81 } 82 } 83 else if (getReportUserAttributes() != null) 84 { 85 type = 1; 86 } 87 else if (getReportDates() != null || getReportDateRanges() != null) 88 { 89 type = 2; 90 } 91 return type; 92 } 93 94 public List retrieveGroupedAttributes() 95 { 96 List result = null; 97 List groups = getReportGroups(); 98 if (groups == null || groups.isEmpty()) 99 { 100 result = Collections.EMPTY_LIST; 101 } 102 else if (calculateType() == 0) 103 { 104 result = new ArrayList (); 105 for (Iterator i = groups.iterator(); i.hasNext();) 106 { 107 List options = ((ReportGroup)i.next()). 108 getReportOptionAttributes(); 109 if (options != null) 110 { 111 result.addAll(options); 112 } 113 } 114 } 115 else 116 { 117 result = new ArrayList (); 118 for (Iterator i = groups.iterator(); i.hasNext();) 119 { 120 List users = ((ReportGroup)i.next()). 121 getReportUserAttributes(); 122 if (users != null) 123 { 124 result.addAll(users); 125 } 126 } 127 } 128 129 return result; 130 } 131 132 public void reset() 133 { 134 reportGroups = null; 135 reportOptionAttributes = null; 136 reportUserAttributes = null; 137 reportDates = null; 138 reportDateRanges = null; 139 } 140 141 public Object get(int i) 142 { 143 Object obj = null; 144 if (getReportGroups() != null) 145 { 146 obj = getReportGroups().get(i); 147 } 148 else if (getReportOptionAttributes() != null) 149 { 150 obj = getReportOptionAttributes().get(i); 151 } 152 else if (getReportUserAttributes() != null) 153 { 154 obj = getReportUserAttributes().get(i); 155 } 156 else if (getReportDates() != null || getReportDateRanges() != null) 158 { 159 obj = getReportDates().get(i); 160 } 161 return obj; 162 } 163 164 public int size() 165 { 166 int size = 0; 167 if (getReportGroups() != null) 168 { 169 size = getReportGroups().size(); 170 } 171 else if (getReportOptionAttributes() != null) 172 { 173 size = getReportOptionAttributes().size(); 174 } 175 else if (getReportUserAttributes() != null) 176 { 177 size = getReportUserAttributes().size(); 178 } 179 else if (getReportDates() != null) 181 { 182 size = getReportDates().size(); 183 } 184 return size; 185 } 186 187 public boolean singleAttribute() 188 { 189 boolean result = false; 190 Integer firstId = null; 191 Iterator options = consolidateReportOptionAttributes().iterator(); 192 if (options.hasNext()) 193 { 194 result = true; 195 for (; options.hasNext() && result;) 196 { 197 try 198 { 199 Integer id = new Integer ( 200 AttributeOptionManager.getInstance( 201 new NumberKey(((ReportOptionAttribute)options.next()) 202 .getOptionId().toString())) 203 .getAttributeId().toString()); 204 if (firstId == null) 205 { 206 firstId = id; 207 } 208 else 209 { 210 result = firstId.equals(id); 211 } 212 } 213 catch (Exception e) 214 { 215 Log.get().warn("Error on attribute id", e); 216 result = false; 217 } 218 } 219 } 220 221 if (firstId == null) 222 { 223 Iterator users = consolidateReportUserAttributes().iterator(); 224 if (users.hasNext()) 225 { 226 result = true; 227 for (; users.hasNext() && result;) 228 { 229 Integer id = ((ReportUserAttribute)users.next()) 230 .getAttributeId(); 231 if (firstId == null) 232 { 233 firstId = id; 234 } 235 else 236 { 237 result = firstId.equals(id); 238 } 239 } 240 } 241 } 242 return result; 243 } 244 245 246 252 public List consolidateReportOptionAttributes() 253 { 254 List result = null; 255 List groups = getReportGroups(); 256 if (groups != null && !groups.isEmpty()) 257 { 258 for (Iterator i = groups.iterator(); i.hasNext();) 259 { 260 ReportGroup group = (ReportGroup)i.next(); 261 List options = group.getReportOptionAttributes(); 262 if (options != null && !options.isEmpty()) 263 { 264 for (Iterator j = options.iterator(); j.hasNext();) 265 { 266 if (result == null) 267 { 268 result = new ArrayList (); 269 } 270 result.add(j.next()); 271 } 272 } 273 } 274 } 275 276 List options = getReportOptionAttributes(); 277 if (options != null && !options.isEmpty()) 278 { 279 for (Iterator j = options.iterator(); j.hasNext();) 280 { 281 if (result == null) 282 { 283 result = new ArrayList (); 284 } 285 result.add(j.next()); 286 } 287 } 288 if (result == null) 289 { 290 result = Collections.EMPTY_LIST; 291 } 292 293 return result; 294 } 295 296 302 public List consolidateReportUserAttributes() 303 { 304 List result = null; 305 List groups = getReportGroups(); 306 if (groups != null && !groups.isEmpty()) 307 { 308 for (Iterator i = groups.iterator(); i.hasNext();) 309 { 310 ReportGroup group = (ReportGroup)i.next(); 311 List users = group.getReportUserAttributes(); 312 if (users != null && !users.isEmpty()) 313 { 314 for (Iterator j = users.iterator(); j.hasNext();) 315 { 316 if (result == null) 317 { 318 result = new ArrayList (); 319 } 320 result.add(j.next()); 321 } 322 } 323 } 324 } 325 326 List users = getReportUserAttributes(); 327 if (users != null && !users.isEmpty()) 328 { 329 for (Iterator j = users.iterator(); j.hasNext();) 330 { 331 if (result == null) 332 { 333 result = new ArrayList (); 334 } 335 result.add(j.next()); 336 } 337 } 338 if (result == null) 339 { 340 result = Collections.EMPTY_LIST; 341 } 342 343 return result; 344 } 345 346 350 public List getReportGroups() 351 { 352 return reportGroups; 353 } 354 355 359 public void setReportGroups(List newReportGroups) 360 { 361 this.reportGroups = newReportGroups; 362 } 363 364 368 public void addReportGroup(ReportGroup newReportGroup) 369 { 370 if (reportGroups == null) 371 { 372 reportGroups = new ArrayList (); 373 if (newReportGroup.getReportOptionAttributes() == null) 376 { 377 newReportGroup 378 .setReportOptionAttributes(getReportOptionAttributes()); 379 setReportOptionAttributes(null); 380 } 381 else if (getReportOptionAttributes() != null) 382 { 383 newReportGroup.getReportOptionAttributes() 384 .addAll(getReportOptionAttributes()); 385 setReportOptionAttributes(null); 386 } 387 388 if (newReportGroup.getReportUserAttributes() == null) 389 { 390 newReportGroup 391 .setReportUserAttributes(getReportUserAttributes()); 392 setReportUserAttributes(null); 393 } 394 else if (getReportUserAttributes() != null) 395 { 396 newReportGroup.getReportUserAttributes() 397 .addAll(getReportUserAttributes()); 398 setReportUserAttributes(null); 399 } 400 } 401 if (!reportGroups.contains(newReportGroup)) 402 { 403 reportGroups.add(newReportGroup); 404 } 405 } 406 407 411 public List getReportOptionAttributes() 412 { 413 return reportOptionAttributes; 414 } 415 416 420 public void setReportOptionAttributes(List newReportOptionAttributes) 421 { 422 this.reportOptionAttributes = newReportOptionAttributes; 423 } 424 425 429 public void addReportOptionAttribute(ReportOptionAttribute newReportOptionAttribute) 430 { 431 if (reportOptionAttributes == null) 432 { 433 reportOptionAttributes = new ArrayList (); 434 } 435 if (!reportOptionAttributes.contains(newReportOptionAttribute)) 436 { 437 reportOptionAttributes.add(newReportOptionAttribute); 438 } 439 } 440 441 445 public List getReportUserAttributes() 446 { 447 return reportUserAttributes; 448 } 449 450 454 public void setReportUserAttributes(List newReportUserAttributes) 455 { 456 this.reportUserAttributes = newReportUserAttributes; 457 } 458 459 463 public void addReportUserAttribute(ReportUserAttribute newReportUserAttribute) 464 { 465 Log.get().debug("Potentially adding " + newReportUserAttribute); 466 467 if (reportUserAttributes == null) 468 { 469 reportUserAttributes = new ArrayList (); 470 } 471 if (!reportUserAttributes.contains(newReportUserAttribute)) 472 { 473 Log.get().debug("added " + newReportUserAttribute); 474 reportUserAttributes.add(newReportUserAttribute); 475 } 476 } 477 478 482 public List getReportDateRanges() 483 { 484 return reportDateRanges; 485 } 486 487 491 public void setReportDateRanges(List newReportDateRanges) 492 { 493 this.reportDateRanges = newReportDateRanges; 494 } 495 496 500 public void addReportDateRange(ReportDateRange newReportDateRange) 501 { 502 if (reportDateRanges == null) 503 { 504 reportDateRanges = new ArrayList (); 505 } 506 reportDateRanges.add(newReportDateRange); 507 } 508 509 513 public List getReportDates() 514 { 515 return reportDates; 516 } 517 518 522 public void setReportDates(List newReportDates) 523 { 524 this.reportDates = newReportDates; 525 } 526 527 531 public void addReportDate(ReportDate newReportDate) 532 { 533 if (reportDates == null) 534 { 535 reportDates = new ArrayList (); 536 } 537 reportDates.add(newReportDate); 538 } 539 540 544 public String getQueryKey() 545 { 546 return queryKey == null ? "" : queryKey; 547 } 548 549 553 public void setQueryKey(String newQueryKey) 554 { 555 this.queryKey = newQueryKey; 556 } 557 } 558 | Popular Tags |