1 package net.javacoding.jspider.core.storage.memory; 2 3 import net.javacoding.jspider.core.storage.spi.FolderDAOSPI; 4 import net.javacoding.jspider.core.storage.spi.StorageSPI; 5 import net.javacoding.jspider.core.model.FolderInternal; 6 import net.javacoding.jspider.core.model.SiteInternal; 7 8 import java.util.*; 9 10 13 class FolderDAOImpl implements FolderDAOSPI { 14 15 protected StorageSPI storage; 16 17 protected Map parents; 18 protected Map children; 19 protected Map byId; 20 protected Map siteRoots; 21 22 public FolderDAOImpl ( StorageSPI storage ) { 23 this.storage = storage; 24 this.parents = new HashMap(); 25 this.children = new HashMap(); 26 this.byId = new HashMap(); 27 this.siteRoots = new HashMap(); 28 } 29 30 public FolderInternal[] findSubFolders(FolderInternal folder) { 31 Set folders = (Set) children.get(folder); 32 if ( folders == null ) { 33 return new FolderInternal[0]; 34 } else { 35 return (FolderInternal[]) folders.toArray(new FolderInternal[folders.size()]); 36 } 37 } 38 39 public FolderInternal[] findSiteRootFolders(SiteInternal site) { 40 Set rootFolders = (Set) siteRoots.get(new Integer (site.getId())); 41 if ( rootFolders == null ) { 42 return new FolderInternal[0]; 43 } else { 44 return (FolderInternal[]) rootFolders.toArray(new FolderInternal[rootFolders.size()]); 45 } 46 } 47 48 public FolderInternal createFolder(int id, FolderInternal parent, String name) { 49 FolderInternal folder = new FolderInternal ( storage, id, parent.getId(), name, parent.getSiteId() ); 50 byId.put(new Integer (id), folder); 51 parents.put(folder, parent); 52 53 Set set = (Set) children.get(parent); 54 if ( set == null ) { 55 set = new HashSet(); 56 children.put(parent,set); 57 } 58 set.add(folder); 59 60 return folder; 61 } 62 63 public FolderInternal createFolder(int id, SiteInternal site, String name) { 64 FolderInternal folder = new FolderInternal ( storage, id, 0, name, site.getId() ); 65 Set roots = (Set) siteRoots.get(new Integer (site.getId())); 66 if (roots == null){ 67 roots = new HashSet(); 68 siteRoots.put(new Integer (site.getId()), roots); 69 } 70 roots.add(folder); 71 72 byId.put(new Integer (id), folder); 73 return folder; 74 } 75 76 public FolderInternal findById(int id) { 77 return (FolderInternal)byId.get(new Integer (id)); 78 } 79 } 80 | Popular Tags |