From b14ec9a9282cb49951b790ce1b48b1a078616926 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sun, 15 Oct 2017 19:00:56 -0700 Subject: Refactor Chapter Ordering Logic The bug impacting the CCB integration was due to the "Introduction" chapter having a higher "score" than every other chapter. It was a mistake to use Score to compared chapter progress, particularly since there are more chapters than scores. This change gathers the chapter ordering logic, which was scattered throughout the code into a new Chapters enum. Playlist and Chapter now use Chapters as a key, instead of loose strings. Same for the ProgressReporter interface. --- .../java/com/p4square/grow/model/Chapters.java | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/com/p4square/grow/model/Chapters.java (limited to 'src/main/java/com/p4square/grow/model/Chapters.java') diff --git a/src/main/java/com/p4square/grow/model/Chapters.java b/src/main/java/com/p4square/grow/model/Chapters.java new file mode 100644 index 0000000..175b6fb --- /dev/null +++ b/src/main/java/com/p4square/grow/model/Chapters.java @@ -0,0 +1,46 @@ +package com.p4square.grow.model; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +import java.util.Optional; + +/** + * The chapters of the training section. + */ +public enum Chapters { + INTRODUCTION, + SEEKER, + BELIEVER, + DISCIPLE, + TEACHER, + LEADER; + + /** + * A case-insensitive version of Chapters.valueOf(). + */ + @JsonCreator + public static Chapters fromString(String s) { + return valueOf(s.toUpperCase()); + } + + @JsonValue + public String identifier() { + return toString().toLowerCase(); + } + + /** + * Convert the Chapter to a score, if possible. + */ + public Optional toScore() { + switch (this) { + case SEEKER: + case BELIEVER: + case DISCIPLE: + case TEACHER: + return Optional.of(Score.numericScore(this.toString())); + default: + return Optional.empty(); + } + } +} -- cgit v1.2.3