Developer

Sample GetMyWeather State

The GetMyWeather sample illustrates a typical custom-state implementation. This type of state is called a service state, because its function is to call a specific Web service (in this case a weather service), and store the results for the application to use. This type of state is commonly integrated with enterprise systems.

public class GetMyWeather extends SmappStatePlugin {        
  private static final Logger LOG = 
                              LoggerFactory.getLogger(GetMyWeather.class);        
      
  // Define Input attributes    
      
  private static final TextBoxAttribute inPostCode =                         
          new TextBoxAttribute("POSTCODE", "Zip or Postal Code", false);        
      
  // Define Output attributes    
      
  private static final OutputAttribute outWeather =                        
          new OutputAttribute("WEATHER", "Your Weather Synopsis");        
  
  private static Attribute[] stateAttr;        
  
  static {       
    stateAttr = new Attribute[] {inPostCode, outWeather};    
  }        
  private static long STATE_ID = 600000L;
        
  @Override    
  public long getStateId() {       
    return STATE_ID;    
  }        
      
  @Override    
  public String getStateName() {       
    return "Example - Get My Weather";    
  }        
      
  @Override    
  public String getRevisionString() {       
    return "1.0.0";    
  }        
      
  @Override    
  public String getStateNotes() {       
    StringBuilder sb = new StringBuilder();
    sb.append("A sample state. When executed, it checks for a ");
    sb.append("Postal/ZIP Code, and returns the weather report for ");
    sb.append(" that area.\n\n Use the following follow up states:\n ");
    sb.append("- OK: Weather report for the area was found\n ");
    sb.append("- FAIL: Unexpected error\n ");
    sb.append("- Dyn -1: Area code entered was not valid\n ");
    sb.append("- Dyn -2: No weather report for the area\n ");
    return sb.toString();
  }
        
  @Override    
  public boolean supportsFailTransition() {       
    return true;    
  } 
       
  @Override    
  protected Attribute[] getStateAttributes() {       
    return stateAttr.clone();    
  } 
       
  @Override    
  protected SmappState processStateLogic(
                             SmappStateProcessingContext context,
                             SmappStateProcessingAction action)
             throws MwizProcessingException, DBException {

    WeatherResult result = null;

    try {        
      // Call the weather Web service        
      // Details are Web service specific and therefore        
      // are encapsulated in the callWeatherService method         
              
      result = callWeatherService();            

      if (result == null)          
        return continueFail();            

      if (result.status == -1)          
        return continueDyn(-1);            

      if (result.status == -2)          
        return continueDyn(-2);            

             
      // Output attribute        
             
      outWeather.setValue(result.text);        
      return continueOk();           
    }       
    catch (DBException dbex) {        
    // Database exception can occur while saving session attributes
      LOG.error("error");        
      return continueFail();       
    }   
  }   
}