Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    DMI changing records needs to call dsResponse.setData(modifiedRecords)?

    Using SmartGWT EE 2.4. I want to load an Address for each User. Below is my DMI class and a junta test case. The test fails if the code is run as shown. It passes if the "resp.setData(dataList);" line is uncommented. Based on the quick start guide, though, it should pass even with the line commented out. The guide says on page 47 under "Adding DMI Business Logic"…

    "For example, adding calculated values derived from DataSource data, or trimming data that the user is not allowed to see. Typically, use dsResponse.getRecords() and iterate over the returned records, adding or modifying properties. If records should be eliminated, pass the modified List of records to dsResponse.setData()."

    Is there something missing in my code?

    // ------ class under test ----------------------
    public class UserDMI {

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public DSResponse addAdressesToUsers(DSRequest dsRequest) throws Exception {
    DSResponse resp = dsRequest.execute();
    List<?> records = resp.getRecords();
    for (Object obj : records) {
    if (obj instanceof Map) {
    DSRequest addrReq = new DSRequest();
    addrReq.setDataSourceName("Address");
    addrReq.setOperationType("fetch");

    final Map record = (Map) obj;
    Object addrId = record.get("address_id");
    if (addrId != null) {
    final HashMap crit = new HashMap();
    crit.put("id", addrId);
    addrReq.setCriteria(crit);
    DSResponse r = addrReq.execute();
    record.put("address", r.getDataMap());
    }
    }
    }
    // JUnit test passes only if the next line is un-commented.
    // resp.setData(dataList);
    return resp;
    }

    }


    // ------ test ----------------------------------
    public class UserDMITest {

    @Test
    public void addAdressesToUsers() throws Exception {
    ISCInit.go();
    UserDMI unit = new UserDMI();

    DSRequest dsRequest = new DSRequest();
    dsRequest.setDataSourceName("User");
    dsRequest.setOperationType("fetch");


    DSResponse resp = unit.addAdressesToUsers(dsRequest);
    assertNotNull(resp);
    final List<?> data = resp.getDataList();
    assertNotNull(data);
    for (Object o : data) {
    assertTrue(o instanceof Map);
    final Map m = (Map)o;

    if (m.get("address_id") != null) {
    assertTrue("User " + m.get("id") + " should have a fullAddress based on address id = "+
    m.get("address_id") +".",
    m.containsKey("address"));
    final Map addrMap = (Map) m.get("address");
    if (m.get("address_id") != null) assertNotNull(addrMap.get("city"));
    } else {
    assertFalse("Record " + m + " should not have a fullAddress.", m.containsKey("address"));
    }
    }
    }
    }

    ,boz
    ps: thanks for this thread that helped me get my tests going: http://forums.smartclient.com/showthread.php?t=15058

    #2
    Oops.
    The commented out line should read "resp.setData(records);"

    Comment


      #3
      So to clarify, you're thinking that you should not have to call setData() because you did not eliminate records, only changed properties?

      Comment


        #4
        Correct. That's how I read the guide. (Maybe it's a bug in the guide?)

        But I also assume that calling setData() shouldn't cause any trouble.

        Comment


          #5
          Yes, the wording in the guide is ambiguous and we'll fix it, the reality is that you should always call setData() and it's always fine to do so. In certain cases it would not technically be required but it's harmless to call anyway, so we'll just say to call it.

          Comment

          Working...
          X