/**
   * Add the route in the database if it's not already, or update its state.
   *
   * @param routes being added to the app
   */
  @Override
  public void onRoutesAdd(Collection<Route> routes) {
    for (Route routeCamel : routes) {
      // adding a performance counter on the route
      if (routeCamel instanceof EventDrivenConsumerRoute) {
        EventDrivenConsumerRoute edcr = (EventDrivenConsumerRoute) routeCamel;
        Processor processor = edcr.getProcessor();
        if (processor instanceof InstrumentationProcessor) {
          InstrumentationProcessor ip = (InstrumentationProcessor) processor;
          ConsolePerformanceCounter counter =
              new ConsolePerformanceCounter(routeCamel.getId(), repository);
          ip.setCounter(counter);
          log.debug("Adding a counter" + counter.toString() + " to " + routeCamel.getId());
        }
      }

      // saving route in database
      log.debug("Route added " + routeCamel.getId());
      com.ninja_squad.console.Route route = repository.findRoute(routeCamel.getId());
      if (route == null) {
        route =
            new com.ninja_squad.console.Route(routeCamel.getId())
                .state(State.Started)
                .uri(routeCamel.getEndpoint().getEndpointUri());
        ObjectMapper mapper = new ObjectMapper();
        AnnotationIntrospector introspector =
            new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
        // make serializer use JAXB annotations (only)
        mapper.setAnnotationIntrospector(introspector);
        String definition = null;
        RouteDefinition routeDefinition = routeCamel.getRouteContext().getRoute();
        try {
          definition = mapper.writeValueAsString(routeDefinition);
        } catch (IOException e) {
          log.error("Error while marshalling route definition", e);
        }
        route.setDefinition(definition);
        for (ProcessorDefinition<?> stepDefinition : routeDefinition.getOutputs()) {
          if (stepDefinition.getId() == null) {
            stepDefinition.setId(stepDefinition.getClass().getSimpleName());
          }
          route.getSteps().put(stepDefinition.getId(), stepDefinition.getLabel());
        }
        repository.save(route);
      }

      // saving state in database
      RouteState routeState = repository.lastRouteState(routeCamel.getId());
      if (routeState == null || routeState.getState().equals(State.Stopped)) {
        routeState = new RouteState();
        routeState.setRouteId(routeCamel.getId());
        routeState.setState(State.Started);
        routeState.setTimestamp(DateTime.now().getMillis());
        repository.save(routeState);
      }
    }
  }
 /**
  * Store a notification of the application state and hour
  *
  * @param context of the Camel app
  */
 @Override
 public void onContextStop(CamelContext context) {
   log.debug("Stopped " + context.getName());
   InstanceState instanceState = new InstanceState();
   instanceState.setName(context.getName());
   instanceState.setState(State.Stopped);
   instanceState.setTimestamp(DateTime.now().toString());
   repository.save(instanceState);
 }
 @Override
 public void onRoutesRemove(Collection<Route> routes) {
   for (Route routeCamel : routes) {
     log.debug("Route stopped : " + routeCamel.getId());
     // saving state in database
     RouteState routeState = repository.lastRouteState(routeCamel.getId());
     if (routeState == null || routeState.getState().equals(State.Started)) {
       routeState = new RouteState();
       routeState.setRouteId(routeCamel.getId());
       routeState.setState(State.Stopped);
       routeState.setTimestamp(DateTime.now().getMillis());
       repository.save(routeState);
     }
   }
 }