001 package com.sptci.prevayler;
002
003 /**
004 * An exception used to indicate that a constraint violations has been
005 * encountered while persisting the prevalent object.
006 *
007 * <p>© Copyright 2008 <a href='http://sptci.com/' target='_top'>Sans
008 * Pareil Technologies, Inc.</a></p>
009 * @author Rakesh Vidyadharan 2008-05-22
010 * @version $Id: ConstraintException.java 4345 2008-06-30 21:22:03Z rakesh $
011 */
012 public class ConstraintException extends PrevalentException
013 {
014 private static final long serialVersionUID = 1L;
015
016 /** The pattern for displaying constraint violation. */
017 private static final String FIELD_PATTERN =
018 "Unique constraint on class: $CLASS$ field(s): $FIELD$ violated!";
019
020 /** The pattern used to generate the message for a oid violation. */
021 private static final String OID_PATTERN =
022 "Object of type: $CLASS$ already exists with objectId: $OID$!";
023
024 /** Default constructor. Cannot be instantiated. */
025 protected ConstraintException() {}
026
027 /**
028 * Create a new instance of the exception with the specified message.
029 *
030 * @param message The message to associate with the exception.
031 */
032 public ConstraintException( final String message )
033 {
034 super( message );
035 }
036
037 /**
038 * Create a new instance of the exception using {@link #FIELD_PATTERN} to
039 * display the message for unique constraint on specified fields being
040 * violated.
041 *
042 * @param object The prevalent object for which the violation was raised.
043 * @param fields A string representing the fields on which the constraint
044 * was defined.
045 */
046 public ConstraintException( final PrevalentObject object,
047 final String fields )
048 {
049 super( FIELD_PATTERN.replace( "$CLASS$", object.getClass().getName() ).
050 replace( "$FIELD$", fields ) );
051 }
052
053 /**
054 * Create a new instance of the exception using {@link #OID_PATTERN} to
055 * display the message for the unique object id constraint being violated.
056 *
057 * @param object The prevalent object for which the violation was raised.
058 */
059 public ConstraintException( final PrevalentObject object )
060 {
061 super( OID_PATTERN.replace( "$CLASS$", object.getClass().getName() ).
062 replace( "$OID$", object.getObjectId().toString() ) );
063 }
064 }