001    /*
002     *   Copyright (c) 2009 The JOMC Project
003     *   Copyright (c) 2005 Christian Schulte <cs@jomc.org>
004     *   All rights reserved.
005     *
006     *   Redistribution and use in source and binary forms, with or without
007     *   modification, are permitted provided that the following conditions
008     *   are met:
009     *
010     *     o Redistributions of source code must retain the above copyright
011     *       notice, this list of conditions and the following disclaimer.
012     *
013     *     o Redistributions in binary form must reproduce the above copyright
014     *       notice, this list of conditions and the following disclaimer in
015     *       the documentation and/or other materials provided with the
016     *       distribution.
017     *
018     *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
019     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
022     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
027     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
028     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029     *
030     *   $Id: ModelException.java 509 2009-09-21 13:54:49Z schulte2005 $
031     *
032     */
033    package org.jomc.model;
034    
035    import java.io.Serializable;
036    import java.util.LinkedList;
037    import java.util.List;
038    import java.util.logging.Level;
039    import javax.xml.bind.JAXBElement;
040    
041    /**
042     * Gets thrown for invalid model objects.
043     *
044     * @author <a href="mailto:cs@jomc.org">Christian Schulte</a>
045     * @version $Id: ModelException.java 509 2009-09-21 13:54:49Z schulte2005 $
046     */
047    public class ModelException extends Exception
048    {
049    
050        /** {@code ModelException} detail. */
051        public static class Detail implements Serializable
052        {
053    
054            /** Serial version UID for compatibility with 1.0.x object streams. */
055            private static final long serialVersionUID = 5383378257214671678L;
056    
057            /**
058             * The detail level.
059             * @serial
060             */
061            private Level level;
062    
063            /**
064             * The detail message.
065             * @serial
066             */
067            private String message;
068    
069            /**
070             * The element this detail is associated with.
071             * @serial
072             */
073            private JAXBElement<? extends ModelObject> element;
074    
075            /**
076             * Creates a new {@code Detail} taking a detail level and message.
077             *
078             * @param level The detail level.
079             * @param message The detail message.
080             */
081            public Detail( final Level level, final String message )
082            {
083                this.level = level;
084                this.message = message;
085            }
086    
087            /**
088             * Gets the level of this detail.
089             *
090             * @return The level of this detail.
091             */
092            public Level getLevel()
093            {
094                return this.level;
095            }
096    
097            /**
098             * Gets the message of this detail.
099             *
100             * @return The message of this detail.
101             */
102            public String getMessage()
103            {
104                return this.message;
105            }
106    
107            /**
108             * Gets the element of this detail.
109             *
110             * @return The element of this detail or {@code null}.
111             */
112            public JAXBElement<? extends ModelObject> getElement()
113            {
114                return this.element;
115            }
116    
117            /**
118             * Sets the element of this detail.
119             *
120             * @param value The new element of this detail or {@code null}.
121             */
122            public void setElement( final JAXBElement<? extends ModelObject> value )
123            {
124                this.element = value;
125            }
126    
127            /**
128             * Creates and returns a string representation of the object.
129             *
130             * @return A string representation of the object.
131             */
132            private String toStringInternal()
133            {
134                return new StringBuilder().append( '{' ).
135                    append( "level=" ).append( this.getLevel().getLocalizedName() ).
136                    append( ", message=" ).append( this.getMessage() ).
137                    append( ", element=" ).append( this.getElement() ).append( '}' ).toString();
138    
139            }
140    
141            /**
142             * Creates and returns a string representation of the object.
143             *
144             * @return A string representation of the object.
145             */
146            @Override
147            public String toString()
148            {
149                return super.toString() + this.toStringInternal();
150            }
151    
152        }
153    
154        /** Serial version UID for compatibility with 1.0.x object streams. */
155        private static final long serialVersionUID = 6078527305669819171L;
156    
157        /**
158         * Details of the instance.
159         * @serial
160         */
161        private final List<Detail> details = new LinkedList<Detail>();
162    
163        /** Creates a new {@code ModelException} instance. */
164        public ModelException()
165        {
166            super();
167        }
168    
169        /**
170         * Creates a new {@code ModelException} instance taking a message.
171         *
172         * @param message The message of the exception.
173         */
174        public ModelException( final String message )
175        {
176            super( message );
177        }
178    
179        /**
180         * Creates a new {@code ModelException} instance taking a causing exception.
181         *
182         * @param t The causing exception.
183         */
184        public ModelException( final Throwable t )
185        {
186            super( t );
187        }
188    
189        /**
190         * Creates a new {@code ModelException} instance taking a message and a causing exception.
191         *
192         * @param message The message of the exception.
193         * @param t The causing exception.
194         */
195        public ModelException( final String message, final Throwable t )
196        {
197            super( message, t );
198        }
199    
200        /**
201         * Gets the details of the instance.
202         *
203         * @return The details of the instance.
204         */
205        public List<Detail> getDetails()
206        {
207            return this.details;
208        }
209    
210    }