Search code examples
mysqlregexactionscript-3apache-flexesri

How to replace a comma between a closed parenthesis and an open parenthesis in ActionScript 3


The basis of this question comes from a FLEX mapping application where I am using checkboxes in a widget to generate a query string for several fields in a geospatial database that is served in a feature layer. I am stuck on concatenating each query after the program compiles each of the queries for each available field in the database. That is shown below towards the end of the function. Here is the function that loops through the checkboxes, generates the query strings, and attempts to concatenate the query strings:

public function compileDefexp():void {
    //conFilter is an HBox that contains the available field names in checkboxes.
    var countfilt:uint = conFILTER.numElements;
    var defExpGEO_ACC:String = "";
    var defExpACT_TYP:String = "";
    var defExp:Array = [];

    //clearing filterList on each checkbox click
    filterList = [];

    //loop through the field and field value checkboxes to generate the query strings
    for(var i:int = 0;i < countfilt; i++){
        var childFilter:Object = conFILTER.getElementAt(i);
        var filterName:String = childFilter.name;
        if(childFilter.selected){
            filterList.push(filterName);
            if(filterName == "GEO_ACC"){
                compileValuelistgeo();
                defExpGEO_ACC = "(" + filterName + ") IN (" + qValuelistGEO_ACC + ")";
                defExp.push(defExpGEO_ACC);
            }else if(filterName == "ACT_TYP"){
                compileValuelistact();
                defExpACT_TYP = "(" + filterName + ") IN (" + qValuelistACT_TYP + ")";
                defExp.push(defExpACT_TYP);
            }
        }
    }

    var defExpStr:String = defExp.toString();
    var countfilterList:uint = filterList.length;
    var filterListstr:String = filterList.toString();

    //Cannot get replace the comma between the closed and open parentheses with AND
    //We need to accomplish this to concatenate the query strings for the definition
    //expression to execute correctly.

    //Tried to use RegExp
    var strPattern:RegExp = /\),\(/g;
    var strReplace:RegExp = /\) AND \(/g;

    defExpStr.replace(strPattern, strReplace);

    geoPoints.definitionExpression = defExpStr;

    trace(defExpStr); //(GEO_ACC) IN (GA Value 1,GA Value 2),(ACT_TYP) IN (AT Value 1)

    //We need this to be:
    //(GEO_ACC) IN (GA Value 1,GA Value 2) AND (ACT_TYP) IN (AT Value 1)
}

Solution

  • I experimented a bit with if statements and utilizing the array's index to compile the query statements together based on the number of filter checkboxes selected. Essentially, instead of attempting to replace the comma in the array we will concatenate each query statement by the index number of the array. The code below is placed after each querylist is compiled:

    var defExpStr:String;
    if (filterList.length == 1){
        defExpStr = defExp[0];
    }else if (filterList.length == 2){
         defExpStr = defExp[0] + " AND " + defExp[1];
    }
    
    geoPoints.definitionExpression = defExpStr;
    geoPoints.visible = true;
    

    In conclusion, if you need a tool that will allow the end user to select multiple fields and values in a database and configure a query with checkboxes this code will do that. This code was specifically developed for the ArcGIS API for FLEX and works on geospatial data that has been serviced through an ArcServer.