KickJava   Java API By Example, From Geeks To Geeks.

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


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%">LinksSelector.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%">30th of July 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 LinksSelector extends Object JavaDoc
37 {
38
39      /**
40      *
41      */

42     protected Locale[] availLocales = null;
43
44
45     /**
46      *
47      */

48     protected Vector titleList=null;
49
50     /**
51      *
52      */

53     protected Vector linksList = null;
54
55
56     /**
57      *
58      */

59     protected String JavaDoc urltablename=null;
60
61      /**
62      *
63      */

64
65     protected int maxLinkCount=0;
66
67     /**
68      * This vector contains the GResURL object(s) which already exist
69      * in the database, but were chosen to be deleted.
70      */

71     protected Vector linksToDelete = null;
72
73     /**
74      *
75      */

76     public LinksSelector(String JavaDoc[] locales,String JavaDoc urltblname,int linkcount)
77     {
78         reset();
79         availLocales = new Locale[locales.length];
80         urltablename= urltblname;
81         maxLinkCount=linkcount;
82         for(int i = 0; i < locales.length; i ++)
83         {
84             PairOfObjects po = LocaleManager.stripLocaleString(locales[i]);
85             if(po != null && po.isValid())
86                 availLocales[i] = new Locale((String JavaDoc) po.getObjectOne(), (String JavaDoc) po.getObjectTwo());
87         }
88     }
89
90     /**
91      *
92      */

93     public void reset()
94     {
95         linksList = new Vector();
96         titleList = new Vector();
97         linksToDelete = new Vector();
98     }
99
100     /**
101      *
102      */

103     public void loadLinks(GlobalResources gres, String JavaDoc tableId, int rowId, String JavaDoc field)
104                 throws SQLException JavaDoc
105     {
106         linksList = gres.loadURLs(tableId, rowId, field);
107
108         //loading titles too
109
int i,j;
110         for(i = 0; i < linksList.size(); i ++)
111         {
112             GResURL url = (GResURL) linksList.get(i);
113             String JavaDoc title[]=new String JavaDoc[availLocales.length];
114             for(j=0;j<availLocales.length;j++)
115             {
116                 title[j]=gres.loadMessage(urltablename, url.getRowID(), BaseConstants.RESURLFIELD_URL, availLocales[j]);
117                 if(title[j]==null) title[j]="";
118             }
119
120             titleList.insertElementAt(title,i);
121
122         }
123
124     }
125
126     /**
127      *
128      */

129     public void saveLinks(GlobalResources gres, String JavaDoc tableId, int rowId, String JavaDoc field)
130                 throws SQLException JavaDoc
131     {
132         // save all new or modified GResURL objects
133
int i,j;
134         for(i = 0; i < linksList.size(); i ++)
135         {
136             GResURL url = (GResURL) linksList.get(i);
137             if( !gres.saveURL(tableId, rowId, field, url) )
138                 LoggingManager.log("Failed to save URL id: " + url.getRowID()+
139                                    " to entry id: " + rowId, this);
140
141             //saving titles too
142
String JavaDoc title[]=(String JavaDoc[])titleList.get(i);
143             for(j=0;j<availLocales.length;j++)
144             {
145                 gres.saveMessage(urltablename, url.getRowID(), BaseConstants.RESURLFIELD_URL, availLocales[j],title[j]);
146             }
147
148         }
149
150         // drop the urls marked to delete
151

152         for(i = 0; i < linksToDelete.size(); i ++)
153         {
154             GResURL url = (GResURL) linksToDelete.get(i);
155             if( !gres.deleteURL(url) )
156                 LoggingManager.log("Failed to delete URL id: " + url.getRowID()+
157                                    " from entry id: " + rowId, this);
158
159             //deleting the title too from messages
160
gres.deleteMessages(urltablename, url.getRowID(), BaseConstants.RESURLFIELD_URL);
161
162         }
163     }
164
165
166
167
168
169     /**
170      *
171      */

172     public int addLink(String JavaDoc urlToAdd,String JavaDoc[] t)
173     {
174         if(linksList == null || titleList==null)
175             reset();
176
177
178         if(urlToAdd != null && t!=null)
179         {
180             if(maxLinkCount>0 && linksList.size()>=maxLinkCount) return 0;
181             GResURL grURL = new GResURL();
182             grURL.setFullURL(urlToAdd);
183
184             if(!isValidLink(grURL)) return -1;
185             linksList.add(grURL);
186             titleList.add(t);
187         }
188         return 1;
189     }
190
191
192     /**
193      *
194      *Method for checking the this link is allready added or not.
195      */

196     private boolean isValidLink(GResURL url)
197     {
198         GResURL gresUrl=null;
199         for(int i=0;i<linksList.size();i++)
200         {
201             gresUrl=(GResURL)linksList.get(i);
202             if( gresUrl.getProtocol().equals(url.getProtocol()) && gresUrl.getURL().equals(url.getURL()) ) return false;
203         }
204
205         return true;
206     }//end
207

208
209     /**
210      *
211      */

212     public void removeLink(int linkId)
213     {
214         if(linksList != null)
215         {
216             GResURL grURL = (GResURL) linksList.elementAt(linkId);
217             if(grURL != null)
218             {
219                 // if the link is already stored in the DB put
220
// it on the "to be removed list"
221
if(grURL.getRowID() != -1)
222                     linksToDelete.add(grURL);
223
224                 // now it is save to kill the 1st reference
225
linksList.removeElementAt(linkId);
226                 //removing from titleList too
227
titleList.removeElementAt(linkId);
228
229             }
230         }
231     }
232
233     /**
234      *
235      */

236     public Vector getList()
237     {
238         return linksList;
239     }
240
241     /**
242      *
243      */

244     public Vector getLinkList(Locale userLocale){
245         //preparing pare of objects of type String, String
246

247         Vector ret=new Vector();
248
249
250
251         int i=0,j=0;
252
253         int localeindex=0;
254
255         for (i=0;i<availLocales.length;i++)
256         {
257             if( availLocales[i].equals(userLocale) )
258             {
259                 localeindex=i;
260                 break;
261             }
262         }
263
264         String JavaDoc link="";
265         String JavaDoc title="";
266         String JavaDoc titles[]=null;
267         for(i=0;i<linksList.size();i++){
268             GResURL gresUrl=(GResURL)linksList.get(i);
269
270             PairOfObjects po=new PairOfObjects();
271             //link=gresUrl.getProtocol()+"://"+gresUrl.getURL();
272
link=gresUrl.getProtocol()+gresUrl.getURL();
273             
274             titles=(String JavaDoc[])titleList.get(i);
275             if(titles!=null) title=titles[localeindex];
276             else title="";
277             po.setObjectOne(link);
278             if( title.equals("") ) po.setObjectTwo(link);
279             else po.setObjectTwo(title);
280
281             ret.add(po);
282         }
283
284         return ret;
285     }
286
287
288     /**
289      *
290      */

291     public Locale[] getLocales() { return availLocales; }
292     public void setLocales(Locale[] l) { this.availLocales = l; }
293
294 }
295
296 // eof
297
Popular Tags