package votorola.a.register.trust; // Copyright 2008, 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.
import java.sql.*;
import java.util.*;
import javax.mail.internet.*;
import votorola.a.*;
import votorola.a.register.*;
import votorola.a.voter.*;
import votorola.g.option.*;
/** Responder for the command 'undoubt' - to withdraw doubt
* that was previously cast on another voter.
*
* @see http://zelea.com/project/votorola/a/mail/guide.xht#undoubt
*/
public final class CR_Undoubt extends CR_Doubt
{
/** Constructs a CR_Undoubt.
*/
public CR_Undoubt( Register register ) { super( register, "a.register.trust.CR_Undoubt." ); }
// - C o m m a n d - R e s p o n d e r ------------------------------------------------
public @Override Exception respond( final String[] argv,
final CommandResponder.Session session )
{
final String commandName = argv[0]; // before rearranged by option parser
if( session.userEmail() == null ) throw new CommandResponder.AnonymousIssueException( commandName );
final ReplyBuilder replyB = session.replyBuilder();
final Map optionMap = compileBaseOptions( session );
final String voter1Email;
{
final int aFirstNonOption = parse( argv, optionMap, session ); // rearranges argv
if( aFirstNonOption == -1 ) return null; // parse error, message already appended
if( optionMap.get("a.voter.CommandResponder.option.help").hasOccured() )
{
help( session );
return null;
}
final int nArg = argv.length - aFirstNonOption;
final int nArgExpected = 1;
if( nArg == nArgExpected )
{
try
{
voter1Email = canonicalEmail( argv[aFirstNonOption], commandName, session );
}
catch( AddressException x ){ return null; } // error message already output to reply
}
else
{
replyB.lappendln( "a.voter.CommandResponder.wrongNumberOfArguments(1,2,3)",
commandName, nArgExpected, nArg );
replyB.lappendlnn( "a.voter.CommandResponder.helpPrompt(1)", commandName );
return null;
}
}
try
{
replyB.setWrapping( false );
final List doubtList =
register().doubtTable().getSignalsFrom( session.userEmail() );
removeDoubtAndEcho( voter1Email, doubtList, session );
appendDoubtList( doubtList, session );
}
catch( SQLException x ) { return x; }
finally{ replyB.resetFormattingToDefaults(); }
return null;
}
//// P r i v a t e ///////////////////////////////////////////////////////////////////////
/** Removes a doubt signal from doubtList (if present), and commits the removal.
* Appends an echo of the change to the reply.
*
* @param session with wrapping turned off in its reply builder
*/
private void removeDoubtAndEcho( final String voter1Email,
final List doubtList, final CommandResponder.Session session )
throws SQLException
{
final ReplyBuilder replyB = session.replyBuilder();
assert !replyB.isWrapping();
replyB.lappendln( "a.register.trust.CR_Doubt.reply.countOld(1)", doubtList.size() );
for( int d = doubtList.size() - 1; d >= 0; --d )
{
final DoubtSignal doubt = doubtList.get( d );
if( !voter1Email.equals( doubt.voter1Email() )) continue;
register().doubtTable().remove( session, voter1Email );
doubtList.remove( d );
break;
}
replyB.lappendlnn( "a.register.trust.CR_Doubt.reply.countNew(1)", doubtList.size() );
}
}