1 5 6 package org.exoplatform.services.jcr.impl.storage.rdb.repository; 7 8 import java.util.HashSet ; 9 import java.util.List ; 10 import java.util.Set ; 11 12 import javax.jcr.ItemExistsException; 13 import javax.jcr.PathNotFoundException; 14 import javax.jcr.RepositoryException; 15 16 import net.sf.hibernate.Session; 17 import net.sf.hibernate.type.StringType; 18 19 import org.apache.commons.logging.Log; 20 import org.exoplatform.container.PortalContainer; 21 import org.exoplatform.services.jcr.core.NodeData; 22 import org.exoplatform.services.jcr.core.ReferenceableNodeLocation; 23 import org.exoplatform.services.jcr.impl.storage.BaseRepositoryManager; 24 import org.exoplatform.services.jcr.impl.storage.rdb.repository.data.ContainerRecord; 25 import org.exoplatform.services.jcr.impl.storage.rdb.repository.data.PathReference; 26 import org.exoplatform.services.jcr.impl.storage.rdb.repository.data.UUIDReference; 27 import org.exoplatform.services.log.LogUtil; 28 import org.exoplatform.services.database.HibernateServiceContainer; 29 import org.exoplatform.services.database.HibernateService; 30 import org.exoplatform.services.database.DatabaseService; 31 32 37 public class RDBRepositoryManagerImpl extends BaseRepositoryManager { 38 39 static private String [] MAPPINGS = 40 { 41 "org/exoplatform/services/jcr/impl/storage/rdb/repository/data/ContainerRecord.hbm.xml" , 42 "org/exoplatform/services/jcr/impl/storage/rdb/repository/data/UUIDReference.hbm.xml" , 43 "org/exoplatform/services/jcr/impl/storage/rdb/repository/data/PathReference.hbm.xml" , 44 } ; 45 46 47 private static String FIND_LOCATION_BY_UUID = "from UUIDReference as ref where ref.uuid=? and ref.container.name=?"; 48 private static String FIND_LOCATION_BY_PATH = "from PathReference as ref where ref.path=? and ref.uuidRef.container.name=?"; 49 private static String FIND_CONTAINER = "from ContainerRecord as container where container.name=?"; 50 51 private static boolean initialized = false; 52 53 protected Log log; 54 private HibernateService hibernateService; 55 private String name; 57 58 private static long id = System.currentTimeMillis(); 60 61 public RDBRepositoryManagerImpl(String name, String dsName) throws RepositoryException { 62 63 log = LogUtil.getLog("org.exoplatform.services.jcr"); 64 this.name = name; 65 log.debug("RDBRepositoryManagerImpl (String name, String dsName, HibernateServiceContainer hibernateContainer) instantiated. DS name ="+dsName); 66 67 try { 68 69 HibernateServiceContainer hibernateContainer = (HibernateServiceContainer)PortalContainer. 70 getInstance().getComponentInstanceOfType(HibernateServiceContainer.class); 71 72 this.hibernateService = hibernateContainer.getHibernateService(dsName); 73 74 hibernateService.addMappingFiles(MAPPINGS); 75 76 78 } catch (Exception e) { 79 e.printStackTrace(); 80 throw new RepositoryException("RDBRepositoryManagerImpl(String name, String dsName) failed Reason: "+e); 81 } 82 83 } 84 85 86 synchronized public String generateUUID(NodeData context) { 87 return String.valueOf(++id); 89 } 90 91 synchronized public ReferenceableNodeLocation getLocationByUUID(String workspaceContainerName, String UUID) 92 throws PathNotFoundException { 93 log.debug("RDBRepositoryManagerImpl is finding location by UUID " + UUID); 94 List refs; 95 try { 96 Session hibernateSession = hibernateService.openSession(); 97 Object [] values = { UUID, workspaceContainerName}; 98 StringType[] types = { new StringType(), new StringType() }; 99 refs = hibernateSession.find(FIND_LOCATION_BY_UUID, values, types); 100 101 } catch (Exception e) { 102 e.printStackTrace(); 103 throw new RuntimeException ("Hibernate throws Exception " + e); 104 } 105 if (refs.isEmpty()) 106 throw new PathNotFoundException("UUID " + UUID + " not found in " + workspaceContainerName); 107 UUIDReference ref = (UUIDReference)refs.get(0); 108 ReferenceableNodeLocation loc = new ReferenceableNodeLocation(ref.getRealPath(), UUID, ref.getRealPath(), ref.getRefPaths()); 109 log.debug("RDBRepositoryManagerImpl found location by UUID " + UUID + " " + loc); 110 111 return loc; 112 } 113 114 synchronized public ReferenceableNodeLocation getLocationByPath(String workspaceContainerName, String path) 115 throws PathNotFoundException { 116 List refs; 117 try { 118 Session hibernateSession = hibernateService.openSession(); 119 120 log.debug("RDBRepositoryManagerImpl is finding location by path " + path+" in workspace "+workspaceContainerName+" session: "+hibernateSession); 121 Object [] values = { path, workspaceContainerName }; 122 StringType[] types = { new StringType(), new StringType() }; 123 refs = hibernateSession.find(FIND_LOCATION_BY_PATH, values, types); 124 125 } catch (Exception e) { 126 e.printStackTrace(); 127 throw new RuntimeException ("Hibernate throws Exception " + e); 128 } 129 if (refs.isEmpty()) 130 throw new PathNotFoundException("Path " + path + " not found in " + workspaceContainerName); 131 PathReference ref = (PathReference)refs.get(0); 132 ReferenceableNodeLocation loc = new ReferenceableNodeLocation(path, ref.getUuidRef().getUuid(), ref.getUuidRef().getRealPath(), 133 ref.getUuidRef().getRefPaths()); 134 log.debug("RDBRepositoryManagerImpl found location by path " + path + " " + loc); 135 return loc; 136 } 137 138 synchronized public void addLocation(String workspaceContainerName, String UUID, String path, boolean isNew) 139 throws PathNotFoundException, ItemExistsException { 140 log.debug("RDBRepositoryManagerImpl is adding reference " + path + " isNew= " + isNew + " to UUID: " + UUID + 141 " containerName: "+workspaceContainerName); 142 try { 143 Session hibernateSession = hibernateService.openSession(); 144 ContainerRecord containerRecord = new ContainerRecord(); 145 containerRecord.setName(workspaceContainerName); 146 List containerRecords = hibernateSession.find(FIND_CONTAINER, workspaceContainerName, new StringType()); 147 if (!containerRecords.isEmpty()) 148 containerRecord = (ContainerRecord)containerRecords.get(0); 149 else { 150 throw new PathNotFoundException("RDBRepositoryManager.addLocation() Container "+workspaceContainerName+" not found"); 151 } 154 UUIDReference uuidRef; 155 Object [] values = { UUID, workspaceContainerName}; 156 StringType[] types = { new StringType(), new StringType() }; 157 List uuidRefs = hibernateSession.find(FIND_LOCATION_BY_UUID, values, types); 158 if (isNew) { 159 if (!uuidRefs.isEmpty()) 160 throw new ItemExistsException("UUID " + UUID + " already exists in " + workspaceContainerName); 161 uuidRef = new UUIDReference(); 162 uuidRef.setUuid(UUID); 163 uuidRef.setRealPath(path); 164 uuidRef.setContainer(containerRecord); 165 166 PathReference pathRef = new PathReference(); 167 pathRef.setPath(path); 168 169 Set pathRefs = new HashSet (); 170 pathRefs.add(pathRef); 171 uuidRef.setReferences(pathRefs); 172 hibernateSession.save(uuidRef); 173 174 pathRef.setUuidRef(uuidRef); 175 hibernateSession.save(pathRef); 176 177 hibernateSession.flush(); 178 } else { 179 if (uuidRefs.isEmpty()) 180 throw new PathNotFoundException("UUID " + UUID + " not found in " + workspaceContainerName); 181 uuidRef = (UUIDReference)uuidRefs.get(0); 182 183 PathReference pathRef = new PathReference(); 188 pathRef.setUuidRef(uuidRef); 189 pathRef.setPath(path); 190 hibernateSession.save(pathRef); 191 192 Set refs = uuidRef.getReferences(); 193 refs.add(pathRef); 194 uuidRef.setReferences(refs); 195 196 hibernateSession.flush(); 197 } 198 } catch (PathNotFoundException e) { 199 throw e; 200 } catch (ItemExistsException e) { 201 throw e; 202 } catch (Exception e) { 203 e.printStackTrace(); 204 throw new RuntimeException ("Hibernate throws Exception " + e); 205 } 206 log.debug("RDBRepositoryManagerImpl added reference " + path + " isNew= " + isNew + " to " + UUID); 207 } 208 209 synchronized public void deleteLocationByPath(String workspaceContainerName, String path) { 210 log.debug("RDBRepositoryManagerImpl is deleting location by path " + path); 211 try { 212 ReferenceableNodeLocation loc = this.getLocationByPath(workspaceContainerName, path); 213 deleteLocationByUUID(workspaceContainerName, loc.getUUID()); 214 } catch (PathNotFoundException e) { 215 } 217 } 218 219 synchronized public void deleteLocationByUUID(String workspaceContainerName, String UUID) { 220 log.debug("RDBRepositoryManagerImpl is deleting location by UUID " + UUID); 221 try { 222 Session hibernateSession = hibernateService.openSession(); 223 Object [] values = { UUID, workspaceContainerName}; 224 StringType[] types = { new StringType(), new StringType() }; 225 List uuidRefs = hibernateSession.find(FIND_LOCATION_BY_UUID, values, types); 226 if (uuidRefs.isEmpty()) 227 return; 228 else 229 hibernateSession.delete((UUIDReference)uuidRefs.get(0)); 230 231 } catch (Exception e) { 233 throw new RuntimeException ("Hibernate throws Exception " + e); 234 } 235 } 236 237 synchronized public void addWorkspaceContainer(String workspaceContainerName) { 238 try { 239 Session hibernateSession = hibernateService.openSession(); 240 ContainerRecord containerRecord = new ContainerRecord(); 241 containerRecord.setName(workspaceContainerName); 242 List containerRecords = hibernateSession.find(FIND_CONTAINER, workspaceContainerName, new StringType()); 243 if (!containerRecords.isEmpty()) { 244 containerRecord = (ContainerRecord)containerRecords.get(0); 245 log.debug("RDBRepositoryManagerImpl.addWorkspaceContainer() "+workspaceContainerName+" already exists"); 246 } else { 247 hibernateSession.save(containerRecord); 248 hibernateSession.flush(); 249 log.debug("RDBRepositoryManagerImpl.addWorkspaceContainer() "+containerRecord.getId()+" "+workspaceContainerName); 250 } 251 252 } catch (Exception e) { 253 e.printStackTrace(); 254 throw new RuntimeException ("Hibernate throws Exception " + e); 255 } 256 } 257 258 } 259 | Popular Tags |