Announcement

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

    How to compare two Records or RecordLists

    Are there any utility methods available on the SmartGWT client side to compare two Records to see if they are equivalent, meaning they have all of the same attributes with equal values for each. Same question for an entire RecordList.

    I'm trying to mimic the behavior of ValuesManager.valuesHaveChanged() but with Records that are manipulated by the user's choice of different custom UI components. When they're done editing the Records we want to compare the original RecordList to the edited one and see if they actually made any changes.

    #2
    There's not an existing comparison helper, but what we'd recommend is iterating down the DataSource fields and comparing only those values.

    Comment


      #3
      I think I've got a method coded to compare the values in two Records and return a Map with any differences.
      Code:
      	public static Map recordChanges(DataSource ds, Record newRecord, Record oldRecord) {
      		
      		Map changesMap = new HashMap();
      		
      		for (DataSourceField field : ds.getFields()) {
      			
      			String valueA = newRecord.getAttribute(field.getName());
      			String valueB = oldRecord.getAttribute(field.getName());
      			
      			// If one is null and the other is not, the record has changed,
      			// or if they are both not null and they are not the same string.
      			if ((valueA==null && valueB!=null)
      					|| (valueA!=null && valueB==null)
      					|| (valueA!=null && valueB!=null && !valueA.equals(valueB)))
      				changesMap.put(field.getName(), newRecord.getAttribute(field.getName()));
      		}
      		return changesMap;
      	}
      My only other issue is that I'm not sure how to make an independent copy of my RecordList to save as the "old" RecordList so I can compare it to the new one after the user makes their edits.

      RecordList.duplicate() just gives me another array pointing to the same records. I've tried ...

      for (Record rec : newRecords.toArray()) {
      oldRecords.add(new Record(rec.getJsObj()));
      }

      But that doesn't seem to give me a new set of records either. How can I clone the records to create a list of new records with the same values?

      Comment


        #4
        Create a new Record() without a JSObject and copy all relevant attributes via setAttribute(getAttribute()) while iterating over DataSourceFields.

        Comment


          #5
          That should work for my current needs as all of the attributes can be compared as Strings. But I have had other scenarios where it would be helpful to have a method that would create a clone of a Record with the same attributes and values of the same object types.

          That sounds like something that the framework code must be doing, for example, when a ValuesManager saves oldValues. Is that something that could be done easily with JSNI? If so, an example would be very helpful.

          Comment

          Working...
          X