package votorola.a.register; // 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.voter.*; import votorola.g.option.*; /** Responder for the command 'set' - to set the value of a register field. * * @see http://zelea.com/project/votorola/a/mail/guide.xht#set */ public class CR_Set extends CommandResponder.Base { /** Constructs a CR_Set. */ CR_Set( Register register ) { super( register, "a.register.CR_Set." ); } /** Constructs a CR_Set as a subclass. */ CR_Set( Register register, String keyPrefix ) { super( register, keyPrefix ); } // - C o m m a n d - R e s p o n d e r ------------------------------------------------ public @Override void help( final CommandResponder.Session session ) { final ReplyBuilder replyB = session.replyBuilder(); replyB.lappendlnn( keyPrefix + "help.summary" ); replyB.indent( 4 ).setWrapping( false ); replyB.lappendlnn( keyPrefix + "help.syntax" ); replyB.exdent( 4 ).setWrapping( true ); replyB.lappendlnn( keyPrefix + "help.body.1" ); replyB.indent( 4 ); final String commandName = replyB.l( keyPrefix + "commandName" ); help_field( commandName, replyB.l( keyPrefix+"field.name" ), register().fieldDescription_name(), register().fieldExample_name(), replyB ); help_field( commandName, replyB.l( keyPrefix+"field.residence" ), register().fieldDescription_residence(), register().fieldExample_residence(), replyB ); help_field( commandName, replyB.l( keyPrefix+"field.link" ), register().fieldDescription_link(), register().fieldExample_link(), replyB ); help_field( commandName, replyB.l( keyPrefix+"field.note" ), register().fieldDescription_note(), register().fieldExample_note(), replyB ); replyB.exdent( 4 ); replyB.lappendlnn( keyPrefix + "help.body.9" ); } private static void help_field( final String commandName, final String fieldName, final String description, final String example, final ReplyBuilder replyB ) { replyB.appendln( fieldName ); if( fieldName.length() > 6 ) replyB.appendln(); replyB.indent( 8 ); replyB.appendlnn( commandName + " " + fieldName + " " + example ); replyB.exdent( 4 ).appendlnn( description ).exdent( 4 ); } 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 = compileOptions( session ); final String fieldName; final String fieldValue; { 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 nArgExpectedMin = 2; if( nArg >= nArgExpectedMin ) { fieldName = argv[aFirstNonOption]; StringBuilder stringB = new StringBuilder(); for( int a = aFirstNonOption + 1;; ) // concatenate remaining arguments { stringB.append( argv[a] ); ++a; if( a >= argv.length ) break; stringB.append( ' ' ); } fieldValue = stringB.toString(); } else { replyB.lappendln( "a.voter.CommandResponder.wrongNumberOfArguments(1,2,3)", commandName, Integer.toString(nArgExpectedMin) + " or more", nArg ); replyB.lappendlnn( "a.voter.CommandResponder.helpPrompt(1)", commandName ); return null; } } final String alterEmail; try{ alterEmail = canonicalEmail( optionMap.get( "a.voter.CommandResponder.option.alter" ).argumentValue(), commandName, session ); } catch( AddressException x ){ return null; } // error message already output to reply replyB.setWrapping( false ); try { commitFieldAndEcho( fieldName, fieldValue, alterEmail, commandName, session ); } catch( SQLException x ) { return x; } catch( VoterInputTable.BadInputException x ) { replyB.setWrapping( true ); replyB.appendlnn( x.toString() ); } finally{ replyB.resetFormattingToDefaults(); } return null; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** Changes and commits the field's value (if permitted), * and appends an echo of the change (or a warning message) to the reply. * * @param session with wrapping turned off in its reply builder */ void commitFieldAndEcho( final String fieldName, final String newValue, final String alterEmail, final String commandName, final CommandResponder.Session session ) throws SQLException, VoterInputTable.BadInputException { final RegistrationC registration = new RegistrationC( alterEmail == null? session.userEmail(): alterEmail, register().voterInputTable() ); final ReplyBuilder replyB = session.replyBuilder(); assert !replyB.isWrapping(); if( !registration.isWriteable( session, register() )) { replyB.lappendln( "a.register.CR_Set.writePermissionDenied(1)", commandName ); return; } final String oldValue; if( "link".equals( fieldName )) { oldValue = registration.getLink(); registration.setLink( newValue, session, register() ); } else if( "name".equals( fieldName )) { oldValue = registration.getName(); registration.setName( newValue, session, register() ); } else if( "note".equals( fieldName )) { oldValue = registration.getNote(); registration.setNote( newValue, session, register() ); } else if( "residence".equals( fieldName )) { oldValue = registration.getResidence(); registration.setResidence( newValue, session, register() ); } else { replyB.lappendln( "a.voter.CommandResponder.unrecognizedArgument(1,2)", commandName, fieldName ); replyB.lappendlnn( "a.voter.CommandResponder.helpPrompt(1)", commandName ); return; } registration.commit( register().voterInputTable(), session ); replyB.lappendln( "a.register.CR_Set.reply.valueOld(1)", valueOrNone( oldValue, replyB.bundle() )); replyB.lappendlnn( "a.register.CR_Set.reply.valueNew(1)", valueOrNone( newValue, replyB.bundle() )); } /** Compiles a map of launch options. */ static HashMap compileOptions( final CommandResponder.Session session ) { final HashMap optionMap = compileBaseOptions( session ); final ResourceBundle bundle = session.replyBuilder().bundle(); String key; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - key = "a.voter.CommandResponder.option.alter"; optionMap.put( key, new Option( bundle.getString(key), Option.REQUIRED_ARGUMENT )); // - - - return optionMap; } protected Register register() { return (Register)electoralService; } private static String valueOrNone( String value, ResourceBundle b ) { if( value != null ) return value; else return b.getString( "a.register.CR_Set.reply.valueNull" ); } }