Skip to content

Commit

Permalink
[Mongo Plugin] Raw is now a command option instead of Form vs Raw (#5446
Browse files Browse the repository at this point in the history
)

* Raw is now a command option instead of Form vs Raw

* Added database migration for migration raw input type to be of raw command type

* Fixed test case failure

* Minor cleanup
  • Loading branch information
trishaanand authored and rishabhsaxena committed Jul 2, 2021
1 parent d6878b5 commit 726c20e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
import static com.external.plugins.constants.ConfigurationIndex.FIND_PROJECTION;
import static com.external.plugins.constants.ConfigurationIndex.FIND_QUERY;
import static com.external.plugins.constants.ConfigurationIndex.FIND_SORT;
import static com.external.plugins.constants.ConfigurationIndex.INPUT_TYPE;
import static com.external.plugins.constants.ConfigurationIndex.INSERT_DOCUMENT;
import static com.external.plugins.constants.ConfigurationIndex.SMART_BSON_SUBSTITUTION;
import static com.external.plugins.constants.ConfigurationIndex.UPDATE_ONE_QUERY;
Expand Down Expand Up @@ -228,7 +227,8 @@ public Mono<ActionExecutionResult> executeParameterized(MongoClient mongoClient,
// Smartly substitute in actionConfiguration.body and replace all the bindings with values.
if (TRUE.equals(smartBsonSubstitution)) {

if (isFormInput(actionConfiguration.getPluginSpecifiedTemplates())) {
// If not raw, then it must be form input.
if (!isRawCommand(actionConfiguration.getPluginSpecifiedTemplates())) {
List<Property> updatedTemplates = smartSubstituteFormCommand(actionConfiguration.getPluginSpecifiedTemplates(),
executeActionDTO.getParams(), parameters);
actionConfiguration.setPluginSpecifiedTemplates(updatedTemplates);
Expand Down Expand Up @@ -412,13 +412,10 @@ public Mono<ActionExecutionResult> executeCommon(MongoClient mongoClient,
.subscribeOn(scheduler);
}

private Boolean isFormInput(List<Property> templates) {
if ((templates.size() >= (1 + INPUT_TYPE)) &&
(templates.get(INPUT_TYPE) != null) &&
("FORM".equals(templates.get(INPUT_TYPE).getValue())) &&
(templates.size() >= (1 + COMMAND)) &&
private Boolean isRawCommand(List<Property> templates) {
if ((templates.size() >= (1 + COMMAND)) &&
(templates.get(COMMAND) != null) &&
(templates.get(COMMAND).getValue() != null)) {
("RAW".equals(templates.get(COMMAND).getValue()))) {
return TRUE;
}
return FALSE;
Expand All @@ -427,9 +424,10 @@ private Boolean isFormInput(List<Property> templates) {
private String convertMongoFormInputToRawCommand(ActionConfiguration actionConfiguration) {
List<Property> templates = actionConfiguration.getPluginSpecifiedTemplates();
if (templates != null) {
if (isFormInput(templates)) {
// The user has configured FORM for command input. Parse the commands appropriately
// If its not raw command, then it must be one of the mongo form commands
if (!isRawCommand(templates)) {

// Parse the commands into raw appropriately
MongoCommand command = null;
switch ((String) templates.get(COMMAND).getValue()) {
case "INSERT":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

public class ConfigurationIndex {
public static final int SMART_BSON_SUBSTITUTION = 0;
public static final int INPUT_TYPE = 1;

@Deprecated
public static final int INPUT_TYPE = 1; // Input type is no longer used. Raw type is now a type of command

public static final int COMMAND = 2;
public static final int COLLECTION = 19;
public static final int FIND_QUERY = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@
"sectionName": "",
"id": 1,
"children": [
{
"label": "",
"configProperty": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"controlType": "DROP_DOWN",
"initialValue": "FORM",
"options": [
{
"label": "Form Input",
"value": "FORM"
},
{
"label": "Raw Input",
"value": "RAW"
}
]
},
{
"label": "Command",
"configProperty": "actionConfiguration.pluginSpecifiedTemplates[2].value",
Expand Down Expand Up @@ -53,6 +37,10 @@
{
"label": "Aggregate",
"value": "AGGREGATE"
},
{
"label": "Raw",
"value": "RAW"
}
],
"hidden": {
Expand All @@ -67,7 +55,7 @@
"controlType": "QUERY_DYNAMIC_INPUT_TEXT",
"evaluationSubstitutionType": "TEMPLATE",
"hidden": {
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "EQUALS",
"value": "RAW"
}
Expand All @@ -79,11 +67,6 @@
"hidden" : {
"conditionType": "OR",
"conditions": [
{
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"comparison": "NOT_EQUALS",
"value": "FORM"
},
{
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "NOT_EQUALS",
Expand Down Expand Up @@ -136,11 +119,6 @@
"hidden" : {
"conditionType": "OR",
"conditions": [
{
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"comparison": "NOT_EQUALS",
"value": "FORM"
},
{
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "NOT_EQUALS",
Expand Down Expand Up @@ -179,11 +157,6 @@
"hidden" : {
"conditionType": "OR",
"conditions": [
{
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"comparison": "NOT_EQUALS",
"value": "FORM"
},
{
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "NOT_EQUALS",
Expand Down Expand Up @@ -231,11 +204,6 @@
"hidden" : {
"conditionType": "OR",
"conditions": [
{
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"comparison": "NOT_EQUALS",
"value": "FORM"
},
{
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "NOT_EQUALS",
Expand Down Expand Up @@ -276,11 +244,6 @@
"hidden" : {
"conditionType": "OR",
"conditions": [
{
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"comparison": "NOT_EQUALS",
"value": "FORM"
},
{
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "NOT_EQUALS",
Expand All @@ -305,11 +268,6 @@
"hidden" : {
"conditionType": "OR",
"conditions": [
{
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"comparison": "NOT_EQUALS",
"value": "FORM"
},
{
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "NOT_EQUALS",
Expand All @@ -326,7 +284,7 @@
"placeholderText" : "{rating : {$gte : 9}}"
},
{
"label": "Key/Field",
"label": "Key",
"configProperty": "actionConfiguration.pluginSpecifiedTemplates[16].value",
"controlType": "QUERY_DYNAMIC_INPUT_TEXT",
"evaluationSubstitutionType": "TEMPLATE",
Expand All @@ -341,11 +299,6 @@
"hidden" : {
"conditionType": "OR",
"conditions": [
{
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"comparison": "NOT_EQUALS",
"value": "FORM"
},
{
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "NOT_EQUALS",
Expand All @@ -370,11 +323,6 @@
"hidden" : {
"conditionType": "OR",
"conditions": [
{
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"comparison": "NOT_EQUALS",
"value": "FORM"
},
{
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "NOT_EQUALS",
Expand Down Expand Up @@ -402,9 +350,9 @@
"conditionType": "OR",
"conditions": [
{
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"comparison": "EQUALS",
"value": "FORM"
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "NOT_EQUALS",
"value": "RAW"
},
{
"path": "actionConfiguration.pluginSpecifiedTemplates[0].value",
Expand All @@ -424,9 +372,9 @@
"conditionType": "OR",
"conditions": [
{
"path": "actionConfiguration.pluginSpecifiedTemplates[1].value",
"comparison": "EQUALS",
"value": "FORM"
"path": "actionConfiguration.pluginSpecifiedTemplates[2].value",
"comparison": "NOT_EQUALS",
"value": "RAW"
},
{
"path": "actionConfiguration.pluginSpecifiedTemplates[0].value",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,8 @@ public void testBsonSmartSubstitution() {
" }");
List<Property> pluginSpecifiedTemplates = new ArrayList<>();
pluginSpecifiedTemplates.add(new Property("jsonSmartSubstitution", "true"));
pluginSpecifiedTemplates.add(null);
pluginSpecifiedTemplates.add(new Property("command", "RAW"));
actionConfiguration.setPluginSpecifiedTemplates(pluginSpecifiedTemplates);

ExecuteActionDTO executeActionDTO = new ExecuteActionDTO();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2630,4 +2630,117 @@ public void addOrderToAllPagesOfApplication(MongoTemplate mongoTemplate) {
}
}
}

@ChangeSet(order = "076", id = "mongo-form-migrate-raw", author = "")
public void migrateRawInputTypeToRawCommand(MongockTemplate mongockTemplate) {

Plugin mongoPlugin = mongockTemplate.findOne(query(where("packageName").is("mongo-plugin")), Plugin.class);

// Fetch all the actions built on top of a mongo database with input type set to raw.
assert mongoPlugin != null;
List<NewAction> rawMongoQueryActions = mongockTemplate.find(
query(new Criteria().andOperator(
where(fieldName(QNewAction.newAction.pluginId)).is(mongoPlugin.getId()))),
NewAction.class
)
.stream()
.filter(mongoAction -> {
boolean result = false;
if (mongoAction.getUnpublishedAction() == null || mongoAction.getUnpublishedAction().getActionConfiguration() == null) {
return false;
}

List<Property> pluginSpecifiedTemplates = mongoAction.getUnpublishedAction().getActionConfiguration().getPluginSpecifiedTemplates();

// Filter out all the unpublished actions which have the input type raw configured.
if (pluginSpecifiedTemplates != null && pluginSpecifiedTemplates.size() >= 2) {
Property inputTypeProperty = pluginSpecifiedTemplates.get(1);
if (inputTypeProperty != null && inputTypeProperty.getValue() != null) {
String inputType = (String) inputTypeProperty.getValue();
if (inputType.equals("RAW")) {
result = true;
}
}
}

ActionDTO publishedAction = mongoAction.getPublishedAction();
if (publishedAction != null && publishedAction.getActionConfiguration() != null) {
pluginSpecifiedTemplates = publishedAction.getActionConfiguration().getPluginSpecifiedTemplates();

// Filter out all the published actions which have the input type raw configured.
if (pluginSpecifiedTemplates != null && pluginSpecifiedTemplates.size() >= 2) {
Property inputTypeProperty = pluginSpecifiedTemplates.get(1);
if (inputTypeProperty != null && inputTypeProperty.getValue() != null) {
String inputType = (String) inputTypeProperty.getValue();
if (inputType.equals("RAW")) {
result = true;
}
}
}
}
return result;
})
.collect(Collectors.toList());

for (NewAction action : rawMongoQueryActions) {

Boolean smartSub = true;
Property smartSubProperty;
Map<Integer, Object> configMap;
List<Property> updatedTemplates;

// Migrate the unpublished actions
List<Property> pluginSpecifiedTemplates = action.getUnpublishedAction().getActionConfiguration().getPluginSpecifiedTemplates();
if (pluginSpecifiedTemplates != null) {
smartSubProperty = pluginSpecifiedTemplates.get(0);
if (smartSubProperty != null) {
Object value = smartSubProperty.getValue();
if (value instanceof Boolean) {
smartSub = (Boolean) value;
} else if (value instanceof String) {
smartSub = Boolean.parseBoolean((String) value);
}
}

configMap = new HashMap<>();
configMap.put(0, smartSub);
configMap.put(2, "RAW");

updatedTemplates = generateMongoFormConfigTemplates(configMap);
action.getUnpublishedAction().getActionConfiguration().setPluginSpecifiedTemplates(updatedTemplates);
}


// Now migrate the published actions
ActionDTO publishedAction = action.getPublishedAction();
if (publishedAction != null && publishedAction.getActionConfiguration() != null) {
pluginSpecifiedTemplates = publishedAction.getActionConfiguration().getPluginSpecifiedTemplates();

if (pluginSpecifiedTemplates != null) {
smartSub = true;
smartSubProperty = pluginSpecifiedTemplates.get(0);
if (smartSubProperty != null) {
Object value = smartSubProperty.getValue();
if (value instanceof Boolean) {
smartSub = (Boolean) value;
} else if (value instanceof String) {
smartSub = Boolean.parseBoolean((String) value);
}
}

configMap = new HashMap<>();
configMap.put(0, smartSub);
configMap.put(2, "RAW");

updatedTemplates = generateMongoFormConfigTemplates(configMap);
action.getPublishedAction().getActionConfiguration().setPluginSpecifiedTemplates(updatedTemplates);
}
}
}

// Now that all the actions have been updated, save all the actions
for (NewAction action : rawMongoQueryActions) {
mongockTemplate.save(action);
}
}
}

0 comments on commit 726c20e

Please sign in to comment.