0
\$\begingroup\$

We are digging into a performance issue with our Agile system. This Process Extension (PX) is used to update the product number field with whatever is in the description field.

The product number field can only be 50 characters, so I have been using the substring method and some nested if statements to get it to work. This is where I fear I have a method that is un-optimized.

The code takes about 2 seconds to run, for each item that needs to be updated. Usually there are a few hundred items that get updated each time - this is why I have been looking for ways to optimize.

I have added a boolean, skip, which checks if the product number is already the same as the product description and skips over it if it is. This did help slightly.

This is the PX I am working with:

import com.agile.agileDSL.ScriptObj.IBaseScriptObj
import com.agile.agileDSL.ScriptObj.AgileDSLException
import com.agile.api.ItemConstants

void invokeScript(IBaseScriptObj obj) {

  //the following block of code updates the product number field with whatever has been entered in the description field
  //get part description
  String itemDescription = obj.getValueByAttId(ItemConstants.ATT_TITLE_BLOCK_DESCRIPTION);
  //check length of part description
boolean textValueExists = true;
String textvalue=null;

try{
    textvalue = obj.getValueByAttId(ItemConstants.ATT_PAGE_TWO_TEXT11);
textValueExists=true;
}catch(Exception e){
textValueExists=false;
}

if(textValueExists==true){
 itemDescriptionLength = itemDescription.length();
  //if there are more than 50 chars, set product number to first 50 characters of description (the max number of characters the product number field can hold)
 boolean skip= false;
if(textvalue!=null){
  if(textvalue.length() >= 50){
     String subValue = textvalue.substring(0,50);
        if(subValue.equalsIgnoreCase(itemDescription.substring(0,50))){
           skip=true;
        }
        } else{
          if(textvalue.equalsIgnoreCase(itemDescription)){
             skip=true;
          }  
        }
      }     
    if(!skip){
        if(itemDescriptionLength >= 50){
            obj.setValueByAttId(ItemConstants.ATT_PAGE_TWO_TEXT11, itemDescription.substring(0,50));
        } else{
            obj.setValueByAttId(ItemConstants.ATT_PAGE_TWO_TEXT11, itemDescription);
        }       
    }
}


  // get type of item that is being updated
  itemType = obj.getValueByAttId(ItemConstants.ATT_TITLE_BLOCK_ITEM_TYPE);  

  // reads properties file and loads data into a property object (props)
  File propsFile = new File('config/AgileElectronicComponents.properties');
  Properties props = new Properties(); 
  props.load(propsFile.newDataInputStream());

  // check if item type is one listed in the properties file.  if not, message is written to part history, "Not Applicable". Otherwise the script continues.
  if ((props.getProperty(itemType)) != null) {

   //declaring variables
   String desc = "";  //running variable to hold attribute values/descriptors to be added to the description
   String attributeOrStrVal;  //variable which is assigned each individual attribute value, then appended to "desc"
   String [ ] str;  //array to store values of retrieved comma separated string from properties file

   // matches itemType to property file data & retrieves string of baseID's/descriptors assigned to said itemType. Splits them into individual values in an array(str).
   str = (props.getProperty(itemType)).split(',');

   // loop through array, retrieving value from the comma separated string.  If a base ID, get attribute value, set it to uppercase and append it to desc variable. Once appended, check if there is an associated string to that base ID (attribute)
   //If there is, append that string directly after its attribute, if not continue.  If it initially encounters something other than a base ID in the main string, that string value is directly appended as is.
   for(String baseIDorStr : str){
      if (baseIDorStr.startsWith("1")){
         Value = obj.getValueByAttId(Integer.parseInt(baseIDorStr));
         desc = desc +  (Value.toUpperCase());
           if (Value != ""){
              UOM = itemType + baseIDorStr;
                 if ((props.getProperty(UOM)) != null){
                    desc = desc + (props.getProperty(UOM));
                 }
           }
      } else{
        desc = desc + baseIDorStr;
      }
   }

    skip= false;
String desValue = obj.getValueByAttId(ItemConstants.ATT_TITLE_BLOCK_DESCRIPTION);
if(desValue!=null){
if(desValue.equalsIgnoreCase(desc.trim())){
    skip=true;
      }
}
// set the description with itemType and desc variable (containing attribute values/descriptors)
if(!skip){
           obj.setValueByAttId(ItemConstants.ATT_TITLE_BLOCK_DESCRIPTION, desc.trim());     
}
  //the folowing block of code updates the prduct number field with whatever has been entered in the description field
  //get part description
  String itemDescr = obj.getValueByAttId(ItemConstants.ATT_TITLE_BLOCK_DESCRIPTION);
  //check length of part description
  itemDescrLength = itemDescr.length();
  //if there are more than 50 chars, set product number to first 50 characters of description (the max number of characters the product number field can hold)
if(textValueExists==true){
String value = obj.getValueByAttId(ItemConstants.ATT_PAGE_TWO_TEXT11);
        skip= false;    
        if(value!=null){
            if(value.length() >= 50){
                String subValue = value.substring(0,50);
                if(subValue.equalsIgnoreCase(itemDescr.substring(0,50))){
                    skip=true;
                }
            } else{
                if(value.equalsIgnoreCase(itemDescr)){
                    skip=true;
                }  
            }   
        }   
        if(!skip){
            if(itemDescrLength >= 50){
                obj.setValueByAttId(ItemConstants.ATT_PAGE_TWO_TEXT11, itemDescr.substring(0,50));
            } else{
                obj.setValueByAttId(ItemConstants.ATT_PAGE_TWO_TEXT11, itemDescr);
            }   
        }
}

   // log to Event Handler Monitor
   obj.logMonitor("Success");

   }else {
   obj.logMonitor("Not applicable");
    }
 }

I'd like to have this code loop or count to 50 as few times as possible. I fear that I have added too many checks that are redundant.

Any input is greatly appreciated.

Quick side note - this is a script px, not a java px so logging and debugging considerably harder (impossible?) than a java px. Sorry if that is too agile specific. I really am hoping someone can say "that substring method is what is super slow" or something like that.

\$\endgroup\$
  • \$\begingroup\$ I see that you're a new contributor, you might notice that your code indentation is off in your post. It would help a lot if you fixed it, because otherwise the code is really hard to read and I think that your post won't get much attention. If you need help on how to indent your code on this site (vs. how to indent in an IDE) you can ask for help here :) \$\endgroup\$ – IEatBagels Aug 12 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.