private static void addTimeHorizons(JsonNode document, ExternalConfigurationModel model)
      throws Exception {
    JsonNode node = StreamAggregatorUtils.readJsonValue(document, "timeHorizons");
    if (node != null) {
      Iterator<JsonNode> timeHorizonValues = node.elements();
      while (timeHorizonValues.hasNext()) {
        String t = timeHorizonValues.next().asText();
        String timeHorizonName = null;
        int granularity = -1;

        // process parameterised time horizons
        if (t.contains("MINUTES_GROUPED")) {
          String[] items = t.split("\\(");
          timeHorizonName = items[0];
          granularity = Integer.parseInt(items[1].replaceAll("\\)", ""));
        } else {
          timeHorizonName = t;
        }

        try {
          TimeHorizon th = TimeHorizon.valueOf(timeHorizonName);

          if (th.equals(TimeHorizon.MINUTES_GROUPED) && granularity == -1) {
            throw new InvalidConfigurationException(
                "Unable to create Grouped Minutes Time Horizon without configuration of Granularity using notation MINUTES_GROUPED(<granularity in minutes>)");
          } else {
            if (th.equals(TimeHorizon.MINUTES_GROUPED)) {
              th.setGranularity(granularity);
            }
          }
          model.addTimeHorizon(th);
        } catch (Exception e) {
          throw new Exception(String.format("Unable to configure Time Horizon %s", t), e);
        }
      }
    }
  }