Tuesday, 2 September 2014

Enable Sonar JavaScript plugin to scan WebSphere Commerce Stores project

Assuming we need to scan Stores project, follow the steps below:

  • Under sonar.modules, add extra entry called StoresJS

    sonar.modules = WebSphereCommerceServerExtensionsLogic,CyberSourceAddOn,Stores,StoresJS,PaypalAddOn,GenericPaymentPlugin,FirstDataAddOn,RewardCardAddOn,Rest
    • Add the entries below to configure the new StoresJS module including the language scanner engine to use
    StoresJS.projectName = Stores
    StoresJS.sonar.sources = WebContent
    StoresJS.sonar.language = js
    StoresJS.sonar.projectBaseDir = ${WORKSPACE}/Stores

    • Update sonar.exclusions to make sure all js files which are OOB (WebSphere Commerce specific) as well as Dojo or JQuery libraries are excluded from scanning. Example below shows how to excluded dojo18 folder and all of its subfolders. It will be easier if all project specific JS files are grouped under one folder and include it under StoresJS.sonar.sources, to avoid updating sonar.exclusions.
    sonar.exclusions=org/omg/**/*.java,com/ibm/**/*.java,**/*.jsp*,com/tmpl/**/*.java,**/dojo18/**

    • Repeat the steps above for any other custom web application you might have (if required).
    Hints:
    • Sonar JS plugin is not based on JSLint or JSHint engines and developers should expect to find different coding issues reported.

    Thursday, 28 July 2011

    JavaScript code to submit parameters manually using Dojo xhrPost

    The sample code below shows how to fill in the content variable within xhrArgs in the right way with the parameters you need to send over to the server in a Post request.
    The code below is useful to manually build up the parameters list.

    function verifyGiftCard(e,num) {
    giftCardNumber=dojo.byId('gift_card_number_1').value;
    giftCardPin=dojo.byId('gift_card_pin_1').value;

    var xhrArgs = {
    url: "/webapp/wcs/stores/servlet/DoPPTGiftCardsBalanceEnquiries",
    content:{
    'dcGiftCardNumber':giftCardNumber,
    'dcGiftCardPin':giftCardNumber
    },
    handleAs: "text",
    load: function(data) {
    console.debug("AJAX call successful");
    console.debug(data);
    },
    error: function(error) {
    console.debug("An unexpected error occurred: ");
    }
    }

    //Call the asynchronous xhrGet
    var deferred = dojo.xhrPost(xhrArgs);
    e.preventDefault();
    }

    Cancel submit event using JavaScript

    Usually when working with AJAX, you need to prevent the form submission from actually happening to prevent the page from refreshing and may be going to a URL which can only serve AJAX calls. The sample below shows how to use event.preventDefault() to stop the click action from propagating. Take care from having a JS coding error just before event.preventDefault() as the method execution in such case will halt and form submission will not stop.

    <input name="gift_card_verify" id="gift_card_verify" type="submit" onclick="verifyGiftCard(event);"/>

    <script type="text/javascript">
    function verifyGiftCard(event) {
    //Do your logic for example submit an AJAX request
    event.preventDefault();
    }
    </script>

    Thursday, 2 June 2011

    Sort the fetch search results of Dojo's ItemAndOrReadStore

    The JavaScript/Dojo sample below shows how to sort the search results of AndOrReadStore by a specific field (service_name in our example) within the loaded JSON file. Take care that sorting is case sensitive. In a more advanced version, in which I automated the generation of the JSON file, I included a special sort key which is simply the transformation of the service_name to upper case.

    var itemAndORStore = new dojox.data.AndOrReadStore( { url : 'serviceWorks/json/services.js' });
    itemAndORStore.fetch( { queryOptions : { ignoreCase : true }, query:{complexQuery : finalQuery}, onComplete : onComplete_3, onError : onError, sort: [{attribute: "service_name"}] });

    Use JavaScript to force URL redirect to HTTPS

    The function below checks the current URL and if it is HTTP, it will force change to HTTPS.

    function checkRedirect() {
    console.debug("checkRedirect");
    var protocol = window.location.protocol;
    console.debug("checkRedirect: current protocol is ", protocol);
    if (protocol == "http:") {
    var newUrl = "https://" + window.location.hostname + "/" + window.location.pathname;
    console.debug("checkRedirect: redirect page to ", newUrl);
    window.location = newUrl;
    console.debug("HTTPS redirect is disabled");
    }
    }