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: * The purpose of this class is to maintain a list of versions of each hibernated
014: * class (the object definition, not the contents of any one object) for the purposes
015: * of initiating an automatic schema update.
016: *
017: * Created Sep 28, 2005
018: * @author mbatchel
019: */
020: package org.pentaho.repository;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.hibernate.LockMode;
028: import org.hibernate.Session;
029: import org.pentaho.core.repository.RepositoryException;
030:
031: public class DefinitionVersionManager {
032: private Map versionMap = new HashMap();
033:
034: private int revision = -1; // Hibernate Revision
035:
036: private String key;
037:
038: private static final String VersionKey = "PENTAHO_HIBERNATE_VERSIONS"; //$NON-NLS-1$
039:
040: protected static String getVersionKey() {
041: return VersionKey;
042: }
043:
044: public static void performAutoUpdateIfRequired() {
045: Session session = HibernateUtil.getSession();
046: try {
047: DefinitionVersionManager mgr = null;
048: try {
049: mgr = (DefinitionVersionManager) session.get(
050: DefinitionVersionManager.class,
051: getVersionKey(), LockMode.READ); // Ignore Caches and just check the object
052: } catch (Exception handledForPG) {
053: // Gotta rollback or the connection gets messed up by PostgreSQL
054: try {
055: session.connection().rollback();
056: } catch (Exception ex) {
057: ex.printStackTrace();
058: }
059: HibernateUtil.closeSession();
060: }
061: if (mgr == null) {
062: // Never been stored before - Create one
063: mgr = new DefinitionVersionManager();
064: mgr.setKey(getVersionKey());
065: }
066: boolean needsUpdate = mgr.versionsUpdated();
067: if (needsUpdate) {
068: // Make sure that hibernate session is closed
069: HibernateUtil.closeSession();
070: // Update the schema using PentahoSchemaUpdate
071: HibernateUtil.updateSchema();
072: // Gets the session
073: session = HibernateUtil.getSession();
074: // Persist the DefinitionVersionManager class with the updated version numbers
075: session.saveOrUpdate(mgr);
076: // Write the changes to the DB
077: session.flush();
078: try {
079: // Commit the work
080: session.connection().commit();
081: } catch (Exception ex) {
082: try {
083: session.connection().rollback();
084: } catch (Exception e2) {
085: throw new RepositoryException(e2);
086: }
087: throw new RepositoryException(ex);
088: }
089: }
090: } finally {
091: HibernateUtil.closeSession();
092: }
093: }
094:
095: public boolean versionsUpdated() {
096: Map verMap = this .getVersionMap();
097: boolean needsUpdate = false;
098: List objectHandlers = HibernateUtil
099: .getHibernatedObjectHandlerList();
100: IHibernatedObjectExtensionList handler;
101: Map objectVersionMap;
102: Iterator it;
103: Map.Entry ent;
104: String clName;
105: Integer clVersion;
106: for (int i = 0; i < objectHandlers.size(); i++) {
107: handler = (IHibernatedObjectExtensionList) objectHandlers
108: .get(i);
109: objectVersionMap = handler.getHibernatedObjectVersionMap();
110: it = objectVersionMap.entrySet().iterator();
111: while (it.hasNext()) {
112: ent = (Map.Entry) it.next();
113: clName = (String) ent.getKey();
114: clVersion = (Integer) ent.getValue();
115: needsUpdate |= checkVersion(verMap, clVersion
116: .intValue(), clName);
117: }
118: }
119: /*
120: * needsUpdate = checkVersion(verMap, ContentItem.ClassVersionNumber,
121: * "ContentItem"); //$NON-NLS-1$ needsUpdate |= checkVersion(verMap,
122: * ContentItemFile.ClassVersionNumber, "ContentItemFile"); //$NON-NLS-1$
123: * needsUpdate |= checkVersion(verMap,
124: * ContentLocation.ClassVersionNumber, "ContentLocation"); //$NON-NLS-1$
125: * needsUpdate |= checkVersion(verMap,
126: * RuntimeElement.ClassVersionNumber, "RuntimeElement"); //$NON-NLS-1$
127: */
128: return needsUpdate;
129: }
130:
131: private boolean checkVersion(Map verMap, int curVerNum,
132: String className) {
133: Long currentVersionNumber = new Long(curVerNum);
134: Long storedVerNum = (Long) verMap.get(className);
135: if (storedVerNum == null) {
136: verMap.put(className, currentVersionNumber);
137: return true;
138: } else if (!storedVerNum.equals(currentVersionNumber)) {
139: verMap.put(className, currentVersionNumber);
140: return true;
141: }
142: return false;
143: }
144:
145: public int getRevision() {
146: return revision;
147: }
148:
149: public void setRevision(int revision) {
150: this .revision = revision;
151: }
152:
153: protected Map getVersionMap() {
154: return versionMap;
155: }
156:
157: protected void setVersionMap(Map versionMap) {
158: this .versionMap = versionMap;
159: }
160:
161: public String getKey() {
162: return key;
163: }
164:
165: public void setKey(String value) {
166: this.key = value;
167: }
168:
169: }
|