001package votorola.a.count; // Copyright 2007, 2013, 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.util.*;
004import votorola.a.*;
005import votorola.a.voter.*;
006import votorola.a.response.*;
007import votorola.g.option.*;
008
009import static votorola.a.voter.IDPair.NOBODY;
010
011
012/** Responder for the command 'unvote' - to withdraw a vote from a poll.
013  *
014  *     @see <a href='../../../../../s/manual.xht#Poll-Response-unvote' target='_top'
015  *                           >../../s/manual.xht#Poll-Response-unvote</a>
016  */
017public final class CR_Unvote extends CR_Vote
018{
019
020
021    /** Constructs a CR_Unvote.
022      */
023    CR_Unvote( PollService _poll ) { super( _poll, "a.count.CR_Unvote." ); }
024
025
026
027   // - C o m m a n d - R e s p o n d e r ------------------------------------------------
028
029
030    public @Override void help( final CommandResponder.Session session )
031    {
032        final ReplyBuilder replyB = session.replyBuilder();
033        replyB.lappendlnn( keyPrefix + "help.summary" );
034        replyB.indent( 4 ).setWrapping( false );
035        replyB.lappendlnn( keyPrefix + "help.syntax" );
036        replyB.exdent( 4 ).setWrapping( true );
037        replyB.lappendlnn( keyPrefix + "help.body(1)", voterService.title() );
038    }
039
040
041
042    public @Override Exception respond( final String[] argv, final CommandResponder.Session session )
043    {
044        final String commandName = argv[0]; // before rearranged by option parser
045        final AuthenticatedUser user = session.user();
046        if( user.email().equals(NOBODY.email()) )
047        {
048            throw new CommandResponder.AnonymousIssueException( commandName );
049        }
050
051        final ReplyBuilder replyB = session.replyBuilder();
052        final Map<String,Option> optionMap = compileBaseOptions( session );
053        {
054            final int aFirstNonOption = parse( argv, optionMap, session ); // rearranges argv
055            if( aFirstNonOption == -1 ) return null; // parse error, message already appended
056
057            if( optionMap.get("a.voter.CommandResponder.option.help").hasOccured() )
058            {
059                help( session );
060                return null;
061            }
062        }
063
064        try
065        {
066            replyB.setWrapping( false );
067            replyB.lappendlnn( "a.count.CR_Vote.reply.poll(1)", poll().title() );
068            final Vote vote = new Vote( user.email(), poll().voterInputTable() );
069            writeVoteAndEcho( /*newCandidateEmail*/null, vote, session );
070
071            echoTraces( vote, session );
072        }
073        catch( VoterInputTable.BadInputException x ) { replyB.appendlnn( x.toString() ); }
074          // though unexpected
075        catch( java.io.IOException|java.sql.SQLException|javax.script.ScriptException|javax.xml.stream.XMLStreamException x )
076        {
077            return x;
078        }
079        finally{ replyB.resetFormattingToDefaults(); }
080        return null;
081    }
082
083
084}