Conditional Mode Execution

In this Lesson

You will

  • Learn how to create an agent capable of bypassing mode execution under certain conditions.
  • Modify the PartyTheme agent to provide conditional execution of the add and change modes.

Concepts

In the Element Visibility lesson the PartyTheme agent was used to control the visibility of an element in the PartyDefinition scene.

Hiding the element on the scene is only one half of what is required for the party theme. The agent must only capture the theme for the ACME tenant. There are two ways to accomplish this

  1. Modify the scene to only request add mode processing for the PartyTheme agent when the ACME tenant is using the product.
  2. Modify the PartyTheme agent to respect its own hint about when a theme is required.

Option #2 is preferred to option #1.

  • With option #1 the designer is in control of whether or not the agent should capture the theme. This shifts the data integrity responsibility to the designer. What if they ask the agent to add a theme for the wrong tenant?
  • With option #2 the agent remains in control of the data integrity. It provides the hint and also ensures the theme is only required and stored for the correct tenant.

In this lesson you will be doing option #2. However, just as a matter of completeness, we should mention how #1 would be implemented. The technique is a useful thing to have in your tool bag. You would create two OK buttons that are positioned directly on top of each other and make only one of the buttons visible according to the hint provided by the agent.

  1. The first button would look like this. (do not make this change to your scene)



  2. The second button would look like this. (do not make this change to your scene)



Customizing an agent to bypass validation and add mode

  1. Open the PartyTheme agent class



  2. Ensure the themeRequired node is properly initialized for both Add and Change mode.
    • You have already initialized the node for add mode processing in the prepareForAddOrReplicateMode method. That method is called when the agent is preparing the scheme to accept values for add mode.

      We will be needing to check for the existence of the ACME tenant in more than one place so let’s move the ACME tenant check into its own method. It will help improve readability.



      • Add the following method to the class
        private boolean isAcmeTenant(){
        	return THEME_TENANT.equals(getConversation().getConnection().getUser().getTenant());
        }
        
      • Replace the prepareForAddOrReplicateMode method with this one
        @Override
        protected void prepareForAddOrReplicateMode(boolean replicate)
        		throws Exception {
        	super.prepareForAddOrReplicateMode(replicate);
        	scheme.setThemeRequired(isAcmeTenant());
        }
        
    • You will now initialize the themeRequired node for change mode processing. There is more than one way this could be done – here is one way.
      1. Override the schemeFocused method contained in the PartyThemeSuper class. This method is called after a row has been loaded from the datasource into the scheme. It provides you an opportunity to add additional information to the scheme.
        [ i get it! ‘schemeFocused’ means the scheme has been focused on something – or contains values related to its key. i think my light bulb just flickered up to 8 watts. ]
      2. Add the following syntax to the schemeFocused method
        scheme.setThemeRequired(isAcmeTenant());
        

        This also takes care of view mode because when an agent is running in view or change mode they load the scheme the same way. So, whether the designer is using this agent in either mode they will be able to hide the theme field if they choose to.

      3. Bonus: What other methods could have been used to initialize the themeRequired node? If you are curious go check out the focusOnContext method in the PartyThemeSuper class. focusOnContext is called by the pump when it wants the agent to load information into its scheme pertaining to the context of the conversation. focusOnContext contains two other methods you could override. Well there are actually three if you really want to push it in a weird direction. Can you identify the methods that could be used? The one we suggested is ‘best practice’ for this need.

  3. Ensure the theme is only required for the ACME tenant.
    Modify the validate_PartyTheme to look like this. ( This method is called for both add and change mode processing )
    //test the theme only if the ACME tenant is being used
    if (isAcmeTenant()){
    	if (!hasContent(scheme.getPartyTheme())){
    		node.setMessage(error("A theme is required"));
    	}
    }
    
  4. Ensure the add and change mode are only executed for the ACME tenant
    • For add mode
      1. Override the isAddReady method contained in the PartyThemeSuper class. If this method returns true the mode will be executed. Return false to bypass the mode processing.
      2. Replace the entire contents of that method with the following syntax
        return isAcmeTenant();
        
    • For change mode
      1. Override the isUpdateReady method contained in the PartyThemeSuper class. If this method returns true the mode will be executed. Return false to bypass the mode processing.
      2. Replace the entire contents of that method with the following syntax
        return isAcmeTenant();
        
  5. Save the class

Your class should now look like this



Test the agent

Test the solution to ensure the theme is not required or stored when ACME is not being used.

  1. Restart the server
  2. Start the solution
  3. Advance to the ‘Create a Party’ layer
  4. Press the OK button without entering any information. The theme error should no longer be displayed.



  5. Add a new party.
  6. Use DBeaver to ensure a row was not added to the ACME.PartyTheme table.

This lesson is over.