public void saveNotification(String institutionName, String status) {
   Session hibernate = null;
   try {
     hibernate = HibernateUtil.getSession();
     hibernate.beginTransaction();
     Notification notification = new Notification();
     notification.setEmail(ApplicationProperties.getProperty("email.admin"));
     notification.setSubject(
         String.format(
             ApplicationProperties.getProperty("email.admin.robot.subject"), institutionName));
     notification.setMessage(
         String.format(
                 ApplicationProperties.getProperty("email.admin.robot.body"),
                 institutionName,
                 status)
             + ApplicationProperties.getProperty("email.signature"));
     notification.setType(NotificationType.ROBOTCHECK);
     notification.setDate(new Date());
     hibernate.save(notification);
     hibernate.getTransaction().commit();
   } catch (Exception ex) {
     if (hibernate != null && hibernate.isOpen() && hibernate.getTransaction().isActive()) {
       hibernate.getTransaction().rollback();
       logger.error("Failure", ex);
     }
   }
 }
  public void checkRobots() {
    Session hibernate = null;

    try {
      hibernate = HibernateUtil.getSession();
      hibernate.beginTransaction();
      Criteria criteria = hibernate.createCriteria(Institution.class);
      criteria.addOrder(Order.asc("id"));
      List<Institution> institutions = criteria.list();
      hibernate.getTransaction().commit();

      Boolean enabled = false;

      for (Institution institution : institutions) {
        Robot robot = RobotContext.getRobot(institution.getInstitutionClass());
        logger.info("institutionId: " + institution.getId());

        enabled = institution.getEnabled();

        if (robot != null) {
          try {
            robot.login();
            institution.setCanLogin(true);
          } catch (Exception ex) {
            institution.setCanLogin(false);
          }

          if (institution.getCanLogin()) {
            try {
              institution.setCanMakeRequest(robot.checkInstitutionId());
            } catch (Exception ex) {
              institution.setCanMakeRequest(false);
            }
          } else {
            institution.setCanMakeRequest(false);
          }
        } else {
          institution.setCanLogin(false);
          institution.setCanMakeRequest(false);
        }

        institution.setEnabled(institution.getCanLogin() && institution.getCanMakeRequest());
        hibernate = HibernateUtil.getSession();
        hibernate.beginTransaction();
        hibernate.update(institution);
        hibernate.getTransaction().commit();

        if (institution.getEnabled() != enabled) {
          if (institution.getEnabled() == true) {
            saveNotification(
                institution.getName(), ApplicationProperties.getProperty("robot.status.enabled"));
          } else {
            saveNotification(
                institution.getName(), ApplicationProperties.getProperty("robot.status.disabled"));
          }
        }
      }
    } catch (Exception ex) {
      if (hibernate != null && hibernate.isOpen() && hibernate.getTransaction().isActive()) {
        hibernate.getTransaction().rollback();
        logger.error("Failure", ex);
      }
    }
  }