001package votorola.a.voter; // Copyright 2009, 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 javax.mail.internet.*;
005import org.apache.wicket.AttributeModifier;
006import org.apache.wicket.markup.html.form.*;
007import org.apache.wicket.util.convert.*;
008import votorola.a.*;
009import votorola.a.web.wic.*;
010import votorola.g.lang.*;
011import votorola.g.mail.*;
012import votorola.g.web.wic.*;
013
014
015/** A Wicket I/O converter of ID pairs.
016  */
017public @ThreadRestricted("wicket") final class IDPairConverter implements IConverter<IDPair>
018{
019
020    private static final long serialVersionUID = 0L;
021
022
023
024    /** A convenience method to initialize a text field by adding a 'maxLength'
025      * attribute to its view, and setting a model type of {@linkplain IDPair IDPair}.
026      *
027      *     @return the same field
028      *
029      *     @see VoterInputTable#MAX_INPUT_LENGTH
030      */
031    public static <C extends TextField<IDPair>> C setMaxLength_Type( final C field )
032    {
033        field.add( AttributeModifier.replace( "maxlength",
034          Integer.toString( VoterInputTable.MAX_INPUT_LENGTH )));
035
036        assert field.getType() == null: "not clobbering a pre-assigned type";
037        field.setType( IDPair.class );
038
039        return field;
040    }
041
042
043
044   // - I - C o n v e r t e r ------------------------------------------------------------
045
046
047    /** @throws IllegalArgumentException if input exceeds
048      *   {@linkplain VoterInputTable#MAX_INPUT_LENGTH MAX_INPUT_LENGTH}
049      */
050    public final IDPair convertToObject( final String input, final Locale _ignored )
051      throws IllegalArgumentException
052    {
053        ModelLengthLimiter.lengthConstrained( input, VoterInputTable.MAX_INPUT_LENGTH );
054     // if( input == null ) return null;
055        if( input.length() == 0 ) return null; // IConverter says input never null
056
057        final boolean isEmail = input.indexOf('@') >= 0;
058        final IDPair idPair;
059        if( isEmail )
060        {
061            try{ idPair = IDPair.fromEmail( InternetAddressX.canonicalAddress( input )); }
062            catch( final AddressException x )
063            {
064                throw new ConversionExceptionLM( VRequestCycle.get().bunG().l(
065                  "g.malformedEmail", input, x.getMessage() ));
066            }
067        }
068        else // username
069        {
070            try{ idPair = IDPair.fromUsername( input ); }
071            catch( final AddressException x )
072            {
073                throw new ConversionExceptionLM( VRequestCycle.get().bunW().l(
074                  "a.voter.IDPairConverter.malformedUsername", input, x.getMessage() ));
075            }
076        }
077        return idPair;
078    }
079
080
081
082    public final String convertToString( final IDPair value, final Locale _ignored )
083    {
084        return value == null? null: value.toString();
085    }
086
087
088
089}