//Name parse task description
//Description parses the task description and fills some custom fields given in the description in text form
//Type Trigger / Create Task / AFTER Trigger
//Comments: often, there is a requirement to parse the e-mails of given format and to create bug reports. 
// TrackStudio has the functionality to creates the tasks from mail import.
// To set user-defined fields from the mail body, you will need this script.
// This script works with the template of following format (# is a part of template!),
//
// There are line where the value will be taken as as:
//#<Name of custom field as defined in TrackStudio>: some text
// like:
//#Hardware: Intel PXX 1,6 Mh
// The user-defined field "Hardware" will be set to "Intel PXX 1,6 Mhz"
//
// and lines with multiple choice, where the last word in the line will be used
//#<Name of custom field as defined in TrackStudio>: Value1 / Value2 /Value3 / Valueused
// These lines are often used for selection of hardware or software variants
// i.e you give the template with multiple choice and a customer removes all values from the end of line until he value he needs, like
// you give the template with
//#Component: Backup/Database/BackOffice/Storefront
// and customer send in
//#Component: Backup/Database/BackOffice
// if the bug is related to BackOffice
import java.util.regex.Pattern;
import java.util.regex.Matcher;

String []customFieldsAll = {"Customfield1", "Customfield2"};
String []customFieldsLastW = {"System", "Component"};


String desc=task.getDescription();
SecuredUDFAdapterManager am=AdapterManager.getInstance().getSecuredUDFAdapterManager();

for(int i = 0; i < customFieldsLastW.length; i++ ) {
  String regexp="[\\s\\S]*^\\s*#"+customFieldsLastW[i]+"\\s*:.*\\b(\\w+)\\s*$[\\s\\S]*";
  Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);
  Matcher matcher = pattern.matcher(desc);
  if (matcher.matches()) {
    am.setTaskUDFValueSimple(task.getSecure(), task.getId(), customFieldsLastW[i], matcher.group(1));
  }
 }
for(int i = 0; i < customFieldsAll.length; i++ ) {
  String regexp="[\\s\\S]*^\\s*#"+customFieldsAll[i]+"\\s*:\\s*\\b(.*)\\b\\s*$[\\s\\S]*";
  Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);
  Matcher matcher = pattern.matcher(desc);
  if (matcher.matches()) {
    am.setTaskUDFValueSimple(task.getSecure(), task.getId(), customFieldsAll[i], matcher.group(1));
  }
 }

return task;
