1 package org.enhydra.snapper.business; 2 6 7 import org.enhydra.dods.DODS; 8 9 import org.enhydra.snapper.spec.*; 10 import org.enhydra.snapper.Log; 11 import org.enhydra.snapper.Snapper; 12 13 import com.lutris.appserver.server.sql.DBTransaction; 14 import java.util.Hashtable ; 15 import java.io.File ; 16 import java.net.URLEncoder ; 17 18 public class Mapper{ 19 20 Hashtable downloadProperties = new Hashtable (); 21 Hashtable siteID = new Hashtable (); 22 Hashtable table = new Hashtable (); 23 Hashtable pathTypeDB = new Hashtable (); 24 25 public Mapper(){ 26 27 try { 28 DBTransaction dbTransaction = DODS.getDatabaseManager().createTransaction(); 29 SiteListImpl sl = new SiteListImpl(); 30 Site[] list = sl.getList(dbTransaction); 31 32 for(int i=0;i<list.length;i++) 33 { 34 Site currentSite = list[i]; 35 String id = currentSite.getID(); 36 String name = currentSite.getName(); 37 38 siteID.put(name,id); 39 boolean download = currentSite.getDOWNLOAD(); 40 41 Path pt; 42 Path[] pathArray; 43 44 pt = PathFactory.getPath("org.enhydra.snapper.business.PathImpl"); 45 46 pathArray = pt.getListForID(dbTransaction, id); 47 48 table.put(name,pathArray); 49 50 if(download) 51 downloadProperties.put(name,"true"); 52 else 53 downloadProperties.put(name,"false"); 54 55 } 56 57 PathTypeImpl typeType=new PathTypeImpl(); 58 PathType[] typeList= typeType.getList(dbTransaction); 59 60 for(int j=0;j<typeList.length;j++) 61 { 62 String type = typeList[j].getName(); 63 String id = typeList[j].getID(); 64 pathTypeDB.put(id,type); 65 } 66 67 dbTransaction.release(); 68 69 }catch (Exception e){ 70 Log.logException(e); 71 72 } 73 } 74 75 public void close() 76 { 77 table.clear(); 78 } 79 80 public String mapFullPathToLogical(String fullPath,String site,String type) 81 { 82 83 String result=new String (); 84 try{ 85 Path[] pathArray = (Path[])table.get(site); 86 String download = (String )downloadProperties.get(site); 87 String id = (String )siteID.get(site); 88 for (int num = 0; num < pathArray.length; num++) 89 { 90 Path currentPath = pathArray[num]; 91 92 StringBuffer finalPath = new StringBuffer (); 93 94 String host = currentPath.getHost(); 95 String pathRoot = currentPath.getRoot(); 96 String mappingRoot = currentPath.getMappingRoot(); 97 String pathType = currentPath.getType(); 98 99 String PathTypeString = (String )pathTypeDB.get(pathType); 100 101 if(PathTypeString.equals("FTP")) 102 { 103 finalPath.append("ftp://"); 104 } 105 106 if(!host.equals("")) 107 { 108 if(host.endsWith(File.separator)) 109 { 110 if(pathRoot.startsWith(File.separator)) 111 { 112 String temp=pathRoot.replaceFirst(File.separator,""); 113 finalPath.append(host+temp); 114 } 115 else 116 finalPath.append(host+pathRoot); 117 } 118 else 119 { 120 if(pathRoot.startsWith(File.separator)) 121 finalPath.append(host+pathRoot); 122 else 123 finalPath.append(host+File.separator+pathRoot); 124 } 125 } 126 else 127 { 128 finalPath.append(pathRoot); 129 } 130 131 String finall=finalPath.toString(); 132 134 if(Snapper.getRelativeIndexPath().equalsIgnoreCase("true")) 135 { 136 if(!mappingRoot.equals("")) 137 { 138 if(mappingRoot.endsWith(File.separator)) 139 result = mappingRoot + fullPath; 140 else 141 result = mappingRoot + File.separator + fullPath; 142 143 if(Snapper.getFileSeparatorConvention()!=null && 144 Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) { 145 result = result.replaceAll("\\\\","/"); 146 } 147 148 return result; 149 } 150 else 151 { 152 if(pathRoot.endsWith(File.separator)) 153 result = pathRoot + fullPath; 154 else 155 result = pathRoot + File.separator + fullPath; 156 157 if(Snapper.getFileSeparatorConvention()!=null && 158 Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) { result = result.replaceAll("\\\\","/"); 159 } 160 161 return result; 162 164 } 165 } else if(fullPath.startsWith(finall)) { 166 167 if(!mappingRoot.equals("")) 168 { 169 if(mappingRoot.endsWith(File.separator)) 170 result = mappingRoot + fullPath.substring(finall.length()+1); 171 else 172 result = mappingRoot + File.separator + fullPath.substring(finall.length()+1); 173 174 if(Snapper.getFileSeparatorConvention()!=null && 175 Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) { 176 result = result.replaceAll("\\\\","/"); 177 } 178 179 return result; 180 } 181 else 182 { 183 if(Snapper.getFileSeparatorConvention()!=null && 184 Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) { 185 fullPath = fullPath.replaceAll("\\\\","/"); 186 } 187 188 return fullPath; 189 190 } 191 } 192 193 194 195 } 196 } 197 catch (Exception e) 198 { 199 Log.logException(e); 200 return new String (); 201 } 202 if(Snapper.getFileSeparatorConvention()!=null && 203 Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) { 204 fullPath = fullPath.replaceAll("\\\\","/"); 205 } 206 207 return fullPath; 208 209 210 } 211 212 213 public String getRelativeDirPath(String fullPath,String fileName,String site) 214 { 215 216 String result=new String (); 217 try{ 218 Path[] pathArray = (Path[])table.get(site); 219 for (int num = 0; num < pathArray.length; num++) 220 { 221 Path currentPath = pathArray[num]; 222 String pathRoot = currentPath.getRoot(); 223 224 if(fullPath.startsWith(pathRoot)) 225 { 226 227 String temp = fullPath.substring(pathRoot.length(),fullPath.length()); 228 if (!fullPath.endsWith("zip") && !fullPath.endsWith("ZIP")){ 229 temp = temp.substring(0,temp.indexOf(fileName)); 230 } 231 else 232 { 233 temp = temp.substring(0,temp.lastIndexOf(File.separator)+1); 234 } 235 if(Snapper.getFileSeparatorConvention()!=null && 236 Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) { 237 temp = temp.replaceAll("\\\\","/"); 238 } 239 240 return temp; 241 242 } 243 } 244 } 245 catch (Exception e) 246 { 247 Log.logException(e); 248 return ""; 249 } 250 return ""; 251 252 253 } 254 public String getRelativeFilePath(String fullPath,String fileName,String site) 255 { 256 257 String result=new String (); 258 try{ 259 Path[] pathArray = (Path[])table.get(site); 260 for (int num = 0; num < pathArray.length; num++) 261 { 262 Path currentPath = pathArray[num]; 263 String pathRoot = currentPath.getRoot(); 264 265 if(fullPath.startsWith(pathRoot)) 266 { 267 268 String temp = fullPath.substring(pathRoot.length(),fullPath.length()); 269 270 if(Snapper.getFileSeparatorConvention()!=null && 271 Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) { 272 temp = temp.replaceAll("\\\\","/"); 273 } 274 275 return temp; 276 277 } 278 } 279 } 280 catch (Exception e) 281 { 282 Log.logException(e); 283 return ""; 284 } 285 return ""; 286 287 288 } 289 290 291 public String getAbsulutDirPath(String fullPath,String site) 292 { 293 294 String result=new String (); 295 try{ 296 Path[] pathArray = (Path[])table.get(site); 297 for (int num = 0; num < pathArray.length; num++) 298 { 299 Path currentPath = pathArray[num]; 300 String pathRoot = currentPath.getRoot(); 301 302 303 if(fullPath.startsWith(pathRoot)) 304 { 305 306 if(Snapper.getFileSeparatorConvention()!=null && 307 Snapper.getFileSeparatorConvention().equalsIgnoreCase("unix")) { 308 pathRoot = pathRoot.replaceAll("\\\\","/"); 309 } 310 311 return pathRoot; 312 313 } 314 } 315 } 316 catch (Exception e) 317 { 318 Log.logException(e); 319 return ""; 320 } 321 return ""; 322 323 324 } 325 326 327 public String manage(String managedLink,String originlink){ 328 if(managedLink.startsWith("DM_DownloadPO")) 329 return originlink; 330 else 331 return managedLink; 332 } 333 334 335 public String getGrantedDownload(String site) 336 { 337 String download = (String )downloadProperties.get(site); 338 if(download.equals("true")) 339 return "true"; 340 else 341 return "false"; 342 } 343 344 345 346 public String getDownloadHref(String fullPath,String site,String type) 347 { 348 try{ 349 String download = (String )downloadProperties.get(site); 350 String id = (String )siteID.get(site); 351 352 if(download.equals("true")) 353 { 354 355 String temp = URLEncoder.encode(fullPath,"UTF-8"); 356 String result= "DM_DownloadPO.po?url="+temp+"&type="+type+"&id="+id; 357 return result; 358 } 359 else 360 { 361 return fullPath; 362 } 363 364 } 365 catch (Exception e) 366 { 367 Log.logException(e); 368 return new String (); 369 } 370 371 372 } 373 374 } | Popular Tags |