1 5 6 package com.raptus.owxv3.api; 7 8 import java.sql.SQLException ; 9 import java.util.*; 10 11 import com.raptus.owxv3.*; 12 import com.raptus.owxv3.modules.categories.CategoryConstants; 13 14 15 16 40 public class CategorySelector extends Object 41 { 42 46 47 protected Vector categories=null; 48 49 50 55 protected Vector categoryNames=null; 56 57 58 62 63 protected Vector linkedCategories=null; 64 65 66 72 73 protected Vector linksToDelete; 74 75 76 80 81 protected Vector folderStats = null; 82 83 86 Hashtable isOpened = null; 87 88 protected String owner=null; 89 90 protected Locale[] availLocales=null; 91 92 protected Locale locale=null; 93 94 protected GlobalResources gres=null; 95 96 97 100 protected Hashtable iteminfos=null; 101 102 103 106 107 protected int categoryItemLimit=0; 108 109 110 111 public CategorySelector(String locales[]) 112 throws SQLException 113 { 114 this(locales,null,"",0); 115 } 116 117 public CategorySelector(String locales[],Locale l) 118 throws SQLException 119 { 120 this(locales,l,"",0); 121 } 122 123 public CategorySelector(String locales[],String own) 124 throws SQLException 125 { 126 this(locales,null,own,0); 127 } 128 129 public CategorySelector(String locales[],Locale l,String own) 130 throws SQLException 131 { 132 this(locales,l,own,0); 133 } 134 135 136 139 140 public CategorySelector(String locales[],Locale l,String own,int catlimit) 141 throws SQLException 142 { 143 availLocales = new Locale[locales.length]; 144 locale=l; 145 owner=own; 148 categories=new Vector(); 149 categoryNames=new Vector(); 150 gres=new GlobalResources(); 151 iteminfos=new Hashtable(); 152 153 linkedCategories=new Vector(); 154 linksToDelete=new Vector(); 155 isOpened = new Hashtable(); 156 folderStats=new Vector(); 157 categoryItemLimit=catlimit; 158 159 160 161 for(int i = 0; i < locales.length; i ++) 162 { 163 PairOfObjects po = LocaleManager.stripLocaleString(locales[i]); 164 if(po != null && po.isValid()) 165 availLocales[i] = new Locale((String ) po.getObjectOne(), (String ) po.getObjectTwo()); 166 } 167 168 loadCategories(); 169 } 171 172 String srctable; 173 174 public void setSrcTable(String srctable) 175 { 176 this.srctable = srctable; 177 } 178 179 public String getSrcTable() 180 { 181 return srctable; 182 } 183 184 public void loadLinkedCategories(int itemID,String fname) throws SQLException 185 { 186 linkedCategories=gres.loadCategoryLinksForItem(srctable, itemID,fname); 187 } 188 189 214 215 public void loadCategories() throws java.sql.SQLException 216 { 217 java.util.Vector v=gres.loadCategories(0,owner); 218 for (int i=0;i<v.size();i++) 219 { 220 GResCategory grcat=(GResCategory)v.elementAt(i); 221 222 categories.add(grcat); 223 if(i<v.size()-1) 224 { 225 folderStats.addElement(new String ("folder_close")); 226 loadChildCategories(grcat.getRowID(), false); 227 } 228 else 229 { 230 folderStats.addElement(new String ("folder_close")); 232 loadChildCategories(grcat.getRowID(), true); 233 } 234 } 236 loadCategoryNames(); 237 238 } 239 240 protected void loadChildCategories(int id, boolean abslast) throws java.sql.SQLException 241 { 242 java.util.Vector child=gres.loadChildCategories(id,owner); 243 for (int i=0;i<child.size();i++) 244 { 245 com.raptus.owxv3.api.GResCategory grcat=(com.raptus.owxv3.api.GResCategory)child.elementAt(i); 246 247 categories.add(grcat); 248 if(i<child.size()-1) 249 { 250 folderStats.addElement(new String ("folder_close")); 251 } 252 else 253 { 254 { 260 folderStats.addElement(new String ("folder_close_last")); 261 } 262 } 263 loadChildCategories(grcat.getRowID(), abslast); 264 } } 267 268 269 270 public Vector getFolderStatus() 271 { 272 Vector folderStatsr = new Vector(); 273 for(int i=0;i<categories.size();i++) 274 { 275 com.raptus.owxv3.api.GResCategory grcat=(com.raptus.owxv3.api.GResCategory)categories.elementAt(i); 276 String stat = (String )folderStats.elementAt(i); 277 if(this.isOpen(grcat.getCategoryID())) 279 { 280 if(stat.equals("folder_close")) 281 { 282 folderStatsr.addElement("folder_open"); 283 } 284 285 if(stat.equals("folder_close_last")) 286 { 287 folderStatsr.addElement("folder_open_last"); 288 } 289 290 if(stat.equals("folder_close_last_end")) 291 { 292 folderStatsr.addElement("folder_open_last_end"); 293 } 294 } 295 else 296 { 297 folderStatsr.addElement(stat); 298 } 299 } 300 return folderStatsr; 301 } 302 303 304 305 306 307 308 309 protected void loadCategoryNames() throws SQLException 310 { 311 GResCategory grcat=null; 312 int i,j; 313 for (i=0;i<categories.size();i++) 314 { 315 grcat=(GResCategory)categories.elementAt(i); 316 317 String catnames[]=new String [availLocales.length]; 318 for(j=0;j<availLocales.length;j++) 319 { 320 catnames[j]=gres.loadMessage(gres.getCategoryTableName(),grcat.getRowID(), CategoryConstants.RESCATFIELD_NAME,availLocales[j]); 321 if(catnames[j]==null) catnames[j]=""; 322 } 323 categoryNames.add(catnames); 324 } 325 } 327 328 329 330 public Vector getCategories() 331 { 332 return getCategories(locale); 333 } 334 335 336 339 340 341 342 public Vector getCategories(Locale l) 343 { 344 int localeindex=0; 345 Vector ret=new Vector(); 346 for(int i=0;i<availLocales.length;i++) 347 { 348 if( l.equals(availLocales[i]) ) 349 { 350 localeindex=i; 351 break; 352 } 353 } 355 String [] catnames=null; 356 for(int i=0;i<categories.size();i++) 357 { 358 PairOfObjects pair=new PairOfObjects(); 359 pair.setObjectOne( (GResCategory)categories.elementAt(i) ); 360 catnames=(String [])categoryNames.elementAt(i); 361 if(catnames[localeindex]!=null) pair.setObjectTwo( catnames[localeindex] ); 362 else pair.setObjectTwo( "" ); 363 ret.add(pair); 364 } 365 366 return ret; 367 } 369 370 371 375 376 public Vector getGResCategories() 377 { 378 return categories; 379 } 380 381 382 383 384 public Vector getCategoryNames() 385 { 386 return getCategoryNames(locale); 387 } 388 389 390 391 396 397 public Vector getCategoryNames(Locale l) 398 { 399 int localeindex=0; 400 Vector ret=new Vector(); 401 for(int i=0;i<availLocales.length;i++) 402 { 403 if( l.equals(availLocales[i]) ) 404 { 405 localeindex=i; 406 break; 407 } 408 } 410 String [] catnames=null; 411 for(int i=0;i<categories.size();i++) 412 { 413 414 catnames=(String [])categoryNames.elementAt(i); 415 if(catnames[localeindex]!=null) ret.add( catnames[localeindex] ); 416 else ret.add( "" ); 417 } 418 419 return ret; 420 421 } 422 423 424 425 public Vector getCategoriesWithItemDesc() throws SQLException 426 { 427 return getCategoriesWithItemDesc(locale); 428 } 429 430 431 435 436 437 438 public Vector getCategoriesWithItemDesc(Locale l) throws SQLException 439 { 440 int localeindex=0; 441 Vector ret=new Vector(); 442 for(int i=0;i<availLocales.length;i++) 443 { 444 if( l.equals(availLocales[i]) ) 445 { 446 localeindex=i; 447 break; 448 } 449 } 451 String [] catnames=null; 452 Vector itemids=null; 453 GResCategory grCat=null; 454 int i=0,j=0; 455 ItemInfoIFace iteminfo=null; 456 GResCatLink grCatLink=null; 457 for(i=0;i<categories.size();i++) 458 { 459 460 grCat=(GResCategory)categories.elementAt(i); 461 itemids=gres.loadCatLinks(grCat.getRowID()); 462 463 if(itemids!=null) 464 { 465 for(j=0;j<itemids.size();j++) 466 { 467 iteminfo=getItemInfo(grCat.getOwner()); 468 Hashtable hash=new Hashtable(); 469 hash.put("cat",grCat ); 470 471 catnames=(String [])categoryNames.elementAt(i); 472 if(catnames[localeindex]!=null) hash.put("name", catnames[localeindex] ); 473 else hash.put("name",""); 474 grCatLink=(GResCatLink)itemids.elementAt(j); 475 hash.put( "linkid",new Integer (grCatLink.getRowID()) ); 476 if(iteminfo!=null) hash.put( "iteminfo",iteminfo.itemInfo(grCatLink.getSrcRowID(),grCat.getOwner(),l) ); 477 else hash.put( "iteminfo",""); 478 479 480 481 ret.add(hash); 482 } } 485 486 } 487 488 return ret; 489 } 491 492 493 protected ItemInfoIFace getItemInfo(String owner) 494 { 495 if(iteminfos.containsKey(owner)) return (ItemInfoIFace)iteminfos.get(owner); 496 497 498 VModuleManager vmm = VModuleManager.getInstance(); 499 VModule vm = vmm.getVModule(owner); 500 ItemInfoIFace iteminfo=null; 501 String beanclassname=null; 502 try 503 { 504 XMLConfigManager cm = XMLConfigManager.getInstance(); 507 beanclassname=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=iteminfobean", "value"); 508 509 if(beanclassname==null) return null; 510 511 Class classObj = Class.forName(beanclassname); 512 if(classObj==null) return null; 513 iteminfo=(ItemInfoIFace)classObj.newInstance(); 514 } 516 catch(Exception e){e.printStackTrace();} 517 if(iteminfo!=null) iteminfos.put(owner,iteminfo); 518 return iteminfo; 519 520 } 521 522 523 public boolean isOpen(String id) 524 { 525 526 527 String v = (String )isOpened.get(id); 528 if(v!= null) 530 { 531 if(v.equals("true")) 532 { 533 return true; 534 } 535 else 536 { 537 return false; 538 } 539 } 540 else 541 { 542 return false; 543 } 544 } 545 546 public void setFolderState(String id, boolean opened) 547 { 548 isOpened.put(id,""+opened); 549 } 550 551 public void togleFolder(String id) 552 { 553 554 555 if(isOpen(id)) 556 { 557 isOpened.put(id, "false"); 558 } 559 else 560 { 561 isOpened.put(id, "true"); 562 } 563 } 565 566 567 public Vector getLinkedCategories() 568 { 569 return linkedCategories; 570 } 571 572 573 public boolean linkItemToCategory(int catid) 574 { 575 Integer I=new Integer (catid); 576 if(!linkedCategories.contains(I)) 577 { 578 GResCategory cat=null; 579 for(int i=0;i<categories.size();i++) 580 { 581 cat=(GResCategory)categories.elementAt(i); 582 if(cat.getRowID()==catid) 583 { 584 break; 585 } 586 } 588 if(cat==null) 589 { 590 LoggingManager.log("NO category with id:"+catid,this); 592 return true; 594 } 595 596 if(categoryItemLimit>0 && cat.getUsageCount()>=categoryItemLimit) return false; 597 linkedCategories.add(I); 598 } 599 600 return true; 601 } 602 603 604 public void unlinkItemFromCategory(int catid) 605 { 606 Integer I=new Integer (catid); 607 if(!linkedCategories.contains(I)) return; 608 609 GResCategory cat=null; 610 for(int i=0;i<categories.size();i++) 611 { 612 cat=(GResCategory)categories.elementAt(i); 613 if(cat.getRowID()==catid) 614 { 615 linksToDelete.add(I); 617 break; 618 } 619 } linkedCategories.remove(I); 621 } 622 623 624 public void saveCatLinks(String srctable,int itemid,String field) 625 throws SQLException 626 { 627 Integer I; 628 for(int i=0;i<linkedCategories.size();i++) 629 { 630 I=(Integer )linkedCategories.elementAt(i); 631 gres.saveCatLink(srctable,itemid,field, I.intValue()); 632 } 633 634 635 for(int i=0;i<linksToDelete.size();i++) 636 { 637 I=(Integer )linksToDelete.elementAt(i); 638 gres.deleteCatLink(srctable,itemid,field, I.intValue()); 639 } 640 } 641 642 643 public Vector getTree(int id,Locale l) 644 { 645 try 646 { 647 return gres.getTree(id,l); 648 } 649 catch(SQLException e){ return new Vector();} 650 } 651 652 public Vector getTree(String id,Locale l) 653 { 654 try 655 { 656 return gres.getTree(id,l); 657 } 658 catch(SQLException e){ return new Vector();} 659 } 660 661 public String getCategoryName(int id, Locale l) 662 { 663 664 String catname=null; 665 try 666 { 667 catname=gres.loadMessage(gres.getCategoryTableName(),id, CategoryConstants.RESCATFIELD_NAME,l); 668 } 669 catch(Exception e) 670 { 671 LoggingManager.log("Error while loading category name with id:"+id+" "+e,this); 672 } 673 if(catname==null) return ""; 674 return catname; 675 } 676 677 678 679 public void loadLinkedCategories(String srctable, int itemid, String field) 680 throws SQLException 681 { 682 683 linkedCategories=gres.loadCategoryLinks(srctable,itemid,field); 684 685 } 686 687 public Locale getLocale() 688 { 689 return locale; 690 } 691 692 public Hashtable getOpenStateTable() 693 { 694 return isOpened; 695 } 696 697 public void inheritState(CategorySelector old) 698 { 699 if(old==null) return; 700 Hashtable h=old.getOpenStateTable(); 701 if(h!=null) isOpened=h; 702 } 703 } 704 | Popular Tags |