KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > api > CategorySelector


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 package com.raptus.owxv3.api;
7
8 import java.sql.SQLException JavaDoc;
9 import java.util.*;
10
11 import com.raptus.owxv3.*;
12 import com.raptus.owxv3.modules.categories.CategoryConstants;
13
14
15
16 /**
17  *
18  * <hr>
19  * <table width="100%" border="0">
20  * <tr>
21  * <td width="24%"><b>Filename</b></td><td width="76%">CategorySelector.java</td>
22  * </tr>
23  * <tr>
24  * <td width="24%"><b>Author</b></td><td width="76%">REEA</td>
25  * </tr>
26  * <tr>
27  * <td width="24%"><b>Date</b></td><td width="76%">1st of October 2001</td>
28  * </tr>
29  * </table>
30  * <hr>
31  * <table width="100%" border="0">
32  * <tr>
33  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
34  * </tr>
35  * </table>
36  * <hr>
37  *
38  *
39  */

40 public class CategorySelector extends Object JavaDoc
41 {
42     /**
43      * Contains all available GResCategory objects allocated by
44      * the GlobalResources fro a specific owner module.
45      */

46
47     protected Vector categories=null;
48
49
50     /**
51      *
52      *Contains String[] objects as category the corespondig category
53      *in categories Vector. The order is ensured by availlocales.
54      */

55     protected Vector categoryNames=null;
56
57
58     /**
59      * Contains Category rowid Integer objects to wich a new (or any other) entry is linked
60      *
61      */

62
63     protected Vector linkedCategories=null;
64
65
66     /**
67      *
68      *Contains Category rowid Integer objects to wich this entry was
69      *linked in the database but it was deleted
70      *
71      */

72
73     protected Vector linksToDelete;
74
75
76     /**
77      *
78      *Contains, String describing folder status and position in the tree
79      */

80
81     protected Vector folderStats = null;
82
83     /**
84      * Contains (RowId, (true,false))
85      */

86     Hashtable isOpened = null;
87
88     protected String JavaDoc owner=null;
89
90     protected Locale[] availLocales=null;
91
92     protected Locale locale=null;
93
94     protected GlobalResources gres=null;
95
96
97     /**
98      *
99      */

100     protected Hashtable iteminfos=null;
101
102
103     /**
104      *
105      */

106
107     protected int categoryItemLimit=0;
108
109
110
111     public CategorySelector(String JavaDoc locales[])
112     throws SQLException JavaDoc
113     {
114         this(locales,null,"",0);
115     }
116
117     public CategorySelector(String JavaDoc locales[],Locale l)
118     throws SQLException JavaDoc
119     {
120         this(locales,l,"",0);
121     }
122
123     public CategorySelector(String JavaDoc locales[],String JavaDoc own)
124     throws SQLException JavaDoc
125     {
126         this(locales,null,own,0);
127     }
128
129     public CategorySelector(String JavaDoc locales[],Locale l,String JavaDoc own)
130     throws SQLException JavaDoc
131     {
132         this(locales,l,own,0);
133     }
134
135
136     /**
137      *
138      */

139
140     public CategorySelector(String JavaDoc locales[],Locale l,String JavaDoc own,int catlimit)
141     throws SQLException JavaDoc
142     {
143         availLocales = new Locale[locales.length];
144         locale=l;
145         //urltablename= urltblname;
146
//maxLinkCount=linkcount;
147
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 JavaDoc) po.getObjectOne(), (String JavaDoc) po.getObjectTwo());
166         }
167
168         loadCategories();
169         //LoggingManager.log("Owner in catsel is "+owner);
170
}
171
172     String JavaDoc srctable;
173
174     public void setSrcTable(String JavaDoc srctable)
175     {
176         this.srctable = srctable;
177     }
178
179     public String JavaDoc getSrcTable()
180     {
181         return srctable;
182     }
183
184     public void loadLinkedCategories(int itemID,String JavaDoc fname) throws SQLException JavaDoc
185     {
186         linkedCategories=gres.loadCategoryLinksForItem(srctable, itemID,fname);
187     }
188
189     /*protected void loadCategories() throws SQLException
190     {
191         Vector v=gres.loadCategories(0,owner);
192         for (int i=0;i<v.size();i++)
193         {
194             GResCategory grcat=(GResCategory)v.elementAt(i);
195
196             categories.add(grcat);
197             loadChildCategories(grcat.getRowID());
198         }
199         loadCategoryNames();
200     }
201
202     protected void loadChildCategories(int id) throws SQLException
203     {
204         Vector child=gres.loadChildCategories(id,owner);
205         for (int i=0;i<child.size();i++)
206         {
207             GResCategory grcat=(GResCategory)child.elementAt(i);
208
209             categories.add(grcat);
210             loadChildCategories(grcat.getRowID());
211         }//enf for
212     }//end void
213     */

214
215      public void loadCategories() throws java.sql.SQLException JavaDoc
216     {
217         java.util.Vector JavaDoc 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 JavaDoc("folder_close"));
226                 loadChildCategories(grcat.getRowID(), false);
227             }
228             else
229             {
230                 //folderStats.addElement(new String("folder_close_last"));
231
folderStats.addElement(new String JavaDoc("folder_close"));
232                 loadChildCategories(grcat.getRowID(), true);
233             }
234         }//end for
235

236         loadCategoryNames();
237
238     }
239
240     protected void loadChildCategories(int id, boolean abslast) throws java.sql.SQLException JavaDoc
241     {
242         java.util.Vector JavaDoc 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 JavaDoc("folder_close"));
251             }
252             else
253             {
254                 //if(abslast)
255
// {
256
// folderStats.addElement(new String("folder_close_last_end"));
257
// }
258
// else
259
{
260                     folderStats.addElement(new String JavaDoc("folder_close_last"));
261                 }
262             }
263             loadChildCategories(grcat.getRowID(), abslast);
264         }//enf for
265
}//end void
266

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 JavaDoc stat = (String JavaDoc)folderStats.elementAt(i);
277             //LoggingManager.log("Stat:"+stat+" CatId:"+grcat.getCategoryID()+" Open:"+this.isOpen(grcat.getCategoryID()));
278
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 JavaDoc
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 JavaDoc catnames[]=new String JavaDoc[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     }//end loadCategoryNames
326

327
328
329
330     public Vector getCategories()
331     {
332         return getCategories(locale);
333     }
334
335
336     /**
337      *return Vector of PairOfObjects with (GResCategory,String)
338      */

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         }//end for
354

355         String JavaDoc[] 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 JavaDoc[])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     }//end getCategories
368

369
370
371     /**
372      *return Vector of GResCategory objects
373      *
374      */

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     /**
392      *return Vector of String category_names
393      *
394      *
395      */

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         }//end for
409

410         String JavaDoc[] catnames=null;
411         for(int i=0;i<categories.size();i++)
412         {
413
414             catnames=(String JavaDoc[])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 JavaDoc
426     {
427         return getCategoriesWithItemDesc(locale);
428     }
429
430
431     /**
432      *return Vector of Hashtables with
433      *(cat => GResCategory, name => String, iteminfo => String, itemid => Integer)
434      */

435
436
437
438     public Vector getCategoriesWithItemDesc(Locale l) throws SQLException JavaDoc
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         }//end for
450

451         String JavaDoc[] 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 JavaDoc[])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 JavaDoc(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                 }//end for
483
}//end if
484

485
486         }
487
488         return ret;
489     }//end getCategories
490

491
492
493     protected ItemInfoIFace getItemInfo(String JavaDoc 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 JavaDoc beanclassname=null;
502        try
503        {
504            //OOPSHERE
505
//beanclassname=vm.getStringProperty(CategoryConstants.VMODULE_PROPERTY_ITEM_INFO_BEAN);
506
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 JavaDoc classObj = Class.forName(beanclassname);
512            if(classObj==null) return null;
513            iteminfo=(ItemInfoIFace)classObj.newInstance();
514            //return null;
515
}
516        catch(Exception JavaDoc e){e.printStackTrace();}
517        if(iteminfo!=null) iteminfos.put(owner,iteminfo);
518        return iteminfo;
519
520     }
521
522
523     public boolean isOpen(String JavaDoc id)
524     {
525
526
527         String JavaDoc v = (String JavaDoc)isOpened.get(id);
528         //LoggingManager.log("Id="+id+" V="+v);
529
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 JavaDoc id, boolean opened)
547     {
548             isOpened.put(id,""+opened);
549     }
550
551     public void togleFolder(String JavaDoc 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         //LoggingManager.log("Folder "+id+" toggle2:"+isOpened.get(id));
564
}
565
566
567     public Vector getLinkedCategories()
568     {
569         return linkedCategories;
570     }
571
572
573     public boolean linkItemToCategory(int catid)
574     {
575         Integer JavaDoc I=new Integer JavaDoc(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              }//end for
587

588             if(cat==null)
589             {
590                 //something realy screwed up, no category with catid
591
LoggingManager.log("NO category with id:"+catid,this);
592                 //ingnore operation
593
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 JavaDoc I=new Integer JavaDoc(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               //the category link is in the DB put in the to be romvoed list
616
linksToDelete.add(I);
617               break;
618           }
619       }//end for
620
linkedCategories.remove(I);
621     }
622
623
624     public void saveCatLinks(String JavaDoc srctable,int itemid,String JavaDoc field)
625     throws SQLException JavaDoc
626     {
627         Integer JavaDoc I;
628         for(int i=0;i<linkedCategories.size();i++)
629         {
630             I=(Integer JavaDoc)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 JavaDoc)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 JavaDoc e){ return new Vector();}
650     }
651
652     public Vector getTree(String JavaDoc id,Locale l)
653     {
654         try
655         {
656             return gres.getTree(id,l);
657         }
658         catch(SQLException JavaDoc e){ return new Vector();}
659     }
660
661     public String JavaDoc getCategoryName(int id, Locale l)
662     {
663         
664         String JavaDoc catname=null;
665         try
666         {
667             catname=gres.loadMessage(gres.getCategoryTableName(),id, CategoryConstants.RESCATFIELD_NAME,l);
668         }
669         catch(Exception JavaDoc 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 JavaDoc srctable, int itemid, String JavaDoc field)
680     throws SQLException JavaDoc
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