1 5 6 package com.raptus.owxv3.api; 7 8 import java.sql.SQLException ; 9 import java.util.*; 10 11 import com.raptus.owxv3.*; 12 import com.raptus.owxv3.modules.base.BaseConstants; 13 14 36 public class LinksSelector extends Object 37 { 38 39 42 protected Locale[] availLocales = null; 43 44 45 48 protected Vector titleList=null; 49 50 53 protected Vector linksList = null; 54 55 56 59 protected String urltablename=null; 60 61 64 65 protected int maxLinkCount=0; 66 67 71 protected Vector linksToDelete = null; 72 73 76 public LinksSelector(String [] locales,String 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 ) po.getObjectOne(), (String ) po.getObjectTwo()); 87 } 88 } 89 90 93 public void reset() 94 { 95 linksList = new Vector(); 96 titleList = new Vector(); 97 linksToDelete = new Vector(); 98 } 99 100 103 public void loadLinks(GlobalResources gres, String tableId, int rowId, String field) 104 throws SQLException 105 { 106 linksList = gres.loadURLs(tableId, rowId, field); 107 108 int i,j; 110 for(i = 0; i < linksList.size(); i ++) 111 { 112 GResURL url = (GResURL) linksList.get(i); 113 String title[]=new String [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 129 public void saveLinks(GlobalResources gres, String tableId, int rowId, String field) 130 throws SQLException 131 { 132 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 String title[]=(String [])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 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 gres.deleteMessages(urltablename, url.getRowID(), BaseConstants.RESURLFIELD_URL); 161 162 } 163 } 164 165 166 167 168 169 172 public int addLink(String urlToAdd,String [] 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 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 } 208 209 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(grURL.getRowID() != -1) 222 linksToDelete.add(grURL); 223 224 linksList.removeElementAt(linkId); 226 titleList.removeElementAt(linkId); 228 229 } 230 } 231 } 232 233 236 public Vector getList() 237 { 238 return linksList; 239 } 240 241 244 public Vector getLinkList(Locale userLocale){ 245 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 link=""; 265 String title=""; 266 String 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(); 273 274 titles=(String [])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 291 public Locale[] getLocales() { return availLocales; } 292 public void setLocales(Locale[] l) { this.availLocales = l; } 293 294 } 295 296 | Popular Tags |