edu.umk.mat.merdacz.JGServer
Class DbWrapper

java.lang.Object
  extended byedu.umk.mat.merdacz.JGServer.DbWrapper

public class DbWrapper
extends java.lang.Object

Wrappes all db stuff. We are creating one object of this (one db connection) per player connection. This makes all quite easy (doesn't need to be synchronized because user requests are parsed sequently)

Author:
Marcin Daczkowski

Field Summary
private  java.lang.Class aClass
          eXist Driver for XML:DB API
private  org.xmldb.api.base.Collection col
          Root collection in database for this server (JGSdb currently hardcoded)
private  org.xmldb.api.base.Database db
          Our servers database instance (using driver from aClass)
private  java.lang.String dbAddress
          Address of db which we got form configuration file.
private  java.lang.String dbPass
          User password used for db connection (from configuration file)
private  java.lang.String dbUser
          User used for db connection (from configuration file)
private static java.lang.String driver
          eXist NXD driver is hardcoded - this should be changed in production version
private  org.xmldb.api.base.Collection game
          JGSdb/games collection.
private static java.lang.String gamesCollectionName
          Hardcoded.
private  org.xmldb.api.base.Collection rooms
          JGSdb/rooms collection
private static java.lang.String roomsCollectionName
          Hardcoded.
private  org.xmldb.api.base.Collection users
          JGSdb/games/{gameName}/users collection
private  org.xmldb.api.modules.XPathQueryService xpqService
          Service to pass XPath and XQuery questions to NXD (now eXist)
private  org.xmldb.api.modules.XUpdateQueryService xuqService
          Service to pass XUpdate queries to NXD (now eXist)
 
Constructor Summary
DbWrapper(java.lang.String dbAddress, java.lang.String dbUser, java.lang.String dbPass)
          Wrappes connection to NXD.
 
Method Summary
 void dbAddInvite(java.lang.String playerName, java.lang.String roomName)
          Uses XUpdate to append <invite> element under <invitations> node in proper roomName room.
 boolean dbAddRoom(java.lang.String roomName, java.lang.String roomOwner, java.lang.String roomType)
          Adds file {roomName}.xml to JGSdb/games/{gameName}/rooms with proper information about created room.
 boolean dbCheckLogin(java.lang.String name, java.lang.String password)
          Checks whether login name and password match
 java.lang.String dbGetRooms()
          Gets rooms from db as XML.
 boolean dbRegister(java.lang.String name, java.lang.String password)
          Registers new user in database.
 void dbRemoveRoom(java.lang.String roomName)
          We don't check ownership of room here.
 void setGame(java.lang.String gameName)
          Must be called before any other method use.
 void unsetGame()
          It probably doesn't really need to be called after logout, but calling it is quite good idea.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

driver

private static java.lang.String driver
eXist NXD driver is hardcoded - this should be changed in production version


aClass

private java.lang.Class aClass
eXist Driver for XML:DB API


db

private org.xmldb.api.base.Database db
Our servers database instance (using driver from aClass)


col

private org.xmldb.api.base.Collection col
Root collection in database for this server (JGSdb currently hardcoded)


gamesCollectionName

private static java.lang.String gamesCollectionName
Hardcoded. In future it should be placed in configuration


game

private org.xmldb.api.base.Collection game
JGSdb/games collection.


roomsCollectionName

private static java.lang.String roomsCollectionName
Hardcoded. In future it should be placed in configuration


rooms

private org.xmldb.api.base.Collection rooms
JGSdb/rooms collection


users

private org.xmldb.api.base.Collection users
JGSdb/games/{gameName}/users collection


dbAddress

private java.lang.String dbAddress
Address of db which we got form configuration file.


dbUser

private java.lang.String dbUser
User used for db connection (from configuration file)


dbPass

private java.lang.String dbPass
User password used for db connection (from configuration file)


xpqService

private org.xmldb.api.modules.XPathQueryService xpqService
Service to pass XPath and XQuery questions to NXD (now eXist)


xuqService

private org.xmldb.api.modules.XUpdateQueryService xuqService
Service to pass XUpdate queries to NXD (now eXist)

Constructor Detail

DbWrapper

public DbWrapper(java.lang.String dbAddress,
                 java.lang.String dbUser,
                 java.lang.String dbPass)
          throws DbWrapperException
Wrappes connection to NXD. It is instantinated before user choose exact game, hence we cannot set all stuff yet. When user chooses game (through setGame request) dbSetGame method *must* be called. We currently do not check it, hence using db* method before calling dbSetGame will cause internal error.

Parameters:
dbAddress -
dbUser -
dbPass -
Throws:
DbWrapperException
Method Detail

setGame

public void setGame(java.lang.String gameName)
             throws DbWrapperException
Must be called before any other method use.

Parameters:
gameName - indicates sub-collection of JGSdb/games which to use
Throws:
DbWrapperException
org.xmldb.api.base.XMLDBException

unsetGame

public void unsetGame()
It probably doesn't really need to be called after logout, but calling it is quite good idea.


dbCheckLogin

public boolean dbCheckLogin(java.lang.String name,
                            java.lang.String password)
                     throws DbWrapperException
Checks whether login name and password match

Parameters:
name -
password -
Returns:
true when login/password match false otherwise
Throws:
DbWrapperException

dbRegister

public boolean dbRegister(java.lang.String name,
                          java.lang.String password)
                   throws DbWrapperException
Registers new user in database. TODO (minor) this should take additional parameters like name, surname, avatar

Parameters:
name -
password -
Returns:
true when registration went ok false otherwise
Throws:
DbWrapperException

dbAddRoom

public boolean dbAddRoom(java.lang.String roomName,
                         java.lang.String roomOwner,
                         java.lang.String roomType)
                  throws DbWrapperException
Adds file {roomName}.xml to JGSdb/games/{gameName}/rooms with proper information about created room.

Doesn't we double check room existence?

Parameters:
roomName -
roomOwner -
roomType -
Returns:
false if room already exists true otherwise
Throws:
DbWrapperException

dbRemoveRoom

public void dbRemoveRoom(java.lang.String roomName)
                  throws DbWrapperException
We don't check ownership of room here. This must be done outside. We don't return any information about room existence (this can be handled only by catching DbWrapperException)

Parameters:
roomName -
Throws:
DbWrapperException

dbAddInvite

public void dbAddInvite(java.lang.String playerName,
                        java.lang.String roomName)
                 throws DbWrapperException
Uses XUpdate to append <invite> element under <invitations> node in proper roomName room.

Parameters:
playerName -
roomName -
Throws:
DbWrapperException

dbGetRooms

public java.lang.String dbGetRooms()
                            throws DbWrapperException
Gets rooms from db as XML. Used for restoring rooms from db after JGServer reboot.

Returns:
XML which contains rooms from db
Throws:
DbWrapperException