KickJava   Java API By Example, From Geeks To Geeks.

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


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.base.BaseConstants;
13
14 /**
15  *
16  * <hr>
17  * <table width="100%" border="0">
18  * <tr>
19  * <td width="24%"><b>Filename</b></td><td width="76%">PicturesSelector.java</td>
20  * </tr>
21  * <tr>
22  * <td width="24%"><b>Author</b></td><td width="76%">Guy Zürcher (gzuercher@raptus.com)</td>
23  * </tr>
24  * <tr>
25  * <td width="24%"><b>Date</b></td><td width="76%">1st of May 2001</td>
26  * </tr>
27  * </table>
28  * <hr>
29  * <table width="100%" border="0">
30  * <tr>
31  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
32  * </tr>
33  * </table>
34  * <hr>
35  */

36 public class PicturesSelector extends Object JavaDoc
37 {
38     /**
39      * Contains all available GResPicture objects allocated by
40      * the GlobalResources.
41      */

42     protected Vector allLinkablePics = null;
43
44     /**
45      * Contains all GResPicture objects that belong to a specific
46      * category.
47      */

48     protected Vector linkablePicsInCategory = null;
49
50     /**
51      * All GResPicture object(s) that are linked to some
52      * entries (new or existing)
53      */

54     protected Vector linkedPics = null;
55
56
57     /**
58      *contains String[] objects representing the title for the corespnding picture in likedPics
59      */

60     protected Vector titleList=null;
61
62
63     /**
64      * This vector contains the GResPicture object(s) which already exist
65      * in the database, but were chosen to be deleted.
66      */

67     protected Vector picsToDelete = null;
68
69     /**
70      * The list of all available categories
71      */

72     protected String JavaDoc[] allCategories = null;
73
74
75     /**
76      *
77      */

78     protected Locale[] availLocales = null;
79
80
81     /**
82      *
83      */

84     protected String JavaDoc filelinktablename=null;
85
86
87     /**
88      *
89      */

90
91     protected int maxPicCount=0;
92
93     /**
94      *
95      */

96     public PicturesSelector(GlobalResources gres,String JavaDoc locales[],String JavaDoc fltn,int picturecount)
97                 throws SQLException JavaDoc
98     {
99         reset();
100
101         allLinkablePics = gres.loadLinkablePics();
102         allCategories = gres.loadAllPictureCategories();
103         switchToCategory( getDefaultCategory() );
104
105         availLocales = new Locale[locales.length];
106
107         for(int i = 0; i < locales.length; i ++)
108         {
109             PairOfObjects po = LocaleManager.stripLocaleString(locales[i]);
110             if(po != null && po.isValid())
111                 availLocales[i] = new Locale((String JavaDoc) po.getObjectOne(), (String JavaDoc) po.getObjectTwo());
112         }
113
114         filelinktablename=fltn;
115         maxPicCount=picturecount;
116     }
117
118     /**
119      *
120      */

121     public void reset()
122     {
123         linkablePicsInCategory = new Vector();
124         linkedPics = new Vector();
125         titleList=new Vector();
126         picsToDelete = new Vector();
127     }
128
129     /**
130      *
131      */

132     public void loadLinkedPictures(GlobalResources gres, String JavaDoc tableId, int rowId, String JavaDoc field)
133                 throws SQLException JavaDoc
134     {
135         Vector v = gres.loadLinkedFilesAndIds(GlobalResources.FILETYPE_PICTURE, tableId, rowId, field);
136         int i,j;
137
138         for(i = 0; i < v.size(); i ++)
139         {
140             PairOfObjects pair=(PairOfObjects)v.get(i);
141
142             GResPicture pic = (GResPicture) pair.getObjectOne();
143             linkedPics.add(pic);
144             LoggingManager.log(pic.getFileName());
145             Integer JavaDoc I=(Integer JavaDoc) pair.getObjectTwo();
146
147             String JavaDoc[] title=new String JavaDoc[availLocales.length];
148             for(j=0;j<availLocales.length;j++)
149             {
150                 title[j]=gres.loadMessage(filelinktablename, I.intValue(), BaseConstants.RESPICFIELD_LINK, availLocales[j]);
151                 if(title[j]==null) title[j]="";
152             }//end for
153
titleList.add(title);
154          }//end for
155

156     }
157
158     /**
159      *
160      */

161     public void saveLinkedPictures(GlobalResources gres, String JavaDoc tableId, int rowId, String JavaDoc field)
162                 throws SQLException JavaDoc
163     {
164         // save all new or modified GResPicture objects
165
int i,j;
166         for(i = 0; i < linkedPics.size(); i ++)
167         {
168             GResPicture pic = (GResPicture) linkedPics.get(i);
169             int rowid=gres.saveFileLink(tableId, rowId, field, pic);
170             if( rowid==-1 )
171             {
172                 LoggingManager.log("Failed to link pic id: " + pic.getRowID() +
173                                    " to entry id: " + rowId, this);
174             }
175             else
176             {
177                 //saving titles too
178
String JavaDoc title[]=(String JavaDoc[])titleList.get(i);
179                 for(j=0;j<availLocales.length;j++)
180                 {
181                     gres.saveMessage(filelinktablename, rowid, BaseConstants.RESPICFIELD_LINK, availLocales[j],title[j]);
182                 }
183             }//end else
184
}//end for
185

186
187
188
189         // drop the urls marked to delete
190
for(i = 0; i < picsToDelete.size(); i ++)
191         {
192             GResPicture pic = (GResPicture) picsToDelete.get(i);
193
194              /* not good method call
195               if( !gres.deleteFileLink(pic) )
196                 LoggingManager.log("Failed to delete URL id: " + pic.getRowID() +
197                                    " from entry id: " + rowId, this);
198               */

199
200             /*added by REEA*/
201             int rowid=gres.deleteFileLink(tableId,rowId,field,pic);
202             if( rowid==-1 )
203             {
204                 LoggingManager.log("Failed to delete URL id: " + pic.getRowID() +
205                                    " from entry id: " + rowId, this);
206             }
207             else
208             {
209                 gres.deleteMessages(filelinktablename, rowid, BaseConstants.RESPICFIELD_LINK);
210             }
211
212
213          }//end for
214
}
215
216
217     /**
218      *
219      */

220     public boolean linkPicture(int picId,String JavaDoc[] title)
221     {
222         if(maxPicCount>0 && linkedPics.size()>=maxPicCount) return false;
223         GResPicture grPic = getGRPicForRowId(linkablePicsInCategory, picId);
224         if(grPic != null && linkedPics.indexOf(grPic) == -1)
225         {
226             linkedPics.add(grPic);
227             titleList.add(title);
228         }
229         return true;
230     }
231
232     /**
233      *picId respresents the index of the file in the linkedPics Vector
234      */

235     public void unlinkPicture(int picId)
236     {
237         GResPicture grPic =(GResPicture) linkedPics.elementAt(picId);
238
239         for(int i=0;i<allLinkablePics.size();i++)
240         {
241             if( grPic.getRowID()==((GResPicture)allLinkablePics.elementAt(i)).getRowID() )
242             {
243
244                 // if the picture is already linked in the DB put
245
// it on the "to be removed list"
246
if(grPic.getRowID() != -1)
247                     picsToDelete.add(grPic);
248
249                 break;
250             }//end if
251
}//end for
252

253         // now it is save to kill
254
linkedPics.removeElementAt(picId);
255         titleList.removeElementAt(picId);
256
257     }
258
259     /**
260      *
261      */

262     public void switchToCategory(String JavaDoc category)
263     {
264         if(category == null)
265             return;
266
267         linkablePicsInCategory.clear();
268
269         Enumeration all = allLinkablePics.elements();
270         while(all.hasMoreElements())
271         {
272             GResPicture grPic = (GResPicture) all.nextElement();
273             if(grPic != null && grPic.getCategory().equals(category))
274                 linkablePicsInCategory.add(grPic);
275         }
276     }
277
278     /**
279      *
280      */

281     protected GResPicture getGRPicForRowId(Vector data, int rowId)
282     {
283         Enumeration availPics = data.elements();
284         while(availPics.hasMoreElements())
285         {
286             GResPicture grPic = (GResPicture) availPics.nextElement();
287             if(grPic != null && grPic.getRowID() == rowId)
288                 return grPic;
289         }
290
291         return null;
292     }
293
294     /**
295      *
296      */

297     protected String JavaDoc getDefaultCategory()
298     {
299         if(allCategories != null && allCategories.length > 0)
300             return allCategories[0];
301
302         LoggingManager.log("Unable to determine the default pictures category!");
303         return null;
304     }
305
306     /**
307      *
308      */

309     public Vector getAllLinkablePics() { return allLinkablePics; }
310
311     /**
312      *
313      */

314     public Vector getLinkablePicsInCategory() { return linkablePicsInCategory; }
315
316     /**
317      *
318      */

319     public Vector getLinkedPics() { return linkedPics; }
320
321
322
323     /**
324      *
325      */

326     public Vector getLinkedPics(Locale userLocale)
327     {
328         //preparing pare of objects of type GresPicture, String
329

330         Vector ret=new Vector();
331
332
333
334         int i=0,j=0;
335
336         int localeindex=0;
337
338         for (i=0;i<availLocales.length;i++)
339         {
340             if( availLocales[i].equals(userLocale) )
341             {
342                 localeindex=i;
343                 break;
344             }
345         }
346
347         String JavaDoc pic="";
348         String JavaDoc title="";
349         String JavaDoc titles[]=null;
350         for(i=0;i<linkedPics.size();i++){
351             GResPicture gresPicture=(GResPicture)linkedPics.get(i);
352
353             PairOfObjects po=new PairOfObjects();
354
355             po.setObjectOne( gresPicture );
356
357             titles=(String JavaDoc[])titleList.get(i);
358             if(titles!=null) title=titles[localeindex];
359             else title="";
360
361             if( title.equals("") ) po.setObjectTwo( gresPicture.getFileName() );
362             else po.setObjectTwo(title);
363
364             ret.add(po);
365         }
366
367         return ret;
368     }
369
370
371
372     /**
373      *
374      */

375     public String JavaDoc[] getAllCategories() { return allCategories; }
376
377      /**
378      *
379      */

380     public Locale[] getLocales() { return availLocales; }
381     public void setLocales(Locale[] l) { this.availLocales = l; }
382
383 }
384
385 // eof
386
Popular Tags