From 624e5d995c2fa593fb517a99e4b8b5f775afbeaf Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Wed, 6 Apr 2016 21:25:41 -0700 Subject: Adding getLookupTable API. --- src/main/java/com/p4square/ccbapi/CCBAPI.java | 10 ++++ .../java/com/p4square/ccbapi/CCBAPIClient.java | 14 ++++- .../com/p4square/ccbapi/model/CustomField.java | 9 ++++ .../ccbapi/model/GetLookupTableRequest.java | 27 ++++++++++ .../ccbapi/model/GetLookupTableResponse.java | 37 ++++++++++++++ .../com/p4square/ccbapi/model/LookupTableItem.java | 59 ++++++++++++++++++++++ .../com/p4square/ccbapi/model/LookupTableType.java | 47 +++++++++++++++++ .../java/com/p4square/ccbapi/CCBAPIClientTest.java | 31 ++++++++++-- .../model/ccb_lookup_table_list_response.xml | 45 +++++++++++++++++ 9 files changed, 274 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/p4square/ccbapi/model/GetLookupTableRequest.java create mode 100644 src/main/java/com/p4square/ccbapi/model/GetLookupTableResponse.java create mode 100644 src/main/java/com/p4square/ccbapi/model/LookupTableItem.java create mode 100644 src/main/java/com/p4square/ccbapi/model/LookupTableType.java create mode 100644 src/test/resources/com/p4square/ccbapi/model/ccb_lookup_table_list_response.xml (limited to 'src') diff --git a/src/main/java/com/p4square/ccbapi/CCBAPI.java b/src/main/java/com/p4square/ccbapi/CCBAPI.java index f81a02f..bae2272 100644 --- a/src/main/java/com/p4square/ccbapi/CCBAPI.java +++ b/src/main/java/com/p4square/ccbapi/CCBAPI.java @@ -17,6 +17,16 @@ public interface CCBAPI extends Closeable { */ GetCustomFieldLabelsResponse getCustomFieldLabels() throws IOException; + /** + * Retrieve the list of items in a particular lookup table. + * + * Lookup tables provide the list of options for various pulldown fields. + * + * @return A GetLookupTableResponse containing the item ids and labels. + * @throws IOException on failure. + */ + GetLookupTableResponse getLookupTable(GetLookupTableRequest request) throws IOException; + /** * Retrieve one or more IndividualProfiles. * diff --git a/src/main/java/com/p4square/ccbapi/CCBAPIClient.java b/src/main/java/com/p4square/ccbapi/CCBAPIClient.java index 404253a..e3750cf 100644 --- a/src/main/java/com/p4square/ccbapi/CCBAPIClient.java +++ b/src/main/java/com/p4square/ccbapi/CCBAPIClient.java @@ -2,9 +2,7 @@ package com.p4square.ccbapi; import com.p4square.ccbapi.exception.CCBErrorResponseException; import com.p4square.ccbapi.model.*; -import com.p4square.ccbapi.serializer.AddressFormSerializer; import com.p4square.ccbapi.serializer.IndividualProfileSerializer; -import com.p4square.ccbapi.serializer.PhoneFormSerializer; import java.io.IOException; import java.io.InputStream; @@ -133,6 +131,18 @@ public class CCBAPIClient implements CCBAPI { return makeRequest("custom_field_labels", EMPTY_MAP, null, GetCustomFieldLabelsResponse.class); } + @Override + public GetLookupTableResponse getLookupTable(final GetLookupTableRequest request) throws IOException { + + if (request.getType() == null) { + throw new IllegalArgumentException("LookupTableType must not be null."); + } + + final String service = request.getType().getIdentifier() + "_list"; + + return makeRequest(service, EMPTY_MAP, null, GetLookupTableResponse.class); + } + @Override public UpdateIndividualProfileResponse updateIndividualProfile(UpdateIndividualProfileRequest request) throws IOException { diff --git a/src/main/java/com/p4square/ccbapi/model/CustomField.java b/src/main/java/com/p4square/ccbapi/model/CustomField.java index c9917e4..5b52bb3 100644 --- a/src/main/java/com/p4square/ccbapi/model/CustomField.java +++ b/src/main/java/com/p4square/ccbapi/model/CustomField.java @@ -71,4 +71,13 @@ public class CustomField { public void setAdminOnly(boolean adminOnly) { this.adminOnly = adminOnly; } + + @Override + public String toString() { + return "CustomField::{" + + "name='" + name + '\'' + + ", label='" + label + '\'' + + ", adminOnly=" + adminOnly + + '}'; + } } diff --git a/src/main/java/com/p4square/ccbapi/model/GetLookupTableRequest.java b/src/main/java/com/p4square/ccbapi/model/GetLookupTableRequest.java new file mode 100644 index 0000000..4da877e --- /dev/null +++ b/src/main/java/com/p4square/ccbapi/model/GetLookupTableRequest.java @@ -0,0 +1,27 @@ +package com.p4square.ccbapi.model; + +/** + * GetLookupTableRequest contains the information necessary to get a list of lookup table values. + */ +public class GetLookupTableRequest { + + private LookupTableType type; + + public LookupTableType getType() { + return type; + } + + /** + * Specify the type of lookup table to query. + * + * @param type A non-null lookup table type. + * @return this + */ + public GetLookupTableRequest withType(final LookupTableType type) { + if (type == null) { + throw new IllegalArgumentException("type may not be null."); + } + this.type = type; + return this; + } +} diff --git a/src/main/java/com/p4square/ccbapi/model/GetLookupTableResponse.java b/src/main/java/com/p4square/ccbapi/model/GetLookupTableResponse.java new file mode 100644 index 0000000..e1feb1e --- /dev/null +++ b/src/main/java/com/p4square/ccbapi/model/GetLookupTableResponse.java @@ -0,0 +1,37 @@ +package com.p4square.ccbapi.model; + +import javax.xml.bind.annotation.*; +import java.util.ArrayList; +import java.util.List; + +/** + * A GetLookupTableResponse contains a list of lookup table values. + */ +@XmlRootElement(name="response") +@XmlAccessorType(XmlAccessType.NONE) +public class GetLookupTableResponse extends CCBAPIResponse { + + @XmlElementWrapper(name = "items") + @XmlElement(name="item") + private List items; + + public GetLookupTableResponse() { + items = new ArrayList<>(); + } + + /** + * @return The list of items in the lookup table. + */ + public List getItems() { + return items; + } + + /** + * Set the list of lookup table items. + * + * @param items The list of items in the lookup table. + */ + public void setItems(final List items) { + this.items = items; + } +} \ No newline at end of file diff --git a/src/main/java/com/p4square/ccbapi/model/LookupTableItem.java b/src/main/java/com/p4square/ccbapi/model/LookupTableItem.java new file mode 100644 index 0000000..07b39eb --- /dev/null +++ b/src/main/java/com/p4square/ccbapi/model/LookupTableItem.java @@ -0,0 +1,59 @@ +package com.p4square.ccbapi.model; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * LookupTableItem is the definition of an item in a pulldown list. + */ +@XmlRootElement(name="item") +@XmlAccessorType(XmlAccessType.NONE) +public class LookupTableItem { + + @XmlElement + private int id; + + @XmlElement + private int order; + + @XmlElement + private String name; + + public int getId() { + return id; + } + + public LookupTableItem setId(final int id) { + this.id = id; + return this; + } + + public int getOrder() { + return order; + } + + public LookupTableItem setOrder(final int order) { + this.order = order; + return this; + } + + public String getName() { + return name; + } + + public LookupTableItem setName(final String name) { + this.name = name; + return this; + } + + @Override + public String toString() { + return "LookupTableItem::{" + + "id=" + id + + ", order=" + order + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/src/main/java/com/p4square/ccbapi/model/LookupTableType.java b/src/main/java/com/p4square/ccbapi/model/LookupTableType.java new file mode 100644 index 0000000..e80abde --- /dev/null +++ b/src/main/java/com/p4square/ccbapi/model/LookupTableType.java @@ -0,0 +1,47 @@ +package com.p4square.ccbapi.model; + +/** + * A collection of lookup table types supported by CCB. + */ +public enum LookupTableType { + ABILITY, + ACTIVITY, + AGE_BRACKET, + AREA, + CHURCH_SERVICE, + DEPARTMENT, + EVENT_GROUPING, + GIFT, + GROUP_GROUPING, + GROUP_TYPE, + HOW_JOINED_CHURCH, + HOW_THEY_HEARD, + MEET_DAY, + MEET_TIME, + MEMBERSHIP_TYPE, + PASSION, + REASON_LEFT_CHURCH, + SCHOOL, + SCHOOL_GRADE, + SIGNIFICANT_EVENT, + SPIRITUAL_MATURITY, + STYLE, + TRANSACTION_GROUPING, + UDF_GRP_PULLDOWN_1, + UDF_GRP_PULLDOWN_2, + UDF_GRP_PULLDOWN_3, + UDF_IND_PULLDOWN_1, + UDF_IND_PULLDOWN_2, + UDF_IND_PULLDOWN_3, + UDF_IND_PULLDOWN_4, + UDF_IND_PULLDOWN_5, + UDF_IND_PULLDOWN_6, + UDF_RESOURCE_PULLDOWN_1; + + /** + * @return the identifier used by the CCB API. + */ + public String getIdentifier() { + return toString().toLowerCase(); + } +} diff --git a/src/test/java/com/p4square/ccbapi/CCBAPIClientTest.java b/src/test/java/com/p4square/ccbapi/CCBAPIClientTest.java index 69f50c2..b7d32e1 100644 --- a/src/test/java/com/p4square/ccbapi/CCBAPIClientTest.java +++ b/src/test/java/com/p4square/ccbapi/CCBAPIClientTest.java @@ -9,12 +9,9 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.time.LocalDate; -import java.util.Collections; -import java.util.Map; import org.easymock.EasyMock; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.junit.Assert.*; @@ -110,6 +107,34 @@ public class CCBAPIClientTest { client.getCustomFieldLabels(); } + @Test + public void testGetLookupTableListSuccess() throws Exception { + // Set expectation. + URI expectedURI = new URI("https://localhost:8080/api.php?srv=significant_event_list"); + InputStream is = getClass().getResourceAsStream("model/ccb_lookup_table_list_response.xml"); + EasyMock.expect(mockHttpClient.sendPostRequest(expectedURI, null)) + .andReturn(is); + EasyMock.replay(mockHttpClient); + + // Test significant_event_list. + GetLookupTableResponse response = client.getLookupTable( + new GetLookupTableRequest().withType(LookupTableType.SIGNIFICANT_EVENT)); + + // Verify results. + EasyMock.verify(mockHttpClient); + assertNull(response.getErrors()); + assertEquals(5, response.getItems().size()); + assertEquals(1, response.getItems().get(0).getId()); + assertEquals(1, response.getItems().get(0).getOrder()); + assertEquals("High School Graduation", response.getItems().get(0).getName()); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetLookupTableListWithNullType() throws Exception { + // This should throw an IllegalArgumentException. + client.getLookupTable(new GetLookupTableRequest()); + } + @Test public void testGetIndividualProfilesById() throws Exception { // Set expectation. diff --git a/src/test/resources/com/p4square/ccbapi/model/ccb_lookup_table_list_response.xml b/src/test/resources/com/p4square/ccbapi/model/ccb_lookup_table_list_response.xml new file mode 100644 index 0000000..632b02c --- /dev/null +++ b/src/test/resources/com/p4square/ccbapi/model/ccb_lookup_table_list_response.xml @@ -0,0 +1,45 @@ + + + + + + + + + significant_event_list + execute + public + + + 1 + 1 + High School Graduation + 1 + + + 2 + 2 + College Graduation + 2 + + + 3 + 3 + Moved Into Town + 3 + + + 4 + 4 + Moved Out of Town + 4 + + + 5 + 5 + Dedicated at Baby Dedication + 5 + + + + \ No newline at end of file -- cgit v1.2.3