KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > snapper > business > Mapper


1 package org.enhydra.snapper.business;
2 /**
3  * @author Milin Radivoj
4  *
5  */

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 JavaDoc;
15 import java.io.File JavaDoc;
16 import java.net.URLEncoder JavaDoc;
17
18 public class Mapper{
19
20     Hashtable JavaDoc downloadProperties = new Hashtable JavaDoc();
21     Hashtable JavaDoc siteID = new Hashtable JavaDoc();
22     Hashtable JavaDoc table = new Hashtable JavaDoc();
23     Hashtable JavaDoc pathTypeDB = new Hashtable JavaDoc();
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 JavaDoc id = currentSite.getID();
36         String JavaDoc 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 JavaDoc type = typeList[j].getName();
63        String JavaDoc id = typeList[j].getID();
64        pathTypeDB.put(id,type);
65       }
66       
67      dbTransaction.release();
68       
69       }catch (Exception JavaDoc e){
70         Log.logException(e);
71          
72       }
73   }
74     
75  public void close()
76   {
77         table.clear();
78    }
79     
80  public String JavaDoc mapFullPathToLogical(String JavaDoc fullPath,String JavaDoc site,String JavaDoc type)
81      {
82          
83        String JavaDoc result=new String JavaDoc();
84        try{
85             Path[] pathArray = (Path[])table.get(site);
86             String JavaDoc download = (String JavaDoc)downloadProperties.get(site);
87             String JavaDoc id = (String JavaDoc)siteID.get(site);
88        for (int num = 0; num < pathArray.length; num++)
89         {
90               Path currentPath = pathArray[num];
91
92               StringBuffer JavaDoc finalPath = new StringBuffer JavaDoc();
93               
94               String JavaDoc host = currentPath.getHost();
95               String JavaDoc pathRoot = currentPath.getRoot();
96               String JavaDoc mappingRoot = currentPath.getMappingRoot();
97               String JavaDoc pathType = currentPath.getType();
98             
99               String JavaDoc PathTypeString = (String JavaDoc)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 JavaDoc 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 JavaDoc finall=finalPath.toString();
132 //final je ftp + host ili je pathRoot
133

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                     //return fullPath.replaceAll("\\\\","/");
163

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 JavaDoc e)
198          {
199             Log.logException(e);
200            return new String JavaDoc();
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 JavaDoc getRelativeDirPath(String JavaDoc fullPath,String JavaDoc fileName,String JavaDoc site)
214  {
215    
216     String JavaDoc result=new String JavaDoc();
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 JavaDoc pathRoot = currentPath.getRoot();
223        
224      if(fullPath.startsWith(pathRoot))
225         {
226  
227         String JavaDoc 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 JavaDoc e)
246      {
247         Log.logException(e);
248        return "";
249      }
250     return "";
251     
252     
253  }
254  public String JavaDoc getRelativeFilePath(String JavaDoc fullPath,String JavaDoc fileName,String JavaDoc site)
255  {
256    
257     String JavaDoc result=new String JavaDoc();
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 JavaDoc pathRoot = currentPath.getRoot();
264        
265      if(fullPath.startsWith(pathRoot))
266         {
267         
268          String JavaDoc 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 JavaDoc e)
281      {
282         Log.logException(e);
283        return "";
284      }
285     return "";
286     
287     
288  }
289  
290  
291  public String JavaDoc getAbsulutDirPath(String JavaDoc fullPath,String JavaDoc site)
292  {
293    
294     String JavaDoc result=new String JavaDoc();
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 JavaDoc 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 JavaDoc e)
317      {
318         Log.logException(e);
319        return "";
320      }
321     return "";
322     
323     
324  }
325  
326  
327     public String JavaDoc manage(String JavaDoc managedLink,String JavaDoc originlink){
328         if(managedLink.startsWith("DM_DownloadPO"))
329           return originlink;
330         else
331           return managedLink;
332     }
333  
334   
335   public String JavaDoc getGrantedDownload(String JavaDoc site)
336     {
337     String JavaDoc download = (String JavaDoc)downloadProperties.get(site);
338       if(download.equals("true"))
339        return "true";
340       else
341        return "false";
342     }
343     
344     
345     
346     public String JavaDoc getDownloadHref(String JavaDoc fullPath,String JavaDoc site,String JavaDoc type)
347     {
348         try{
349            String JavaDoc download = (String JavaDoc)downloadProperties.get(site);
350            String JavaDoc id = (String JavaDoc)siteID.get(site);
351              
352              if(download.equals("true"))
353                      {
354                          
355                        String JavaDoc temp = URLEncoder.encode(fullPath,"UTF-8");
356                        String JavaDoc 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 JavaDoc e)
366          {
367         Log.logException(e);
368            return new String JavaDoc();
369      }
370    
371       
372     }
373     
374 }
Popular Tags