Question

I'm working with beanshell to parse SWIFT data and need to extract values by referencing these SWIFT tags. Right now, I statically get these values as such:

String getACRU = swiftMessage.getTagData("19A",":ACRU//");
String getANTO = swiftMessage.getTagData("19A",":ANTO//");
String getCHAR = swiftMessage.getTagData("19A",":CHAR//");
String getCOUN = swiftMessage.getTagData("19A",":COUN//");
String getEXEC = swiftMessage.getTagData("19A",":EXEC//");
String getISDI = swiftMessage.getTagData("19A",":ISDI//");
String getLADT = swiftMessage.getTagData("19A",":LADT//");
String getLEVY = swiftMessage.getTagData("19A",":LEVY//");
String getLOCL = swiftMessage.getTagData("19A",":LOCL//");
String getLOCO = swiftMessage.getTagData("19A",":LOCO//");
String getMARG = swiftMessage.getTagData("19A",":MARG//");
String getOTHR = swiftMessage.getTagData("19A",":OTHR//");
String getPOST = swiftMessage.getTagData("19A",":POST//");
String getREGF = swiftMessage.getTagData("19A",":REGF//");
String getSHIP = swiftMessage.getTagData("19A",":SHIP//");
String getSPCN = swiftMessage.getTagData("19A",":SPCN//");
String getSTAM = swiftMessage.getTagData("19A",":STAM//");
String getSTEX = swiftMessage.getTagData("19A",":STEX//");
String getTRAN = swiftMessage.getTagData("19A",":TRAN//");
String getTRAX = swiftMessage.getTagData("19A",":TRAX//");
String getVATA = swiftMessage.getTagData("19A",":VATA//");
String getWITH = swiftMessage.getTagData("19A",":WITH//");
String getCOAX = swiftMessage.getTagData("19A",":COAX//");
String getACCA = swiftMessage.getTagData("19A",":ACCA//");

My question is two-fold: what's the best way to elegantly rewrite this and what is the best way in beanshell to add a method/function that would remove the first three characters, change the comma to a period and once all those values have been parsed out of the message, to add them all up?

Was it helpful?

Solution 2

It seems that this worked quite well. Store all the values I need in an array:

String [] tagArray = new String [] { ":ACRU//",":ANTO//",":CHAR//",":COUN//",":EXEC//",":ISDI//",":LADT//",":LEVY//",":LOCL//",":LOCO//",":MARG//",":OTHR//",":POST//",":REGF//",":SHIP//",":SPCN//",":STAM//",":STEX//",":TRAN//",":TRAX//",":VATA//",":WITH//",":COAX//",":ACCA//" };

And create a function to loop and add:

double sumTags(SwiftMessage inboundSwiftmessage, String inboundTagNumber, String [] inboundTagArray){
  double getTotal;
  for( tagArrayData : inboundTagArray ){
    String getData = stripData(inboundSwiftmessage.getTagData(inboundTagNumber,tagArrayData));
    getTotal = getTotal + Double.parseDouble(getData);
  }
  return getTotal;
}

And this is the function to remove the first 3 characters and convert, then remove, the comma into a period:

String stripData(String inboundString){
  if (inboundString==null){
    return "0";
  }
  else
  {
    char strippedString;
    StringBuffer strippedBuffer = new StringBuffer("");
    char [] inboundArray = inboundString.toCharArray();
    for (int counter = 3 ; counter < inboundArray.length; counter++)
    {
      strippedString = inboundArray[counter];
      strippedBuffer.append(strippedString);
    }
    return strippedBuffer.toString().replace(",",".");
  }
}

OTHER TIPS

Sorry, I'm still newbie on BeanShell and Java, but can it does works? (It's like a workaround...)

String [] tagArray = new String []
{ "ACRU", "ANTO", "CHAR", "COUN", "EXEC",
  "ISDI", "LADT", "LEVY", "LOCL", "LOCO",
  "MARG", "OTHR", "POST", "REGF", "SHIP",
  "SPCN", "STAM", "STEX", "TRAN", "TRAX",
  "VATA", "WITH", "COAX", "ACCA" };

for (i: tagArray) {
  // it was a test: print(i);
  eval("String get" + i + " = swiftMessage.getTagData(\"19A\", \":" + i + "//\")");
}

(Sorry for my bad english too...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top