001/*
002 *  jDTAUS Banking Messages
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.banking.messages;
022
023import java.util.Locale;
024import org.jdtaus.core.container.ContainerFactory;
025import org.jdtaus.core.text.Message;
026
027/**
028 * Message stating that a field holds invalid data.
029 *
030 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
031 * @version $JDTAUS: IllegalDataMessage.java 8810 2012-12-04 00:45:37Z schulte $
032 */
033public final class IllegalDataMessage extends Message
034{
035
036    /** Constant for DTAUS alphabet fields. */
037    public static final int TYPE_ALPHA = 1;
038
039    /** Constant for numeric DTAUS alphabet fields. */
040    public static final int TYPE_NUMERIC = 2;
041
042    /** Constant for alpha-numeric DTAUS alphabet fields. */
043    public static final int TYPE_ALPHA_NUMERIC = 3;
044
045    /** Constant for EBCDIC packed number fields. */
046    public static final int TYPE_PACKET_POSITIVE = 4;
047
048    /** Constant for well-known constant value fields. */
049    public static final int TYPE_CONSTANT = 5;
050
051    /** Constant for reserved fields. */
052    public static final int TYPE_RESERVED = 6;
053
054    /** Constant for two-digit year information date fields. */
055    public static final int TYPE_SHORTDATE = 7;
056
057    /** Constant for four-digit year information date fields. */
058    public static final int TYPE_LONGDATE = 8;
059
060    /** Constant for field 3 of an A record. */
061    public static final int TYPE_FILETYPE = 9;
062
063    /** Constant for {@code Bankleitzahl} fields. */
064    public static final int TYPE_BANKLEITZAHL = 10;
065
066    /**  Constant for {@code Kontonummer} fields. */
067    public static final int TYPE_KONTONUMMER = 11;
068
069    /**
070     * Constant for either a {@code Referenznummer10} or
071     * {@code Referenznummer11} fields.
072     */
073    public static final int TYPE_REFERENZNUMMER = 12;
074
075    /** Constant for {@code Textschluessel} fields. */
076    public static final int TYPE_TEXTSCHLUESSEL = 13;
077
078    /** Constant for currency fields. */
079    public static final int TYPE_CURRENCY = 14;
080
081    /** All type constants. */
082    private static final int[] TYPES =
083    {
084        TYPE_ALPHA, TYPE_NUMERIC, TYPE_ALPHA_NUMERIC, TYPE_PACKET_POSITIVE, TYPE_CONSTANT, TYPE_RESERVED,
085        TYPE_SHORTDATE, TYPE_LONGDATE, TYPE_FILETYPE, TYPE_BANKLEITZAHL, TYPE_KONTONUMMER, TYPE_REFERENZNUMMER,
086        TYPE_TEXTSCHLUESSEL, TYPE_CURRENCY
087    };
088
089    /** Serial version UID for backwards compatibility with 1.0.x classes. */
090    private static final long serialVersionUID = 5634623930947949635L;
091
092    /**
093     * Constant of the field holding illegal data.
094     * @serial
095     */
096    private final int field;
097
098    /**
099     * Absolute position of the field holding illegal data.
100     * @serial
101     */
102    private final long position;
103
104    /**
105     * Constant for the type of the field holding illegal data.
106     * @serial
107     */
108    private final int type;
109
110    /**
111     * The illegal data.
112     * @serial
113     */
114    private final String invalidData;
115
116    /**
117     * Creates a new {@code IllegalDataMessage} instance taking information about the illegal data.
118     *
119     * @param field Constant for the field holding illegal data.
120     * @param type Constant for the type of the field.
121     * @param position Absolute position of the field holding illegal data.
122     * @param invalidData The illegal data held in {@code field} at {@code position}.
123     *
124     * @throws IllegalArgumentException if {@code type} does not match one of {@link #TYPE_ALPHA TYPE_<i>XYZ</i>}
125     * constants.
126     *
127     * @see org.jdtaus.banking.dtaus.spi.Fields
128     */
129    public IllegalDataMessage( final int field, final int type, final long position, final String invalidData )
130    {
131        super();
132        this.field = field;
133        this.type = type;
134        this.position = position;
135        this.invalidData = invalidData;
136        this.assertValidType();
137    }
138
139    /**
140     * Checks a given integer to match one of the {@code TYPE_<i>XYZ</i>} constants.
141     *
142     * @throws IllegalArgumentException if {@code type} does not match one of {@link #TYPE_ALPHA TYPE_<i>XYZ</i>}
143     * constants.
144     */
145    private void assertValidType()
146    {
147        boolean valid = false;
148        for ( int i = TYPES.length - 1; i >= 0 && !valid; i-- )
149        {
150            if ( TYPES[i] == this.type )
151            {
152                valid = true;
153                break;
154            }
155        }
156
157        if ( !valid )
158        {
159            throw new IllegalArgumentException( Integer.toString( this.type ) );
160        }
161    }
162
163    /**
164     * {@inheritDoc}
165     *
166     * @return A string describing the field holding illegal data, the absolute position of that field, and the illegal
167     * data held by the field.
168     * <ul>
169     * <li>[0]: a string describing the field holding illegal data.</li>
170     * <li>[1]: the absolute position of that field.</li>
171     * <li>[2]: the invalid data.</li>
172     * </ul>
173     */
174    public Object[] getFormatArguments( final Locale locale )
175    {
176        return new Object[]
177            {
178                Integer.toHexString( this.field ).toUpperCase( locale ),
179                new Long( this.position ),
180                this.invalidData
181            };
182    }
183
184    /**
185     * {@inheritDoc}
186     *
187     * @return The corresponding text from the message's {@code ResourceBundle}
188     * <blockquote><pre>
189     * "{2}" is no valid value for field {0} (at {1, number}).
190     * </pre></blockquote>
191     */
192    public String getText( final Locale locale )
193    {
194        return this.getIllegalDataMessage(
195            locale, Integer.toHexString( this.field ).toUpperCase(), new Long( this.position ), this.invalidData );
196
197    }
198
199    //--Messages----------------------------------------------------------------
200
201// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausMessages
202    // This section is managed by jdtaus-container-mojo.
203
204    /**
205     * Gets the text of message <code>illegalData</code>.
206     * <blockquote><pre>"{2}" ist kein gültiger Wert für Feld {0} (Position {1, number}).</pre></blockquote>
207     * <blockquote><pre>"{2}" is no valid value for field {0} (at {1, number}).</pre></blockquote>
208     *
209     * @param locale The locale of the message instance to return.
210     * @param fld format parameter.
211     * @param pos format parameter.
212     * @param data format parameter.
213     *
214     * @return the text of message <code>illegalData</code>.
215     */
216    private String getIllegalDataMessage( final Locale locale,
217            final java.lang.String fld,
218            final java.lang.Number pos,
219            final java.lang.String data )
220    {
221        return ContainerFactory.getContainer().
222            getMessage( this, "illegalData", locale,
223                new Object[]
224                {
225                    fld,
226                    pos,
227                    data
228                });
229
230    }
231
232// </editor-fold>//GEN-END:jdtausMessages
233
234    //----------------------------------------------------------------Messages--
235}