org.gswing.components.common.db
Class DbReadAccess

java.lang.Object
  extended by org.gswing.components.common.db.DbAccess
      extended by org.gswing.components.common.db.DbReadAccess
All Implemented Interfaces:
IConstituencyBrowsing, IDbReadAccess
Direct Known Subclasses:
DbWriteAccess

public class DbReadAccess
extends DbAccess
implements IDbReadAccess, IConstituencyBrowsing

Purpose: A read only connection to the election database.


Constructor Summary
DbReadAccess()
          Constructor that uses the database connection values from the configuration file.
DbReadAccess(String url, String dbName, String userName, String password)
          Constructor.
 
Method Summary
protected  String fixString(String str)
          Fix string that has illegal characters.
 Candidate getCandidate(int candidateNo)
          Get candidate object in the database matching the candidate no.
 int getCandidateNo(Candidate candidate)
          Get the unique candidate number of a candidate.
 Vector<Candidate> getCandidates()
          Get all candidates in the database.
 List<SimpleConstituency> getConstituencies(Election election)
          Fetch list of constituencies in an election.
 String getConstituency(int constituencyNo)
          Get the constituency name for this constituency number.
 Constituency getConstituency(String constituencyName, char type)
          Get the constituency for this constituency name and type.
 Constituency getConstituency(String constituencyName, String parent)
          Get the unique constituency number for this constituency.
 ListModel getConstituencyList(String parent, char type)
          Get the list of constituencies for a particular parent.
 Election getElection(Date date, String electionType)
          Get the election details for a particular election.
 Election getElection(int electionNo)
          Get the election details for a particular election.
 Vector<ResultKey> getElectionDates(Constituency constituency)
          Get election dates that have taken place in this constituency.
 Vector<Election> getElections()
          Get all elections in the database.
 Vector<Election> getElections(boolean includeByElections, char type)
          Get elections in the database.
 int getElectorate(int electionNo)
          Get the total size of the electorate for this election.
protected  int getElectorate(String constituency, int electionNo)
          Get the size of the electorate for this contituency at this election.
 Election getLatestDate(Constituency constituency)
          Get the election of the latest result for this constituency.
 Vector<Candidate> getLeaders(Election election)
          Get list of leaders (as candidates) for this election.
 Vector<Candidate> getLeaders(Election election, int constituencyNo)
          Get list of leaders (as candidates) for this election and constituency.
 int getNextCandidateNo()
          Get the next unique candidate number.
 int getNextConstituencyNo()
          Get the next unique constituency number for this constituency.
 Result getNextResult(Result oldResult)
          Gets the next result.
 int getNoOfCandidates()
          Get number of candidates in the database.
 int getNoOfParties()
          Get the number of parties in the database.
 Vector<Party> getParties()
          Get all party objects in the database.
 Party getParty(String abbreviation)
          Get the party from the abbreviation.
 String getPartyAbbreviation(String party)
          Get the abbreviated party name from the full name.
 Result getPreviousResult(Result oldResult, ResultKey key)
          Gets the previous result.
 String getPreviousWinner(Result oldResult)
          Gets the previous result by type (eg.
 String getRegion(String constituency)
          Get the region that a constituency belongs to.
 ListModel getRegionList()
          Get list of regions in database.
 Result getResult(ResultKey key)
          Get a result for this constituency on this date.
 String getState()
          Get the top level state name.
 int getVotesCast(Constituency constituency, Election election)
          Fetch the number of votes cast in a constituency for a particular election.
protected  boolean hasIllegalChar(String candidate)
          Does this candidate string have illegal characters.
 boolean partyExists(String party, boolean isAbbreviation)
          Does this party exist in the database?
 boolean resultExists(Election election, int candidateNo, String constituency)
          Check whether this result exists in the database.
 boolean resultExists(Election election, String participant, String constituency)
          Check whether this result exists in the database.
 
Methods inherited from class org.gswing.components.common.db.DbAccess
closeConnection, createStatement, getConnection, getDatabaseName, getNumberOfTables, getPassword, getTableList, hasValidConnection, isConnected, setDriverClassName, setPassword, tableExists
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.gswing.framework.services.IDbReadAccess
tableExists
 

Constructor Detail

DbReadAccess

public DbReadAccess(String url,
                    String dbName,
                    String userName,
                    String password)
Constructor.

Parameters:
url - The URL of this database connection.
dbName - The database name or catalogue.
userName - The database user name.
password - The database password.

DbReadAccess

public DbReadAccess()
Constructor that uses the database connection values from the configuration file. No password is used.

Method Detail

getState

public String getState()
Get the top level state name.

Specified by:
getState in interface IConstituencyBrowsing
Returns:
the State or nation name.

getRegionList

public ListModel getRegionList()
Get list of regions in database.

Specified by:
getRegionList in interface IConstituencyBrowsing
Returns:
The list of regions in the database.

getConstituencyList

public ListModel getConstituencyList(String parent,
                                     char type)
Get the list of constituencies for a particular parent.

Specified by:
getConstituencyList in interface IConstituencyBrowsing
Parameters:
parent - The parent constituency.
type - The constituency type.
Returns:
A list of constituencies.

getRegion

public String getRegion(String constituency)
Get the region that a constituency belongs to.

Parameters:
constituency - The constituency name.
Returns:
The region name.

getResult

public Result getResult(ResultKey key)
Get a result for this constituency on this date.

Specified by:
getResult in interface IDbReadAccess
Parameters:
key - The data that makes an election result unique.
Returns:
The result object.

getPreviousResult

public Result getPreviousResult(Result oldResult,
                                ResultKey key)
Gets the previous result. If this returns null then there is no previous.

Specified by:
getPreviousResult in interface IDbReadAccess
Parameters:
oldResult - The current result.
key - The data that makes an election result unique.
Returns:
The previous result.

getPreviousWinner

public String getPreviousWinner(Result oldResult)
Gets the previous result by type (eg. Central or Devolved elections)). If this returns null then there is no previous.

Parameters:
oldResult - The current result.
Returns:
The party of the previous winner.

getNextResult

public Result getNextResult(Result oldResult)
Gets the next result. If this returns null then there is no next.

Specified by:
getNextResult in interface IDbReadAccess
Parameters:
oldResult - The current result.
Returns:
The next result.

getLatestDate

public Election getLatestDate(Constituency constituency)
Get the election of the latest result for this constituency.

Specified by:
getLatestDate in interface IDbReadAccess
Parameters:
constituency - The constituency reference
Returns:
The latest election reference.

getCandidates

public Vector<Candidate> getCandidates()
Get all candidates in the database.

Specified by:
getCandidates in interface IDbReadAccess
Returns:
A Vector of Candidates sorted by surname.

getCandidate

public Candidate getCandidate(int candidateNo)
Get candidate object in the database matching the candidate no.

Parameters:
candidateNo - The unique candidate no in the database.
Returns:
The candidate object.

getCandidateNo

public int getCandidateNo(Candidate candidate)
Get the unique candidate number of a candidate.

Specified by:
getCandidateNo in interface IDbReadAccess
Parameters:
candidate - The object containing candidate details.
Returns:
The unique candidate number. If no candidate exists return -1.

getNextCandidateNo

public int getNextCandidateNo()
Get the next unique candidate number.

Returns:
The next unique candiate number or id.

getNoOfCandidates

public int getNoOfCandidates()
Get number of candidates in the database.

Specified by:
getNoOfCandidates in interface IDbReadAccess
Returns:
The number of candidates in the database.

getParties

public Vector<Party> getParties()
Get all party objects in the database.

Specified by:
getParties in interface IDbReadAccess
Returns:
A vector of abbreviated party names.

getPartyAbbreviation

public String getPartyAbbreviation(String party)
Get the abbreviated party name from the full name.

Specified by:
getPartyAbbreviation in interface IDbReadAccess
Parameters:
party - The party's full name.
Returns:
The abbreviated party name.

getParty

public Party getParty(String abbreviation)
Get the party from the abbreviation.

Specified by:
getParty in interface IDbReadAccess
Parameters:
abbreviation - The abbreviated party name.
Returns:
The party object.

getNoOfParties

public int getNoOfParties()
Get the number of parties in the database.

Specified by:
getNoOfParties in interface IDbReadAccess
Returns:
The number of parties.

partyExists

public boolean partyExists(String party,
                           boolean isAbbreviation)
Does this party exist in the database?

Specified by:
partyExists in interface IDbReadAccess
Parameters:
party - The party name.
isAbbreviation - Whether the party name is abbreviated or full.
Returns:
True if the party exists in the database.

getElectionDates

public Vector<ResultKey> getElectionDates(Constituency constituency)
Get election dates that have taken place in this constituency.

Specified by:
getElectionDates in interface IDbReadAccess
Parameters:
constituency - The constituency reference.
Returns:
A sorted vector of dates.

getElections

public Vector<Election> getElections()
Get all elections in the database.

Specified by:
getElections in interface IDbReadAccess
Returns:
Vector of elections sorted by date.

getElections

public Vector<Election> getElections(boolean includeByElections,
                                     char type)
Get elections in the database.

Specified by:
getElections in interface IDbReadAccess
Parameters:
includeByElections - Should by-elections be included in the list?
type - The election type - Result.NATIONAL, Result.REGIONAL, Result.COUNCIL, Result.CONSTITUENCY or Result.LOCAL
Returns:
Vector of elections sorted by date.

getElection

public Election getElection(int electionNo)
Get the election details for a particular election.

Specified by:
getElection in interface IDbReadAccess
Parameters:
electionNo - The unique election number.
Returns:
The election data.

getElection

public Election getElection(Date date,
                            String electionType)
Get the election details for a particular election.

Parameters:
date - The date of the election.
electionType - - Result.CONSTITUENCY, Result.REGIONAL or Result.LOCAL.
Returns:
The election object.

resultExists

public boolean resultExists(Election election,
                            int candidateNo,
                            String constituency)
Check whether this result exists in the database.

Specified by:
resultExists in interface IDbReadAccess
Parameters:
election - The election reference
candidateNo - The unique candidate number.
constituency - The constituency name.
Returns:
True if the result exists.

resultExists

public boolean resultExists(Election election,
                            String participant,
                            String constituency)
Check whether this result exists in the database.

Specified by:
resultExists in interface IDbReadAccess
Parameters:
election - The election reference.
participant - The unique candidate number.
constituency - The constituency name.
Returns:
True if the result exists.

getElectorate

protected int getElectorate(String constituency,
                            int electionNo)
Get the size of the electorate for this contituency at this election.

Parameters:
constituency - The constituency to search.
electionNo - The unique election number (from election table).
Returns:
The size of the electorate.

getElectorate

public int getElectorate(int electionNo)
Get the total size of the electorate for this election.

Specified by:
getElectorate in interface IDbReadAccess
Parameters:
electionNo - The unique election number.
Returns:
Total of eligible votes from all constituencies.

getVotesCast

public int getVotesCast(Constituency constituency,
                        Election election)
Fetch the number of votes cast in a constituency for a particular election.

Specified by:
getVotesCast in interface IDbReadAccess
Parameters:
constituency - The constituency object.
election - The election object.
Returns:
The number of votes case or -1 if there is no result.

getConstituency

public Constituency getConstituency(String constituencyName,
                                    String parent)
Get the unique constituency number for this constituency.

Specified by:
getConstituency in interface IDbReadAccess
Parameters:
constituencyName - The constituency name.
parent - The constituency's parent name.
Returns:
The unique constituency number or id. If this constituency doesn't exist return null.

getConstituency

public Constituency getConstituency(String constituencyName,
                                    char type)
Get the constituency for this constituency name and type.

Specified by:
getConstituency in interface IDbReadAccess
Parameters:
constituencyName - The constituency name.
type - The constituency type.
Returns:
The constituency reference. If this constituency doesn't exist return null.

getConstituency

public String getConstituency(int constituencyNo)
Get the constituency name for this constituency number.

Parameters:
constituencyNo - The constituency number on the database.
Returns:
The constituency reference. If this constituency doesn't exist return null.

getConstituencies

public List<SimpleConstituency> getConstituencies(Election election)
Fetch list of constituencies in an election.

Specified by:
getConstituencies in interface IDbReadAccess
Parameters:
election - The election.
Returns:
List of constituencies.

getNextConstituencyNo

public int getNextConstituencyNo()
Get the next unique constituency number for this constituency.

Returns:
The next unique constituency number or id.

getLeaders

public Vector<Candidate> getLeaders(Election election)
Get list of leaders (as candidates) for this election.

Specified by:
getLeaders in interface IDbReadAccess
Parameters:
election - The election object.
Returns:
Vector of party leaders.

getLeaders

public Vector<Candidate> getLeaders(Election election,
                                    int constituencyNo)
Get list of leaders (as candidates) for this election and constituency.

Specified by:
getLeaders in interface IDbReadAccess
Parameters:
election - The election object.
constituencyNo - The constituency identifier.
Returns:
Vector of party leaders.

hasIllegalChar

protected boolean hasIllegalChar(String candidate)
Does this candidate string have illegal characters. Current "'" is illegal. This is because Postgresql would accept insertions with this character.

Parameters:
candidate - The candidate name. return True if this candidate name has an illegal character.

fixString

protected String fixString(String str)
Fix string that has illegal characters. Insert a "\" before the illegal character.

Parameters:
str - The string containing the illegal char.
Returns:
The new fixed string.


Copyright © 2008. All Rights Reserved.