//Name 	 set_resolution_to_next_step
//Description 	Set Task resolution to next step from message resolution
//Type 	Trigger / Add Message / BEFORE Trigger

//You may have a process that involves many states.  One way to handle this is
//to have a workflow with each of those states.  Another way to handle this is
//to use the Task resolution field to contain the value of the next or current step and
//to have 2 messages with a list resolutions that describe each of the states.

//The workflow has two states: Awaiting processing
//                             In progress
//the message "Move to next step" takes task from "In progress" to
//"Awaiting processing" and has this srcript attached to it.
//the message "Move to previous step" takes the task from "In progress" to
//"Awaiting processing" and has the script set_resolution_to_previous_step attached to it.
//Both message types have a set of resolutions in the following style:
//01 Step one
//02 Step two
//03 Step three
//04 Step.... etc

//Another message "Work started" takes the task from state "Awaiting processing"
//to "In progress".

//This script also forces a change of handler when the step is changed


String stepCurrent=null;
String stepNext=null;

//Get the resolution of the message
if (message.getResolution()!=null)
stepNext= message.getResolution().getName();

//Get the current value of the Task resolution
if (message.getTask().getResolution().getName()!=null){
stepCurrent = message.getTask().getResolution().getName();
}

int iCurrent = 0;
int iCurrent = 0;

//Convert the step strings into Integers
if (stepCurrent!=null)
    iCurrent = Integer.parseInt(stepCurrent.substring(0,2));

if (stepNext!= null)
    iNext = Integer.parseInt(stepNext.substring(0,2));

if (iNext<=iCurrent && iNext!=0 && iCurrent!=0){
    throw new UserMessageException("You must set a resolution that is a processing step higher than the current one.");
} else {
    if (message.getHandler().equals(message.getTask().getHandler())){
        throw new UserMessageException("You must also change the handler when moving to the next step.");
    }
}

return message;
