Skip to content

Commit

Permalink
MODFQMMGR-376: Add textbox custom fields to the Users entity type (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvsharp authored Jan 17, 2025
1 parent 60e0268 commit fb76407
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 15 deletions.
26 changes: 23 additions & 3 deletions src/main/java/org/folio/fqm/repository/EntityTypeRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ public class EntityTypeRepository {

public static final String ID_FIELD_NAME = "id";
public static final String DEFINITION_FIELD_NAME = "definition";
public static final String STRING_EXTRACTOR = "%s ->> '%s'";
public static final String CUSTOM_FIELD_NAME = "jsonb ->> 'name'";
public static final String CUSTOM_FIELD_REF_ID = "jsonb ->> 'refId'";
public static final String CUSTOM_FIELD_TYPE = "jsonb ->> 'type'";
public static final String CUSTOM_FIELD_FILTER_VALUE_GETTER = "jsonb -> 'selectField' -> 'options' ->> 'values'";
public static final String CUSTOM_FIELD_TYPE_SINGLE_CHECKBOX = "SINGLE_CHECKBOX";
public static final String CUSTOM_FIELD_TYPE_SINGLE_SELECT_DROPDOWN = "SINGLE_SELECT_DROPDOWN";
public static final String CUSTOM_FIELD_TYPE_RADIO_BUTTON = "RADIO_BUTTON";
public static final String CUSTOM_FIELD_TYPE_TEXTBOX_SHORT = "TEXTBOX_SHORT";
public static final String CUSTOM_FIELD_TYPE_TEXTBOX_LONG = "TEXTBOX_LONG";
public static final String CUSTOM_FIELD_VALUE_GETTER = """
(
SELECT f.entry ->> 'value'
Expand All @@ -70,7 +73,9 @@ FROM jsonb_array_elements(
private static final List<String> SUPPORTED_CUSTOM_FIELD_TYPES = List.of(
CUSTOM_FIELD_TYPE_SINGLE_CHECKBOX,
CUSTOM_FIELD_TYPE_SINGLE_SELECT_DROPDOWN,
CUSTOM_FIELD_TYPE_RADIO_BUTTON
CUSTOM_FIELD_TYPE_RADIO_BUTTON,
CUSTOM_FIELD_TYPE_TEXTBOX_SHORT,
CUSTOM_FIELD_TYPE_TEXTBOX_LONG
);

private final DSLContext readerJooqContext;
Expand Down Expand Up @@ -182,6 +187,8 @@ private List<EntityTypeColumn> fetchColumnNamesForCustomFields(String entityType
return handleSingleSelectCustomField(name, refId, sourceViewName, sourceViewExtractor, columnValues);
} else if (CUSTOM_FIELD_TYPE_SINGLE_CHECKBOX.equals(type)) {
return handleBooleanCustomField(name, refId, sourceViewExtractor);
} else if (CUSTOM_FIELD_TYPE_TEXTBOX_SHORT.equals(type) || CUSTOM_FIELD_TYPE_TEXTBOX_LONG.equals(type)) {
return handleTextboxCustomField(name, refId, sourceViewExtractor);
}
return null;
} catch (Exception e) {
Expand Down Expand Up @@ -214,7 +221,7 @@ private EntityTypeColumn handleBooleanCustomField(String name,
.dataType(new BooleanType().dataType("booleanType"))
.values(CUSTOM_FIELD_BOOLEAN_VALUES)
.visibleByDefault(false)
.valueGetter(sourceViewExtractor + " ->> '" + refId + "'")
.valueGetter(String.format(STRING_EXTRACTOR, sourceViewExtractor, refId))
.labelAlias(name)
.queryable(true)
.isCustomField(true);
Expand All @@ -225,7 +232,7 @@ private EntityTypeColumn handleSingleSelectCustomField(String name,
String sourceViewName,
String sourceViewExtractor,
List<ValueWithLabel> columnValues) {
String filterValueGetter = sourceViewExtractor + " ->> '" + refId + "'";
String filterValueGetter = String.format(STRING_EXTRACTOR, sourceViewExtractor, refId);
String valueGetter = String.format(
CUSTOM_FIELD_VALUE_GETTER,
executionContext.getTenantId(),
Expand All @@ -246,6 +253,19 @@ private EntityTypeColumn handleSingleSelectCustomField(String name,
.isCustomField(true);
}

private EntityTypeColumn handleTextboxCustomField(String name,
String refId,
String sourceViewExtractor) {
return new EntityTypeColumn()
.name(name)
.dataType(new StringType().dataType("stringType"))
.visibleByDefault(false)
.valueGetter(String.format(STRING_EXTRACTOR, sourceViewExtractor, refId))
.labelAlias(name)
.queryable(true)
.isCustomField(true);
}

@SneakyThrows
private EntityType unmarshallEntityType(String str) {
return objectMapper.readValue(str, EntityType.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ void shouldReturnValidEntityTypeDefinition() {

@Test
void shouldGetEntityTypeDefinitionWithCustomFields() {
String valueGetter1 = "custom_fields_source_view.jsonb -> 'customFields' ->> 'customColumn1'";
String filterValueGetter2 = "custom_fields_source_view.jsonb -> 'customFields' ->> 'customColumn2'";
String filterValueGetter3 = "custom_fields_source_view.jsonb -> 'customFields' ->> 'customColumn3'";
String valueGetter2 = String.format(CUSTOM_FIELD_VALUE_GETTER, null, "custom_fields_source_view", "customColumn2", "custom_fields_source_view.jsonb -> 'customFields'", "customColumn2");
String valueGetter3 = String.format(CUSTOM_FIELD_VALUE_GETTER, null, "custom_fields_source_view", "customColumn3", "custom_fields_source_view.jsonb -> 'customFields'", "customColumn3");
String genericValueGetter = "custom_fields_source_view.jsonb -> 'customFields' ->> '%s'";
String refId1 = "customColumn1";
String refId2 = "customColumn2";
String refId3 = "customColumn3";
String refId4 = "customColumn4";
String refId5 = "customColumn5";
String valueGetter2 = String.format(CUSTOM_FIELD_VALUE_GETTER, null, "custom_fields_source_view", refId2, "custom_fields_source_view.jsonb -> 'customFields'", refId2);
String valueGetter3 = String.format(CUSTOM_FIELD_VALUE_GETTER, null, "custom_fields_source_view", refId3, "custom_fields_source_view.jsonb -> 'customFields'", refId3);
var radioButtonValues = List.of(
new ValueWithLabel().label("label1").value("opt1"),
new ValueWithLabel().label("label2").value("opt2")
Expand Down Expand Up @@ -77,7 +80,7 @@ void shouldGetEntityTypeDefinitionWithCustomFields() {
new EntityTypeColumn()
.name("custom_column_1")
.dataType(new BooleanType().dataType("booleanType"))
.valueGetter(valueGetter1)
.valueGetter(String.format(genericValueGetter, refId1))
.labelAlias("custom_column_1")
.values(CUSTOM_FIELD_BOOLEAN_VALUES)
.visibleByDefault(false)
Expand All @@ -87,7 +90,7 @@ void shouldGetEntityTypeDefinitionWithCustomFields() {
.name("custom_column_2")
.dataType(new StringType().dataType("stringType"))
.valueGetter(valueGetter2)
.filterValueGetter(filterValueGetter2)
.filterValueGetter(String.format(genericValueGetter, refId2))
.labelAlias("custom_column_2")
.values(radioButtonValues)
.visibleByDefault(false)
Expand All @@ -97,11 +100,27 @@ void shouldGetEntityTypeDefinitionWithCustomFields() {
.name("custom_column_3")
.dataType(new StringType().dataType("stringType"))
.valueGetter(valueGetter3)
.filterValueGetter(filterValueGetter3)
.filterValueGetter(String.format(genericValueGetter, refId3))
.labelAlias("custom_column_3")
.values(singleSelectValues)
.visibleByDefault(false)
.queryable(true)
.isCustomField(true),
new EntityTypeColumn()
.name("custom_column_4")
.dataType(new StringType().dataType("stringType"))
.valueGetter(String.format(genericValueGetter, refId4))
.labelAlias("custom_column_4")
.visibleByDefault(false)
.queryable(true)
.isCustomField(true),
new EntityTypeColumn()
.name("custom_column_5")
.dataType(new StringType().dataType("stringType"))
.valueGetter(String.format(genericValueGetter, refId5))
.labelAlias("custom_column_5")
.visibleByDefault(false)
.queryable(true)
.isCustomField(true)
);
EntityType expectedEntityType = new EntityType()
Expand Down
34 changes: 30 additions & 4 deletions src/test/resources/test-db/scripts/custom-fields-source-view.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,40 @@ VALUES (
}
}
}'
),
),
(
'2c4e9797-422f-4962-a302-174af09b23fb',
'{
"id": "2c4e9797-422f-4962-a302-174af09b23fb",
"name": "custom_column_4",
"type": "TEXTBOX_SHORT",
"order": 4,
"refId": "customColumn4",
"visible": true,
"entityType": "user",
"selectField": {}
}'
),
(
'2c4e9797-422f-4962-a302-174af09b23fc',
'{
"id": "2c4e9797-422f-4962-a302-174af09b23fc",
"name": "custom_column_5",
"type": "TEXTBOX_LONG",
"order": 5,
"refId": "customColumn5",
"visible": true,
"entityType": "user",
"selectField": {}
}'
),
(
'2c4e9797-422f-4962-a302-174af09b23fb',
'2c4e9797-422f-4962-a302-174af09b23ff',
'{
"id": "2c4e9797-422f-4962-a302-174af09b23fb",
"id": "2c4e9797-422f-4962-a302-174af09b23ff",
"name": "invalid_custom_column",
"type": "SINGLE_SELECT_DROPDOWN",
"order": 4,
"order": 6,
"refId": "invalidCustomColumn",
"visible": true,
"entityType": "user",
Expand Down

0 comments on commit fb76407

Please sign in to comment.