IBM Support

Validate customer data when saving a document or folder

Technical Blog Post


Abstract

Validate customer data when saving a document or folder

Body

With IBM Content Navigator it is easy to add extensions to validate user input or enforce property formatting.  By using External Data Services (EDS) we can interact with the user input data and back-end systems in a seamless, nonintrusive manner.  The EDS interface ensures that extensions written for your application are not overwritten when the latest fix pack or version is applied taking the worry out of customizing the application.

Navigator provides all the required interfaces limiting the development activity to just a quick and simple servlet with the custom validation.

image

Process Flow

The EDS process is simple and has just 2 quick steps:

  1. Get the list of Objects to validate
  2. Call the validation routine when one of the listed objects is accessed.

Part One - Get the list of objects: GetObjectTypesServlet.java

The first step in the development process is to tell Navigator what object types to validate - we can interact with any supported Navigator object type.  Navigator will call the GetObjectTypes servlet and expects a JSON array with the set of objects to validate:

{“symbolicName” : “<value>”}

In this example, we will validate the folder object type “CustomerDossier.”

@WebServlet(name = "GetObjectTypesServlet", urlPatterns = { "/types" })

public class GetObjectTypesServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String repositoryId = request.getParameter("repositoryId");
//Return a JSON response with the Object Class
JSONObject jsonElement = new JSONObject();
JSONArray jsonResponse = new JSONArray();
jsonElement.put("symbolicName", "CustomerDossier");
jsonResponse.add(jsonElement);
PrintWriter writer = response.getWriter();
jsonResponse.serialize(writer);

}

}

Part Two - Implement your custom validation: UpdateObjectTypeServlet

Navigator will call the validation routine when the user interacts with your specified object type and will provide helpful information about when your code was called and the current property values as a JSON object.

The JSON object has 3 main sections:

  1. Information about the Request
  2. Information about the Object
  3. Information about the User

The following code establishes the servlet and reads the JSON request

@WebServlet(name = "UpdateObjectTypeServlet", urlPatterns = { "/type/*" })
public class UpdateObjectTypeServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            
        String objectType = request.getPathInfo().substring(1);
                    
        // Get the request json
        InputStream requestInputStream = request.getInputStream();
        JSONObject jsonRequest = JSONObject.parse(requestInputStream);
        String requestMode = jsonRequest.get("requestMode").toString();
        String objectId = jsonRequest.get("objectId").toString();
        String repositoryId = jsonRequest.get("repositoryId ").toString();

        JSONArray requestProperties = (JSONArray)jsonRequest.get("properties");
        JSONObject clientContext = (JSONObject)jsonRequest.get("clientContext");
        String userid = (String)clientContext.get("userid");
        String locale = (String)clientContext.get("locale");
        String desktop = (String)clientContext.get("desktop");

Each request will have a requestMode specified to identify when the validation routine was called:

  1. initialNewObject and InitialExistingObject:  The first step in the process and is called when an item – a document or folder, for example – is opened or created. 
  2. inProgressChanges: Is called when a user updates a value in the UI but is only called if there are dependent properties.
  3. finalNewObject and finalExistingObject: The final step in the process and is called prior to committing the changes to the server.  To deny the commit request, return an error.

Force formatting of a specific property

To ensure all data is entered into the system correctly, we can define a specific format using regular expressions – this is done by using the initialNewObject and initialExistingObject requestModes and simply returning the properly formatted JSON.

In the following example, the PhoneNumber property is formatted

// Create an Empty JSON response and write your results here
JSONObject jsonResponse = new JSONObject();  
responseProperties = new JSONArray();
        
if (requestMode.equals("initialNewObject") || requestMode.equals("initialExistingObject")) {
     //ensure the Phone Number is in the proper format eg, 704-555-1212
     // the reg-ex for this format \d\d\d-\d\d\d-\d\d\d\d and we need to escape it
     JSONObject temp = new JSONObject();
     temp.put("symbolicName", “PhoneNumber”);
     temp.put("format", "\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d");
     temp.put("formatDescription", "nnn-nnn-nnnn");
     responseProperties.add(temp);
     jsonResponse.put("properties", responseProperties);
}
PrintWriter writer = response.getWriter();
jsonResponse.serialize(writer);

Validate values prior to saving

Having data in the proper format is a great first step, and with just a little addition, we can add a check to verify that the entered data is correct.  By handling the finalNewObject and finalExistingObject request modes, we can add a look-up to an external system or perform basic checks.

To validate a specific property, we need to inspect the requestProperties JSONArray from the initial JSON object.  The JSONArray has no method to retrieve an item by property name so it will be necessary to loop through the list of properties and perform simple name matching until you find the property you would like to validate.  The following example expands on the previous formatting example.

if (requestMode.equals("finalExistingObject") || requestMode.equals("finalNewObject")) {
    //The Incoming JSON will have the properties and their values in the requestProperties array  
  
    //  Loop through the properties and use simple name matching to find a property
    for( int i = 0 ;  i < requestProperties.size(); i++) {
        JSONObject requestProperty = (JSONObject) requestProperties.get(i);
        
        String propName = requestProperty.get("symbolicName").toString();
        String propValue = String.valueOf(requestProperty.get("value"));
        if (propName.equals("PhoneNumber")) {
            if (!propValue.equals("704-999-9999")) {
                // the value does not match our simple rule, add an error message to the return JSON
                        JSONObject temp = new JSONObject();
                        temp.put("symbolicName", "PhoneNumber);
                        temp.put("customValidationError", "You entered an invalid phone number.");
                        responseProperties.add(temp);
            }
        }
     }
}
PrintWriter writer = response.getWriter();
jsonResponse.serialize(writer);

Part Three - Sit Back and Enjoy

Your custom validation routine is now ready to be compiled and deployed to your local web container.  Simply follow the instructions for your development tool to create the WAR file and deploy. 

Once your servlets are deployed, the final step is to let Navigator know where to call your customization. 

  1. Open your Content Navigator admin desktop and navigate to the plug-ins component
  2. Click "New Plug-in"
  3. Select "Jar file path" and enter the path to the local edsPlugin.jar file that is installed with Navigator (\<install directory>\ECMClient\plugins) and click "Load"
  4. Enter the URL to your newly created servlet

You are now ready to test!

[{"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Product":{"code":"SSCTJ4","label":"IBM Case Manager"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"","Edition":"","Line of Business":{"code":"LOB45","label":"Automation"}}]

UID

ibm11281334