001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019package org.apache.isis.persistence.jdo.spring.exceptions;
020
021import org.springframework.dao.OptimisticLockingFailureException;
022
023/**
024 * Exception thrown on an optimistic locking violation for a mapped object.
025 * Provides information about the persistent class and the identifier.
026 */
027@SuppressWarnings("serial")
028public class ObjectOptimisticLockingFailureException extends OptimisticLockingFailureException {
029
030        private Object persistentClass;
031
032        private Object identifier;
033
034
035        /**
036         * Create a general ObjectOptimisticLockingFailureException with the given message,
037         * without any information on the affected object.
038         * @param msg the detail message
039         * @param cause the source exception
040         */
041        public ObjectOptimisticLockingFailureException(String msg, Throwable cause) {
042                super(msg, cause);
043        }
044
045        /**
046         * Create a new ObjectOptimisticLockingFailureException for the given object,
047         * with the default "optimistic locking failed" message.
048         * @param persistentClass the persistent class
049         * @param identifier the ID of the object for which the locking failed
050         */
051        public ObjectOptimisticLockingFailureException(Class<?> persistentClass, Object identifier) {
052                this(persistentClass, identifier, null);
053        }
054
055        /**
056         * Create a new ObjectOptimisticLockingFailureException for the given object,
057         * with the default "optimistic locking failed" message.
058         * @param persistentClass the persistent class
059         * @param identifier the ID of the object for which the locking failed
060         * @param cause the source exception
061         */
062        public ObjectOptimisticLockingFailureException(
063                        Class<?> persistentClass, Object identifier, Throwable cause) {
064
065                this(persistentClass, identifier,
066                                "Object of class [" + persistentClass.getName() + "] with identifier [" + identifier +
067                                "]: optimistic locking failed", cause);
068        }
069
070        /**
071         * Create a new ObjectOptimisticLockingFailureException for the given object,
072         * with the given explicit message.
073         * @param persistentClass the persistent class
074         * @param identifier the ID of the object for which the locking failed
075         * @param msg the detail message
076         * @param cause the source exception
077         */
078        public ObjectOptimisticLockingFailureException(
079                        Class<?> persistentClass, Object identifier, String msg, Throwable cause) {
080
081                super(msg, cause);
082                this.persistentClass = persistentClass;
083                this.identifier = identifier;
084        }
085
086        /**
087         * Create a new ObjectOptimisticLockingFailureException for the given object,
088         * with the default "optimistic locking failed" message.
089         * @param persistentClassName the name of the persistent class
090         * @param identifier the ID of the object for which the locking failed
091         */
092        public ObjectOptimisticLockingFailureException(String persistentClassName, Object identifier) {
093                this(persistentClassName, identifier, null);
094        }
095
096        /**
097         * Create a new ObjectOptimisticLockingFailureException for the given object,
098         * with the default "optimistic locking failed" message.
099         * @param persistentClassName the name of the persistent class
100         * @param identifier the ID of the object for which the locking failed
101         * @param cause the source exception
102         */
103        public ObjectOptimisticLockingFailureException(
104                        String persistentClassName, Object identifier, Throwable cause) {
105
106                this(persistentClassName, identifier,
107                                "Object of class [" + persistentClassName + "] with identifier [" + identifier +
108                                "]: optimistic locking failed", cause);
109        }
110
111        /**
112         * Create a new ObjectOptimisticLockingFailureException for the given object,
113         * with the given explicit message.
114         * @param persistentClassName the name of the persistent class
115         * @param identifier the ID of the object for which the locking failed
116         * @param msg the detail message
117         * @param cause the source exception
118         */
119        public ObjectOptimisticLockingFailureException(
120                        String persistentClassName, Object identifier, String msg, Throwable cause) {
121
122                super(msg, cause);
123                this.persistentClass = persistentClassName;
124                this.identifier = identifier;
125        }
126
127
128        /**
129         * Return the persistent class of the object for which the locking failed.
130         * If no Class was specified, this method returns null.
131         */
132        public Class<?> getPersistentClass() {
133                return (this.persistentClass instanceof Class ? (Class<?>) this.persistentClass : null);
134        }
135
136        /**
137         * Return the name of the persistent class of the object for which the locking failed.
138         * Will work for both Class objects and String names.
139         */
140        public String getPersistentClassName() {
141                if (this.persistentClass instanceof Class) {
142                        return ((Class<?>) this.persistentClass).getName();
143                }
144                return (this.persistentClass != null ? this.persistentClass.toString() : null);
145        }
146
147        /**
148         * Return the identifier of the object for which the locking failed.
149         */
150        public Object getIdentifier() {
151                return identifier;
152        }
153
154}