Monday, July 22, 2013

How to customize tag Converter in ATG

Tag Converters


Oracle ATG Web Commerce provides tag converter classes that let you explicitly control how form data is converted on input and displayed on output, and when exceptions are thrown. Certain DSP tags such as dsp:inputand dsp:valueofcan specify these tag converters; details about syntax and usage is provided in the ATG Page Developer's Guide.

Tag Converter can mask the input data and also change the format of the data displayed.


Tag Converter can be applied to following DSP tags.

dsp:a
dsp:input
dsp:param
dsp:postfield
dsp:select
dsp:setvalue
dsp:textarea
dsp:valueof


 Out of Box, ATG Provides following tag converter 


The following table lists the standard ATG tag converters:


Converter
Inputdatatype
Outputdatatype
Function
creditCard
numeric string
integer, float, etc.
Determines how a credit card number is displayed.
currency
currencyConversion
euro

numeric string
string
Displays currency in a format appropriate for the locale
date
String
Date
Parses strings into Dates; displays Dates in a variety of formats.
map
Map
Map
Ensures that key-value pairs are parsed correctly for the specified map property.
nullable
any
any
If field is left empty, corresponding property is set to null
number
numeric string
integer, float, etc.
Displays numeric data-types in a variety of formats
required
any
any
Throws a form exception if the user specifies a null or empty value
valueishtml
string
string
Displays a string as formatted HTML text rather than escaped text.


Create Our Own Custom Tag Converter


Now will go through, how to customize our own tag converter.

Here i have taken a example to , "mask all the input data leaving the number of character specified by user not to mask"

for example:  My input is "ASDG123$%%%3456", and output will be "XXXXXXXXXXX3456"




Step 1: Create a Component "MaskStringConverter"


1) Place MaskStringConverter.properties to config folder

MaskStringConverter.properties

  $class=com.att.ecom.checkout.view.MaskStringConverter
  $scope=global
  loggingDebug=true


2)  Create a java class MaskStringConverter extends GenericService implements TagConverter

placed the complete code for understanding MaskStringConverter.java

Step 2:  Register new TagConverter "MaskStringConverter" with the TagConverterManager (atg.droplet.TagConverterManager) by calling TagConverterManager.registerTagConverter()

Please check below code, where marked in Yellow 


MaskStringConverter.java 

// Constant Variable mentioning number of character not to be masked
static final String NUMBERSUNMASKED = "numcharsunmasked";

// Register the tag converter with the manager
    public void doStartService() {
        TagConverterManager.registerTagConverter(this);
        if (isLoggingInfo()) {
            logInfo("Registered '" + getName() + "' tag converter");
        }
    }


// Array of input attributes , can have as many as input attributes. here we have only one //attribute numcharsunmasked mentioned in jsp
    private static final TagAttributeDescriptor[] S_TAG_ATTRIBUTE_DESCRIPTORS = { new TagAttributeDescriptor(
            NUMBERSUNMASKED, "If this attribute is present, The number of digits mentioned will not be masked", false,
            false) };



// Masking Logic
    public Object convertStringToObject(DynamoHttpServletRequest request, String string, Properties properties)
            throws TagConversionException {
        Integer numberNotMasked = 0;
        if (StringUtils.isEmpty(string)) {
            return null;
        }
        if (!StringUtils.isEmpty(properties.getProperty(NUMBERSUNMASKED))
                && StringUtils.isNumeric(properties.getProperty(NUMBERSUNMASKED))) {
            logInfo("get the number of character not to be masked");
            numberNotMasked = new Integer(properties.getProperty(NUMBERSUNMASKED).toString());
            return buildOutput(string, numberNotMasked);
        }
        return null;
    }

    public String convertObjectToString(DynamoHttpServletRequest request, Object pValue, Properties properties)
            throws TagConversionException {
        int numberNotMasked = 0;
        if (pValue == null) {
            return null;
        }
        if (!StringUtils.isEmpty(properties.getProperty(NUMBERSUNMASKED))
                && StringUtils.isNumeric(properties.getProperty(NUMBERSUNMASKED))) {
            logInfo("get the number of character not to be masked");
            numberNotMasked = new Integer(properties.getProperty(NUMBERSUNMASKED).toString());
            return buildOutput(pValue.toString(), numberNotMasked);
        }
        return null;
    }

    private String buildOutput(String driversLicense, int numberNotMasked) {

        String convertedString = "";

        int maskedLength = driversLicense.length() - numberNotMasked;
        convertedString = org.apache.commons.lang.StringUtils.repeat("X", maskedLength)
                + driversLicense.subSequence(maskedLength, driversLicense.length());

        return convertedString;
    }

   

 //getting the converter name which has to be mentioned in jsp.
  @Override
    public String getName() {
        return "inputStringMasking";
    }



//Returns an array of TagAttributeDescriptors (atg.droplet.TagAttributeDescriptor).
    @Override
    public TagAttributeDescriptor[] getTagAttributeDescriptors() {
        return S_TAG_ATTRIBUTE_DESCRIPTORS;
    }



Step 3 : Initial and Start Tag Converter on ATG Start up  by putting entry in InitialService.properties file in config folder 

InitialService.properties 

$class=atg.nucleus.InitialService

initialServices+=\
    /att/ecom/checkout/view/MaskStringConverter



Step 4 :  Call from jsp, the attributes for custom component should be

<dsp:input id="maskingInputId" converter="inputStringMasking" converterattributes="numcharsunmasked=4" bean="MyInputStringBean" maxlength="25" value="${maskingInput}">   



For more details on Tag Converters and Customization, Follow ATGPlatformProgGuide.

1 comment: