KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > storage > rdb > repository > RDBRepositoryManagerImpl


1 /****************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  */

5
6 package org.exoplatform.services.jcr.impl.storage.rdb.repository;
7
8 import java.util.HashSet JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Set JavaDoc;
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 /**
33  * Created by The eXo Platform SARL .
34  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
35  * @version $Id: RDBRepositoryManagerImpl.java,v 1.1 2004/11/02 18:34:38 geaz Exp $
36  */

37 public class RDBRepositoryManagerImpl extends BaseRepositoryManager {
38
39    static private String JavaDoc[] 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 JavaDoc FIND_LOCATION_BY_UUID = "from UUIDReference as ref where ref.uuid=? and ref.container.name=?";
48     private static String JavaDoc FIND_LOCATION_BY_PATH = "from PathReference as ref where ref.path=? and ref.uuidRef.container.name=?";
49     private static String JavaDoc 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 Session hibernateSession;
56
private String JavaDoc name;
57
58     // ?
59
private static long id = System.currentTimeMillis();
60
61     public RDBRepositoryManagerImpl(String JavaDoc name, String JavaDoc 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 // this.hibernateSession = hibernateService.openSession();
77

78         } catch (Exception JavaDoc 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 JavaDoc generateUUID(NodeData context) {
87         // Temporary... Use hibernate for generate UUID ?
88
return String.valueOf(++id);
89     }
90
91     synchronized public ReferenceableNodeLocation getLocationByUUID(String JavaDoc workspaceContainerName, String JavaDoc UUID)
92         throws PathNotFoundException {
93             log.debug("RDBRepositoryManagerImpl is finding location by UUID " + UUID);
94             List JavaDoc refs;
95             try {
96                 Session hibernateSession = hibernateService.openSession();
97                 Object JavaDoc[] values = { UUID, workspaceContainerName};
98                 StringType[] types = { new StringType(), new StringType() };
99                 refs = hibernateSession.find(FIND_LOCATION_BY_UUID, values, types);
100
101             } catch (Exception JavaDoc e) {
102                 e.printStackTrace();
103                 throw new RuntimeException JavaDoc("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 JavaDoc workspaceContainerName, String JavaDoc path)
115         throws PathNotFoundException {
116             List JavaDoc 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 JavaDoc[] values = { path, workspaceContainerName };
122                 StringType[] types = { new StringType(), new StringType() };
123                 refs = hibernateSession.find(FIND_LOCATION_BY_PATH, values, types);
124
125             } catch (Exception JavaDoc e) {
126                 e.printStackTrace();
127                 throw new RuntimeException JavaDoc("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 JavaDoc workspaceContainerName, String JavaDoc UUID, String JavaDoc 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 JavaDoc 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 // session.save(containerRecord);
152
// log.debug("NEW ContainerRecord "+containerRecord.getId()+" "+workspaceContainerName);
153
}
154                 UUIDReference uuidRef;
155                 Object JavaDoc[] values = { UUID, workspaceContainerName};
156                 StringType[] types = { new StringType(), new StringType() };
157                 List JavaDoc 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 JavaDoc pathRefs = new HashSet JavaDoc();
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 // Object[] values1 = {path, workspaceContainerName};
184
// List pathRefs = session.find(FIND_LOCATION_BY_PATH, values1, types);
185
// if (pathRefs.isEmpty())
186
// throw new ItemExistsException("Path " + path + " already exists in " + workspaceContainerName);
187
PathReference pathRef = new PathReference();
188                     pathRef.setUuidRef(uuidRef);
189                     pathRef.setPath(path);
190                     hibernateSession.save(pathRef);
191
192                     Set JavaDoc 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 JavaDoc e) {
203                 e.printStackTrace();
204                 throw new RuntimeException JavaDoc("Hibernate throws Exception " + e);
205             }
206             log.debug("RDBRepositoryManagerImpl added reference " + path + " isNew= " + isNew + " to " + UUID);
207     }
208
209     synchronized public void deleteLocationByPath(String JavaDoc workspaceContainerName, String JavaDoc 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             // log.debug("RDBRepositoryManagerImpl is deleting location by path "+path);
216
}
217     }
218
219     synchronized public void deleteLocationByUUID(String JavaDoc workspaceContainerName, String JavaDoc UUID) {
220         log.debug("RDBRepositoryManagerImpl is deleting location by UUID " + UUID);
221         try {
222             Session hibernateSession = hibernateService.openSession();
223             Object JavaDoc[] values = { UUID, workspaceContainerName};
224             StringType[] types = { new StringType(), new StringType() };
225             List JavaDoc 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 (PathNotFoundException e) {
232
} catch (Exception JavaDoc e) {
233             throw new RuntimeException JavaDoc("Hibernate throws Exception " + e);
234         }
235     }
236
237   synchronized public void addWorkspaceContainer(String JavaDoc workspaceContainerName) {
238     try {
239         Session hibernateSession = hibernateService.openSession();
240         ContainerRecord containerRecord = new ContainerRecord();
241         containerRecord.setName(workspaceContainerName);
242         List JavaDoc 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 JavaDoc e) {
253       e.printStackTrace();
254       throw new RuntimeException JavaDoc("Hibernate throws Exception " + e);
255     }
256   }
257
258 }
259
Popular Tags