Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements database administrative methods 
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 * 
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-08-24 14:34:17 -0700 (Mon, 24 Aug 2009) $'
10
 * '$Revision: 5030 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat.util;
28

    
29
import java.util.Map;
30
import java.util.TreeSet;
31
import java.util.Vector;
32

    
33
import org.apache.log4j.Logger;
34

    
35
import edu.ucsb.nceas.metacat.database.DBVersion;
36
import edu.ucsb.nceas.metacat.properties.PropertyService;
37
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
38
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
39

    
40
public class DatabaseUtil {
41
	
42
	private static Logger logMetacat = Logger.getLogger(DatabaseUtil.class);
43

    
44
	/**
45
	 * private constructor - all methods are static so there is no no need to
46
	 * instantiate.
47
	 */
48
	private DatabaseUtil() {}
49
	
50
	/**
51
	 * Reports whether database is fully configured.
52
	 * 
53
	 * @return a boolean that is true if database is not unconfigured and false
54
	 *         otherwise
55
	 */
56
	public static boolean isDatabaseConfigured() throws MetacatUtilException {
57
		String databaseConfiguredString = PropertyService.UNCONFIGURED;
58
		try {
59
			databaseConfiguredString = 
60
				PropertyService.getProperty("configutil.databaseConfigured");
61
		} catch (PropertyNotFoundException pnfe) {
62
			throw new MetacatUtilException("Could not determine if database is configured: "
63
					+ pnfe.getMessage());
64
		}
65
		return !databaseConfiguredString.equals(PropertyService.UNCONFIGURED);
66
	}	
67
	
68
	/**
69
	 * Gets the available upgrade versions for metacat by getting a list of 
70
	 * upgrade script from properties in metacat.properties that start with 
71
	 * "database.upgradeVersion". It then creats a DBVersion object for each 
72
	 * upgrade version.
73
	 * 
74
	 * @return a TreeSet of DBVersion objects holding individual version information
75
	 */
76
	public static TreeSet<DBVersion> getUpgradeVersions() throws PropertyNotFoundException {
77
		
78
		TreeSet<DBVersion> versionSet = new TreeSet<DBVersion>();
79
		
80
		Vector<String> upgradeVersionKeys = PropertyService.getPropertyNamesByGroup("database.upgradeVersion");
81
		for (String upgradeVersionKey : upgradeVersionKeys) {
82
			String upgradeVersionId = upgradeVersionKey.substring(24);
83
			logMetacat.debug("Creating DBVersion object for version: " + upgradeVersionId);
84
			DBVersion dbVersion = new DBVersion(upgradeVersionId);
85
			versionSet.add(dbVersion);
86
		}
87

    
88
		return versionSet;
89
	}
90
	
91
	/**
92
	 * Gets the available upgrade versions for metacat by getting a list of 
93
	 * upgrade script from properties in metacat.properties that start with 
94
	 * "database.upgradeVersion". It then creats a DBVersion object for each 
95
	 * upgrade version.
96
	 * 
97
	 * @return a TreeSet of DBVersion objects holding individual version information
98
	 */
99
	public static Map<String, String> getScriptSuffixes() throws PropertyNotFoundException {
100
		
101
		return PropertyService.getPropertiesByGroup("database.scriptsuffix");
102
	}
103
}
(3-3/15)