Search in sources :

Example 1 with Alert

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");
}
Also used : BindException(org.springframework.validation.BindException) Alert(org.openmrs.notification.Alert) Before(org.junit.Before)

Example 2 with 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");
    }
}
Also used : Alert(org.openmrs.notification.Alert)

Example 3 with Alert

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);
        }
    }
}
Also used : Role(org.openmrs.Role) Collection(java.util.Collection) Alert(org.openmrs.notification.Alert) IOException(java.io.IOException) URLConnection(java.net.URLConnection) URL(java.net.URL) Date(java.util.Date) IOException(java.io.IOException)

Example 4 with Alert

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();
}
Also used : StringDescription(org.hamcrest.StringDescription) BindException(org.springframework.validation.BindException) Alert(org.openmrs.notification.Alert) Before(org.junit.Before)

Example 5 with Alert

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);
}
Also used : Role(org.openmrs.Role) User(org.openmrs.User) Alert(org.openmrs.notification.Alert)

Aggregations

Alert (org.openmrs.notification.Alert)5 Before (org.junit.Before)2 Role (org.openmrs.Role)2 BindException (org.springframework.validation.BindException)2 IOException (java.io.IOException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Collection (java.util.Collection)1 Date (java.util.Date)1 StringDescription (org.hamcrest.StringDescription)1 User (org.openmrs.User)1