From 34513e1fd739896e7151cb5ec18915fb881f5e46 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Wed, 21 Sep 2016 21:55:59 -0700 Subject: Refactoring AbstractFormSerializer Refactoring AbstractFormSerializer to separate the form building logic into a standalone FormBuilder class. --- .../ccbapi/serializer/AbstractFormSerializer.java | 14 +++ .../ccbapi/serializer/AddressFormSerializer.java | 9 +- .../p4square/ccbapi/serializer/FormBuilder.java | 119 +++++++++++++++++++++ .../serializer/IndividualProfileSerializer.java | 76 ++++++------- .../ccbapi/serializer/PhoneFormSerializer.java | 4 +- 5 files changed, 178 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/p4square/ccbapi/serializer/FormBuilder.java diff --git a/src/main/java/com/p4square/ccbapi/serializer/AbstractFormSerializer.java b/src/main/java/com/p4square/ccbapi/serializer/AbstractFormSerializer.java index 06a83a1..969b11d 100644 --- a/src/main/java/com/p4square/ccbapi/serializer/AbstractFormSerializer.java +++ b/src/main/java/com/p4square/ccbapi/serializer/AbstractFormSerializer.java @@ -24,6 +24,20 @@ public abstract class AbstractFormSerializer implements Serializer { return sb.toString(); } + @Override + public void encode(final T obj, final StringBuilder sb) { + FormBuilder builder = new FormBuilder(sb); + encode(obj, builder); + } + + /** + * This method performs the actual serialize of T into a URL encoded form. + * + * @param obj The object to serialize + * @param builder The FormBuilder to use. + */ + protected abstract void encode(final T obj, final FormBuilder builder); + /** * Append a field to the form. * diff --git a/src/main/java/com/p4square/ccbapi/serializer/AddressFormSerializer.java b/src/main/java/com/p4square/ccbapi/serializer/AddressFormSerializer.java index 00af004..22ed5c2 100644 --- a/src/main/java/com/p4square/ccbapi/serializer/AddressFormSerializer.java +++ b/src/main/java/com/p4square/ccbapi/serializer/AddressFormSerializer.java @@ -8,7 +8,14 @@ import java.net.URLEncoder; /** * Encode an Address object as form data for CCB. */ -public class AddressFormSerializer extends AbstractFormSerializer
{ +public class AddressFormSerializer implements Serializer
{ + + @Override + public String encode(final Address address) { + final StringBuilder sb = new StringBuilder(); + encode(address, sb); + return sb.toString(); + } @Override public void encode(final Address address, final StringBuilder builder) { diff --git a/src/main/java/com/p4square/ccbapi/serializer/FormBuilder.java b/src/main/java/com/p4square/ccbapi/serializer/FormBuilder.java new file mode 100644 index 0000000..b9f59d7 --- /dev/null +++ b/src/main/java/com/p4square/ccbapi/serializer/FormBuilder.java @@ -0,0 +1,119 @@ +package com.p4square.ccbapi.serializer; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * Utility for building URL encoded form payloads. + */ +public class FormBuilder { + + /** + * This is the datetime format specified by the CCB API Doc. + */ + private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + private final StringBuilder builder; + + /** + * Construct a new FormBuilder. + */ + public FormBuilder() { + this(new StringBuilder()); + } + + /** + * Construct a new FormBuilder which will append fields to the given StringBuilder. + * + * @param builder The StringBuilder to append to. + */ + public FormBuilder(StringBuilder builder) { + this.builder = builder; + } + + /** + * This methods provides access to the StringBuilder. + * + * @return The StringBuilder passed in at construct time. + */ + public StringBuilder getStringBuilder() { + return builder; + } + + /** + * Build the entire URL encoded form. + * + * @return All form fields as a URL encoded string. + */ + public String build() { + return builder.toString(); + } + + /** + * Append a field to the form. + * + * @param key The form key, which must be URLEncoded before calling this method. + * @param value The value associated with the key. The value will be URLEncoded by this method. + */ + public void appendField(final String key, final String value) { + if (builder.length() > 0) { + builder.append("&"); + } + + try { + builder.append(key).append("=").append(URLEncoder.encode(value, "UTF-8")); + } catch (UnsupportedEncodingException e) { + throw new AssertionError("UTF-8 encoding should always be available."); + } + } + + /** + * Append an integer field to the form. + * + * @param key The form key, which must be URLEncoded before calling this method. + * @param value The value associated with the key. + */ + public void appendField(final String key, final int value) { + if (builder.length() > 0) { + builder.append("&"); + } + builder.append(key).append("=").append(value); + } + + /** + * Append a boolean field to the form. + * + * @param key The form key, which must be URLEncoded before calling this method. + * @param value The value associated with the key. + */ + public void appendField(final String key, final boolean value) { + if (builder.length() > 0) { + builder.append("&"); + } + builder.append(key).append("=").append(value ? "true" : "false"); + } + + /** + * Append a LocalDate field to the form. + * + * @param key The form key, which must be URLEncoded before calling this method. + * @param value The value associated with the key. + */ + public void appendField(final String key, final LocalDate value) { + appendField(key, value.toString()); + + } + + /** + * Append a LocalDateTime field to the form. + * + * @param key The form key, which must be URLEncoded before calling this method. + * @param value The value associated with the key. + */ + public void appendField(final String key, final LocalDateTime value) { + appendField(key, DATE_TIME_FORMAT.format(value)); + } +} diff --git a/src/main/java/com/p4square/ccbapi/serializer/IndividualProfileSerializer.java b/src/main/java/com/p4square/ccbapi/serializer/IndividualProfileSerializer.java index 52b1a44..c9cc095 100644 --- a/src/main/java/com/p4square/ccbapi/serializer/IndividualProfileSerializer.java +++ b/src/main/java/com/p4square/ccbapi/serializer/IndividualProfileSerializer.java @@ -16,85 +16,85 @@ public class IndividualProfileSerializer extends AbstractFormSerializer entry : request.getCustomTextFields().entrySet()) { - if (entry.getValue() != null) { - appendField(builder, entry.getKey(), entry.getValue()); - } - } - for (Map.Entry entry : request.getCustomDateFields().entrySet()) { - if (entry.getValue() != null) { - appendField(builder, entry.getKey(), entry.getValue()); - } - } - for (Map.Entry entry : request.getCustomPulldownFields().entrySet()) { - if (entry.getValue() != null) { - appendField(builder, entry.getKey(), entry.getValue()); - } - } + request.getCustomTextFields().entrySet().stream() + .filter(entry -> entry.getValue() != null) + .forEach(entry -> builder.appendField(entry.getKey(), entry.getValue())); + request.getCustomDateFields().entrySet().stream() + .filter(entry -> entry.getValue() != null) + .forEach(entry -> builder.appendField(entry.getKey(), entry.getValue())); + request.getCustomPulldownFields().entrySet().stream() + .filter(entry -> entry.getValue() != null) + .forEach(entry -> builder.appendField(entry.getKey(), entry.getValue())); } } diff --git a/src/main/java/com/p4square/ccbapi/serializer/PhoneFormSerializer.java b/src/main/java/com/p4square/ccbapi/serializer/PhoneFormSerializer.java index 3569321..360e330 100644 --- a/src/main/java/com/p4square/ccbapi/serializer/PhoneFormSerializer.java +++ b/src/main/java/com/p4square/ccbapi/serializer/PhoneFormSerializer.java @@ -7,7 +7,7 @@ import com.p4square.ccbapi.model.Phone; */ public class PhoneFormSerializer extends AbstractFormSerializer { @Override - public void encode(final Phone phone, final StringBuilder builder) { + public void encode(final Phone phone, final FormBuilder builder) { // Sanity check. if (phone.getType() == null) { throw new IllegalArgumentException("Phone type cannot be null"); @@ -19,6 +19,6 @@ public class PhoneFormSerializer extends AbstractFormSerializer { } else { key = phone.getType().toString().toLowerCase() + "_phone"; } - appendField(builder, key, phone.getNumber()); + builder.appendField(key, phone.getNumber()); } } -- cgit v1.2.3