001package votorola.a.count; // Copyright 2009, 2012, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import java.sql.*;
004import votorola.a.*;
005import votorola.a.voter.*;
006import votorola.g.*;
007import votorola.g.lang.*;
008import votorola.g.logging.*;
009import votorola.g.sql.*;
010
011
012/** The voter input table for all polls.
013  */
014public final @ThreadSafe class InputTable extends VoterInputTable<PollService>
015{
016
017
018    /** Partially constructs an InputTable.
019      *
020      *     @see #voterService()
021      *     @see #init()
022      */
023    InputTable( PollService _voterService ) { super( _voterService, "in_vote" ); }
024
025
026
027    public @Override void init() throws SQLException
028    {
029        final String sKey = statementKeyBase + "init";
030        synchronized( database )
031        {
032            PreparedStatement s = database.statementCache().get( sKey );
033            if( s == null )
034            {
035                s = database.connection().prepareStatement(
036                 "CREATE TABLE IF NOT EXISTS \"" + tableName + "\""
037                  + " (serviceName character varying,"
038                  +  " voterEmail character varying,"
039                  +  " xml character varying NOT NULL,"
040                  +  " PRIMARY KEY ( serviceName, voterEmail ))" );
041                database.statementCache().put( sKey, s );
042            }
043            s.execute();
044        }
045    }
046
047
048
049   // - V o t e r - I n p u t - T a b l e ------------------------------------------------
050
051
052    public @Override void delete( final String voterEmail ) throws SQLException
053    {
054        final String sKey = statementKeyBase + "delete";
055        synchronized( database )
056        {
057            PreparedStatement s = database.statementCache().get( sKey );
058            if( s == null )
059            {
060                s = database.connection().prepareStatement(
061                  "DELETE FROM \"" + tableName + "\" WHERE serviceName = ? AND voterEmail = ?" );
062                database.statementCache().put( sKey, s );
063            }
064            s.setString( 1, voterService.name() );
065            s.setString( 2, voterEmail );
066            s.executeUpdate();
067        }
068    }
069
070
071
072    public @Override String get( final String voterEmail ) throws SQLException
073    {
074        final String sKey = statementKeyBase + "get";
075        synchronized( database )
076        {
077            PreparedStatement s = database.statementCache().get( sKey );
078            if( s == null )
079            {
080                s = database.connection().prepareStatement(
081                 "SELECT xml FROM \"" + tableName + "\""
082                  + " WHERE serviceName = ? AND voterEmail = ?" );
083                database.statementCache().put( sKey, s );
084            }
085            s.setString( 1, voterService.name() );
086            s.setString( 2, voterEmail );
087            final ResultSet r = s.executeQuery();
088            try
089            {
090                if( !r.next() ) return null;
091
092                return r.getString( 1 );
093            }
094            finally{ r.close(); }
095        }
096    }
097
098
099
100    public @Override void put( final String voterEmail, final String xml,
101      final ServiceSession userSession ) throws BadInputException, SQLException
102    {
103        if( userSession !=  null) testAccessAllowed( voterEmail, userSession );
104        lengthConstrained( voterEmail );
105        LoggerX.i(getClass()).finer( "voter input table " + tableName + ", storing for voter " + voterEmail + " : " + xml );
106        synchronized( database )
107        {
108            // effect an "upsert" in PostgreSQL
109            // http://stackoverflow.com/questions/1109061/insert-on-duplicate-update-postgresql/6527838#6527838
110            final Connection c = database.connection();
111            {
112                final String sKey = statementKeyBase + "putU";
113                PreparedStatement s = database.statementCache().get( sKey );
114                if( s == null )
115                {
116                    s = c.prepareStatement( "UPDATE \"" + tableName + "\""
117                      + " SET xml = ? WHERE serviceName = ? AND voterEmail = ?" );
118                    database.statementCache().put( sKey, s );
119                }
120                s.setString( 1, xml );
121                s.setString( 2, voterService.name() );
122                s.setString( 3, voterEmail );
123                final int updatedRows = s.executeUpdate();
124                if( updatedRows > 0 ) { assert updatedRows == 1; return; }
125            }
126            {
127                final String sKey = statementKeyBase + "putI";
128                PreparedStatement s = database.statementCache().get( sKey );
129                if( s == null )
130                {
131                    s = c.prepareStatement( "INSERT INTO \"" + tableName + "\""
132                      + " (serviceName, voterEmail, xml) SELECT ?, ?, ? WHERE NOT EXISTS"
133                      + " (SELECT 1 FROM \"" + tableName + "\""
134                      +   " WHERE serviceName = ? AND voterEmail = ?)" );
135                    database.statementCache().put( sKey, s );
136                }
137                s.setString( 1, voterService.name() );
138                s.setString( 2, voterEmail );
139                s.setString( 3, xml );
140                s.setString( 4, voterService.name() );
141                s.setString( 5, voterEmail );
142                s.executeUpdate();
143            }
144        }
145    }
146
147
148
149}