From b145e757c01f57b7c30071c68075f4895a30cd91 Mon Sep 17 00:00:00 2001 From: Jym Dyer Date: Thu, 30 Nov 2023 11:59:39 -0800 Subject: [PATCH] OTP-888 Refactor mobilityProfile into its own object --- .../controllers/api/OtpUserController.java | 61 +++++++++++-------- .../middleware/models/MobilityProfile.java | 31 ++++++++++ .../middleware/models/OtpUser.java | 14 +---- .../latest-spark-swagger-output.yaml | 29 ++++++--- 4 files changed, 87 insertions(+), 48 deletions(-) create mode 100644 src/main/java/org/opentripplanner/middleware/models/MobilityProfile.java diff --git a/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java b/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java index 230190c05..90c0f8447 100644 --- a/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java +++ b/src/main/java/org/opentripplanner/middleware/controllers/api/OtpUserController.java @@ -7,6 +7,7 @@ import org.eclipse.jetty.http.HttpStatus; import org.opentripplanner.middleware.auth.Auth0Connection; import org.opentripplanner.middleware.auth.RequestingUser; +import org.opentripplanner.middleware.models.MobilityProfile; import org.opentripplanner.middleware.models.OtpUser; import org.opentripplanner.middleware.persistence.Persistence; import org.opentripplanner.middleware.utils.JsonUtils; @@ -55,13 +56,19 @@ OtpUser preCreateHook(OtpUser user, Request req) { Auth0Connection.ensureApiUserHasApiKey(req); user.applicationId = requestingUser.apiUser.id; } - user.mobilityMode = calculateMobilityMode(user); + if (user.mobilityProfile == null) { + user.mobilityProfile = new MobilityProfile(); + } + user.mobilityProfile.mobilityMode = calculateMobilityMode(user); return super.preCreateHook(user, req); } @Override OtpUser preUpdateHook(OtpUser user, OtpUser preExistingUser, Request req) { - user.mobilityMode = calculateMobilityMode(user); + if (user.mobilityProfile == null) { + user.mobilityProfile = new MobilityProfile(); + } + user.mobilityProfile.mobilityMode = calculateMobilityMode(user); return super.preUpdateHook(user, preExistingUser, req); } @@ -184,8 +191,8 @@ public static boolean isPhoneNumberValidE164(String phoneNumber) { /** * Calculate and return the "mobility mode", a keyword or compound keyword specified by Georgia Tech, - * based on a number {@code OtpUser} fields related to mobility. - * @param user with fields that are consulted to calculate mobility mode + * based on a number of {@code OtpUser} moblity profile fields. + * @param user whose mobility profile is consulted to calculate mobility mode * @return mobility mode as a single string */ private static String calculateMobilityMode(OtpUser user) { @@ -195,42 +202,44 @@ private static String calculateMobilityMode(OtpUser user) { String mModeTemp = "None"; String visionTemp = "None"; - if (user.mobilityDevices == null) { - user.mobilityDevices = Collections.EMPTY_LIST; - } - if (user.mobilityDevices.isEmpty() || user.mobilityDevices.contains("none")) { - user.mobilityDevices.clear(); - } else { - if (user.mobilityDevices.contains("white cane")) { + if (user.mobilityProfile.mobilityDevices == null) { + user.mobilityProfile.mobilityDevices = Collections.EMPTY_LIST; + } + if (user.mobilityProfile.mobilityDevices.isEmpty() || user.mobilityProfile.mobilityDevices.contains("none")) { + user.mobilityProfile.mobilityDevices.clear(); + } else { + if (user.mobilityProfile.mobilityDevices.contains("white cane")) { visionTemp = "Blind"; } - if (user.mobilityDevices.contains("manual walker") - || user.mobilityDevices.contains("wheeled walker") - || user.mobilityDevices.contains("cane") - || user.mobilityDevices.contains("crutches") - || user.mobilityDevices.contains("stroller") - || user.mobilityDevices.contains("service animal")) { + if (user.mobilityProfile.mobilityDevices.contains("manual walker") + || user.mobilityProfile.mobilityDevices.contains("wheeled walker") + || user.mobilityProfile.mobilityDevices.contains("cane") + || user.mobilityProfile.mobilityDevices.contains("crutches") + || user.mobilityProfile.mobilityDevices.contains("stroller") + || user.mobilityProfile.mobilityDevices.contains("service animal")) { mModeTemp = "Device"; } - if (user.mobilityDevices.contains("mobility scooter")) { + if (user.mobilityProfile.mobilityDevices.contains("mobility scooter")) { mModeTemp = "MScooter"; } - if (user.mobilityDevices.contains("electric wheelchair")) { + if (user.mobilityProfile.mobilityDevices.contains("electric wheelchair")) { mModeTemp = "WChairE"; } - if (user.mobilityDevices.contains("manual wheelchair")) { + if (user.mobilityProfile.mobilityDevices.contains("manual wheelchair")) { mModeTemp = "WChairM"; } - if ("None".equals(mModeTemp) && user.isMobilityLimited) { + if ("None".equals(mModeTemp) && user.mobilityProfile.isMobilityLimited) { mModeTemp = "Some"; } } - if (visionTemp.isEmpty() && "low-vision".equals(user.visionLimitation)) { - visionTemp = "LowVision"; - } else if (visionTemp.isEmpty() && "legally blind".equals(user.visionLimitation)) { - visionTemp = "Blind"; + if (visionTemp.isEmpty()) { + if (MobilityProfile.VisionLimitation.LOW_VISION == user.mobilityProfile.visionLimitation) { + visionTemp = "LowVision"; + } else if (MobilityProfile.VisionLimitation.LEGALLY_BLIND == user.mobilityProfile.visionLimitation) { + visionTemp = "Blind"; + } } // Create combinations for mobility mode and vision @@ -239,7 +248,7 @@ private static String calculateMobilityMode(OtpUser user) { return visionTemp; } else if (MOBILITY_DEVICES.contains(mModeTemp)) { return mModeTemp + "-" + visionTemp; - } + } } else if (MOBILITY_DEVICES.contains(mModeTemp)) { return mModeTemp; } diff --git a/src/main/java/org/opentripplanner/middleware/models/MobilityProfile.java b/src/main/java/org/opentripplanner/middleware/models/MobilityProfile.java new file mode 100644 index 000000000..0f2215d2d --- /dev/null +++ b/src/main/java/org/opentripplanner/middleware/models/MobilityProfile.java @@ -0,0 +1,31 @@ +package org.opentripplanner.middleware.models; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.io.Serializable; +import java.util.Collection; + +/** + * Mobility profile data + */ +public class MobilityProfile implements Serializable { + public enum VisionLimitation { + @JsonProperty("legally blind") LEGALLY_BLIND, + @JsonProperty("low-vision") LOW_VISION, + @JsonProperty("none") NONE + } + + /** Whether the user indicates that their mobility is limited (slower). */ + public boolean isMobilityLimited; + + /** User may indicate zero or more mobility devices. */ + public Collection mobilityDevices; + + /** Compound keyword that controller calculates from mobility and vision values. */ + @JsonIgnore + public String mobilityMode; + + /** User may indicate levels of vision limitation. */ + public VisionLimitation visionLimitation; +} diff --git a/src/main/java/org/opentripplanner/middleware/models/OtpUser.java b/src/main/java/org/opentripplanner/middleware/models/OtpUser.java index 68d95626b..937fe0fa5 100644 --- a/src/main/java/org/opentripplanner/middleware/models/OtpUser.java +++ b/src/main/java/org/opentripplanner/middleware/models/OtpUser.java @@ -33,21 +33,11 @@ public enum Notification { /** Whether the user has consented to terms of use. */ public boolean hasConsentedToTerms; - /** Whether the user has indicated that their mobility is limited (slower). */ - public boolean isMobilityLimited; - /** Whether the phone number has been verified. */ public boolean isPhoneNumberVerified; - /** User may have indicated zero or more mobility devices. */ - public Collection mobilityDevices; - - /** Compound keyword that controller calculates from mobility and vision values. */ - @JsonIgnore - public String mobilityMode; - - /** One of "low-vision" "legally blind" "none" */ - public String visionLimitation; + /** Mobilty profile. */ + public MobilityProfile mobilityProfile; /** * Notification preferences for this user diff --git a/src/main/resources/latest-spark-swagger-output.yaml b/src/main/resources/latest-spark-swagger-output.yaml index c92ec144a..29d94657d 100644 --- a/src/main/resources/latest-spark-swagger-output.yaml +++ b/src/main/resources/latest-spark-swagger-output.yaml @@ -2550,18 +2550,10 @@ definitions: type: "boolean" hasConsentedToTerms: type: "boolean" - isMobilityLimited: - type: "boolean" isPhoneNumberVerified: type: "boolean" - mobilityDevices: - type: "array" - items: - type: "string" - mobilityMode: - type: "string" - visionLimitation: - type: "string" + mobilityProfile: + $ref: "#/definitions/MobilityProfile" notificationChannel: type: "array" items: @@ -2588,6 +2580,23 @@ definitions: type: "boolean" applicationId: type: "string" + MobilityProfile: + type: "object" + properties: + isMobilityLimited: + type: "boolean" + mobilityDevices: + type: "array" + items: + type: "string" + mobilityMode: + type: "string" + visionLimitation: + type: "string" + enum: + - "LEGALLY_BLIND" + - "LOW_VISION" + - "NONE" GetUsageResult: type: "object" properties: