Repository: corona-warn-app/cwa-server
File: services/distribution/src/main/java/app/coronawarn/server/services/distribution/statistics/StatisticsToProtobufMapping.java (Java)

Code before:
  }


  /**
   * Create protobuf statistic object from raw JSON statistics.
   *
   * @return Statistics protobuf statistics object.
   */
  @Bean
  public Statistics constructProtobufStatistics() {
    try {
      String content = this.jsonFileLoader.getContent();
      List<StatisticsJsonStringObject> jsonStringObjects = SerializationUtils
          .deserializeJson(content, typeFactory -> typeFactory
              .constructCollectionType(List.class, StatisticsJsonStringObject.class));

      StatisticsJsonValidator validator = new StatisticsJsonValidator();
      jsonStringObjects = new ArrayList<>(validator.validate(jsonStringObjects));

      return Statistics.newBuilder()
          .addAllCardIdSequence(getAllCardIdSequence())
          .addAllKeyFigureCards(buildAllKeyFigureCards(jsonStringObjects))
          .build();
    } catch (BucketNotFoundException | ConnectionException | FilePathNotFoundException ex) {
      logger.error(ex.getMessage());
      logger.error("Statistics file will not be generated due to previous errors!");
      return Statistics.newBuilder().build();
    }
  }

  private List<Integer> getAllCardIdSequence() {
    String[] idSequenceArray = distributionServiceConfig.getCardIdSequence()
        .replace("[", "").replace("]", "")
        .split(",");
    List<Integer> idIntegerSequence = new ArrayList<>();
    for (String id : idSequenceArray) {
      idIntegerSequence.add(Integer.parseInt(id));
    }
    return idIntegerSequence;
  }

  private LocalDate effectiveDateStringToLocalDate(String effectiveDate) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    return LocalDate.parse(effectiveDate, formatter);
  }

  @SuppressWarnings("checkstyle:Indentation")
  private List<KeyFigureCard> buildAllKeyFigureCards(List<StatisticsJsonStringObject> jsonStringObjects) {
    Map<Integer, Optional<KeyFigureCard>> figureCardMap = new HashMap<>();

    figureCardMap.put(INFECTIONS_CARD_ID, Optional.empty());

Review comment (suggestion):
I would prefer to keep it in one single line... loggers behave from time to time differently and not all lines are kept always together - don't mentioning log analyzer tools...

```suggestion
      logger.error("{} - Statistics file not generated!", ex.getMessage());
```
or
```suggestion
      logger.error("Statistics file not generated!", ex);
```

Code after:
  }


  /**
   * Create protobuf statistic object from raw JSON statistics.
   *
   * @return Statistics protobuf statistics object.
   */
  @Bean
  public Statistics constructProtobufStatistics() {
    try {
      String content = this.jsonFileLoader.getContent();
      List<StatisticsJsonStringObject> jsonStringObjects = SerializationUtils
          .deserializeJson(content, typeFactory -> typeFactory
              .constructCollectionType(List.class, StatisticsJsonStringObject.class));

      StatisticsJsonValidator validator = new StatisticsJsonValidator();
      jsonStringObjects = new ArrayList<>(validator.validate(jsonStringObjects));

      return Statistics.newBuilder()
          .addAllCardIdSequence(getAllCardIdSequence())
          .addAllKeyFigureCards(buildAllKeyFigureCards(jsonStringObjects))
          .build();
    } catch (BucketNotFoundException | ConnectionException | FilePathNotFoundException ex) {
      logger.error("Statistics file not generated!", ex);
      return Statistics.newBuilder().build();
    }
  }

  private List<Integer> getAllCardIdSequence() {
    String[] idSequenceArray = distributionServiceConfig.getCardIdSequence()
        .replace("[", "").replace("]", "")
        .split(",");
    List<Integer> idIntegerSequence = new ArrayList<>();
    for (String id : idSequenceArray) {
      idIntegerSequence.add(Integer.parseInt(id));
    }
    return idIntegerSequence;
  }

  private LocalDate effectiveDateStringToLocalDate(String effectiveDate) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    return LocalDate.parse(effectiveDate, formatter);
  }

  @SuppressWarnings("checkstyle:Indentation")
  private List<KeyFigureCard> buildAllKeyFigureCards(List<StatisticsJsonStringObject> jsonStringObjects) {
    Map<Integer, Optional<KeyFigureCard>> figureCardMap = new HashMap<>();

    figureCardMap.put(INFECTIONS_CARD_ID, Optional.empty());
    figureCardMap.put(INCIDENCE_CARD_ID, Optional.empty());