Skip to content

Commit

Permalink
Refactored the exception classes - TRUNK-4696
Browse files Browse the repository at this point in the history
Refactored the exception classes - TRUNK-4696

Refactored the exception classes - TRUNK-4696
  • Loading branch information
wluyima committed Mar 10, 2017
1 parent 4a25b84 commit 1a41376
Show file tree
Hide file tree
Showing 23 changed files with 410 additions and 451 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http:https://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http:https://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.api;

/**
* An instance of this exception is thrown if a delete operation is attempted on an object that is
* referenced by others, typically this should be thrown when deleting an existing object is
* detrimental to the integrity of objects referencing it or any other existing associated data.
*
* @since 2.1
*/
public class CannotDeleteObjectInUseException extends InvalidOperationOnObjectException {

public CannotDeleteObjectInUseException(String message) {
super(message);
}

public CannotDeleteObjectInUseException(String message, Throwable cause) {
super(message, cause);
}

public CannotDeleteObjectInUseException(String messageKey, Object[] parameters) {
super(messageKey, parameters);
}

/**
* @see InvalidOperationOnObjectException#InvalidOperationOnObjectException(Class)
*/
public CannotDeleteObjectInUseException(Class clazz) {
super(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http:https://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http:https://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.api;

/**
* An instance of this exception is thrown if an update operation is attempted on an object that is
* referenced by others, typically this should be thrown when altering an existing object is
* detrimental to the integrity of objects referencing it or any other existing associated data.
*
* @since 2.1
*/
public class CannotUpdateObjectInUseException extends InvalidOperationOnObjectException {

public CannotUpdateObjectInUseException(String message) {
super(message);
}

public CannotUpdateObjectInUseException(String message, Throwable cause) {
super(message, cause);
}

public CannotUpdateObjectInUseException(String messageKey, Object[] parameters) {
super(messageKey, parameters);
}

/**
* @see InvalidOperationOnObjectException#InvalidOperationOnObjectException(Class)
*/
public CannotUpdateObjectInUseException(Class clazz) {
super(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http:https://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http:https://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.api;

/**
* An instance of this exception is thrown if an operation is attempted on an object, typically this
* should be thrown when any operation is made to an existing object and is considered detrimental
* to the integrity of any existing associated data. This is a more generic exception that doesn't
* convey any specifics on the actual operation that was attempted on the object.
*
* @see UnchangeableObjectException
* @see CannotDeleteObjectInUseException
* @see CannotUpdateObjectInUseException
* @since 2.1
*/
public class InvalidOperationOnObjectException extends APIException {

public InvalidOperationOnObjectException(String message) {
super(message);
}

public InvalidOperationOnObjectException(String message, Throwable cause) {
super(message, cause);
}

public InvalidOperationOnObjectException(String messageKey, Object[] parameters) {
super(messageKey, parameters);
}

/**
* @param clazz the type of the object on which the operation was attempted
*/
public InvalidOperationOnObjectException(Class clazz) {
this("An invalid operation was attempted on an instance of " + clazz.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http:https://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http:https://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.api;

/**
* An instance of this exception is thrown when a required property has not been set on an Object.
*
* @since 2.1
*/
public class MissingRequiredPropertyException extends APIException {

public MissingRequiredPropertyException(String message) {
super(message);
}

public MissingRequiredPropertyException(String message, Throwable cause) {
super(message, cause);
}

public MissingRequiredPropertyException(String messageKey, Object[] parameters) {
super(messageKey, parameters);
}

/**
* @param clazz the class of the object on which the property is required
* @param property the name of the missing required property
*/
public MissingRequiredPropertyException(Class clazz, String property) {
this(clazz.getName() + "." + property + " is required");
}
}
41 changes: 41 additions & 0 deletions api/src/main/java/org/openmrs/api/UnchangeableObjectException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http:https://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http:https://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.api;

/**
* An instance of this exception is thrown if an attempt is made to update an unchangeable object
*
* @since 2.1
*/
public class UnchangeableObjectException extends InvalidOperationOnObjectException {

public UnchangeableObjectException() {
this("Cannot update an unchangeable object");
}

public UnchangeableObjectException(String message) {
super(message);
}

public UnchangeableObjectException(String message, Throwable cause) {
super(message, cause);
}

public UnchangeableObjectException(String messageKey, Object[] parameters) {
super(messageKey, parameters);
}

/**
* @see InvalidOperationOnObjectException#InvalidOperationOnObjectException(Class)
*/
public UnchangeableObjectException(Class clazz) {
super(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http:https://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http:https://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.api;

/**
* An instance of this exception is thrown if an attempt is made to update an unchangeable property
* on an object
*
* @since 2.1
*/
public class UnchangeablePropertyException extends APIException {

public UnchangeablePropertyException(String message) {
super(message);
}

public UnchangeablePropertyException(String message, Throwable cause) {
super(message, cause);
}

public UnchangeablePropertyException(String messageKey, Object[] parameters) {
super(messageKey, parameters);
}

/**
* @param clazz the class of the object on which it was changed
* @param property the name of the unchangeable property
*/
public UnchangeablePropertyException(Class clazz, String property) {
this(clazz.getName() + "." + property + " cannot be changed");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.hibernate.type.Type;
import org.openmrs.Retireable;
import org.openmrs.Voidable;
import org.openmrs.api.APIException;
import org.openmrs.api.UnchangeableObjectException;
import org.openmrs.util.OpenmrsUtil;

/**
Expand All @@ -39,7 +39,7 @@
public abstract class ImmutableEntityInterceptor extends EmptyInterceptor {

private static final Log log = LogFactory.getLog(ImmutableEntityInterceptor.class);

/**
* Returns the class handled by the interceptor
*/
Expand Down Expand Up @@ -108,7 +108,8 @@ public boolean onFlushDirty(Object entity, Serializable id, Object[] currentStat
if (log.isDebugEnabled()) {
log.debug("The following fields cannot be changed for " + getSupportedType() + ":" + changedProperties);
}
throw new APIException("Editing some fields " + changedProperties + " on " + getSupportedType().getSimpleName() + " is not allowed");
throw new UnchangeableObjectException("Editing some fields " + changedProperties + " on "
+ getSupportedType().getSimpleName() + " is not allowed");
}
}

Expand Down
Loading

0 comments on commit 1a41376

Please sign in to comment.