001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jun 15, 2005
014: * @author Marc Batchelor
015: *
016: */
017: package org.pentaho.repository.runtime;
018:
019: import java.util.Collection;
020: import java.util.List;
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.hibernate.HibernateException;
024: import org.hibernate.Session;
025: import org.pentaho.core.repository.IRuntimeRepository;
026: import org.pentaho.core.repository.IRuntimeElement;
027: import org.pentaho.repository.runtime.RuntimeElement;
028: import org.pentaho.core.session.IPentahoSession;
029: import org.pentaho.core.system.IPentahoInitializer;
030: import org.pentaho.core.system.PentahoBase;
031: import org.pentaho.core.system.PentahoSystem;
032: import org.pentaho.messages.Messages;
033: import org.pentaho.repository.HibernateUtil;
034: import org.pentaho.core.repository.RepositoryException;
035: import org.pentaho.util.UUIDUtil;
036:
037: public class RuntimeRepository extends PentahoBase implements
038: IRuntimeRepository, IPentahoInitializer {
039:
040: /**
041: *
042: */
043: private static final long serialVersionUID = -6093228119094501691L;
044:
045: private static final boolean debug = PentahoSystem.debug;
046:
047: private static Log log = LogFactory.getLog(RuntimeRepository.class);
048:
049: private static final ThreadLocal threadSession = new ThreadLocal();
050:
051: /**
052: * @return Returns the userSession.
053: */
054: public static IPentahoSession getUserSession() {
055: IPentahoSession userSession = (IPentahoSession) threadSession
056: .get();
057: return userSession;
058: }
059:
060: public RuntimeRepository() {
061:
062: }
063:
064: public List getMessages() {
065: return null;
066: }
067:
068: public static IRuntimeRepository getInstance(IPentahoSession sess) {
069: IRuntimeRepository repo = new RuntimeRepository();
070: repo.setSession(sess);
071: return repo;
072: }
073:
074: public void setSession(IPentahoSession sess) {
075: threadSession.set(sess);
076: genLogIdFromSession(getUserSession());
077: HibernateUtil.beginTransaction();
078: }
079:
080: public void init(IPentahoSession sess) {
081: this .setSession(sess);
082: }
083:
084: /**
085: * Loads an existing RuntimeElement
086: *
087: * @param instId
088: * The instance Id
089: * @return the RuntimeElement
090: * @throws RepositoryException
091: */
092: public IRuntimeElement loadElementById(String instId,
093: Collection allowableReadAttributeNames)
094: throws RepositoryException {
095: if (debug)
096: debug(Messages.getString(
097: "RTREPO.DEBUG_LOAD_ELEMENT_BY_ID", instId)); //$NON-NLS-1$
098: Session session = HibernateUtil.getSession();
099: try {
100: RuntimeElement runtimeElement = (RuntimeElement) session
101: .load(RuntimeElement.class, instId);
102: runtimeElement
103: .setAllowableAttributeNames(allowableReadAttributeNames);
104: return runtimeElement;
105: } catch (HibernateException ex) {
106: error(Messages.getErrorString(
107: "RTREPO.ERROR_0001_LOAD_ELEMENT", instId), ex); //$NON-NLS-1$
108: throw new RepositoryException(Messages.getErrorString(
109: "RTREPO.ERROR_0001_LOAD_ELEMENT", instId), ex); //$NON-NLS-1$
110: }
111: }
112:
113: /**
114: *
115: * Creates a new RuntimeElement
116: *
117: * @param parId
118: * Parent ID of this instance
119: * @param parType
120: * Parent type of the instance
121: * @return the created runtime element
122: */
123: public IRuntimeElement newRuntimeElement(String parId,
124: String parType, boolean transientOnly) {
125: if (debug)
126: debug(Messages.getString(
127: "RTREPO.DEBUG_NEW_ELEMENT_PARENT", parId, parType)); //$NON-NLS-1$
128: Session session = HibernateUtil.getSession();
129: String instanceId = UUIDUtil.getUUIDAsString();
130: if (debug)
131: debug(Messages.getString(
132: "RTREPO.DEBUG_CREATE_INSTANCE", instanceId)); //$NON-NLS-1$
133: RuntimeElement re = new RuntimeElement(instanceId, parId,
134: parType);
135: if (!transientOnly) {
136: try {
137: session.save(re);
138: } catch (HibernateException ex) {
139: error(
140: Messages
141: .getErrorString("RTREPO.ERROR_0002_SAVING_ELEMENT"), ex); //$NON-NLS-1$
142: throw new RepositoryException(
143: Messages
144: .getErrorString("RTREPO.ERROR_0002_SAVING_ELEMENT"), ex);//$NON-NLS-1$
145: }
146: }
147: return re;
148: }
149:
150: /**
151: *
152: * Creates a new RuntimeElement
153: *
154: * @param parId
155: * Parent Id of the runtime element
156: * @param parType
157: * Parent type of the runtime element
158: * @param solnId
159: * Solution Id of the element
160: * @return The created runtime element
161: */
162: public IRuntimeElement newRuntimeElement(String parId,
163: String parType, String solnId, boolean transientOnly) {
164: if (debug)
165: debug(Messages
166: .getString(
167: "RTREPO.DEBUG_NEW_ELEMENT_PARENT_SOLN", parId, parType, solnId)); //$NON-NLS-1$
168: Session session = HibernateUtil.getSession();
169: String instanceId = UUIDUtil.getUUIDAsString();
170: if (debug)
171: debug(Messages.getString(
172: "RTREPO.DEBUG_CREATE_INSTANCE", instanceId)); //$NON-NLS-1$
173: RuntimeElement re = new RuntimeElement(instanceId, parId,
174: parType, solnId);
175: if (!transientOnly) {
176: try {
177: session.save(re);
178: } catch (HibernateException ex) {
179: error(
180: Messages
181: .getErrorString("RTREPO.ERROR_0003_SAVING_ELEMENT"), ex); //$NON-NLS-1$
182: throw new RepositoryException(
183: Messages
184: .getErrorString("RTREPO.ERROR_0003_SAVING_ELEMENT"), ex); //$NON-NLS-1$
185: }
186: }
187: return re;
188: }
189:
190: /* ILogger Needs */
191: public Log getLogger() {
192: return log;
193: }
194:
195: public boolean usesHibernate() {
196: return true;
197: }
198:
199: }
|