1 4 package com.openedit.store; 5 6 import java.util.ArrayList ; 7 import java.util.Collections ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.LinkedList ; 11 import java.util.List ; 12 import java.util.Map ; 13 14 import org.dom4j.Element; 15 16 20 public class Category 21 { 22 protected String fieldName; 23 protected String fieldId; 24 protected String fieldDescription; 25 protected String fieldShortDecription; 26 protected boolean fieldUserSelected; 28 protected String fieldCatalogHome; 29 protected int fieldItemCount; 30 protected List fieldChildren; 31 protected Category fieldParentCatalog; 32 protected Map fieldProperties; 33 protected Map fieldImages; 34 protected Map fieldLinkedFiles; protected String fieldBrochure; 36 protected String fieldSpecSheet; 37 protected List fieldOptions; 38 39 public Category() 40 { 41 } 42 43 public Category( String inName ) 44 { 45 fieldName = inName; 46 } 47 48 public Category(String inId, String inName) 49 { 50 setId(inId); 51 if( inName != null) 52 { 53 setName(inName.trim()); 54 } 55 } 56 57 92 public String getName() 93 { 94 return fieldName; 95 } 96 97 public void setName( String inString ) 98 { 99 fieldName = inString; 100 } 101 102 public void loadData( Element inElement ) 103 { 104 setName( inElement.attributeValue( "name" ) ); 105 setId( inElement.attributeValue( "id" ) ); 106 setCatalogHome(inElement.attributeValue( "catalogHome" ) ); 107 String val = inElement.attributeValue( "itemcount" ); 108 if ( val != null && val.length() > 0) 109 { 110 setItemCount( Integer.parseInt(val)); 111 } 112 setUserSelected("true".equalsIgnoreCase(inElement.attributeValue("userselected"))); 113 } 114 public String getCatalogHome() 115 { 116 return fieldCatalogHome; 117 } 118 public void setCatalogHome(String inCatalogHome) 119 { 120 fieldCatalogHome = inCatalogHome; 121 } 122 public String getId() 123 { 124 return fieldId; 125 } 126 public void setId(String inId) 127 { 128 fieldId = inId; 129 } 130 public String toString() 131 { 132 return getName(); 133 } 134 public boolean isUserSelected() 135 { 136 return fieldUserSelected; 137 } 138 public void setUserSelected(boolean inUserSelected) 139 { 140 fieldUserSelected = inUserSelected; 141 } 142 public int getItemCount() { 143 return fieldItemCount; 144 } 145 public void setItemCount(int inItemCount) { 146 fieldItemCount = inItemCount; 147 } 148 public boolean isContainsItems() 149 { 150 return getItemCount() > 0; 151 } 152 155 public List getChildren() 156 { 157 if( fieldChildren == null ) 158 { 159 fieldChildren = new ArrayList (); 160 } 161 return fieldChildren; 162 } 163 166 public void setChildren( List inChildren ) 167 { 168 fieldChildren = inChildren; 169 for ( Iterator iter = inChildren.iterator(); iter.hasNext(); ) 170 { 171 Category cat = (Category) iter.next(); 172 cat.setParentCatalog(this); 173 } 174 } 175 public Category addChild( Category inNewChild ) 176 { 177 inNewChild.setParentCatalog(this); 178 179 for (int i = 0; i < getChildren().size(); i++) 180 { 181 Category element = (Category) getChildren().get(i); 182 if ( element.getId().equals(inNewChild.getId())) 183 { 184 getChildren().set(i, inNewChild); 185 return inNewChild; 186 } 187 } 188 getChildren().add(inNewChild); 189 return inNewChild; 190 } 191 public Category getChild(String inId) 192 { 193 for (Iterator iter = getChildren().iterator(); iter.hasNext();) 194 { 195 Category element = (Category) iter.next(); 196 if ( element.getId().equals(inId)) 197 { 198 return element; 199 } 200 } 201 return null; 202 } 203 204 public void removeChild( Category inChild ) 205 { 206 Category child = getChild(inChild.getId()); 207 if ( child != null) 208 { 209 getChildren().remove(child); 210 child.setParentCatalog(null); 211 } 212 213 inChild.setParentCatalog( null ); 214 } 215 public boolean hasParent(String inId) 216 { 217 Category parent = this; 218 while( parent != null) 219 { 220 if ( parent.getId().equals(inId) ) 221 { 222 return true; 223 } 224 parent = parent.getParentCatalog(); 225 } 226 return false; 227 } 228 229 232 public boolean hasChildren() 233 { 234 return fieldChildren != null && fieldChildren.size() > 0; 235 } 236 public boolean hasCatalog( String inId ) 237 { 238 if( getId().equals( inId) ) 239 { 240 return true; 241 } 242 if ( hasChildren() ) 243 { 244 for (Iterator iter = getChildren().iterator(); iter.hasNext();) 245 { 246 Category child = (Category) iter.next(); 247 if( child.hasCatalog(inId)) 248 { 249 return true; 250 } 251 } 252 } 253 return false; 254 } 255 256 257 public boolean hasChild(String inId) 258 { 259 if ( hasChildren() ) 260 { 261 for (Iterator iter = getChildren().iterator(); iter.hasNext();) 262 { 263 Category child = (Category) iter.next(); 264 if( child.getId().equals( inId) ) 265 { 266 return true; 267 } 268 } 269 } 270 return false; 271 } 272 273 public boolean isAncestorOf( Category inCatalog ) 274 { 275 for (Iterator children = getChildren().iterator(); children.hasNext();) 276 { 277 Category child = (Category) children.next(); 278 if (child == inCatalog) 279 { 280 return true; 281 } 282 else if (child.hasChildren() && child.isAncestorOf(inCatalog)) 283 { 284 return true; 285 } 286 } 287 return false; 288 } 289 public Category getParentCatalog() 290 { 291 return fieldParentCatalog; 292 } 293 public void setParentCatalog( Category parentCatalog ) 294 { 295 fieldParentCatalog = parentCatalog; 296 } 297 298 307 public List listAncestorsAndSelf( int inStartLevel ) 308 { 309 LinkedList result = new LinkedList (); 310 Category catalog = this; 311 while ( catalog != null ) 312 { 313 result.addFirst( catalog ); 314 catalog = catalog.getParentCatalog(); 315 } 316 return result.subList( inStartLevel, result.size() ); 317 } 318 public List getChildrenInRows(int inColCount) 319 { 320 List children = getChildren(); 322 double rowscount = (double)children.size() / (double)inColCount; 323 324 List rows = new ArrayList (); 325 for (int i = 0; i < rowscount; i++) 326 { 327 int start = i*inColCount; 328 int end = i*inColCount + inColCount; 329 List sublist = children.subList(start,Math.min( children.size(),end )); 330 rows.add(sublist); 331 } 332 return rows; 333 } 334 public int getLevel() 335 { 336 int i = 1; 337 Category parent = this; 338 while(parent != null) 339 { 340 parent = parent.getParentCatalog(); 341 } 342 return i; 343 } 344 345 public String getDescription() 346 { 347 return fieldDescription; 348 } 349 public void setDescription(String inDescription) 350 { 351 fieldDescription = inDescription; 352 } 353 public String get(String inKey) 354 { 355 Category cat = this; 356 while(cat != null ) 357 { 358 String val = cat.getProperty(inKey); 359 if ( val != null) 360 { 361 return val; 362 } 363 cat = cat.getParentCatalog(); 364 } 365 return null; 366 } 367 public Map getProperties() 368 { 369 if ( fieldProperties == null ) 370 { 371 fieldProperties = new HashMap (); 372 } 373 return fieldProperties; 374 } 375 376 public void setProperties(Map inProperties) 377 { 378 fieldProperties = inProperties; 379 } 380 381 public void setProperty( String inKey, String inValue ) 382 { 383 if ( inValue != null ) 384 { 385 getProperties().put( inKey, inValue ); 386 } 387 else 388 { 389 getProperties().remove( inKey ); 390 } 391 } 392 393 public void setProperty( String inKey, boolean inValue ) 394 { 395 setProperty( inKey, String.valueOf( inValue ) ); 396 } 397 public boolean isPropertyTrue(String inKey) 398 { 399 String val = getProperty(inKey); 400 if ( "true".equalsIgnoreCase(val)) 401 { 402 return true; 403 } 404 return false; 405 } 406 public String getProperty( String inKey ) 407 { 408 return (String )getProperties().get( inKey ); 409 } 410 411 414 public void removeProperty(String inKey) 415 { 416 getProperties().remove(inKey); 417 } 418 public Image getImage( String inKey) 419 { 420 return (Image)getImages().get( inKey ); 421 } 422 public Map getImages() 423 { 424 if ( fieldImages == null) 425 { 426 fieldImages = new HashMap (4); 427 } 428 return fieldImages; 429 } 430 public List loadImages(String inType) 431 { 432 List list = new ArrayList (getImages().keySet()); 433 Collections.sort(list); 434 List images = new ArrayList (); 435 for (Iterator iter = list.iterator(); iter.hasNext();) 436 { 437 Image image = getImage((String ) iter.next()); 438 if ( image.getType().equals(inType)) 439 { 440 images.add(image); 441 } 442 } 443 return images; 444 445 } 446 public List getImageList() 447 { 448 List list = new ArrayList (getImages().keySet()); 449 Collections.sort(list); 450 List images = new ArrayList (); 451 for (Iterator iter = list.iterator(); iter.hasNext();) 452 { 453 Image image = getImage((String ) iter.next()); 454 images.add(image); 455 456 } 457 return images; 458 } 459 460 467 public void addImage( Image inImage ) 468 { 469 getImages().put(inImage.getId(), inImage); 470 } 471 472 public String getShortDescription() 473 { 474 return fieldShortDecription; 475 } 476 public void setShortDescription(String inShortDecription) 477 { 478 fieldShortDecription = inShortDecription; 479 } 480 481 485 public List getLinkedFileList() 486 { 487 List list = new ArrayList (getLinkedFiles().keySet()); 488 Collections.sort(list); 489 List files = new ArrayList (); 490 for (Iterator iter = list.iterator(); iter.hasNext();) 491 { 492 LinkedFile file = getLinkedFile( (String ) iter.next()); 493 files.add(file); 494 495 } 496 return files; 497 } 498 public LinkedFile getLinkedFile(String inFilename) 499 { 500 return (LinkedFile)getLinkedFiles().get(inFilename); 501 } 502 public void putLinkedFile(String inFilename, LinkedFile inFile) 503 { 504 getLinkedFiles().put(inFilename,inFile); 505 } 506 public Map getLinkedFiles() 507 { 508 if (fieldLinkedFiles == null) 509 { 510 fieldLinkedFiles = new HashMap (4); 511 } 512 return fieldLinkedFiles; 513 } 514 515 public void clearImages() 516 { 517 getImages().clear(); 518 } 519 520 public void clearLinkedFiles() 521 { 522 getLinkedFiles().clear(); 523 } 524 525 public String getBrochure() 526 { 527 return fieldBrochure; 528 } 529 public void setBrochure(String inBrochure) 530 { 531 fieldBrochure = inBrochure; 532 } 533 public String getSpecSheet() 534 { 535 return fieldSpecSheet; 536 } 537 public void setSpecSheet(String inSpecSheet) 538 { 539 fieldSpecSheet = inSpecSheet; 540 } 541 542 public List getOptions() 543 { 544 if (fieldOptions == null) 545 { 546 fieldOptions = new ArrayList (); 547 } 548 return fieldOptions; 549 } 550 551 public List getAllOptions() 552 { 553 Map optionsMap = new HashMap (); 554 555 Category parent = this; 556 while (parent != null) 557 { 558 if( parent.fieldOptions != null) 559 { 560 List catalogOptions = parent.getOptions(); 561 562 for (Iterator iter = catalogOptions.iterator(); iter.hasNext();) 563 { 564 Option option = (Option) iter.next(); 565 if( !optionsMap.containsKey(option.getId())) 566 { 567 optionsMap.put(option.getId(), option); 568 } 569 } 570 } 571 parent = parent.getParentCatalog(); 572 } 573 574 return new ArrayList (optionsMap.values()); 575 } 576 577 public void setOptions(List inOptions) 578 { 579 fieldOptions = inOptions; 580 } 581 582 public void addOption(Option inOption) 583 { 584 getOptions().add(inOption); 585 } 586 public void removeOption(String id) 587 { 588 List options = getOptions(); 589 for (int i = 0; i < options.size(); i++) 590 { 591 Option option = (Option)options.get(i); 592 if (option.getId().equals( id ) ) 593 { 594 getOptions().remove(i); 595 } 596 } 597 } 598 599 public void clearOptions() 600 { 601 getOptions().clear(); 602 } 603 604 public Option getOption(String inOptionId) 605 { 606 for (Iterator it = getOptions().iterator(); it.hasNext();) 607 { 608 Option option = (Option)it.next(); 609 if (inOptionId.equals(option.getId())) 610 { 611 return option; 612 } 613 } 614 615 if (getParentCatalog() != null) 616 { 617 return getParentCatalog().getOption(inOptionId); 618 } 619 620 return null; 621 627 628 } 629 630 public void clearChildren() 631 { 632 getChildren().clear(); 633 } 634 635 public Category getChildByName(String inCatName) 636 { 637 for (Iterator iter = getChildren().iterator(); iter.hasNext();) 638 { 639 Category cat = (Category) iter.next(); 640 if ( cat.getName().equals(inCatName)) 641 { 642 return cat; 643 } 644 } 645 return null; 646 } 647 648 } 649 | Popular Tags |