|
From: <rga...@us...> - 2002-11-10 15:00:19
|
Update of /cvsroot/csms/csms-core/src/java/org/fanfoot/db In directory usw-pr-cvs1:/tmp/cvs-serv1158 Added Files: Players.java DBException.java Log Message: Initial creation of access classes for the database --- NEW FILE: Players.java --- /* * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Community Sports Management System. * * The Initial Developer of the Original Code is * We know What you Want.net * * Portions created by the Initial Developer are Copyright (C) 2002 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Ross Gardler <ross [at] saafe.org> * Mike Lever <mikel [at] fanfoot.com> * Stuart Gardler <stuart [at] wkwyw.net> * * ***** END LICENSE BLOCK ***** */ /* * Event.java * * Created on 20 September 2002, 21:59 */ package org.fanfoot.db; import org.saafe.utils.XMLUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * Provides access to the player information in the database. * *@author rgardler *@created 7 November 2002 */ public class Players { // FIXME: the location of the players database should be set in the properties String strDBFilePath = "file://localhost/home/rgardler/projects/csms/csms-core/src/resources/conf/players.xml"; Document thePlayers; /** * Create a players object to connect to the default playres * database * *@exception DBException Description of the Exception */ public Players() throws DBException { try { thePlayers = XMLUtils.parseXML( strDBFilePath ); } catch ( javax.xml.parsers.ParserConfigurationException pce ) { String strMessage = "Unable to Create Parser to parse in players DB"; throw new DBException( strMessage, pce ); } catch ( org.w3c.dom.DOMException de ) { String strMessage = "DOMException when parsing the players DB"; throw new DBException( strMessage, de ); } catch ( org.xml.sax.SAXException se ) { String strMessage = "SAXException when parsing the players DB"; throw new DBException( strMessage, se ); } catch ( java.io.IOException ioe ) { String strMessage = "Unable to read the players DB file"; throw new DBException( strMessage, ioe ); } } /** * Get the player associated with the supplied id. * *@param id the ID of the plaer required *@return an element representing the players record *@throws DBException if the player cannot be found */ public Element getPlayer( String id ) throws DBException { Element root = thePlayers.getDocumentElement(); NodeList ndl = root.getElementsByTagName( "player" ); Element elem = null; for ( int i = 0; i < ndl.getLength(); i++ ) { elem = (Element) ndl.item( i ); if ( elem.getAttribute( "id" ).equals( id ) ) { break; } else { elem = null; } } if ( elem == null ) { throw new DBException( DBException.UNRECOGNISED_ID_MESSAGE + id ); } return elem; } /** * Get the scoring events associated with the given game and the * given player. * *@param playerID the ID of the player *@param gameID the ID of the game *@return the element representing the scoring events from the * indicated game. Returns null if no events for that game are * recorded *@throws DBException if the player cannot be found */ public Element getPlayerScoringEvents( String playerID, String gameID ) throws DBException { Element player = getPlayer( playerID ); NodeList ndl = player.getElementsByTagName( "scoringEvents" ); Element scoringEvents = (Element) ndl.item( 0 ); ndl = scoringEvents.getElementsByTagName( "game" ); Element elem = null; for ( int i = 0; i < ndl.getLength(); i++ ) { elem = (Element) ndl.item( i ); if ( elem.getAttribute( "id" ).equals( gameID ) ) { break; } else { elem = null; } } return elem; } } --- NEW FILE: DBException.java --- /* * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Community Sports Management System. * * The Initial Developer of the Original Code is * We know What you Want.net * * Portions created by the Initial Developer are Copyright (C) 2002 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Ross Gardler <ross [at] saafe.org> * Mike Lever <mikel [at] fanfoot.com> * Stuart Gardler <stuart [at] wkwyw.net> * * ***** END LICENSE BLOCK ***** */ /* * DBException.java * */ package org.fanfoot.db; /** *@author rgardler *@created 09 November 2002 */ public class DBException extends java.lang.Exception { /** The root exception leading to this one */ Exception root = null; /** * Message used when an attempt is made to retrieve a player with a * non-existent ID */ public static String UNRECOGNISED_ID_MESSAGE = "No player with the ID exists: ID = "; /** * Creates a new instance of <code>DBException</code> without * detail message. */ public DBException() { } /** * Constructs an instance of <code>DBException</code> with the * specified detail message. * *@param msg the detail message. */ public DBException( String msg ) { super( msg ); } /** * Constructs an instance of <code>DBExcepotion</code> with the * specified detail message and the root exception supplied * *@param msg the detail message. *@param e the root exception */ public DBException( String msg, Exception e ) { super( msg ); root = e; } /** Print the stack trace of the nested exception */ public void printStackTrace() { System.out.println( this.getMessage() ); System.out.println( "Nested Exception:" ); root.printStackTrace(); } } |