use of org.openmrs.notification.Alert in project openmrs-core by openmrs.
the class AlertValidatorTest method setUp.
@Before
public void setUp() {
validator = new AlertValidator();
alert = new Alert();
errors = new BindException(alert, "alert");
}
use of org.openmrs.notification.Alert in project openmrs-core by openmrs.
the class AlertValidator method validate.
/**
* @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
* @should fail validation if Alert Text is null or empty or whitespace
* @should pass validation if all required values are set
* @should pass validation if field lengths are correct
* @should fail validation if field lengths are not correct
*/
@Override
public void validate(Object obj, Errors errors) {
Alert alert = (Alert) obj;
if (alert == null) {
errors.rejectValue("alert", "error.general");
} else {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "text", "Alert.text.required");
ValidateUtil.validateFieldLengths(errors, obj.getClass(), "text");
}
}
use of org.openmrs.notification.Alert in project openmrs-core by openmrs.
the class CheckInternetConnectivityTask method execute.
/**
* @see org.openmrs.scheduler.tasks.AbstractTask#execute()
*/
@Override
public void execute() {
// TODO url should be provided as a property to taskconfig
String url = "http://www.google.com:80/index.html";
try {
URLConnection connection = new URL(url).openConnection();
connection.connect();
} catch (IOException ioe) {
try {
String text = "At " + new Date() + " there was an error reported connecting to the internet address " + url + ": " + ioe;
// TODO role should be provided as a property to taskconfig
Role role = Context.getUserService().getRole("System Developer");
Collection<User> users = Context.getUserService().getUsersByRole(role);
Context.getAlertService().saveAlert(new Alert(text, users));
} catch (Exception e) {
// Uh oh, just log it.
log.error("Failed to check internet connectivity", e);
}
}
}
use of org.openmrs.notification.Alert in project openmrs-core by openmrs.
the class HasFieldErrorsTest method setUp.
@Before
public void setUp() {
Alert target = new Alert();
item = new BindException(target, "alert");
description = new StringDescription();
}
use of org.openmrs.notification.Alert in project openmrs-core by openmrs.
the class AlertServiceImpl method notifySuperUsers.
/**
* @see org.openmrs.notification.AlertService#notifySuperUsers(String, Exception, Object...)
*/
@Override
public void notifySuperUsers(String messageCode, Exception cause, Object... messageArguments) {
// Generate an internationalized error message with the beginning of the stack trace from cause added onto the end
String message = Context.getMessageSourceService().getMessage(messageCode, messageArguments, Context.getLocale());
if (cause != null) {
StringBuilder stackTrace = new StringBuilder();
for (StackTraceElement traceElement : cause.getStackTrace()) {
stackTrace.append(traceElement);
stackTrace.append("\n");
if (stackTrace.length() >= Alert.TEXT_MAX_LENGTH) {
break;
}
}
message = message + ":" + stackTrace;
// limit message to Alert.TEXT_MAX_LENGTH
message = message.substring(0, Math.min(message.length(), Alert.TEXT_MAX_LENGTH));
}
// Send an alert to all administrators
Alert alert = new Alert(message, Context.getUserService().getUsersByRole(new Role(RoleConstants.SUPERUSER)));
// Set the alert so that if any administrator 'reads' it it will be marked as read for everyone who received it
alert.setSatisfiedByAny(true);
// TODO switch this to use the daemon user when ticket TRUNK-120 is complete
if (alert.getCreator() == null) {
alert.setCreator(new User(1));
}
// save the alert to send it to all administrators
Context.getAlertService().saveAlert(alert);
}
Aggregations