View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2009 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.monitoring.notification;
18  
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import org.springframework.dao.DataIntegrityViolationException;
22  import org.springframework.dao.DataRetrievalFailureException;
23  import org.springframework.dao.OptimisticLockingFailureException;
24  import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException;
25  import org.springframework.orm.ObjectRetrievalFailureException;
26  import org.springframework.util.StringUtils;
27  
28  import ch.elca.el4j.services.persistence.generic.exceptions.InsertionFailureException;
29  /**
30   * This class is used to notify on events which are persistence based.
31   *
32   * @svnLink $Revision: 3951 $;$Date: 2009-10-19 17:44:35 +0200 (Mo, 19. Okt 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/services/monitoring/notification/PersistenceNotificationHelper.java $
33   *
34   * @author Martin Zeltner (MZE)
35   */
36  public final class PersistenceNotificationHelper {
37  	/**
38  	 * Private logger of this class.
39  	 */
40  	private static Logger s_logger
41  		= LoggerFactory.getLogger(PersistenceNotificationHelper.class);
42  	
43  	/**
44  	 * Hide default constructor.
45  	 */
46  	private PersistenceNotificationHelper() {
47  	}
48  	
49  	/**
50  	 * Method to log that an object does not exist. This method will always
51  	 * throw an exception.
52  	 *
53  	 * @param detailedMessage
54  	 *            Is the detailed message.
55  	 * @param objectName
56  	 *            Is the name of the object.
57  	 * @throws DataRetrievalFailureException
58  	 *             Will be thrown in every case.
59  	 */
60  	public static void notifyDataRetrievalFailure(
61  		String detailedMessage, String objectName)
62  		throws DataRetrievalFailureException {
63  		String message = StringUtils.hasText(detailedMessage)
64  			? detailedMessage
65  				: "The desired " + objectName + " does not exist.";
66  		s_logger.info(message);
67  		throw new DataRetrievalFailureException(message);
68  	}
69  
70  	/**
71  	 * Method to log that an object made problems while insertion. This method
72  	 * will always throw an exception.
73  	 *
74  	 * @param detailedMessage
75  	 *            Is the detailed message.
76  	 * @param objectName
77  	 *            Is the name of the object.
78  	 * @throws InsertionFailureException
79  	 *             Will be thrown in every case.
80  	 */
81  	public static void notifyInsertionFailure(
82  		String detailedMessage, String objectName)
83  		throws InsertionFailureException {
84  		String message = StringUtils.hasText(detailedMessage)
85  			? detailedMessage
86  				: "Insertion of " + objectName + " failed.";
87  		s_logger.info(message);
88  		throw new InsertionFailureException(message);
89  	}
90  	
91  	/**
92  	 * Notify violation of data integrity of an object.
93  	 * Will always throw an exception.
94  	 *
95  	 * @param detailedMessage
96  	 *            Is the detailed message.
97  	 * @param objectName
98  	 *            Is the name of the object.
99  	 * @throws DataIntegrityViolationException
100 	 *             Will be thrown in every case.
101 	 */
102 	public static void notifyDataIntegrityViolationFailure(
103 		String detailedMessage, String objectName)
104 		throws DataIntegrityViolationException {
105 		String message = StringUtils.hasText(detailedMessage)
106 			? detailedMessage
107 				: "Integrity of " + objectName + " violated.";
108 		s_logger.info(message);
109 		throw new DataIntegrityViolationException(message);
110 	}
111 
112 
113 	/**
114 	 * Method to log that the object has already been modified. This method
115 	 * will always throw an exception.
116 	 *
117 	 * @param detailedMessage
118 	 *            Is the detailed message.
119 	 * @param objectName
120 	 *            Is the name of the object.
121 	 * @param optionalException optionally returns the original {@link OptimisticLockingFailureException}
122 	 *         (in order not to loose information)
123 	 * @throws OptimisticLockingFailureException
124 	 *             Will be thrown in every case.
125 	 */
126 	public static void notifyOptimisticLockingFailure(
127 		String detailedMessage, String objectName, OptimisticLockingFailureException optionalException)
128 		throws OptimisticLockingFailureException {
129 		String message = StringUtils.hasText(detailedMessage)
130 			? detailedMessage
131 				: objectName + " was modified or deleted in the meantime.";
132 		s_logger.info(message);
133 		
134 		// in order not to loose information
135 		if (optionalException != null) {
136 			throw optionalException;
137 		} else {
138 			throw new OptimisticLockingFailureException(message);
139 		}
140 	}
141 
142 	/**
143 	 * Method to log that an object could not be retrieved. This method will
144 	 * always throw an exception.
145 	 * 
146 	 * @param entityClass
147 	 *            The persistent class.
148 	 * @param identifier
149 	 *            The ID of the object that should have been retrieved.
150 	 * @param objectName
151 	 *            The name of the object.
152 	 */
153 	public static void notifyObjectRetrievalFailure(Class<?> entityClass,
154 		Object identifier, String objectName)
155 		throws ObjectRetrievalFailureException {
156 		notifyObjectRetrievalFailure(entityClass, identifier, objectName,
157 			null, null);
158 	}
159 	
160 	/**
161 	 * Method to log that an object could not be retrieved. This method will
162 	 * always throw an exception.
163 	 * 
164 	 * @param entityClass
165 	 *            The persistent class.
166 	 * @param identifier
167 	 *            The ID of the object that should have been retrieved.
168 	 * @param objectName
169 	 *            The name of the object.
170 	 * @param detailedMessage
171 	 *            A detailed message.
172 	 * @param cause
173 	 *            The source of the exception.
174 	 */
175 	public static void notifyObjectRetrievalFailure(Class<?> entityClass,
176 		Object identifier, String objectName, String detailedMessage,
177 		Throwable cause) throws ObjectRetrievalFailureException {
178 		String message = StringUtils.hasText(detailedMessage) ? detailedMessage
179 			: "The desired " + objectName + " with ID " + identifier
180 				+ " could not be" + " retrieved.";
181 		s_logger.info(message);
182 
183 		throw new ObjectRetrievalFailureException(entityClass, identifier,
184 			message, cause);
185 	}
186 	
187 	
188 	/**
189 	 * Method to log that the object could not be updated correctly. This method
190 	 * will always throw an exception.
191 	 *
192 	 * @param detailedMessage
193 	 *            Is the detailed message.
194 	 * @param objectName
195 	 *            Is the name of the object.
196 	 * @param sql
197 	 *            Is the SQL we tried to execute.
198 	 * @param expected
199 	 *            Is the expected number of rows affected.
200 	 * @param actual
201 	 *            Is the actual number of rows affected.
202 	 * @throws JdbcUpdateAffectedIncorrectNumberOfRowsException
203 	 *             Will be thrown in every case.
204 	 */
205 	public static void notifyJdbcUpdateAffectedIncorrectNumberOfRows(
206 		String detailedMessage, String objectName,
207 		String sql, int expected, int actual)
208 		throws JdbcUpdateAffectedIncorrectNumberOfRowsException {
209 		String message = StringUtils.hasText(detailedMessage)
210 			? detailedMessage
211 				: objectName + " could not be updated correctly.";
212 		s_logger.info(message);
213 		throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(
214 			sql, expected, actual);
215 	}
216 	
217 	/**
218 	 * Same behaviour as call of method
219 	 * <code>notifyDataRetrievalFailure(null, String)</code>.
220 	 *
221 	 * @see #notifyDataRetrievalFailure(String, String)
222 	 */
223 	public static void notifyDataRetrievalFailure(String objectName)
224 		throws DataRetrievalFailureException {
225 		notifyDataRetrievalFailure(null, objectName);
226 	}
227 
228 	/**
229 	 * Same behaviour as call of method
230 	 * <code>notifyInsertionFailure(null, String)</code>.
231 	 *
232 	 * @see #notifyInsertionFailure(String, String)
233 	 */
234 	public static void notifyInsertionFailure(String objectName)
235 		throws InsertionFailureException {
236 		notifyInsertionFailure(null, objectName);
237 	}
238 
239 	/**
240 	 * Same behaviour as call of method
241 	 * <code>notifyDataIntegrityViolationFailure(null, String)</code>.
242 	 *
243 	 * @see #notifyDataIntegrityViolationFailure(String, String)
244 	 */
245 	public static void notifyDataIntegrityViolationFailure(String objectName)
246 		throws DataIntegrityViolationException {
247 		notifyDataIntegrityViolationFailure(null, objectName);
248 	}
249 
250 	
251 	/**
252 	 * Same behaviour as call of method
253 	 * <code>notifyOptimisticLockingFailure(null, String)</code>.
254 	 *
255 	 * @see #notifyOptimisticLockingFailure(String, String, OptimisticLockingFailureException)
256 	 */
257 	public static void notifyOptimisticLockingFailure(String objectName)
258 		throws OptimisticLockingFailureException {
259 		notifyOptimisticLockingFailure(null, objectName, null);
260 	}
261 	
262 	/**
263 	 * Same behaviour as call of method
264 	 * <code>notifyOptimisticLockingFailure(null, String, String, int, int)
265 	 * </code>.
266 	 *
267 	 * @see #notifyJdbcUpdateAffectedIncorrectNumberOfRows(String, String,
268 	 *      String, int, int)
269 	 */
270 	public static void notifyJdbcUpdateAffectedIncorrectNumberOfRows(
271 		String objectName, String sql, int expected, int actual)
272 		throws JdbcUpdateAffectedIncorrectNumberOfRowsException {
273 		notifyJdbcUpdateAffectedIncorrectNumberOfRows(
274 			null, objectName, sql, expected, actual);
275 	}
276 }