views:

65

answers:

2

Hello,

I have a complex object graph to represent operation scheduling. At one point, I have to serialize the whole graph to the web UI (via XML/JSON) to let user modify the schedule using a Javascript based gantt chart component. After the user finished editing, the state of the graph in the Java/server layer have to be synchronized with the modified state.

I'd like to ask about the best strategy to implement such state synchronization of complex object graph, e.g. how should the changes of the state represented so that mimicking the changes in the server side can be made easy? How would others implement this?

FYI, my current technology stack: JSF+Richfaces, Seam 2.1, Hibernate 3. But I believe that the solution to this problem can be used in other technology stack too.

+2  A: 

I would suggest to avoid synchronizing the two graphs but record the edit operations as "Delete Node/Insert Node/Replace Node" operations. Send this operations to the server and apply them to the original graph.

This idea stem from rewriting an AST (Abstract Syntax Tree). In eclipse JDT you have an AST from your source. When a refactoring is applied (e.g. rename a method) it should be possible to create a preview of the changes. For this to work rewrite events (Delte/Insert/Replace) are recorded and applied to a copy of the AST. From this the preview is generated. If the user accepts the events are applied to the original AST.

Your problem is similar in that way, that you have two object graphs that start out identical and should be modified in the same way. This should be much easier to implement as an synchronization operation you just have to record the events while the user is modifing the graph.

Arne
A: 

I'm working on a large-scale project with the same (normal?) problem. The designers implemented the following solution, based on DTO pattern and CMP2:

The general idea is that each bean, that represents an object in the graph, can record its own changes. Therefore each bean has an additional structure (set) where just keys (column names) of the changed bean attributes (values) are stored. The server logic uses these hints to update the stored data. The server logic can use these hints to only update the changes.

The (simplified!) bean itself looks a bit like this:

public TableBean {

  private String column1;

  // ... more fields

  private Set<String> changes = new Set<String>();

  public String getColumn1() {
    return column1;
  }

  public void setColumn1(String value) {
    if (!value.equals(column1)) {
      changes.add("column1");
      column1 = value;
    }
  }

  public Collection<String> getChanges() {
    return changes;
  } 
}

Subrelations are treated accordingly, there's another structure that records those child graphs, that have to be deleted.

This may even be a standard j2ee pattern, please comment if this the case.

(apart from this, I favour Arne's solution for new designs!)

Andreas_D
What's the name of this pattern?
bungrudi
As I tried to explain - I don't know, if this is or follows a standard pattern and I'd like to know this as well.
Andreas_D