From 918e806c83f812721f0e9527fbc3e3e67d071580 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Thu, 7 Apr 2016 21:25:12 -0700 Subject: Adding CustomFieldCache for CCB User Defined Fields. --- src/com/p4square/grow/ccb/CustomFieldCache.java | 126 ++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/com/p4square/grow/ccb/CustomFieldCache.java (limited to 'src/com/p4square/grow/ccb/CustomFieldCache.java') diff --git a/src/com/p4square/grow/ccb/CustomFieldCache.java b/src/com/p4square/grow/ccb/CustomFieldCache.java new file mode 100644 index 0000000..ba473fb --- /dev/null +++ b/src/com/p4square/grow/ccb/CustomFieldCache.java @@ -0,0 +1,126 @@ +package com.p4square.grow.ccb; + +import com.p4square.ccbapi.CCBAPI; +import com.p4square.ccbapi.model.*; +import org.apache.log4j.Logger; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * CustomFieldCache maintains an index from custom field labels to names. + */ +public class CustomFieldCache { + + private static final Logger LOG = Logger.getLogger(CustomFieldCache.class); + + private final CCBAPI mAPI; + + private CustomFieldCollection mTextFields; + private CustomFieldCollection mDateFields; + private CustomFieldCollection mIndividualPulldownFields; + private CustomFieldCollection mGroupPulldownFields; + + private final Map> mItemByNameTable; + + public CustomFieldCache(final CCBAPI api) { + mAPI = api; + mTextFields = new CustomFieldCollection<>(); + mDateFields = new CustomFieldCollection<>(); + mIndividualPulldownFields = new CustomFieldCollection<>(); + mGroupPulldownFields = new CustomFieldCollection<>(); + mItemByNameTable = new HashMap<>(); + } + + public CustomField getTextFieldByLabel(final String label) { + if (mTextFields.size() == 0) { + refresh(); + } + return mTextFields.getByLabel(label); + } + + public CustomField getDateFieldByLabel(final String label) { + if (mDateFields.size() == 0) { + refresh(); + } + return mDateFields.getByLabel(label); + } + + public CustomField getIndividualPulldownByLabel(final String label) { + if (mIndividualPulldownFields.size() == 0) { + refresh(); + } + return mIndividualPulldownFields.getByLabel(label); + } + + public CustomField getGroupPulldownByLabel(final String label) { + if (mGroupPulldownFields.size() == 0) { + refresh(); + } + return mGroupPulldownFields.getByLabel(label); + } + + public LookupTableItem getPulldownItemByName(final LookupTableType type, final String name) { + Map items = mItemByNameTable.get(type); + if (items == null) { + if (!cacheLookupTable(type)) { + return null; + } + items = mItemByNameTable.get(type); + } + + return items.get(name); + } + + private synchronized void refresh() { + try { + // Get all of the custom fields. + final GetCustomFieldLabelsResponse resp = mAPI.getCustomFieldLabels(); + + final CustomFieldCollection newTextFields = new CustomFieldCollection<>(); + final CustomFieldCollection newDateFields = new CustomFieldCollection<>(); + final CustomFieldCollection newIndPulldownFields = new CustomFieldCollection<>(); + final CustomFieldCollection newGrpPulldownFields = new CustomFieldCollection<>(); + + for (final CustomField field : resp.getCustomFields()) { + if (field.getName().startsWith("udf_ind_text_")) { + newTextFields.add(field); + } else if (field.getName().startsWith("udf_ind_date_")) { + newDateFields.add(field); + } else if (field.getName().startsWith("udf_ind_pulldown_")) { + newIndPulldownFields.add(field); + } else if (field.getName().startsWith("udf_grp_pulldown_")) { + newGrpPulldownFields.add(field); + } else { + LOG.warn("Unknown custom field type " + field.getName()); + } + } + + this.mTextFields = newTextFields; + this.mDateFields = newDateFields; + this.mIndividualPulldownFields = newIndPulldownFields; + this.mGroupPulldownFields = newGrpPulldownFields; + + } catch (IOException e) { + // Error fetching labels. + LOG.error("Error fetching custom fields: " + e.getMessage(), e); + } + } + + private synchronized boolean cacheLookupTable(final LookupTableType type) { + try { + final GetLookupTableResponse resp = mAPI.getLookupTable(new GetLookupTableRequest().withType(type)); + mItemByNameTable.put(type, + resp.getItems().stream().collect(Collectors.toMap(LookupTableItem::getName, Function.identity()))); + return true; + + } catch (IOException e) { + LOG.error("Exception caching lookup table of type " + type, e); + } + + return false; + } +} -- cgit v1.2.3 From 10c5fd17b603f125ae2c0ef14b1a65341dbdf961 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sat, 9 Apr 2016 09:48:56 -0700 Subject: CustomFieldCache is now case-insensitive. --- src/com/p4square/grow/ccb/CustomFieldCache.java | 6 +++--- tst/com/p4square/grow/ccb/CustomFieldCacheTest.java | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'src/com/p4square/grow/ccb/CustomFieldCache.java') diff --git a/src/com/p4square/grow/ccb/CustomFieldCache.java b/src/com/p4square/grow/ccb/CustomFieldCache.java index ba473fb..d93e6d9 100644 --- a/src/com/p4square/grow/ccb/CustomFieldCache.java +++ b/src/com/p4square/grow/ccb/CustomFieldCache.java @@ -72,7 +72,7 @@ public class CustomFieldCache { items = mItemByNameTable.get(type); } - return items.get(name); + return items.get(name.toLowerCase()); } private synchronized void refresh() { @@ -113,8 +113,8 @@ public class CustomFieldCache { private synchronized boolean cacheLookupTable(final LookupTableType type) { try { final GetLookupTableResponse resp = mAPI.getLookupTable(new GetLookupTableRequest().withType(type)); - mItemByNameTable.put(type, - resp.getItems().stream().collect(Collectors.toMap(LookupTableItem::getName, Function.identity()))); + mItemByNameTable.put(type, resp.getItems().stream().collect( + Collectors.toMap(item -> item.getName().toLowerCase(), Function.identity()))); return true; } catch (IOException e) { diff --git a/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java b/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java index e374496..bcfd260 100644 --- a/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java +++ b/tst/com/p4square/grow/ccb/CustomFieldCacheTest.java @@ -174,6 +174,26 @@ public class CustomFieldCacheTest { assertEquals("Believer", item.getName()); } + @Test + public void testGetPullDownOptionsMixedCase() throws Exception { + // Setup mocks + Capture requestCapture = EasyMock.newCapture(); + EasyMock.expect(api.getLookupTable(EasyMock.capture(requestCapture))).andReturn(lookupTableResponse); + EasyMock.replay(api); + + // Test the cache + LookupTableItem item = cache.getPulldownItemByName( + LookupTableType.valueOf("udf_ind_pulldown_6".toUpperCase()), + "BeLiEvEr"); + + // Verify result. + EasyMock.verify(api); + assertEquals(LookupTableType.UDF_IND_PULLDOWN_6, requestCapture.getValue().getType()); + assertEquals(2, item.getId()); + assertEquals(2, item.getOrder()); + assertEquals("Believer", item.getName()); + } + @Test public void testGetPullDownOptionMissing() throws Exception { // Setup mocks -- cgit v1.2.3