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 PicturesSelector extends Object 37 { 38 42 protected Vector allLinkablePics = null; 43 44 48 protected Vector linkablePicsInCategory = null; 49 50 54 protected Vector linkedPics = null; 55 56 57 60 protected Vector titleList=null; 61 62 63 67 protected Vector picsToDelete = null; 68 69 72 protected String [] allCategories = null; 73 74 75 78 protected Locale[] availLocales = null; 79 80 81 84 protected String filelinktablename=null; 85 86 87 90 91 protected int maxPicCount=0; 92 93 96 public PicturesSelector(GlobalResources gres,String locales[],String fltn,int picturecount) 97 throws SQLException 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 ) po.getObjectOne(), (String ) po.getObjectTwo()); 112 } 113 114 filelinktablename=fltn; 115 maxPicCount=picturecount; 116 } 117 118 121 public void reset() 122 { 123 linkablePicsInCategory = new Vector(); 124 linkedPics = new Vector(); 125 titleList=new Vector(); 126 picsToDelete = new Vector(); 127 } 128 129 132 public void loadLinkedPictures(GlobalResources gres, String tableId, int rowId, String field) 133 throws SQLException 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 I=(Integer ) pair.getObjectTwo(); 146 147 String [] title=new String [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 } titleList.add(title); 154 } 156 } 157 158 161 public void saveLinkedPictures(GlobalResources gres, String tableId, int rowId, String field) 162 throws SQLException 163 { 164 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 String title[]=(String [])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 } } 186 187 188 189 for(i = 0; i < picsToDelete.size(); i ++) 191 { 192 GResPicture pic = (GResPicture) picsToDelete.get(i); 193 194 199 200 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 } } 215 216 217 220 public boolean linkPicture(int picId,String [] 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 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(grPic.getRowID() != -1) 247 picsToDelete.add(grPic); 248 249 break; 250 } } 253 linkedPics.removeElementAt(picId); 255 titleList.removeElementAt(picId); 256 257 } 258 259 262 public void switchToCategory(String 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 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 297 protected String 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 309 public Vector getAllLinkablePics() { return allLinkablePics; } 310 311 314 public Vector getLinkablePicsInCategory() { return linkablePicsInCategory; } 315 316 319 public Vector getLinkedPics() { return linkedPics; } 320 321 322 323 326 public Vector getLinkedPics(Locale userLocale) 327 { 328 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 pic=""; 348 String title=""; 349 String 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 [])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 375 public String [] getAllCategories() { return allCategories; } 376 377 380 public Locale[] getLocales() { return availLocales; } 381 public void setLocales(Locale[] l) { this.availLocales = l; } 382 383 } 384 385 | Popular Tags |