|
From: <rga...@us...> - 2002-11-21 21:59:41
|
Update of /cvsroot/csms/csms-core/src/java/org/fanfoot/db In directory sc8-pr-cvs1:/tmp/cvs-serv16165 Modified Files: Players.java Added Files: DB.java ID.java Log Message: Now connects to a database via XML:DB --- NEW FILE: DB.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 java.util.logging.Level; import java.util.logging.Logger; import org.xmldb.api.DatabaseManager; import org.xmldb.api.base.Collection; import org.xmldb.api.base.Database; import org.xmldb.api.base.ResourceSet; import org.xmldb.api.base.XMLDBException; import org.xmldb.api.modules.XMLResource; import org.xmldb.api.modules.XPathQueryService; /** * Provides utility methods for accessing the DB * *@author rgardler *@created 19 November 2002 */ public class DB { /** The name of the class to use as the database driver */ private String driver = "org.apache.xindice.client.xmldb.DatabaseImpl"; /** The URI of the database to use */ private String dbURI; /** Where to write log info */ final static Logger logger = Logger.getLogger( "org.fanfoot.db" ); /** * Create a DB objet to connect to the indicated DB * *@param dbURI the URI of the DB to connect to *@exception DBException Description of the Exception */ public DB( String dbURI ) throws DBException { this.dbURI = dbURI; initDatabaseDriver(); } /** * Query the indicated collection with the XPath query supplied. * *@param strQuery XPath query to run *@param strCollection Description of the Parameter *@return *@throws DBException if unable to query the database */ public ResourceSet runXPathQuery( String strCollection, String strQuery ) throws DBException { Collection col = getCollection( strCollection ); XPathQueryService service; ResourceSet resultSet; try { service = (XPathQueryService) col.getService( "XPathQueryService", "1.0" ); } catch ( XMLDBException xde ) { throw new DBException( "Unable to get XPathQueryService", xde ); } try { resultSet = service.query( strQuery ); } catch ( XMLDBException xde ) { throw new DBException( "Unable to execute XPath Query", xde ); } finally { if ( col != null ) { try { col.close(); } catch ( XMLDBException xde ) { // FIXME handle this error, it is not critical but will be a memeory leak } } } return resultSet; } /** * Get the collection named from the DB in use * *@param strCollectionName Description of the Parameter *@return The collection value *@throws DBException if unable the get the collection */ public Collection getCollection( String strCollectionName ) throws DBException { Collection col; try { col = DatabaseManager.getCollection( dbURI + strCollectionName ); } catch ( XMLDBException xde ) { throw new DBException( "Unable to get the collection " + strCollectionName, xde ); } return col; } /** * Add the supplied string as a resource to the collection * indicated. * *@param strID the id of the resource (if null an ID will be generated) *@param strResource the resource to add *@param strCollection the collection to add it to *@throws DBException if any problem occurs connecting to the DB */ public void addResource( String strID, String strResource, String strCollection ) throws DBException { Collection col = getCollection( strCollection ); try { XMLResource document = (XMLResource) col.createResource( strID, "XMLResource" ); document.setContent( strResource ); col.storeResource( document ); } catch ( XMLDBException xdbe ) { logger.severe( "Cannot add resource " + strResource + " to " + strCollection ); throw new DBException( "Cannot add resource to " + strCollection, xdbe ); } } /** * Initialise the database manager * *@exception DBException Description of the Exception */ private void initDatabaseDriver() throws DBException { try { Class c = Class.forName( driver ); Database database = (Database) c.newInstance(); DatabaseManager.registerDatabase( database ); } catch ( ClassNotFoundException cnfe ) { throw new DBException( "Unable to load driver for database", cnfe ); } catch ( IllegalAccessException ia ) { throw new DBException( "Unable to create driver object for the database", ia ); } catch ( InstantiationException ie ) { throw new DBException( "Unable to create driver object for the database", ie ); } catch ( XMLDBException xde ) { throw new DBException( "Unable to register the database", xde ); } } } --- NEW FILE: ID.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 java.util.logging.Level; import java.util.logging.Logger; /** * Provides utility methods for generating ID's for database entries. * *@author rgardler *@created 21 November 2002 */ public class ID { /** ID number of next player to be created */ // FIXME: this number needs to be saved between invocations of the application private static int intPlayerID = 100; /** * Create an ID number for a player in the indicated postion * *@return an ID which consists of the position code followed by a * unique number for that position *@throws DBException if the position is not recognised */ public static String getPlayerID() throws DBException { String strID = new Integer( intPlayerID ).toString(); intPlayerID = intPlayerID + 1; return strID; } } Index: Players.java =================================================================== RCS file: /cvsroot/csms/csms-core/src/java/org/fanfoot/db/Players.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Players.java 10 Nov 2002 15:00:16 -0000 1.1 --- Players.java 21 Nov 2002 21:59:37 -0000 1.2 *************** *** 35,42 **** --- 35,47 ---- */ package org.fanfoot.db; + import java.util.logging.Level; + import java.util.logging.Logger; import org.saafe.utils.XMLUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; + import org.xmldb.api.base.ResourceSet; + import org.xmldb.api.base.XMLDBException; + import org.xmldb.api.modules.XMLResource; /** * Provides access to the player information in the database. *************** *** 46,77 **** */ 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 ); ! } } --- 51,100 ---- */ public class Players { ! /** The URI of the database to use */ ! String strDBURI; ! /** The DB access object */ ! DB db; ! /** Where to write log info */ ! final Logger logger = Logger.getLogger( "org.fanfoot.db" ); ! /** ! * The name of the collection in the database that stores the ! * players data ! */ ! public static String COLLECTION_NAME = "player"; ! /** The goalkeeper playing position */ ! public static String POSITION_GOALKEEPER = "Goalkeeper"; ! /** The goalkeeper playing position */ ! public static String POSITION_CENTREBACK = "Centre Back"; ! /** The goalkeeper playing position */ ! public static String POSITION_FULLBACK = "Full Back"; ! /** The goalkeeper playing position */ ! public static String POSITION_MIDFIELD = "Midfield"; ! /** The goalkeeper playing position */ ! public static String POSITION_FORWARD = "Forward"; /** ! * Create a players object to connect to the default players ! * database and default collection * *@exception DBException Description of the Exception */ public Players() throws DBException { ! // FIXME: the location of the players database should be set in the properties ! new Players( "xmldb:xindice-embed:///db" ); ! } ! ! ! /** ! * Create a players object to connect to the indicated players ! * database with the default colelction ! * ! *@param uri the uri of the DB to use ! *@exception DBException Description of the Exception ! */ ! public Players( String uri ) throws DBException { ! super(); ! strDBURI = uri; ! db = new DB( strDBURI ); } *************** *** 81,104 **** * *@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; } --- 104,134 ---- * *@param id the ID of the plaer required ! *@return an Element representing the players record, null if there ! * is none ! *@throws DBException if there is more than one player with the ! * supplied ID or is the resource cannot be parsed for any ! * reason. */ public Element getPlayer( String id ) throws DBException { ! Document result = null; ! String strXPath = "//player[@id='" + id + "']"; ! XMLResource res = null; ! ResourceSet rs = db.runXPathQuery( COLLECTION_NAME, strXPath ); ! try { ! if ( rs.getSize() > 1 ) { ! logger.info( "There are " + rs.getSize() + " players with ID=" + id ); ! throw new DBException( "There appears to be more than one player with the ID " + id ); ! } else if ( rs.getSize() == 1 ) { ! res = (XMLResource) rs.getResource( 0 ); ! logger.info( "Got resource: " + (String) res.getContent() ); ! result = (Document) res.getContentAsDOM(); } else { ! return null; } + } catch ( XMLDBException xde ) { + throw new DBException( "Error manipulating the resource set", xde ); } ! return result.getDocumentElement(); } *************** *** 110,114 **** *@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 --- 140,144 ---- *@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 *************** *** 132,135 **** --- 162,206 ---- return elem; } + + + /** + * Add the supplied player to the database. No checks for + * duplicates are done. + * + *@param firstName is the first name of the player + *@param surname is the surname of the player + *@param registeredTeam is the team the player with + *@param team is the team the player is currently playing for + *@param position is the players normal position + *@exception DBException Description of the Exception + */ + public void addPlayer( String firstName, + String surname, String registeredTeam, + String team, String position ) + throws DBException { + String strID = ID.getPlayerID(); + logger.info( "Adding player with ID = " + strID ); + StringBuffer sb = new StringBuffer(); + sb.append( "<player id='" ); + sb.append( strID ); + sb.append( "'><firstName>" ); + sb.append( firstName ); + sb.append( "</firstName>" ); + sb.append( "<surname>" ); + sb.append( surname ); + sb.append( "</surname>" ); + sb.append( "<registeredTeam>" ); + sb.append( registeredTeam ); + sb.append( "</registeredTeam>" ); + sb.append( "<team>" ); + sb.append( team ); + sb.append( "</team>" ); + sb.append( "<position>" ); + sb.append( position ); + sb.append( "</position>" ); + sb.append( "</player>" ); + db.addResource( strID, sb.toString(), COLLECTION_NAME ); + } + } |