1 5 package com.raptus.owxv3.api; 6 7 import java.sql.SQLException ; 8 import java.util.*; 9 10 import com.raptus.owxv3.*; 11 import com.raptus.owxv3.modules.base.BaseConstants; 12 13 37 public class FilesSelector extends Object 38 { 39 43 protected Vector allLinkableFiles = null; 44 45 49 protected Vector linkableFilesInCategory = null; 50 51 55 protected Vector linkedFiles = null; 56 57 58 59 62 protected Vector titleList=null; 63 64 68 protected Vector filesToDelete = null; 69 70 73 protected String [] allCategories = null; 74 75 76 77 80 protected Locale[] availLocales = null; 81 82 83 86 protected String filelinktablename=null; 87 88 91 92 protected int maxFileCount=0; 93 94 95 98 public FilesSelector(GlobalResources gres,String locales[],String fltn,int maxlinks) 99 throws SQLException 100 { 101 reset(); 102 103 allLinkableFiles = gres.loadLinkableFiles(); 104 allCategories = gres.loadAllFileCategories(); 105 switchToCategory( getDefaultCategory() ); 106 107 availLocales = new Locale[locales.length]; 108 109 for(int i = 0; i < locales.length; i ++) 110 { 111 PairOfObjects po = LocaleManager.stripLocaleString(locales[i]); 112 if(po != null && po.isValid()) 113 availLocales[i] = new Locale((String ) po.getObjectOne(), (String ) po.getObjectTwo()); 114 } 115 116 filelinktablename=fltn; 117 maxFileCount=maxlinks; 118 } 119 120 123 public void reset() 124 { 125 linkableFilesInCategory = new Vector(); 126 linkedFiles = new Vector(); 127 titleList=new Vector(); 128 filesToDelete = new Vector(); 129 } 130 131 134 public void loadLinkedFilesAndIds(GlobalResources gres, String tableId, int rowId, String field) 135 throws SQLException 136 { 137 Vector v = gres.loadLinkedFilesAndIds(GlobalResources.FILETYPE_FILE, tableId, rowId, field); 138 int i,j; 139 140 for(i = 0; i < v.size(); i ++) 141 { 142 PairOfObjects pair=(PairOfObjects)v.get(i); 143 144 GResFile file = (GResFile) pair.getObjectOne(); 145 linkedFiles.add(file); 146 147 148 Integer I=(Integer ) pair.getObjectTwo(); 149 150 String [] title=new String [availLocales.length]; 151 for(j=0;j<availLocales.length;j++) 152 { 153 title[j]=gres.loadMessage(filelinktablename, I.intValue(), BaseConstants.RESFILEFIELD_LINK, availLocales[j]); 154 if(title[j]==null) title[j]=""; 155 } titleList.add(title); 157 } } 159 160 163 public void saveLinkedFiles(GlobalResources gres, String tableId, int rowId, String field) 164 throws SQLException 165 { 166 int i,j; 168 for(i = 0; i < linkedFiles.size(); i ++) 169 { 170 GResFile file = (GResFile) linkedFiles.get(i); 171 172 int rowid=gres.saveFileLink(tableId, rowId, field, file); 173 if( rowid==-1 ) 174 { 175 LoggingManager.log("Failed to link file id: " + file.getRowID() + 176 " to entry id: " + rowId, this); 177 178 } 179 else 180 { 181 String title[]=(String [])titleList.get(i); 183 for(j=0;j<availLocales.length;j++) 184 { 185 gres.saveMessage(filelinktablename, rowid, BaseConstants.RESFILEFIELD_LINK, availLocales[j],title[j]); 186 } 187 } 189 190 191 } 193 194 for(i = 0; i < filesToDelete.size(); i ++) 196 { 197 GResFile file = (GResFile) filesToDelete.get(i); 198 199 204 205 206 207 int rowid=gres.deleteFileLink(tableId,rowId,field,file); 208 if( rowid==-1 ) 209 { 210 LoggingManager.log("Failed to delete URL id: " + file.getRowID() + 211 " from entry id: " + rowId, this); 212 213 } 214 else 215 { 216 218 gres.deleteMessages(filelinktablename, rowid, BaseConstants.RESFILEFIELD_LINK); 219 } 220 } } 222 223 224 227 public boolean linkFile(int fileId,String [] title) 228 { 229 if(maxFileCount>0 && linkedFiles.size()>=maxFileCount) return false; 230 GResFile grfile = getGRFileForRowId(linkableFilesInCategory, fileId); 231 if(grfile != null && linkedFiles.indexOf(grfile) == -1) 232 { 233 linkedFiles.add(grfile); 234 titleList.add(title); 235 } 236 return true; 237 238 } 239 240 243 public void unlinkFile(int fileId) 244 { 245 GResFile grfile =(GResFile)linkedFiles.elementAt(fileId); 247 248 for(int i=0;i<allLinkableFiles.size();i++) 249 { 250 if( grfile.getRowID()==((GResFile)allLinkableFiles.elementAt(i)).getRowID() ) 251 { 252 253 256 if(grfile.getRowID() != -1) 257 filesToDelete.add(grfile); 258 259 break; 260 } 261 } 262 263 265 linkedFiles.removeElementAt(fileId); 266 titleList.removeElementAt(fileId); 267 268 269 } 270 271 274 public void switchToCategory(String category) 275 { 276 if(category == null) 277 return; 278 279 linkableFilesInCategory.clear(); 280 281 Enumeration all = allLinkableFiles.elements(); 282 while(all.hasMoreElements()) 283 { 284 GResFile grfile = (GResFile) all.nextElement(); 285 if(grfile != null && grfile.getCategory().equals(category)) 286 linkableFilesInCategory.add(grfile); 287 } 288 } 289 290 293 protected GResFile getGRFileForRowId(Vector data, int rowId) 294 { 295 Enumeration availFiles = data.elements(); 296 while(availFiles.hasMoreElements()) 297 { 298 GResFile grfile = (GResFile) availFiles.nextElement(); 299 if(grfile != null && grfile.getRowID() == rowId) 300 return grfile; 301 } 302 303 return null; 304 } 305 306 309 protected String getDefaultCategory() 310 { 311 if(allCategories != null && allCategories.length > 0) 312 return allCategories[0]; 313 314 LoggingManager.log("Unable to determine the default pictures category!",this); 315 return null; 316 } 317 318 321 public Vector getAllLinkableFiles() { return allLinkableFiles; } 322 323 326 public Vector getLinkableFilesInCategory() { return linkableFilesInCategory; } 327 328 331 public Vector getLinkedFiles() { return linkedFiles; } 332 333 334 337 public Vector getLinkedFiles(Locale userLocale) 338 { 339 341 Vector ret=new Vector(); 342 try{ 343 344 345 int i=0,j=0; 346 347 int localeindex=0; 348 349 for (i=0;i<availLocales.length;i++) 350 { 351 if( availLocales[i].equals(userLocale) ) 352 { 353 localeindex=i; 354 break; 355 } 356 } 357 358 String link=""; 359 String title=""; 360 String titles[]=null; 361 for(i=0;i<linkedFiles.size();i++){ 362 GResFile gresFile=(GResFile)linkedFiles.get(i); 363 364 PairOfObjects po=new PairOfObjects(); 365 366 po.setObjectOne( gresFile.getFileURL() ); 367 368 titles=(String [])titleList.get(i); 369 if(titles!=null) title=titles[localeindex]; 370 else title=""; 371 372 if( title.equals("") ) po.setObjectTwo( gresFile.getFileName() ); 373 else po.setObjectTwo(title); 374 375 ret.add(po); 376 } 377 }catch(Exception e){ LoggingManager.log("Error:"+e,this);} 378 return ret; 379 } 380 381 382 385 public String [] getAllCategories() { return allCategories; } 386 387 388 391 public Locale[] getLocales() { return availLocales; } 392 public void setLocales(Locale[] l) { this.availLocales = l; } 393 394 } 395 396 | Popular Tags |