1 17 package org.pentaho.repository.runtime; 18 19 import java.util.Collection ; 20 import java.util.List ; 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.hibernate.HibernateException; 24 import org.hibernate.Session; 25 import org.pentaho.core.repository.IRuntimeRepository; 26 import org.pentaho.core.repository.IRuntimeElement; 27 import org.pentaho.repository.runtime.RuntimeElement; 28 import org.pentaho.core.session.IPentahoSession; 29 import org.pentaho.core.system.PentahoBase; 30 import org.pentaho.core.system.PentahoSystem; 31 import org.pentaho.messages.Messages; 32 import org.pentaho.repository.HibernateUtil; 33 import org.pentaho.core.repository.RepositoryException; 34 import org.pentaho.util.UUIDUtil; 35 36 public class RuntimeRepository extends PentahoBase implements IRuntimeRepository { 37 38 41 private static final long serialVersionUID = -6093228119094501691L; 42 43 private static final boolean debug = PentahoSystem.debug; 44 45 private static Log log = LogFactory.getLog(RuntimeRepository.class); 46 47 private static final ThreadLocal threadSession = new ThreadLocal (); 48 49 52 public static IPentahoSession getUserSession() { 53 IPentahoSession userSession = (IPentahoSession) threadSession.get(); 54 return userSession; 55 } 56 57 public RuntimeRepository() { 58 59 } 60 61 public List getMessages() { 62 return null; 63 } 64 65 public static IRuntimeRepository getInstance(IPentahoSession sess) { 66 IRuntimeRepository repo = new RuntimeRepository(); 67 repo.setSession(sess); 68 return repo; 69 } 70 71 public void setSession(IPentahoSession sess) { 72 threadSession.set(sess); 73 genLogIdFromSession(getUserSession()); 74 HibernateUtil.beginTransaction(); 75 } 76 77 85 public IRuntimeElement loadElementById(String instId, Collection allowableReadAttributeNames) throws RepositoryException { 86 if (debug) 87 debug(Messages.getString("RTREPO.DEBUG_LOAD_ELEMENT_BY_ID", instId)); Session session = HibernateUtil.getSession(); 89 try { 90 RuntimeElement runtimeElement = (RuntimeElement) session.load(RuntimeElement.class, instId); 91 runtimeElement.setAllowableAttributeNames(allowableReadAttributeNames); 92 return runtimeElement; 93 } catch (HibernateException ex) { 94 error(Messages.getErrorString("RTREPO.ERROR_0001_LOAD_ELEMENT", instId), ex); throw new RepositoryException(Messages.getErrorString("RTREPO.ERROR_0001_LOAD_ELEMENT", instId), ex); } 97 } 98 99 109 public IRuntimeElement newRuntimeElement(String parId, String parType, boolean transientOnly) { 110 if (debug) 111 debug(Messages.getString("RTREPO.DEBUG_NEW_ELEMENT_PARENT", parId, parType)); Session session = HibernateUtil.getSession(); 113 String instanceId = UUIDUtil.getUUIDAsString(); 114 if (debug) 115 debug(Messages.getString("RTREPO.DEBUG_CREATE_INSTANCE", instanceId)); RuntimeElement re = new RuntimeElement(instanceId, parId, parType); 117 if (!transientOnly) { 118 try { 119 session.save(re); 120 } catch (HibernateException ex) { 121 error(Messages.getErrorString("RTREPO.ERROR_0002_SAVING_ELEMENT"), ex); throw new RepositoryException(Messages.getErrorString("RTREPO.ERROR_0002_SAVING_ELEMENT"), ex); } 124 } 125 return re; 126 } 127 128 140 public IRuntimeElement newRuntimeElement(String parId, String parType, String solnId, boolean transientOnly) { 141 if (debug) 142 debug(Messages.getString("RTREPO.DEBUG_NEW_ELEMENT_PARENT_SOLN", parId, parType, solnId)); Session session = HibernateUtil.getSession(); 144 String instanceId = UUIDUtil.getUUIDAsString(); 145 if (debug) 146 debug(Messages.getString("RTREPO.DEBUG_CREATE_INSTANCE", instanceId)); RuntimeElement re = new RuntimeElement(instanceId, parId, parType, solnId); 148 if (!transientOnly) { 149 try { 150 session.save(re); 151 } catch (HibernateException ex) { 152 error(Messages.getErrorString("RTREPO.ERROR_0003_SAVING_ELEMENT"), ex); throw new RepositoryException(Messages.getErrorString("RTREPO.ERROR_0003_SAVING_ELEMENT"), ex); } 155 } 156 return re; 157 } 158 159 160 public Log getLogger() { 161 return log; 162 } 163 164 public boolean usesHibernate() { 165 return true; 166 } 167 168 } 169 | Popular Tags |