Axeda Groovy Scripto: Retrieving list of Models

    This script will return, in XML format, all models for a particular user. It is designed to be called as a web service, in which case the username parameter will be supplied by the platform from the user authentication information passed in the web service call. You should define this script as a Custom Object of type Action.

     

    You can test this script in the Groovy development environment on the platform by explicitly supplying the username parameter (i.e., your email address).

     

    import com.axeda.drm.sdk.Context;
    import com.axeda.drm.sdk.user.User;
    import com.axeda.drm.sdk.device.*;
    import com.axeda.drm.sdk.model.*;
    import com.axeda.common.sdk.jdbc.StringQuery;
    import java.util.*;
    import groovy.xml.MarkupBuilder
    import org.custommonkey.xmlunit.*
    import com.axeda.common.sdk.id.Identifier;
    
    def writer
    def xml
    
    try {
      String username = parameters.username
    
      Context ctx = Context.create(username);
      ModelFinder mf = new ModelFinder(ctx);
    
      List dList = mf.findAll();
      Context.create();
      writer = new StringWriter()
      xml = new MarkupBuilder(writer)
      xml.Response() {
        for (d in dList) Model('name': d.getName());
      }
    } catch (Exception ex) {
      writer = new StringWriter()
      xml = new MarkupBuilder(writer)
      xml.Response() {
        Fault {
          Code('Groovy Exception')
          Message(ex.getMessage())
          StringWriter sw = new StringWriter();
          PrintWriter pw = new PrintWriter(sw);
          ex.printStackTrace(pw);
          Detail(sw.toString())
        }
      }
    }
    //logger.info(writer.toString());
    return ['Content-Type': 'text/xml', 'Content': writer.toString()]