KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > repository > runtime > RuntimeRepository


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jun 15, 2005
14  * @author Marc Batchelor
15  *
16  */

17 package org.pentaho.repository.runtime;
18
19 import java.util.Collection JavaDoc;
20 import java.util.List JavaDoc;
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     /**
39      *
40      */

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 JavaDoc threadSession = new ThreadLocal JavaDoc();
48
49     /**
50      * @return Returns the userSession.
51      */

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 JavaDoc 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     /**
78      * Loads an existing RuntimeElement
79      *
80      * @param instId
81      * The instance Id
82      * @return the RuntimeElement
83      * @throws RepositoryException
84      */

85     public IRuntimeElement loadElementById(String JavaDoc instId, Collection JavaDoc allowableReadAttributeNames) throws RepositoryException {
86         if (debug)
87             debug(Messages.getString("RTREPO.DEBUG_LOAD_ELEMENT_BY_ID", instId)); //$NON-NLS-1$
88
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); //$NON-NLS-1$
95
throw new RepositoryException(Messages.getErrorString("RTREPO.ERROR_0001_LOAD_ELEMENT", instId), ex); //$NON-NLS-1$
96
}
97     }
98
99     /**
100      *
101      * Creates a new RuntimeElement
102      *
103      * @param parId
104      * Parent ID of this instance
105      * @param parType
106      * Parent type of the instance
107      * @return the created runtime element
108      */

109     public IRuntimeElement newRuntimeElement(String JavaDoc parId, String JavaDoc parType, boolean transientOnly) {
110         if (debug)
111             debug(Messages.getString("RTREPO.DEBUG_NEW_ELEMENT_PARENT", parId, parType)); //$NON-NLS-1$
112
Session session = HibernateUtil.getSession();
113         String JavaDoc instanceId = UUIDUtil.getUUIDAsString();
114         if (debug)
115             debug(Messages.getString("RTREPO.DEBUG_CREATE_INSTANCE", instanceId)); //$NON-NLS-1$
116
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); //$NON-NLS-1$
122
throw new RepositoryException(Messages.getErrorString("RTREPO.ERROR_0002_SAVING_ELEMENT"), ex);//$NON-NLS-1$
123
}
124         }
125         return re;
126     }
127
128     /**
129      *
130      * Creates a new RuntimeElement
131      *
132      * @param parId
133      * Parent Id of the runtime element
134      * @param parType
135      * Parent type of the runtime element
136      * @param solnId
137      * Solution Id of the element
138      * @return The created runtime element
139      */

140     public IRuntimeElement newRuntimeElement(String JavaDoc parId, String JavaDoc parType, String JavaDoc solnId, boolean transientOnly) {
141         if (debug)
142             debug(Messages.getString("RTREPO.DEBUG_NEW_ELEMENT_PARENT_SOLN", parId, parType, solnId)); //$NON-NLS-1$
143
Session session = HibernateUtil.getSession();
144         String JavaDoc instanceId = UUIDUtil.getUUIDAsString();
145         if (debug)
146             debug(Messages.getString("RTREPO.DEBUG_CREATE_INSTANCE", instanceId)); //$NON-NLS-1$
147
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); //$NON-NLS-1$
153
throw new RepositoryException(Messages.getErrorString("RTREPO.ERROR_0003_SAVING_ELEMENT"), ex); //$NON-NLS-1$
154
}
155         }
156         return re;
157     }
158
159     /* ILogger Needs */
160     public Log getLogger() {
161         return log;
162     }
163
164     public boolean usesHibernate() {
165         return true;
166     }
167
168 }
169
Popular Tags