KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
9 import java.util.*;
10
11 import javax.sql.DataSource JavaDoc;
12
13 import com.raptus.owxv3.*;
14 import com.raptus.owxv3.api.dataxs.*;
15 import com.raptus.owxv3.modules.categories.CategoryConstants;
16
17 /**
18  *
19  * <hr>
20  * <table width="100%" border="0">
21  * <tr>
22  * <td width="24%"><b>Filename</b></td><td width="76%">GlobalResources.java</td>
23  * </tr>
24  * <tr>
25  * <td width="24%"><b>Author</b></td><td width="76%">Guy Z�rcher (gzuercher@raptus.com)</td>
26  * </tr>
27  * <tr>
28  * <td width="24%"><b>Date</b></td><td width="76%">24th of April 2001</td>
29  * </tr>
30  * </table>
31  * <hr>
32  * <table width="100%" border="0">
33  * <tr>
34  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
35  * </tr>
36  * </table>
37  * <hr>
38  */

39 public class GlobalResources extends Object JavaDoc
40 {
41     /**
42      *
43      */

44     public static final String JavaDoc FILETYPE_FILE = "file";
45     public static final String JavaDoc FILETYPE_PICTURE = "picture";
46     //public static final String FILETYPE_SOUND = "sound";
47
//public static final String FILETYPE_MOVIE = "movie";
48
//public static final String FILETYPE_PDF = "pdf";
49

50     /**
51      *
52      */

53     protected DataSource JavaDoc ds = null;
54
55     /**
56      *
57      */

58     protected OwxmsgsManager msgsMgr = null;
59
60     /**
61      *
62      */

63     protected OwxurlsManager urlsMgr = null;
64
65     /**
66      *
67      */

68     protected OwxfilesManager filesMgr = null;
69
70     /**
71      *
72      */

73     protected OwxflinksManager flinksMgr = null;
74
75
76     /**
77      *
78      */

79     protected OwxcategoryManager categoryMgr=null;
80
81     /**
82      *
83      */

84     protected OwxcatlinksManager catlinksMgr=null;
85     
86     /**
87      *
88      */

89     protected OwxfieldsManager fieldsMgr=null;
90     
91
92     /**
93      *
94      */

95     public GlobalResources()
96     {
97         ds = VModuleManager.getInstance().getDataSource(VModuleManager.GLOBALRESOURCES_DS);
98
99         msgsMgr = new OwxmsgsManager();
100         msgsMgr.setDataSource(ds);
101
102         urlsMgr = new OwxurlsManager();
103         urlsMgr.setDataSource(ds);
104
105         filesMgr = new OwxfilesManager();
106         filesMgr.setDataSource(ds);
107
108         flinksMgr = new OwxflinksManager();
109         flinksMgr.setDataSource(ds);
110
111         categoryMgr=new OwxcategoryManager();
112         categoryMgr.setDataSource(ds);
113
114         catlinksMgr=new OwxcatlinksManager();
115         catlinksMgr.setDataSource(ds);
116         
117         fieldsMgr=new OwxfieldsManager();
118         fieldsMgr.setDataSource(ds);
119     }
120
121     /**
122      *
123      */

124     protected void prepareURLObject(Owxurls url, GResURL grURL)
125     {
126         if(url == null || grURL == null)
127             return;
128
129         grURL.setRowID(url.getRowid());
130         grURL.setProtocol(url.getFname());
131         grURL.setFullURL(url.getUrl());
132     }
133
134     /**
135      *
136      */

137     protected void prepareFileObject(Owxfiles file, GResFile grFile)
138     {
139         if(file == null || grFile == null)
140             return;
141
142         grFile.setRowID(file.getRowid());
143         grFile.setUsageCount(file.getUsagectr());
144         grFile.setFileSize(file.getFsize());
145         grFile.setFileURL(file.getUrl());
146         grFile.setCategory(file.getCategory());
147         grFile.setType(file.getFtype());
148         grFile.setInfo(file.getFinfo());
149     }
150
151     // MESSAGES ////////////////////////////////////////////////////////////////
152

153     /**
154      *
155      */

156     public String JavaDoc loadMessage(String JavaDoc srctable, int srcrowid, String JavaDoc field, Locale locale)
157                   throws SQLException
158     {
159         String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
160                        "fname = '" + field + "' AND localekey = '" + locale.toString() + "'";
161 // LoggingManager.log(where, this);
162
Owxmsgs[] msgs = msgsMgr.loadByWhere(where);
163         if(msgs != null && msgs.length > 0)
164             return msgs[0].getMsg();
165
166         return null;
167     }
168
169     /**
170      *
171      */

172     public boolean saveMessage(String JavaDoc srctable, int srcrowid, String JavaDoc field, Locale locale, String JavaDoc msg)
173                    throws SQLException
174     {
175         // check if the record is already existing. If so, load its defaults
176
Owxmsgs msgObj = null;
177         String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
178                        "fname = '" + field + "' AND localekey = '" + locale.toString() + "'";
179         Owxmsgs[] msgObjs = msgsMgr.loadByWhere(where);
180         if(msgObjs.length == 0)
181         {
182             // It's necessary to create a new record ; fill it up with default keys
183
msgObj = new Owxmsgs();
184             msgObj.setSrctbl(srctable);
185             msgObj.setSrcrowid(srcrowid);
186             msgObj.setFname(field);
187             msgObj.setLocalekey(locale.toString());
188         }
189         else
190             msgObj = msgObjs[0];
191
192         // in order to keep the new record ID's low, the message is not deleted if
193
// it is empty, but set to a null value (means no display)
194
if(msg == null || msg.trim().length() == 0)
195             msgObj.setMsg(null);
196         else
197             msgObj.setMsg(msg);
198
199         msgsMgr.save(msgObj);
200         LoggingManager.log("Saved message for " + srctable + " #" + srcrowid + ", " +
201                            field + ", " + locale.getDisplayLanguage(), this);
202
203         return true;
204     }
205
206     /**
207      *
208      */

209     public boolean deleteMessage(String JavaDoc srctable, int srcrowid, String JavaDoc field, Locale locale)
210                    throws SQLException
211     {
212
213         // check if the record is existing
214
String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
215                        "fname = '" + field + "' AND localekey = '" + locale.toString() + "'";
216         Owxmsgs[] msgObjs = msgsMgr.loadByWhere(where);
217         if(msgObjs.length > 0)
218         {
219             int result = msgsMgr.deleteByKey(msgObjs[0].getRowid());
220             LoggingManager.log("Deleted message #" + msgObjs[0].getRowid() + " for " + srctable + ", " +
221                                field + ", " + locale.getDisplayLanguage() + ", count " + result, this);
222             return true;
223         }
224
225         return false;
226     }
227
228     /**
229      *
230      */

231     public boolean deleteMessages(String JavaDoc srctable, int srcrowid, String JavaDoc field)
232                    throws SQLException
233     {
234         // loop through all records of the same locale
235
String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
236                        "fname = '" + field + "'";
237         Owxmsgs[] msgObjs = msgsMgr.loadByWhere(where);
238         for(int i = 0; i < msgObjs.length; i ++)
239         {
240             int result = msgsMgr.deleteByKey(msgObjs[i].getRowid());
241             LoggingManager.log("Deleted message #" + msgObjs[i].getRowid() + " for " + srctable + ", " +
242                                field + ", count " + result, this);
243         }
244
245         return true;
246     }
247     
248     
249     /**
250      *Method for deleting all the messages assigned to a entry
251      */

252     public boolean deleteAllMessages(String JavaDoc srctable, int srcrowid)
253                    throws SQLException
254     {
255         // loop through all records of the same locale
256
String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid ;
257         Owxmsgs[] msgObjs = msgsMgr.loadByWhere(where);
258         for(int i = 0; i < msgObjs.length; i ++)
259         {
260             int result = msgsMgr.deleteByKey(msgObjs[i].getRowid());
261             LoggingManager.log("Deleted message #" + msgObjs[i].getRowid() + " for " + srctable + ", "
262                                 +msgObjs[i].getFname()+ ", count " + result, this);
263         }
264
265         return true;
266     }
267     
268
269     // URLS ////////////////////////////////////////////////////////////////////
270

271     /**
272      *
273      */

274     public Vector loadURLsAndTitles(String JavaDoc srctable, int srcrowid, String JavaDoc field,Locale locale)
275                   throws SQLException
276     {
277         String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
278                        "fname = '" + field + "'";
279
280         Vector ret = new Vector();
281         Owxurls[] urls = urlsMgr.loadByWhere(where);
282         for(int i = 0; i < urls.length; i ++)
283         {
284             GResTitledURL grURL = new GResTitledURL();
285             prepareURLObject(urls[i], grURL);
286             grURL.setTitle( loadMessage(urlsMgr.getTableID(),urls[i].getRowid() , field, locale) );
287             ret.add(grURL);
288         }
289
290         return ret;
291     }
292
293
294     /**
295      *
296      */

297      public Vector loadURLs(String JavaDoc srctable, int srcrowid, String JavaDoc field)
298                   throws SQLException
299     {
300         String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
301                        "fname = '" + field + "'";
302
303         Vector ret = new Vector();
304         Owxurls[] urls = urlsMgr.loadByWhere(where);
305         for(int i = 0; i < urls.length; i ++)
306         {
307             GResURL grURL = new GResURL();
308             prepareURLObject(urls[i], grURL);
309             ret.add(grURL);
310         }
311
312         return ret;
313     }
314
315
316
317
318     /**
319      *
320      */

321     public boolean saveURL(String JavaDoc srctable, int srcrowid, String JavaDoc field, GResURL url)
322                    throws SQLException
323     {
324         // check if the record is already existing. If so, load its defaults
325
Owxurls urlObj = null;
326         String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
327                        "fname = '" + field + "' AND url = '" + url.getURL() + "'";
328         Owxurls[] urlObjs = urlsMgr.loadByWhere(where);
329         if(urlObjs.length == 0)
330         {
331             // It's necessary to create a new record ; fill it up with default keys
332
urlObj = new Owxurls();
333             urlObj.setSrctbl(srctable);
334             urlObj.setSrcrowid(srcrowid);
335             urlObj.setFname(field);
336         }
337         else
338             urlObj = urlObjs[0];
339
340         urlObj.setUrl(url.getURL());
341         urlsMgr.save(urlObj);
342
343         //setting the new id to reflect in the GResURL object too
344
url.setRowID(urlObj.getRowid());
345
346         LoggingManager.log("Saved URL for " + srctable + " #" + srcrowid + ", " +
347                    field + ", " + url.getDisplayedURL(), this);
348         return true;
349     }
350
351     /**
352      *
353      */

354     public boolean deleteURL(GResURL url)
355                    throws SQLException
356     {
357         if(url == null)
358             return false;
359         
360         urlsMgr.deleteByKey(url.getRowID());
361         
362         LoggingManager.log("Deleted URL #" + url.getRowID() + ", " + url.getProtocol() +
363                            ", " + url.getDisplayedURL(), this);
364         return true;
365     }
366
367     /**
368      *returns the list of deleted URL id, or null
369      */

370     public Vector deleteURLs(String JavaDoc srctable, int srcrowid, String JavaDoc field)
371                    throws SQLException
372     {
373         try
374         {
375             Vector ret=new Vector();
376
377             // check if the record is already existing. If so, load its defaults
378
Owxurls urlObj = null;
379             String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
380                            "fname = '" + field + "'";
381             Owxurls[] urlObjs = urlsMgr.loadByWhere(where);
382             for(int i = 0; i < urlObjs.length; i ++)
383             {
384                 ret.add( new Integer JavaDoc(urlObjs[i].getRowid()) );
385                 GResURL grURL = new GResURL();
386                 prepareURLObject(urlObjs[i], grURL);
387                 deleteURL(grURL);
388             }
389             return ret;
390         }
391         catch(Exception JavaDoc e)
392         {
393           LoggingManager.log("FAILED to delete the URLs, error:"+e, this);
394
395         }
396
397         LoggingManager.log("Deleted URL for " + srctable + " #" + srcrowid + ", " + field, this);
398         return null;
399     }
400
401     // FILES and PICTURES //////////////////////////////////////////////////////
402

403     /**
404      *returns a Vector that contains PairOfObject(GresFile,Integer(linkid) )
405      */

406     public Vector loadLinkedFilesAndIds(String JavaDoc ftype, String JavaDoc srctable, int srcrowid, String JavaDoc field)
407                                   throws SQLException
408     {
409         String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
410                        "fname = '" + field + "'";
411 // LoggingManager.log(where, this);
412
Vector ret = new Vector();
413         Owxflinks[] flinks = flinksMgr.loadByWhere(where);
414         Owxfiles files[] = filesMgr.loadAll();
415         int i=0,j=0;
416         for(i = 0; i < flinks.length; i ++)
417         {
418
419             for(j=0;j<files.length;j++)
420             {
421                 if(flinks[i].getFileid()==files[j].getRowid())
422                 {
423                     GResFile grObj = (ftype.equals(FILETYPE_FILE) ? new GResFile() : (GResFile) new GResPicture());
424                     prepareFileObject(files[j], grObj);
425                     PairOfObjects pair=new PairOfObjects();
426                     pair.setObjectOne(grObj);
427                     pair.setObjectTwo( new Integer JavaDoc(flinks[i].getRowid()) );
428                     ret.add(pair);
429                     break;
430                 }
431             }//end for
432
}//end for
433

434         return ret;
435     }
436
437     /**
438      *returns a Vector that contains GResFile or GResPicture objects
439      */

440     public Vector loadLinkedFiles(String JavaDoc ftype, String JavaDoc srctable, int srcrowid, String JavaDoc field)
441                                   throws SQLException
442     {
443         String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
444                        "fname = '" + field + "'";
445
446         Vector ret = new Vector();
447         Owxflinks[] flinks = flinksMgr.loadByWhere(where);
448         Owxfiles files[] = filesMgr.loadAll();
449         int i=0,j=0;
450         for(i = 0; i < flinks.length; i ++)
451         {
452              for(j=0;j<files.length;j++)
453             {
454                 if(flinks[i].getFileid()==files[j].getRowid())
455                 {
456                     GResFile grObj = (ftype.equals(FILETYPE_FILE) ? new GResFile() : (GResFile) new GResPicture());
457                     prepareFileObject(files[j], grObj);
458
459                     ret.add(grObj);
460                     break;
461                 }
462             }
463         }
464
465         return ret;
466     }
467
468
469     /**
470      *returns a Vector that contains GResTitledFile objects
471      */

472     public Vector loadLinkedFilesAndTitles(String JavaDoc ftype, String JavaDoc srctable, int srcrowid, String JavaDoc field, String JavaDoc linkfield, Locale locale)
473                                   throws SQLException
474     {
475         String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
476                        "fname = '" + field + "'";
477
478         Vector ret = new Vector();
479         Owxflinks[] flinks = flinksMgr.loadByWhere(where);
480         Owxfiles files[] = filesMgr.loadAll();
481         int i=0,j=0;
482         for(i = 0; i < flinks.length; i ++)
483         {
484             for(j=0;j<files.length;j++)
485             {
486                 if(flinks[i].getFileid()==files[j].getRowid())
487                 {
488                     GResTitledFile grObj = new GResTitledFile();
489                     prepareFileObject(files[j], grObj);
490                     grObj.setTitle( loadMessage(flinksMgr.getTableID(), flinks[i].getRowid(), linkfield,locale) );
491                     ret.add(grObj);
492                     break;
493                 }
494             }
495         }
496
497         return ret;
498     }
499
500
501
502     /**
503      *
504      */

505     public Vector loadLinkablePics()
506                   throws SQLException
507     {
508         String JavaDoc where = "ftype = '" + FILETYPE_PICTURE +
509                        "' AND fsize > 0 AND (url != '' OR url != null)";
510
511         Vector ret = new Vector();
512         Owxfiles[] pics = filesMgr.loadByWhere(where, "category DESC", 0);
513         for(int i = 0; i < pics.length; i ++)
514         {
515             GResPicture grPic = new GResPicture();
516             prepareFileObject((Owxfiles) pics[i], grPic);
517             ret.add(grPic);
518         }
519
520         return ret;
521     }
522
523     /**
524      *
525      */

526     public String JavaDoc[] loadAllPictureCategories()
527                     throws SQLException
528     {
529         String JavaDoc sql = "select distinct category from owxfiles where ftype='" +
530                      FILETYPE_PICTURE + "' AND (url != '' OR url != null);";
531
532         ResultSet rs = null;
533         Statement stmt = null;
534         Connection conn = ds.getConnection();
535         try
536         {
537             stmt = conn.createStatement();
538             rs = stmt.executeQuery(sql);
539
540             Vector v = new Vector();
541             while(rs.next())
542                 v.addElement(rs.getString(1));
543
544             rs.close();
545             stmt.close();
546             if(conn!=null) conn.close();
547             return (String JavaDoc[]) v.toArray( new String JavaDoc[v.size()] );
548         }
549         catch(SQLException e) {
550             if(rs != null) try { rs.close(); } catch(Exception JavaDoc e2) { }
551             if(stmt !=null) try { stmt.close(); } catch(Exception JavaDoc e2) { }
552             throw e;
553         }
554
555     }
556
557
558
559 //--------------------------------Added by REEA-----------------------------------------
560

561
562
563
564     /**
565      *
566      */

567     public String JavaDoc[] loadAllFileCategories()
568                     throws SQLException
569     {
570         String JavaDoc sql = "select distinct category from owxfiles where ftype='" +
571                      FILETYPE_FILE + "' AND url != '' AND url IS NOT NULL;";
572
573         ResultSet rs = null;
574         Statement stmt = null;
575         Connection conn = ds.getConnection();
576         try
577         {
578             stmt = conn.createStatement();
579             rs = stmt.executeQuery(sql);
580
581             Vector v = new Vector();
582             while(rs.next())
583                 v.addElement(rs.getString(1));
584
585             rs.close();
586             stmt.close();
587             if(conn!=null) conn.close();
588             return (String JavaDoc[]) v.toArray( new String JavaDoc[v.size()] );
589         }
590         catch(SQLException e) {
591             if(rs != null) try { rs.close(); } catch(Exception JavaDoc e2) { }
592             if(stmt !=null) try { stmt.close(); } catch(Exception JavaDoc e2) { }
593             throw e;
594         }
595
596     }
597
598
599
600
601
602     /**
603      *
604      */

605     public Vector loadLinkableFiles()
606                   throws SQLException
607     {
608         String JavaDoc where = "ftype = '" + FILETYPE_FILE +
609                        "' AND fsize > 0 AND url != '' AND url IS NOT null";
610
611         Vector ret = new Vector();
612         Owxfiles[] pics = filesMgr.loadByWhere(where, "category, ftype, url desc", 0);
613         for(int i = 0; i < pics.length; i ++)
614         {
615             GResFile grfile = new GResFile();
616             prepareFileObject((Owxfiles) pics[i], grfile);
617             ret.add(grfile);
618         }
619
620         return ret;
621     }
622
623
624
625
626
627
628
629
630
631
632     /**
633      *
634      *Method for getting all the category names
635      */

636
637
638     public String JavaDoc[] loadAllCategories()
639                     throws SQLException
640     {
641         String JavaDoc sql = "select distinct category from owxfiles where url != '' AND url IS NOT NULL;";
642
643         ResultSet rs = null;
644         Statement stmt = null;
645         Connection conn = ds.getConnection();
646         try
647         {
648             stmt = conn.createStatement();
649             rs = stmt.executeQuery(sql);
650
651             Vector v = new Vector();
652             while(rs.next())
653                 v.addElement(rs.getString(1));
654
655             rs.close();
656             stmt.close();
657             if(conn!=null) conn.close();
658
659             return (String JavaDoc[]) v.toArray( new String JavaDoc[v.size()] );
660         }
661         catch(SQLException e) {
662             if(rs != null) try { rs.close(); } catch(Exception JavaDoc e2) { }
663             if(stmt !=null) try { stmt.close(); } catch(Exception JavaDoc e2) { }
664             throw e;
665         }
666
667     }
668
669     /**
670      *Method for loading the files as a Vector of GResFile or GResPicture objects
671      */

672     public Vector loadFiles()
673                   throws SQLException
674     {
675         return loadFiles("");
676     }
677     
678     /**
679      *Method for loading the files as a Vector of GResFile or GResPicture objects
680      */

681     public Vector loadFiles(String JavaDoc filter)
682                   throws SQLException
683     {
684         String JavaDoc where = "fsize > 0 and url!='' and url IS NOT NULL";
685         if(filter!=null && !filter.trim().equals(""))
686         {
687             String JavaDoc f=filter.toLowerCase();
688             where=where+" and (lower(url) LIKE '%"+FILETYPE_FILE.toLowerCase()+"/%"+f+"%' or lower(url) LIKE '%"+FILETYPE_PICTURE.toLowerCase()+"/%"+f+"%' or lower(category) LIKE '%"+f+"%'"+
689                  " or lower(ftype) LIKE '%"+f+"%')";
690         }
691         
692         Vector ret = new Vector();
693         Owxfiles[] files = filesMgr.loadByWhere(where, "category, ftype, url desc", 0);
694         for(int i = 0; i < files.length; i ++)
695         {
696             if(files[i].getFtype().compareToIgnoreCase(FILETYPE_PICTURE)==0)
697             {
698                 GResPicture gresPic=new GResPicture();
699                 prepareFileObject((Owxfiles) files[i], gresPic);
700                 ret.add(gresPic);
701             }
702             else
703             {
704                 GResFile gresFile=new GResFile();
705                 prepareFileObject((Owxfiles) files[i], gresFile);
706                 ret.add(gresFile);
707             }
708         }
709
710         return ret;
711     }
712
713
714
715
716
717
718     /**
719      *Method for loading a sigle file based on his id
720      */

721     public GResFile loadFile(int fileid)
722                   throws SQLException
723     {
724         String JavaDoc where = "rowid="+fileid;
725
726
727
728         Owxfiles[] files = filesMgr.loadByWhere(where);
729         if(files.length==0)
730             return null;
731
732         if(files[0].getFtype().compareToIgnoreCase(FILETYPE_PICTURE)==0)
733             {
734                 GResPicture gresPic=new GResPicture();
735                 prepareFileObject((Owxfiles) files[0], gresPic);
736                 return gresPic;
737             }
738             else
739             {
740                 GResFile gresFile=new GResFile();
741                 prepareFileObject((Owxfiles) files[0], gresFile);
742                 return gresFile;
743             }
744
745     }
746
747
748     /**
749      *Method for loading a file based on his name
750      */

751     public GResFile loadFile(String JavaDoc filename)
752                   throws SQLException
753     {
754         String JavaDoc where = "url LIKE '%"+filename+"'";
755
756
757
758         Owxfiles[] files = filesMgr.loadByWhere(where);
759         if(files.length==0)
760             return null;
761
762         if(files[0].getFtype().compareToIgnoreCase(FILETYPE_PICTURE)==0)
763             {
764                 GResPicture gresPic=new GResPicture();
765                 prepareFileObject((Owxfiles) files[0], gresPic);
766                 return gresPic;
767             }
768             else
769             {
770                 GResFile gresFile=new GResFile();
771                 prepareFileObject((Owxfiles) files[0], gresFile);
772                 return gresFile;
773             }
774
775     }
776
777
778     /**
779      *Method for loading the file assigment as a Vector of GResFileLink objects
780      */

781     public Vector loadFileLinks() throws SQLException
782     {
783         return loadFileLinks("");
784     }
785
786     /**
787      *Method for loading the file assigment as a Vector of GResFileLink objects
788      *base on a filter
789      */

790
791     public Vector loadFileLinks(String JavaDoc filter) throws SQLException
792     {
793
794         Vector ret=new Vector();
795         Owxflinks[] links=null;
796         VModuleManager vmanager=VModuleManager.getInstance();
797         if(filter.equals(""))
798         {
799             links=flinksMgr.loadAll();
800             for(int i=0;i<links.length;i++)
801             {
802                 GResFileLink gresflink=new GResFileLink();
803
804                 gresflink.setSrctable(links[i].getSrctbl());
805                 gresflink.setOwnerVModule( vmanager.getTableOwnerVModule(links[i].getSrctbl()) );
806                 gresflink.setLinkid(links[i].getRowid());
807                 gresflink.setGResFile( loadFile(links[i].getFileid()) );
808                 ret.add(gresflink);
809             }//end for
810
}
811         else
812         {
813             Vector files=loadFiles(filter);
814             if(files.size()==0)
815             {
816                 //no files match the filter
817
return ret;
818             }
819             StringBuffer JavaDoc where=new StringBuffer JavaDoc();
820             where.append("fileid IN(");
821             String JavaDoc sep="";
822             for(int i=0;i<files.size();i++)
823             {
824                 where.append(sep);
825                 where.append(((GResFile)files.get(i)).getRowID());
826                 sep=",";
827             }//end for
828
where.append(")");
829             links=flinksMgr.loadByWhere(where.toString());
830             GResFile file=null;
831             for(int i=0;i<links.length;i++)
832             {
833                 GResFileLink gresflink=new GResFileLink();
834
835                 gresflink.setSrctable(links[i].getSrctbl());
836                 gresflink.setOwnerVModule( vmanager.getTableOwnerVModule(links[i].getSrctbl()) );
837                 gresflink.setLinkid(links[i].getRowid());
838
839                 for(int j=0;j<files.size();j++)
840                 {
841                     file=(GResFile)files.get(j);
842                     if(file.getRowID()==links[i].getFileid())
843                     {
844                         gresflink.setGResFile(file);
845                         break;
846                     }
847                 }//end for
848
ret.add(gresflink);
849             }//end big for
850
}//end else
851
return ret;
852
853     }
854
855
856
857     /**
858      *Method for getting the used file space in bytes.
859      */

860     public int getUsedFileSpace() throws SQLException
861     {
862         String JavaDoc sql = "select sum(fsize) from owxfiles;";
863
864         ResultSet rs = null;
865         Statement stmt = null;
866         Connection conn = ds.getConnection();
867         try
868         {
869             stmt = conn.createStatement();
870             rs = stmt.executeQuery(sql);
871
872             int usedspace=0;
873             if(rs.next())
874                 usedspace=rs.getInt(1);
875
876             rs.close();
877             stmt.close();
878             if(conn!=null) conn.close();
879             return usedspace;
880         }
881         catch(SQLException e) {
882             if(rs != null) try { rs.close(); } catch(Exception JavaDoc e2) { }
883             if(stmt !=null) try { stmt.close(); } catch(Exception JavaDoc e2) { }
884             throw e;
885         }
886
887     }
888
889
890
891     /**
892      *Method for saving a link
893      *returns -1 if something went wrong e
894      *else returns the new id of the link just created
895      */

896     public int saveFileLink(String JavaDoc srctable, int srcrowid, String JavaDoc field, GResFile grFile)
897                    throws SQLException
898     {
899
900
901         String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
902                        "fname = '" + field + "' AND fileid = " + grFile.getRowID();
903         Owxflinks[] flinksObjs = flinksMgr.loadByWhere(where);
904
905         Owxflinks flinksObj=null;
906         boolean newFileLink=false;
907         if(flinksObjs.length==0)
908         {
909             flinksObj=new Owxflinks();
910             flinksObj.setSrctbl(srctable);
911             flinksObj.setSrcrowid(srcrowid);
912             flinksObj.setFname(field);
913             flinksObj.setFileid(grFile.getRowID());
914             newFileLink=true;
915         }
916         else flinksObj=flinksObjs[0];
917
918         flinksMgr.save(flinksObj);
919         if(newFileLink)
920         {
921             incFileUsage(grFile);
922         }
923         return flinksObj.getRowid();
924     }
925
926
927
928      /**
929       *Method for deleting a Link
930       *returns -1 if something went wrong e
931       *else returns the id of the link that we are deleting
932       */

933
934
935     public int deleteFileLink(String JavaDoc srctable, int srcrowid, String JavaDoc field,GResFile grFile)
936                    throws SQLException
937     {
938         String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
939                        "fname = '" + field + "' and fileid="+grFile.getRowID();
940
941         Owxflinks flinks[]=flinksMgr.loadByWhere(where);
942         if(flinks.length==0) return -1;
943         flinksMgr.deleteByKey(flinks[0].getRowid());
944         decFileUsage(grFile);
945         return flinks[0].getRowid();
946     }
947
948
949     /**
950      *this method shouldn't be called
951      *
952      */

953
954     public boolean deleteFileLink(GResFile grFile)
955                    throws SQLException
956     {
957
958         return false;
959     }
960
961
962     /**
963      *returns a list of ids deleted or null if something went wrong
964      */

965     public Vector deleteFileLinks(String JavaDoc srctable, int srcrowid, String JavaDoc field)
966                    throws SQLException
967     {
968         try
969         {
970             Vector ret=new Vector();
971
972             String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
973                        "fname = '" + field + "'";
974
975             Owxflinks flinks[]=flinksMgr.loadByWhere(where);
976             for(int i=0;i<flinks.length;i++)
977             {
978                 GResFile grFile=loadFile(flinks[i].getFileid());
979                 flinksMgr.deleteByKey(flinks[i].getRowid());
980                 decFileUsage(grFile);
981                 ret.add( new Integer JavaDoc(flinks[i].getRowid()) );
982             }
983
984             return ret;
985         }
986         catch(Exception JavaDoc e)
987         {
988             LoggingManager.log("FAILED to delete the file Links"+e,this);
989             return null;
990         }
991     }
992
993
994
995     /**
996      *Method for incrementing the usage counter field of a file
997      */

998
999     private boolean incFileUsage(GResFile grFile) throws SQLException{
1000        grFile.setUsageCount(grFile.getUsageCount()+1);
1001        return saveFile(grFile);
1002    }
1003
1004
1005    /**
1006     *Method for decrementing the usage counter field of a file
1007     */

1008
1009    private boolean decFileUsage(GResFile grFile) throws SQLException{
1010        int usagecount=grFile.getUsageCount();
1011        if(usagecount>0)
1012        {
1013            grFile.setUsageCount(usagecount-1);
1014            return saveFile(grFile);
1015        }
1016        return false;
1017    }
1018
1019
1020
1021    /**
1022     *Method for saving a file
1023     */

1024    public boolean saveFile(GResFile grFile)
1025                   throws SQLException
1026    {
1027
1028        Owxfiles fileobj=new Owxfiles();
1029        if(grFile.getRowID()!=0)
1030        {
1031            fileobj.setRowid(grFile.getRowID());
1032            fileobj.setIsNew(false);
1033        }
1034        fileobj.setUrl( grFile.getFileURL() );
1035        fileobj.setCategory( grFile.getCategory() );
1036        fileobj.setUsagectr( grFile.getUsageCount() );
1037        fileobj.setFtype( grFile.getType() );
1038        fileobj.setFsize( (int)grFile.getFileSize() );
1039        fileobj.setFinfo( grFile.getInfo() );
1040
1041        filesMgr.save(fileobj);
1042        return true;
1043    }
1044    
1045     /**
1046     *Method for saving a file when overwrite flag is set
1047     */

1048    public boolean saveFileOverwrite(GResFile grFile)
1049                   throws SQLException
1050    {
1051        String JavaDoc where = "url ='"+grFile.getFileURL()+"'";
1052        
1053
1054
1055        Owxfiles[] files = filesMgr.loadByWhere(where);
1056        if(files.length>0)
1057        {
1058            grFile.setRowID(files[0].getRowid());
1059        }
1060        return saveFile(grFile);
1061    }
1062
1063
1064    /**
1065     *Method for saving a file
1066     */

1067    public boolean saveFileCategory(int rowId,String JavaDoc category)
1068                  throws SQLException
1069    {
1070        String JavaDoc where = "rowid="+rowId;
1071
1072
1073        Owxfiles[] files = filesMgr.loadByWhere(where);
1074        files[0].setCategory(category);
1075        filesMgr.save(files[0]);
1076        return true;
1077
1078    }
1079
1080
1081    /**
1082     *Method for deleting a file
1083     */

1084    public boolean deleteFile(GResFile grFile)
1085                   throws SQLException
1086    {
1087         int rowdeleted=filesMgr.deleteByKey(grFile.getRowID());
1088        if(rowdeleted==1) return true;
1089        return false;
1090    }
1091
1092
1093
1094    /**
1095     *method for deleting a Link
1096     *
1097     */

1098    public boolean deleteLink(int linkid)
1099                   throws SQLException
1100    {
1101        //loading an flinks record
1102

1103        String JavaDoc where = "rowid="+linkid;
1104        Owxflinks[] flinks = flinksMgr.loadByWhere(where);
1105
1106        if(flinks.length==0) return false;
1107        //loading the coresponding file
1108

1109        where="rowid="+flinks[0].getFileid();
1110        Owxfiles files[]=filesMgr.loadByWhere(where);
1111
1112        if(files.length>0)
1113        {
1114            GResFile gresFile=new GResFile();
1115            prepareFileObject((Owxfiles) files[0], gresFile);
1116            decFileUsage(gresFile);
1117        }
1118
1119
1120        flinksMgr.deleteByKey(linkid);
1121        return true;
1122    }
1123
1124  /******************* METHODS FOR CATEGORY **********************************/
1125
1126
1127    /**
1128     *Method for saving a file
1129     * @param grCat a GresCategory object that holds al the data necessary to save a category
1130     * @return -1 if the id exists
1131       1 in case of success, 0 in case of an sql error
1132     */

1133    public int saveCategory(GResCategory grCat)
1134                   throws SQLException
1135    {
1136
1137        GResCategory oldcat=loadCategory(grCat.getCategoryID());
1138        if(oldcat!=null) return -1;
1139        Owxcategory catobj=new Owxcategory();
1140        if(grCat.getRowID()!=0)
1141        {
1142            catobj.setRowid(grCat.getRowID());
1143            catobj.setIsNew(false);
1144        }
1145        else
1146        {
1147            catobj.setUsagectr(0);
1148        }
1149        catobj.setParent( grCat.getParent() );
1150        catobj.setOwner( grCat.getOwner() );
1151        catobj.setId( grCat.getCategoryID() );
1152        catobj.setLevel( grCat.getLevel() );
1153
1154        // if it was static, save state :))
1155
catobj.setStatic(grCat.getStatic() );
1156        categoryMgr.save(catobj);
1157        grCat.setRowID( catobj.getRowid() );
1158        return 1;
1159    }
1160
1161    /**
1162     *Method for getting the owxcategory table name
1163     * @return the owxcategory table name
1164     */

1165
1166    public String JavaDoc getCategoryTableName()
1167    {
1168        return categoryMgr.getTableID();
1169    }
1170
1171
1172    /**
1173     *Method for putting all the the data from an Owxcategory object
1174     *to a GresCategory object
1175     * @param owxcat an Owxcategory object
1176     * @param grCat a GResCategory object
1177     */

1178    protected void prepareCategoryObject(Owxcategory owxcat, GResCategory grCat)
1179    {
1180        if(owxcat==null || grCat==null) return;
1181
1182        grCat.setRowID(owxcat.getRowid());
1183        grCat.setCategoryID(owxcat.getId());
1184        grCat.setOwner(owxcat.getOwner());
1185        grCat.setParent(owxcat.getParent());
1186        grCat.setUsageCount(owxcat.getUsagectr());
1187        grCat.setLevel(owxcat.getLevel());
1188        grCat.setStatic(owxcat.getStatic());
1189    }
1190
1191
1192    /**
1193     *Method for putting all the the data from an Owxcatlinks object
1194     *to a GresCatLink object
1195     * @param owxcatlink an Owxcatlinks object
1196     * @param grCatLink a GResCatLink object
1197     */

1198    protected void prepareCatLinkObject(Owxcatlinks owxcatlink, GResCatLink grCatLink)
1199    {
1200        if(owxcatlink==null || grCatLink==null) return;
1201
1202        grCatLink.setRowID(owxcatlink.getRowid());
1203        grCatLink.setSrctbl(owxcatlink.getSrctbl());
1204        grCatLink.setSrcRowID(owxcatlink.getSrcrowid());
1205        grCatLink.setFname(owxcatlink.getFname());
1206        grCatLink.setCatID(owxcatlink.getCatid());
1207    }
1208
1209
1210
1211
1212    /**
1213     *Method for getting a category based on row id
1214     * @param rowid the id of a category
1215     * @return a GResCategory object
1216     */

1217    public GResCategory loadCategory( int rowid) throws SQLException
1218    {
1219       String JavaDoc where = "rowid="+rowid;
1220       Owxcategory[] owxcategs = categoryMgr.loadByWhere(where);
1221       if(owxcategs.length==0)
1222           return null;
1223
1224       GResCategory cat=new GResCategory();
1225       prepareCategoryObject(owxcategs[0],cat);
1226
1227       return cat;
1228    }
1229
1230    /**
1231     * Method for getting a category based on id
1232     * @param id the catid of a category
1233     * @param owner the owner vmodule
1234     * @return a GResCategory object
1235     */

1236    public GResCategory loadCategory( String JavaDoc id, String JavaDoc owner) throws SQLException
1237    {
1238       String JavaDoc where = "id='"+id+"' and owner='"+owner+"'";
1239       Owxcategory[] owxcategs = categoryMgr.loadByWhere(where);
1240       if(owxcategs.length==0)
1241           return null;
1242
1243       GResCategory cat=new GResCategory();
1244       prepareCategoryObject(owxcategs[0],cat);
1245
1246       return cat;
1247    }
1248
1249    /**
1250     * Method for getting a category based on id
1251     * @param id the catid of a category
1252     * @return a GResCategory object
1253     */

1254    public GResCategory loadCategory( String JavaDoc id) throws SQLException
1255    {
1256       String JavaDoc where = "id='"+id+"'";
1257       Owxcategory[] owxcategs = categoryMgr.loadByWhere(where);
1258       if(owxcategs.length==0)
1259           return null;
1260
1261       GResCategory cat=new GResCategory();
1262       prepareCategoryObject(owxcategs[0],cat);
1263
1264       return cat;
1265    }
1266
1267
1268
1269    /**
1270     *Method for loading all categories belonging to an owner
1271     *and on a certain level
1272     * @param level the categories loaded should be on this level
1273     * @param owner owner vmodule
1274     * @return a Vector of GresCategory objects on a certain level
1275     */

1276    public Vector loadCategories(int level,String JavaDoc owner) throws SQLException
1277    {
1278        String JavaDoc where =new String JavaDoc();
1279        if(owner.equals("")) where="level="+level;
1280        else where="level="+level+" and owner='"+owner+"'";
1281        Vector ret=new Vector();
1282        String JavaDoc orderBy=" rowid ASC";
1283        Owxcategory[] owxcategs = categoryMgr.loadByWhere(where,orderBy,0);
1284
1285        for(int i=0;i<owxcategs.length;i++)
1286        {
1287           GResCategory cat=new GResCategory();
1288           prepareCategoryObject(owxcategs[i],cat);
1289           ret.add(cat);
1290        }//end for
1291

1292        return ret;
1293    }
1294
1295    /**
1296     * @return a Vector of static GresCategory objects on a certain level
1297     */

1298    public Vector loadStaticCategories() throws SQLException
1299    {
1300        String JavaDoc where="static='1'";
1301        String JavaDoc orderBy=" rowid ASC";
1302        Vector ret=new Vector();
1303        Owxcategory[] owxcategs = categoryMgr.loadByWhere(where,orderBy,0);
1304        for(int i=0;i<owxcategs.length;i++)
1305        {
1306           GResCategory cat=new GResCategory();
1307           prepareCategoryObject(owxcategs[i],cat);
1308           ret.add(cat);
1309        }//end for
1310

1311        return ret;
1312    }
1313
1314    /**
1315     * @param parentid the id of a perent category
1316     * @param owner only categories owned by 'owner' are returned
1317     * @return a Vector of GresCategory objects that are children of the specified
1318     *id
1319     */

1320
1321    public Vector loadChildCategories(int parentid,String JavaDoc owner) throws SQLException
1322    {
1323        String JavaDoc where =new String JavaDoc();
1324        if(owner.equals("")) where="parent="+parentid;
1325        else where="parent="+parentid+" and owner='"+owner+"'";
1326        String JavaDoc orderBy=" rowid ASC";
1327        Vector ret=new Vector();
1328        Owxcategory[] owxcategs = categoryMgr.loadByWhere(where,orderBy,0);
1329        for(int i=0;i<owxcategs.length;i++)
1330        {
1331           GResCategory cat=new GResCategory();
1332           prepareCategoryObject(owxcategs[i],cat);
1333           ret.add(cat);
1334        }//end for
1335

1336        return ret;
1337    }
1338
1339
1340    /**
1341     *Generic method to do an SQL update
1342     * @param sqlupdate the update command to execute
1343     */

1344
1345    protected void doSQLUpdate(String JavaDoc sqlupdate) throws java.sql.SQLException JavaDoc
1346    {
1347        Statement stmt = null;
1348        try
1349        {
1350            Connection conn = ds.getConnection();
1351            stmt = conn.createStatement();
1352            stmt.executeUpdate(sqlupdate);
1353            stmt.close();
1354            if(conn!=null) conn.close();
1355        }
1356        catch(SQLException e) {
1357            if(stmt !=null) try { stmt.close(); } catch(Exception JavaDoc e2) { }
1358            throw e;
1359        }
1360    }
1361
1362    /**
1363     *Method delete a category identified by a GResCategory object
1364     * @param cat a GResCategoryObject
1365     */

1366    public void deleteCategory(GResCategory cat) throws SQLException
1367    {
1368        categoryMgr.deleteByKey(cat.getRowID());
1369        doSQLUpdate("DELETE FROM owxcatlinks WHERE catid='"+cat.getRowID()+"'");
1370        deleteMessages(
1371            getCategoryTableName(),
1372            cat.getRowID(),
1373            CategoryConstants.RESCATFIELD_NAME);
1374    }
1375
1376    /**
1377     * @param catID an id of a category
1378     * @return a vector of items, contained in the specified category
1379     */

1380    public Vector loadCategoryItemIDs(String JavaDoc catID) throws java.sql.SQLException JavaDoc
1381    {
1382        GResCategory cat=loadCategory(catID);
1383        String JavaDoc sql = "select * from owxcatlinks where catid="+cat.getRowID()+";";
1384        //LoggingManager.log(sql);
1385
Vector result = new Vector();
1386
1387        ResultSet rs = null;
1388        Statement stmt = null;
1389        Connection conn = ds.getConnection();
1390        try
1391        {
1392            stmt = conn.createStatement();
1393            rs = stmt.executeQuery(sql);
1394
1395            while(rs.next())
1396            {
1397                int id = rs.getInt("srcrowid");
1398                result.addElement(new Integer JavaDoc(id));
1399            }
1400
1401            rs.close();
1402            stmt.close();
1403            if(conn!=null) conn.close();
1404            //LoggingManager.log("Got "+result.size()+" elements");
1405
return result;
1406        }
1407        catch(SQLException e) {
1408            if(rs != null) try { rs.close(); } catch(Exception JavaDoc e2) { }
1409            if(stmt !=null) try { stmt.close(); } catch(Exception JavaDoc e2) { }
1410            throw e;
1411        }
1412    }
1413
1414     /**
1415     * @param rowID an rowid of a category
1416     * @return a vector of items, contained in the specified category
1417     */

1418    public Vector loadCategoryItemIDs(int rowID) throws java.sql.SQLException JavaDoc
1419    {
1420
1421        String JavaDoc sql = "select * from owxcatlinks where catid="+rowID+";";
1422        //LoggingManager.log(sql);
1423
Vector result = new Vector();
1424
1425        ResultSet rs = null;
1426        Statement stmt = null;
1427        Connection conn = ds.getConnection();
1428        try
1429        {
1430            stmt = conn.createStatement();
1431            rs = stmt.executeQuery(sql);
1432
1433            while(rs.next())
1434            {
1435                int id = rs.getInt("srcrowid");
1436                result.addElement(new Integer JavaDoc(id));
1437            }
1438
1439            rs.close();
1440            stmt.close();
1441            if(conn!=null) conn.close();
1442            //LoggingManager.log("Got "+result.size()+" elements");
1443
return result;
1444        }
1445        catch(SQLException e) {
1446            if(rs != null) try { rs.close(); } catch(Exception JavaDoc e2) { }
1447            if(stmt !=null) try { stmt.close(); } catch(Exception JavaDoc e2) { }
1448            throw e;
1449        }
1450    }
1451
1452
1453
1454    /**
1455     * Method for loading the CategoryLinks of a certain Category
1456     * @param catRowID the row id of a category
1457     * @return a Vector of GrCatLink objects
1458     */

1459    public Vector loadCatLinks(int catRowID) throws java.sql.SQLException JavaDoc
1460    {
1461        // loop through all records of the same locale
1462
String JavaDoc where = "catid="+catRowID;
1463        Owxcatlinks[] catlinksObj = catlinksMgr.loadByWhere(where);
1464        Vector ret=new Vector();
1465        for(int i=0;i<catlinksObj.length;i++)
1466        {
1467            GResCatLink grCatLink=new GResCatLink();
1468            prepareCatLinkObject(catlinksObj[i],grCatLink);
1469            ret.add(grCatLink);
1470        }
1471
1472        return ret;
1473
1474    }
1475
1476
1477    /**
1478     * Method for loading all category links
1479     * @param srcrowid the source table of items
1480     * @param fname logical name
1481     * @return Vector of catids as Integers
1482     */

1483
1484    public Vector loadCategoryLinksForItem( String JavaDoc srctable, int srcrowid,String JavaDoc fname)
1485    throws SQLException
1486    {
1487         // loop through all records of
1488
String JavaDoc where = "srctbl='"+srctable+"' and srcrowid="+srcrowid+" and fname='"+fname+"'";
1489        Owxcatlinks[] catlinksObj = catlinksMgr.loadByWhere(where);
1490        Vector ret=new Vector();
1491        for(int i=0;i<catlinksObj.length;i++)
1492        {
1493
1494            ret.add(new Integer JavaDoc(catlinksObj[i].getCatid()) );
1495        }
1496
1497        return ret;
1498    }
1499
1500
1501
1502
1503    /**
1504     *Method for deleting a category link
1505     * @param linkid the id of the category link
1506     * @return true if delete was succesful, false otherwise
1507     */

1508    public boolean deleteCatLink(int linkid)
1509                   throws SQLException
1510    {
1511        // loop through all records of the same locale
1512
String JavaDoc where = "rowid="+linkid;
1513        Owxcatlinks[] catlinksObj = catlinksMgr.loadByWhere(where);
1514        if(catlinksObj.length==0) return false;
1515        catlinksMgr.deleteByKey(catlinksObj[0].getRowid());
1516        decCategoryUsageCtr( catlinksObj[0].getCatid() );
1517        return true;
1518    }
1519    
1520    /**
1521     *Method for deleting all category links between an entry and categories
1522     * @param srctable the source table of the entry
1523     * @param srcrowid the id of entry
1524     * @return true if delete was succesful, false otherwise
1525     */

1526    public boolean deleteCatLink(String JavaDoc srctable,int srcrowid)
1527                   throws SQLException
1528    {
1529        // loop through all catlinks of entry with id srcrowid
1530
String JavaDoc where = "srctbl='"+srctable+"' AND srcrowid="+srcrowid;
1531        Owxcatlinks[] catlinksObj = catlinksMgr.loadByWhere(where);
1532        if(catlinksObj.length==0) return false;
1533        for(int i=0;i<catlinksObj.length;i++)
1534        {
1535            catlinksMgr.deleteByKey(catlinksObj[i].getRowid());
1536            decCategoryUsageCtr( catlinksObj[i].getCatid() );
1537        }
1538        return true;
1539    }
1540
1541
1542    /**
1543     *Method for decrementing the usage counter of a Category
1544     * @param catId the id of the category
1545     *
1546     */

1547    protected void decCategoryUsageCtr(int catId) throws SQLException
1548    {
1549       String JavaDoc where = "rowid="+catId;
1550       Owxcategory[] owxcategs = categoryMgr.loadByWhere(where);
1551       if(owxcategs.length==0) return;
1552
1553       int usagectr=owxcategs[0].getUsagectr();
1554
1555       if(usagectr>0) owxcategs[0].setUsagectr(usagectr-1);
1556       categoryMgr.save(owxcategs[0]);
1557    }
1558
1559
1560
1561    /**
1562     *Method for incrementing the usage counter of a Category
1563     * @param catId the id of the category
1564     *
1565     */

1566    protected void incCategoryUsageCtr(int catId) throws SQLException
1567    {
1568       String JavaDoc where = "rowid="+catId;
1569       Owxcategory[] owxcategs = categoryMgr.loadByWhere(where);
1570       if(owxcategs.length==0) return;
1571
1572       int usagectr=owxcategs[0].getUsagectr();
1573
1574       owxcategs[0].setUsagectr(usagectr+1);
1575       categoryMgr.save(owxcategs[0]);
1576    }
1577
1578
1579
1580    /**
1581     *This method calls getTree(cat.getRowID(), l)
1582     */

1583    public Vector getTree(GResCategory cat,Locale l) throws SQLException
1584    {
1585        return getTree(cat.getRowID(), l);
1586    }
1587
1588    /**
1589     * Method for getting the parent names of a category
1590     * @param rowID the id of the category
1591     * @param l Locale used to select the category name
1592     *
1593     * @return a vector of parent names for a specified category
1594     */

1595    public Vector getTree(int rowID, Locale l) throws SQLException
1596    {
1597        GResCategory ccat = loadCategory( rowID);
1598        Vector result;
1599        
1600        // condition added by jancsi
1601
if(ccat == null)
1602        {
1603            return new Vector();
1604        }
1605        
1606        if(ccat.getParent() != 0)
1607        {
1608            result = getTree(ccat.getParent(), l);
1609        }
1610        else
1611        {
1612            result = new Vector();
1613        }
1614
1615        result.addElement(loadCategoryName(ccat, l));
1616
1617        return result;
1618    }
1619
1620    public Vector getTree(String JavaDoc catId,Locale l) throws SQLException
1621    {
1622        GResCategory ccat = loadCategory(catId);
1623        Vector result;
1624        if(ccat.getParent() != 0)
1625        {
1626            result = getTree(ccat.getParent(), l);
1627        }
1628        else
1629        {
1630            result = new Vector();
1631        }
1632
1633        result.addElement(loadCategoryName(ccat, l));
1634
1635        return result;
1636    }
1637
1638
1639    /**
1640     * Method for getting the parent names and ids of a category
1641     * @param rowID the id of the category
1642     * @param l Locale used to select the category name
1643     * @return a vector of parent names and Ids as PairOfObjects for a specified category
1644     */

1645    public Vector getTreeWithIDs(int rowID, Locale l) throws SQLException
1646    {
1647        GResCategory ccat = loadCategory( rowID);
1648        Vector result;
1649        if(ccat.getParent() != 0)
1650        {
1651            result = getTree(ccat.getParent(), l);
1652        }
1653        else
1654        {
1655            result = new Vector();
1656        }
1657        PairOfObjects pair=new PairOfObjects();
1658        pair.setObjectOne(loadCategoryName(ccat, l));
1659        pair.setObjectTwo(new Integer JavaDoc(ccat.getRowID()));
1660        result.addElement(pair);
1661
1662        return result;
1663    }
1664
1665
1666    /**
1667     * @param grcat a GresCategory object
1668     * @param l the Locale to use
1669     * @return the name of a catergory in a given locale
1670     */

1671    protected String JavaDoc loadCategoryName(GResCategory grcat, Locale l) throws java.sql.SQLException JavaDoc
1672    {
1673        if(grcat!=null)
1674        {
1675            return loadMessage(getCategoryTableName(),grcat.getRowID(), CategoryConstants.RESCATFIELD_NAME,l);
1676        }
1677        else
1678        {
1679            return "No name";
1680        }
1681    }//end loadCategoryNames
1682

1683
1684
1685
1686    /**
1687     *Method for saving a category link
1688     * @param srctable the source table of the item that is liked to the category
1689     * @param srcrowid the id of the item
1690     * @param field logical name
1691     * @param catid the category id
1692     * @return true in case of success, false otherwise
1693     */

1694    public boolean saveCatLink(String JavaDoc srctable, int srcrowid, String JavaDoc field, int catid)
1695                   throws SQLException
1696    {
1697        String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
1698                       "fname = '" + field + "' AND catid = " + catid;
1699        Owxcatlinks[] catlinksObjs = catlinksMgr.loadByWhere(where);
1700
1701        Owxcatlinks catlinksObj=null;
1702        if(catlinksObjs.length==0)
1703        {
1704            catlinksObj=new Owxcatlinks();
1705            catlinksObj.setSrctbl(srctable);
1706            catlinksObj.setSrcrowid(srcrowid);
1707            catlinksObj.setFname(field);
1708            catlinksObj.setCatid(catid);
1709        }
1710        else catlinksObj=catlinksObjs[0];
1711
1712        catlinksMgr.save(catlinksObj);
1713        incCategoryUsageCtr(catid);
1714
1715        return true;
1716    }
1717
1718
1719
1720
1721    /**
1722     *Method for deleteing a category link
1723     * @param srctable the source table of the item that is liked to the category
1724     * @param srcrowid the id of the item
1725     * @param field logical name
1726     * @param catid the category id
1727     * @return true in case of success, false otherwise
1728     */

1729    public boolean deleteCatLink(String JavaDoc srctable, int srcrowid, String JavaDoc field, int catid)
1730                   throws SQLException
1731    {
1732
1733        String JavaDoc where = "srctbl = '" + srctable + "' AND srcrowid = " + srcrowid + " AND " +
1734                       "fname = '" + field + "' AND catid = " + catid;
1735
1736        Owxcatlinks[] catlinksObj = catlinksMgr.loadByWhere(where);
1737        if(catlinksObj.length==0) return false;
1738        catlinksMgr.deleteByKey(catlinksObj[0].getRowid());
1739        decCategoryUsageCtr( catlinksObj[0].getCatid() );
1740        return true;
1741    }
1742
1743
1744    /**
1745     * @param srctable the source table of the item that is liked to the category
1746     * @param srcrowid the id of the item
1747     * @param field logical name
1748     * @return Vector of catids as Integers
1749     */

1750
1751    public Vector loadCategoryLinks(String JavaDoc srctable,int srcrowid,String JavaDoc fname)
1752    throws SQLException
1753    {
1754         // loop through all records of
1755
String JavaDoc where = "srctbl='"+srctable+"' AND srcrowid="+srcrowid+" and fname='"+fname+"'";
1756        Owxcatlinks[] catlinksObj = catlinksMgr.loadByWhere(where);
1757        Vector ret=new Vector();
1758        for(int i=0;i<catlinksObj.length;i++)
1759        {
1760
1761            ret.add(new Integer JavaDoc(catlinksObj[i].getCatid()) );
1762        }
1763
1764        return ret;
1765    }
1766    
1767    
1768    
1769    //--------------METHODS FOR fc_fields------------------------------------------------
1770

1771    
1772    
1773   
1774    /**
1775     * Method for loading locale insesitive fields of an entry identified by(srctable,srcrowid)
1776     * returns Hastable of (fieldname,GResField) objects
1777     */

1778    public Hashtable loadFields(String JavaDoc srctable,int srcrowid)
1779    throws SQLException
1780    {
1781        String JavaDoc where = "srctbl='"+srctable+"' AND srcrowid="+srcrowid;
1782        Owxfields[] fieldsObj = fieldsMgr.loadByWhere(where);
1783        Hashtable ret=new Hashtable();
1784        for(int i=0;i<fieldsObj.length;i++)
1785        {
1786            GResField grfield=new GResField();
1787            grfield.setName(fieldsObj[i].getFname());
1788            grfield.setNumberValue( new Double JavaDoc(fieldsObj[i].getNumberval()) );
1789            
1790            grfield.setTextValue(fieldsObj[i].getTextval());
1791            grfield.setDateValue(fieldsObj[i].getDateval());
1792            grfield.setFieldType(fieldsObj[i].getType());
1793            ret.put(grfield.getName(),grfield );
1794        }
1795
1796        return ret;
1797    }
1798    
1799    /**
1800     * Method for loading locale insesitive fields of an entry identified by(srctable,srcrowid)
1801     * returns Hastable of (fieldname,fieldvalue) objects
1802     */

1803    public Hashtable loadFieldValues(String JavaDoc srctable,int srcrowid)
1804    throws SQLException
1805    {
1806        String JavaDoc where = "srctbl='"+srctable+"' AND srcrowid="+srcrowid;
1807        Owxfields[] fieldsObj = fieldsMgr.loadByWhere(where);
1808        Hashtable ret=new Hashtable();
1809        for(int i=0;i<fieldsObj.length;i++)
1810        {
1811            GResField grfield=new GResField();
1812            grfield.setName(fieldsObj[i].getFname());
1813            grfield.setNumberValue( new Double JavaDoc(fieldsObj[i].getNumberval()) );
1814            grfield.setTextValue(fieldsObj[i].getTextval());
1815            grfield.setDateValue(fieldsObj[i].getDateval());
1816            grfield.setFieldType(fieldsObj[i].getType());
1817            ret.put(grfield.getName(),grfield.getValue() );
1818        }
1819
1820        return ret;
1821    }
1822    
1823    /**
1824     *Method for saving a field value is DB
1825     */

1826    public void saveField(String JavaDoc srctable,int srcrowid,GResField field)
1827    throws SQLException
1828    {
1829        Owxfields saveField=null;
1830        String JavaDoc where = "srctbl='"+srctable+"' AND srcrowid="+srcrowid+" AND fname='"+field.getName()+"'";
1831        //LoggingManager.log("Saving field: "+where);
1832
Owxfields[] fieldsObj = fieldsMgr.loadByWhere(where);
1833        if(fieldsObj.length >0) saveField=fieldsObj[0];
1834        else saveField=new Owxfields();
1835        
1836        saveField.setSrctbl(srctable);
1837        saveField.setSrcrowid(srcrowid);
1838        saveField.setFname(field.getName());
1839        if(field.getFieldType()==Constants.FIELDTYPE_NUMBER)
1840        {
1841            if(field.getNumberValue()!=null) saveField.setNumberval(field.getNumberValue().doubleValue());
1842        }
1843        else if(field.getFieldType()==Constants.FIELDTYPE_TEXT)
1844        {
1845            saveField.setTextval(field.getTextValue());
1846        }
1847        else if(field.getFieldType()==Constants.FIELDTYPE_DATE)
1848        {
1849            saveField.setDateval(field.getDateValue());
1850        }
1851        else if(field.getFieldType()==Constants.FIELDTYPE_PRICE)
1852        {
1853            if(field.getNumberValue()!=null) saveField.setNumberval(field.getNumberValue().doubleValue());
1854            saveField.setTextval(field.getTextValue());
1855        }
1856        saveField.setType(field.getFieldType());
1857        fieldsMgr.save(saveField);
1858    }
1859    
1860    /**
1861     *Method for deleting all fields assigned to an entry
1862     */

1863    public void deleteFields(String JavaDoc srctable,int srcrowid)
1864    throws SQLException
1865    {
1866        
1867        String JavaDoc where = "srctbl='"+srctable+"' AND srcrowid="+srcrowid;
1868        Owxfields[] fieldsObj = fieldsMgr.loadByWhere(where);
1869        for(int i=0;i<fieldsObj.length;i++)
1870        {
1871           int result = fieldsMgr.deleteByKey(fieldsObj[i].getRowid());
1872           LoggingManager.log("Deleted field #" + fieldsObj[i].getRowid() + " for " + srctable + ", " +
1873                               fieldsObj[i].getFname() + ", count " + result, this);
1874        }
1875    }
1876    
1877    
1878    
1879    /**
1880     * Method for loading locale sesitive fields of an entry identified by(srctable,srcrowid)
1881     * returns Hastable of (fieldname,GResLocalizedField) objects
1882     */

1883    public Hashtable loadLocalizedFields(String JavaDoc srctable,int srcrowid)
1884    throws SQLException
1885    {
1886        String JavaDoc where = "srctbl='"+srctable+"' AND srcrowid="+srcrowid;
1887        Owxmsgs[] msgs = msgsMgr.loadByWhere(where);
1888        Hashtable ret=new Hashtable();
1889        PairOfObjects po=null;
1890        for(int i=0;i<msgs.length;i++)
1891        {
1892            GResLocalizedField grlfield=null;
1893            grlfield=(GResLocalizedField)ret.get(msgs[i].getFname());
1894            if(grlfield==null) grlfield=new GResLocalizedField();
1895            grlfield.setName( msgs[i].getFname() );
1896            po = LocaleManager.stripLocaleString( msgs[i].getLocalekey() );
1897            grlfield.setValue( msgs[i].getMsg(),new Locale((String JavaDoc) po.getObjectOne(), (String JavaDoc) po.getObjectTwo()) );
1898            ret.put(grlfield.getName(),grlfield);
1899            
1900        }
1901
1902        return ret;
1903    }
1904    
1905    
1906    /**
1907     * Method for loading locale sesitive fields of an entry identified by(srctable,srcrowid)
1908     * returns Hastable of (fieldname,fieldvalue) objects
1909     */

1910    public Hashtable loadLocalizedFieldValues(String JavaDoc srctable,int srcrowid,Locale locale)
1911    throws SQLException
1912    {
1913        String JavaDoc where = "srctbl='"+srctable+"' AND srcrowid="+srcrowid+" AND localekey='"+locale.toString()+"'";
1914        Owxmsgs[] msgs = msgsMgr.loadByWhere(where);
1915        Hashtable ret=new Hashtable();
1916        PairOfObjects po=null;
1917        for(int i=0;i<msgs.length;i++)
1918        {
1919            GResLocalizedField grlfield=null;
1920            grlfield=(GResLocalizedField)ret.get(msgs[i].getFname());
1921            if(grlfield==null) grlfield=new GResLocalizedField();
1922            if(msgs[i].getMsg()==null)
1923            {
1924                ret.put( msgs[i].getFname(),"");
1925            }
1926            else
1927            {
1928                ret.put( msgs[i].getFname(),msgs[i].getMsg() );
1929            }
1930        }//end for
1931

1932        return ret;
1933    }
1934    
1935    /**
1936     * Method for loading disctint values of a locale sensitive field, used to display search dropwdown
1937     *
1938     * Note: This method is intended for (element) public use.
1939     */

1940    public Vector loadValuesFor(String JavaDoc srctable,String JavaDoc fieldname,String JavaDoc locale, boolean numeric)
1941    {
1942        Vector ret=new Vector();
1943        Connection conn=null;
1944        Statement stmt=null;
1945        ResultSet rs=null;
1946        String JavaDoc sql = null;
1947        try
1948        {
1949            conn = ds.getConnection();
1950            stmt = conn.createStatement();
1951            
1952// 20030119/gz: Need only to have "visible" and "enabled" entries in public combos...
1953
// Merged with method for non-locale sensitive fields.
1954
//
1955
// old statement:
1956
// ps=conn.prepareStatement("select distinct msg from owxmsgs where srctbl=? and fname=? and localekey=?");
1957
if(locale == null || locale.length() == 0 || numeric == true)
1958            {
1959                String JavaDoc fldtype = ((numeric == true) ? "numberval" : "textval");
1960                sql = "select distinct " + fldtype + " from owxfields, " + srctable + " where srctbl=" +
1961                      "'" + srctable + "' and owxfields.srcrowid=" + srctable +
1962                      ".rowid and fname='" + fieldname + "' and flagVisible = true AND " +
1963                      "(flagtimedpub = false OR (flagtimedpub = true AND " +
1964                      "( (showfrom = null OR showfrom<=now()) AND " +
1965                      "(showuntil = null OR showuntil > now()))) ) ;";
1966            }
1967            else
1968            {
1969                sql = "select distinct msg from owxmsgs, " + srctable + " where srctbl=" +
1970                      "'" + srctable + "' and owxmsgs.srcrowid=" + srctable +
1971                      ".rowid and fname='" + fieldname + "' and localekey='" + locale + "' and " +
1972                      " flagVisible = true AND (flagtimedpub = false OR " +
1973                      "(flagtimedpub = true AND ( (showfrom = null OR " +
1974                      "showfrom<=now()) AND (showuntil = null OR showuntil > " +
1975                      "now()))) ) ;";
1976            }
1977
1978// LoggingManager.log("loadValuesFor() query is: " + sql, this);
1979
rs = stmt.executeQuery(sql);
1980            while(rs.next())
1981            {
1982                String JavaDoc str = rs.getString(1);
1983                if(str != null)
1984                {
1985                    ret.add(str);
1986// LoggingManager.log("loadValuesFor() loaded value: " + str, this);
1987
}
1988            }
1989            
1990            rs.close();
1991            stmt.close();
1992            conn.close();
1993        }
1994        catch(SQLException e)
1995        {
1996            if(rs != null) try { rs.close(); } catch(Exception JavaDoc e2) { }
1997            if(stmt !=null) try { stmt.close(); } catch(Exception JavaDoc e2) { }
1998            if(conn !=null) try { conn.close(); } catch(Exception JavaDoc e2) { }
1999            LoggingManager.log("Exception while loading combobox entries. " +
2000                               "Query was " + sql + " ExceptionMessage: " +
2001            e.getMessage(), this);
2002        }
2003         return ret;
2004    }//end loadValuesFor
2005

2006     /**
2007     *Method for saving a field value is DB
2008     */

2009    public void saveLocalizedField(String JavaDoc srctable,int srcrowid,GResLocalizedField field)
2010    throws SQLException
2011    {
2012        Hashtable h=field.getValues();
2013        Locale l=null;
2014        for(Enumeration e=h.keys();e.hasMoreElements();)
2015        {
2016            l=(Locale)e.nextElement();
2017            saveMessage(srctable, srcrowid, field.getName(), l, (String JavaDoc)h.get(l));
2018        }//end for
2019

2020    }
2021    
2022    /**
2023     *Method for deleting localizedFields assigned to an entry
2024     */

2025    public void deleteLocalizedFields(String JavaDoc srctable,int srcrowid)
2026    throws SQLException
2027    {
2028        deleteAllMessages(srctable,srcrowid);
2029    }
2030
2031
2032    public String JavaDoc loadField(String JavaDoc srctable, int srcrowid, String JavaDoc field, Locale locale)
2033                  throws SQLException
2034    {
2035        //presume that field is locale sensitive;
2036
String JavaDoc fieldVal=loadMessage(srctable, srcrowid,field,locale);
2037        if(fieldVal!=null) return fieldVal;
2038        //field not locale sensitive search in owxfields
2039

2040        String JavaDoc where = "srctbl='"+srctable+"' AND srcrowid="+srcrowid+" AND fname='"+field+"'";
2041        Owxfields[] fieldsObj = fieldsMgr.loadByWhere(where);
2042
2043        if(fieldsObj.length>0)
2044        {
2045            GResField grfield=new GResField();
2046            grfield.setName(fieldsObj[0].getFname());
2047            grfield.setNumberValue( new Double JavaDoc(fieldsObj[0].getNumberval()) );
2048            grfield.setTextValue(fieldsObj[0].getTextval());
2049            grfield.setDateValue(fieldsObj[0].getDateval());
2050            grfield.setFieldType(fieldsObj[0].getType());
2051            return grfield.getValue();
2052        }//end if
2053

2054        return null;
2055    }//end loadField
2056

2057
2058}
2059// eof
2060
Popular Tags