All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.eobjects.metamodel.DataContext Maven / Gradle / Ivy

/**
 *  This file is part of MetaModel.
 *
 *  MetaModel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MetaModel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MetaModel.  If not, see .
 */
package dk.eobjects.metamodel;

import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.ArrayUtils;

import dk.eobjects.metamodel.data.DataSet;
import dk.eobjects.metamodel.query.Query;
import dk.eobjects.metamodel.schema.Schema;

/**
 * A DataContext represents the central entry point for interactions with
 * datastores. The DataContext contains of the structure of data (in the form of
 * schemas) and interactions (in the form of queries) with data.
 */
public class DataContext {

	private IDataContextStrategy _strategy;
	private Map _schemas = new TreeMap(
			SchemaNameComparator.getInstance());
	private String[] _schemaNames;

	/**
	 * Creates a DataContext based on a specified IDataContextStrategy. This
	 * enables development of custom datastore formats, by developing your own
	 * strategy-objects.
	 * 
	 * @param strategy
	 */
	public DataContext(IDataContextStrategy strategy) {
		_strategy = strategy;
	}

	/**
	 * Enforces a refresh of the schemas.
	 */
	public DataContext refreshSchemas() {
		_schemas.clear();
		_schemaNames = null;
		return this;
	}

	/**
	 * @return the schemas in this data context. Schemas are cached for reuse in
	 *         many situations so if you want to update the schemas, use the
	 *         refreshSchemas() method.
	 * @throws MetaModelException
	 *             if an error occurs retrieving the schema model
	 */
	public Schema[] getSchemas() throws MetaModelException {
		String[] schemaNames = getSchemaNames();
		for (int i = 0; i < schemaNames.length; i++) {
			String name = schemaNames[i];
			if (!_schemas.containsKey(name)) {
				Schema schema = getSchemaByName(name);
				_schemas.put(name, schema);
			}
		}
		Schema[] result = _schemas.values().toArray(new Schema[0]);
		return result;
	}

	/**
	 * @return an array of valid schema names
	 * @throws MetaModelException
	 *             if an error occurs retrieving the schema model
	 */
	public String[] getSchemaNames() throws MetaModelException {
		if (_schemaNames == null) {
			_schemaNames = _strategy.getSchemaNames();
		}
		return _schemaNames;
	}

	/**
	 * @return the schema that you are most probable to be interested in. The
	 *         default schema is determined by finding the schema with most
	 *         available of tables. In a lot of situations there will only be a
	 *         single available schema and in that case this will of course be
	 *         the schema returned.
	 * @throws MetaModelException
	 *             if an error occurs retrieving the schema model
	 */
	public Schema getDefaultSchema() throws MetaModelException {
		Schema result = null;
		String defaultSchemaName = _strategy.getDefaultSchemaName();
		if (defaultSchemaName != null) {
			result = getSchemaByName(defaultSchemaName);
		}
		if (result == null) {
			Schema[] schemas = getSchemas();
			if (schemas.length == 1) {
				result = schemas[0];
			} else {
				int highestTableCount = -1;
				for (int i = 0; i < schemas.length; i++) {
					Schema schema = schemas[i];
					if (schema.getTableCount() > highestTableCount) {
						highestTableCount = schema.getTableCount();
						result = schema;
					}
				}
			}
		}
		return result;
	}

	/**
	 * Gets a schema by a specified name
	 * 
	 * @param name
	 *            the name of the desired schema
	 * @return the Schema with the specified name or null if no such schema
	 *         exists
	 * @throws MetaModelException
	 *             if an error occurs retrieving the schema model
	 */
	public Schema getSchemaByName(String name) throws MetaModelException {
		Schema schema = _schemas.get(name);
		if (schema != null) {
			return schema;
		} else {
			if (ArrayUtils.indexOf(getSchemaNames(), name) != -1) {
				schema = _strategy.getSchemaByName(name);
				_schemas.put(name, schema);
			} else {
				// schema name does not exist
				schema = null;
			}
		}
		return schema;
	}

	/**
	 * Executes a query against the data context.
	 * 
	 * @param query
	 *            the query object to execute
	 * @return the dataset produced from executing the query
	 * @throws MetaModelException
	 *             if the specified query does not make sense or cannot be
	 *             executed because of restraints on the type of datastore.
	 */
	public DataSet executeQuery(Query query) throws MetaModelException {
		DataSet dataSet = _strategy.executeQuery(query);
		return dataSet;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy