From def547acc8218f56f082fbe276d8d26b4617afaa Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 21 Aug 2015 16:24:07 -0700 Subject: [PATCH] GUAC-830: Move parsing/formatting into field types. --- .../guacamole/auth/jdbc/user/ModeledUser.java | 129 +++++------------- .../glyptodon/guacamole/form/DateField.java | 49 +++++++ .../guacamole/form/NumericField.java | 42 ++++++ .../glyptodon/guacamole/form/TimeField.java | 49 +++++++ .../guacamole/form/TimeZoneField.java | 23 ++++ 5 files changed, 200 insertions(+), 92 deletions(-) diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/user/ModeledUser.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/user/ModeledUser.java index 99bca4850..586686b67 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/user/ModeledUser.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/user/ModeledUser.java @@ -25,9 +25,7 @@ package org.glyptodon.guacamole.auth.jdbc.user; import com.google.inject.Inject; import java.sql.Date; import java.sql.Time; -import java.text.DateFormat; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Calendar; import java.util.Collection; @@ -268,40 +266,39 @@ public class ModeledUser extends ModeledDirectoryObject implements Us return userPermissionService.getPermissionSet(getCurrentUser(), this); } - /** - * Converts the given date into a string which follows the format used by - * date attributes. - * - * @param date - * The date value to format, which may be null. - * - * @return - * The formatted date, or null if the provided time was null. - */ - private String formatDate(Date date) { - DateFormat dateFormat = new SimpleDateFormat(DateField.FORMAT); - return date == null ? null : dateFormat.format(date); - } + @Override + public Map getAttributes() { - /** - * Converts the given time into a string which follows the format used by - * time attributes. - * - * @param time - * The time value to format, which may be null. - * - * @return - * The formatted time, or null if the provided time was null. - */ - private String formatTime(Time time) { - DateFormat timeFormat = new SimpleDateFormat(TimeField.FORMAT); - return time == null ? null : timeFormat.format(time); + Map attributes = new HashMap(); + + // Set disabled attribute + attributes.put(DISABLED_ATTRIBUTE_NAME, getModel().isDisabled() ? "true" : null); + + // Set password expired attribute + attributes.put(EXPIRED_ATTRIBUTE_NAME, getModel().isExpired() ? "true" : null); + + // Set access window start time + attributes.put(ACCESS_WINDOW_START_ATTRIBUTE_NAME, TimeField.format(getModel().getAccessWindowStart())); + + // Set access window end time + attributes.put(ACCESS_WINDOW_END_ATTRIBUTE_NAME, TimeField.format(getModel().getAccessWindowEnd())); + + // Set account validity start date + attributes.put(VALID_FROM_ATTRIBUTE_NAME, DateField.format(getModel().getValidFrom())); + + // Set account validity end date + attributes.put(VALID_UNTIL_ATTRIBUTE_NAME, DateField.format(getModel().getValidUntil())); + + // Set timezone attribute + attributes.put(TIMEZONE_ATTRIBUTE_NAME, getModel().getTimeZone()); + + return attributes; } /** * Parses the given string into a corresponding date. The string must * follow the standard format used by date attributes, as defined by - * DATE_FORMAT and as would be produced by formatDate(). + * DateField.FORMAT and as would be produced by DateField.format(). * * @param dateString * The date string to parse, which may be null. @@ -318,19 +315,19 @@ public class ModeledUser extends ModeledDirectoryObject implements Us throws ParseException { // Return null if no date provided - if (dateString == null || dateString.isEmpty()) + java.util.Date parsedDate = DateField.parse(dateString); + if (parsedDate == null) return null; - // Parse date according to format - DateFormat dateFormat = new SimpleDateFormat(DateField.FORMAT); - return new Date(dateFormat.parse(dateString).getTime()); + // Convert to SQL Date + return new Date(parsedDate.getTime()); } /** * Parses the given string into a corresponding time. The string must * follow the standard format used by time attributes, as defined by - * TIME_FORMAT and as would be produced by formatTime(). + * TimeField.FORMAT and as would be produced by TimeField.format(). * * @param timeString * The time string to parse, which may be null. @@ -347,67 +344,15 @@ public class ModeledUser extends ModeledDirectoryObject implements Us throws ParseException { // Return null if no time provided - if (timeString == null || timeString.isEmpty()) + java.util.Date parsedDate = TimeField.parse(timeString); + if (parsedDate == null) return null; - // Parse time according to format - DateFormat timeFormat = new SimpleDateFormat(TimeField.FORMAT); - return new Time(timeFormat.parse(timeString).getTime()); + // Convert to SQL Time + return new Time(parsedDate.getTime()); } - /** - * Parses the given string into a time zone ID string. As these strings are - * equivalent, the only transformation currently performed by this function - * is to ensure that a blank time zone string is parsed into null. - * - * @param timeZone - * The time zone string to parse, which may be null. - * - * @return - * The ID of the time zone corresponding to the given string, or null - * if the given time zone string was null or blank. - */ - private String parseTimeZone(String timeZone) { - - // Return null if no time zone provided - if (timeZone == null || timeZone.isEmpty()) - return null; - - // Otherwise, assume time zone is valid - return timeZone; - - } - - @Override - public Map getAttributes() { - - Map attributes = new HashMap(); - - // Set disabled attribute - attributes.put(DISABLED_ATTRIBUTE_NAME, getModel().isDisabled() ? "true" : null); - - // Set password expired attribute - attributes.put(EXPIRED_ATTRIBUTE_NAME, getModel().isExpired() ? "true" : null); - - // Set access window start time - attributes.put(ACCESS_WINDOW_START_ATTRIBUTE_NAME, formatTime(getModel().getAccessWindowStart())); - - // Set access window end time - attributes.put(ACCESS_WINDOW_END_ATTRIBUTE_NAME, formatTime(getModel().getAccessWindowEnd())); - - // Set account validity start date - attributes.put(VALID_FROM_ATTRIBUTE_NAME, formatDate(getModel().getValidFrom())); - - // Set account validity end date - attributes.put(VALID_UNTIL_ATTRIBUTE_NAME, formatDate(getModel().getValidUntil())); - - // Set timezone attribute - attributes.put(TIMEZONE_ATTRIBUTE_NAME, getModel().getTimeZone()); - - return attributes; - } - @Override public void setAttributes(Map attributes) { @@ -446,7 +391,7 @@ public class ModeledUser extends ModeledDirectoryObject implements Us } // Translate timezone attribute - getModel().setTimeZone(parseTimeZone(attributes.get(TIMEZONE_ATTRIBUTE_NAME))); + getModel().setTimeZone(TimeZoneField.parse(attributes.get(TIMEZONE_ATTRIBUTE_NAME))); } diff --git a/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/DateField.java b/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/DateField.java index d27020bdb..20ff2575c 100644 --- a/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/DateField.java +++ b/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/DateField.java @@ -22,6 +22,11 @@ package org.glyptodon.guacamole.form; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + /** * Represents a date field. The field may contain only date values which * conform to a standard pattern, defined by DateField.FORMAT. @@ -45,4 +50,48 @@ public class DateField extends Field { super(name, Field.Type.DATE); } + /** + * Converts the given date into a string which follows the format used by + * date fields. + * + * @param date + * The date value to format, which may be null. + * + * @return + * The formatted date, or null if the provided time was null. + */ + public static String format(Date date) { + DateFormat dateFormat = new SimpleDateFormat(DateField.FORMAT); + return date == null ? null : dateFormat.format(date); + } + + /** + * Parses the given string into a corresponding date. The string must + * follow the standard format used by date fields, as defined by FORMAT + * and as would be produced by format(). + * + * @param dateString + * The date string to parse, which may be null. + * + * @return + * The date corresponding to the given date string, or null if the + * provided date string was null or blank. + * + * @throws ParseException + * If the given date string does not conform to the standard format + * used by date fields. + */ + public static Date parse(String dateString) + throws ParseException { + + // Return null if no date provided + if (dateString == null || dateString.isEmpty()) + return null; + + // Parse date according to format + DateFormat dateFormat = new SimpleDateFormat(DateField.FORMAT); + return dateFormat.parse(dateString); + + } + } diff --git a/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/NumericField.java b/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/NumericField.java index 5e06cb33e..360048e3f 100644 --- a/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/NumericField.java +++ b/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/NumericField.java @@ -39,4 +39,46 @@ public class NumericField extends Field { super(name, Field.Type.NUMERIC); } + /** + * Formats the given integer in the format required by a numeric field. + * + * @param i + * The integer to format, which may be null. + * + * @return + * A string representation of the given integer, or null if the given + * integer was null. + */ + public static String format(Integer i) { + + if (i == null) + return null; + + return i.toString(); + + } + + /** + * Parses the given string as an integer, where the given string is in the + * format required by a numeric field. + * + * @param str + * The string to parse as an integer, which may be null. + * + * @return + * The integer representation of the given string, or null if the given + * string was null. + * + * @throws NumberFormatException + * If the given string is not in a parseable format. + */ + public static Integer parse(String str) throws NumberFormatException { + + if (str == null || str.isEmpty()) + return null; + + return new Integer(str); + + } + } diff --git a/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/TimeField.java b/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/TimeField.java index 15891deb2..3d7ee7d48 100644 --- a/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/TimeField.java +++ b/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/TimeField.java @@ -22,6 +22,11 @@ package org.glyptodon.guacamole.form; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + /** * Represents a time field. The field may contain only time values which * conform to a standard pattern, defined by TimeField.FORMAT. @@ -45,4 +50,48 @@ public class TimeField extends Field { super(name, Field.Type.TIME); } + /** + * Parses the given string into a corresponding time. The string must + * follow the standard format used by time fields, as defined by + * FORMAT and as would be produced by format(). + * + * @param timeString + * The time string to parse, which may be null. + * + * @return + * The time corresponding to the given time string, or null if the + * provided time string was null or blank. + * + * @throws ParseException + * If the given time string does not conform to the standard format + * used by time fields. + */ + public static Date parse(String timeString) + throws ParseException { + + // Return null if no time provided + if (timeString == null || timeString.isEmpty()) + return null; + + // Parse time according to format + DateFormat timeFormat = new SimpleDateFormat(TimeField.FORMAT); + return timeFormat.parse(timeString); + + } + + /** + * Converts the given time into a string which follows the format used by + * time fields. + * + * @param time + * The time value to format, which may be null. + * + * @return + * The formatted time, or null if the provided time was null. + */ + public static String format(Date time) { + DateFormat timeFormat = new SimpleDateFormat(TimeField.FORMAT); + return time == null ? null : timeFormat.format(time); + } + } diff --git a/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/TimeZoneField.java b/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/TimeZoneField.java index 5d07e918e..09a5e642e 100644 --- a/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/TimeZoneField.java +++ b/guacamole-ext/src/main/java/org/glyptodon/guacamole/form/TimeZoneField.java @@ -40,4 +40,27 @@ public class TimeZoneField extends Field { super(name, Field.Type.TIMEZONE); } + /** + * Parses the given string into a time zone ID string. As these strings are + * equivalent, the only transformation currently performed by this function + * is to ensure that a blank time zone string is parsed into null. + * + * @param timeZone + * The time zone string to parse, which may be null. + * + * @return + * The ID of the time zone corresponding to the given string, or null + * if the given time zone string was null or blank. + */ + public static String parse(String timeZone) { + + // Return null if no time zone provided + if (timeZone == null || timeZone.isEmpty()) + return null; + + // Otherwise, assume time zone is valid + return timeZone; + + } + }