New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track changes in collection. Tracking VO changes while looking at master Entity #98

Closed
efreet007 opened this Issue Feb 10, 2015 · 13 comments

Comments

Projects
None yet
3 participants
@efreet007

efreet007 commented Feb 10, 2015

Is it possible to track changes in collection element properties?
For example:

    @Test
    public void shouldTrackSubordinateSalaryChanges() {
        // given:
        Javers javers = JaversBuilder.javers().build();
        Employee bob = new Employee("Bob", 9_000, "Scrum master" );
        bob.addSubordinates(new Employee("Trainee One", 1_000));
        javers.commit("hr.manager", bob);

        bob.getSubordinates().get(0).setSalary(1_200);
        javers.commit("hr.manager", bob);

        // when:
        List<Change> changes = javers.getChangeHistory(InstanceIdDTO.instanceId("Bob", Employee.class),5);

        // then:
        assertThat(changes).hasSize(2).overridingErrorMessage("Expecting NewObject change and ValueChange change for salary");

    }

@bartoszwalacik bartoszwalacik self-assigned this Feb 10, 2015

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented Feb 10, 2015

Not sure what do you mean. In this example you have ValueChange (salary) but on Trainee not Bob. Why you expect to see it on Bob's change history?

@efreet007

This comment has been minimized.

efreet007 commented Feb 11, 2015

Beacuse I want to get Bob's changelog, something like this:

commit 2.0, author: hr.manager, 2015-02-11 09:49:45
changed object: org.javers.core.examples.model.Employee/Bob
list changed on 'subordinates' property: [(0).'org.javers.core.examples.model.Employee/Trainee One'
value changed on 'salary' property: '1000' -> '1200'
commit 1.0, author: hr.manager, 2015-02-11 09:49:45
new object: org.javers.core.examples.model.Employee/Bob

Is it possible?

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented Feb 11, 2015

well, I could think about some solution for that issue if Subordinate was a ValueObject (part of Boss aggregate). ValueObjects are owned by Entities and tracking changes in whole Aggregate (root Entity + ValueObject) makes sense.
Not sure if you are familiar with DDD terminology. Does it make sens in your domain?

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented Feb 12, 2015

shortly, we don't plan to implement a feature to view a change history of one Entity from another Entity.
Entities are loosely coulped in JaVers, sorry but I can't help you unless you switch this dependant object to ValueObject

@efreet007

This comment has been minimized.

efreet007 commented Feb 13, 2015

I've switched dependant object to ValueObject, still no VO changes in changelog.. Am I doing something wrong?

VO:
public class VO {
private String name;
private int salary;
public VO(String name, int salary) {
this.name = name;
this.salary = salary;
}

public void setSalary(int salary) { this.salary = salary;    }
public int getSalary() {        return salary;    }
public void setName(String name) {        this.name = name;    }
public String getName() { return name; }

}

Employee:
...
private List vos = new ArrayList<>();
public void addVO(VO vo) { vos.add(vo); }
public void setVos(List vos) { this.vos = vos; }
public List getVOs() { return vos; }
...

Test:
@test
public void shouldTrackSubordinateSalaryChanges() {
// given:
Javers javers = JaversBuilder.javers().registerValueObject(VO.class).build();
Employee bob = new Employee("Bob", 9_000, "Scrum master" );
bob.addVO(new VO("Trainee One", 1_000));
javers.commit("hr.manager", bob);

    bob.getVOs().get(0).setSalary(1_200);
    javers.commit("hr.manager", bob);

    // when:
    List<Change> changes = javers.getChangeHistory(InstanceIdDTO.instanceId("Bob", Employee.class),5);
    String changeLog = javers.processChangeList(changes, new SimpleTextChangeLog());

    // then:
    System.out.println(changeLog);
}

Changelog:
commit 1.0, author: hr.manager, 2015-02-13 11:53:11
new object: org.javers.core.examples.model.Employee/Bob

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented Feb 15, 2015

Hi, you are doing all right. Maybe I didn't express this issue clearly.
Now when you changed your domain model to Entity (master) + ValueObjects (dependant),
we have to implement in Javers a new feature, to support your use case.

With this feature, you would be able to track changes done on dependant VO while looking at master Entity.

@efreet007

This comment has been minimized.

efreet007 commented Feb 16, 2015

Great, I am waiting for this feature

@bartoszwalacik bartoszwalacik changed the title from Track changes in collection to Track changes in collection. Tracking VO changes while looking at master Entity Feb 16, 2015

@eladh

This comment has been minimized.

eladh commented Feb 18, 2015

Hi .

let me understand :

if i have an Object like EntityConn and the following Object is @entity with @id (javax.persistence) :
I cannot track changes of the children by looking at the EntityConn root object ?
is this behavior is for every Entity (in this example - will i be able to track endRuleMD changes from the entityConn ? (RuleMD is also is @entity with @id) .

public class EntityConn implements Serializable {  
    private Collection<EntityConn> children;  
    private RuleMD endRuleMD;
    private Map<String, List<EntityConnAttValue>> attValues;  
         ............  
}  

So - i need to do complex traversing on the Object Tree and for each @entity i need to call :
getChangeHistory(InstanceIdDTO.instanceId("instance@ID", EntityType.class), x) ???

10x
elad.

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented Feb 20, 2015

Hi @eladh,
Javers treats Entities as independent beeings. To see the history of an Entity instance, you need to query by its Id.

So for example if you have two entities A and B, connected by the reference:
A->B

When B changes its state to B'
A->B'
you need to query by B.id to see this change.

What you can see from the A point of view is the reference change (when changed from A->B to A->C).

What we are working now is to tracking ValueObject changes while looking at master Entity.
So if you model B as a VO you will see changes done on it, while looking at A

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented Feb 20, 2015

@efreet007, we will start working on this feature after delivering SQLRepository #67, which is almost done and available as Snapshot (requires some testing)

@eladh

This comment has been minimized.

eladh commented Mar 2, 2015

thanks :-)

@bartoszwalacik bartoszwalacik changed the title from Track changes in collection. Tracking VO changes while looking at master Entity to JQL - Track changes in collection. Tracking VO changes while looking at master Entity Mar 19, 2015

@bartoszwalacik bartoszwalacik added JQL and removed enhancement labels Mar 19, 2015

@bartoszwalacik bartoszwalacik changed the title from JQL - Track changes in collection. Tracking VO changes while looking at master Entity to Track changes in collection. Tracking VO changes while looking at master Entity Mar 19, 2015

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented Apr 29, 2015

JQL is released in javers 1.2.0
take a look at new query API and examples
http://javers.org/documentation/jql-examples/

@bartoszwalacik

This comment has been minimized.

Member

bartoszwalacik commented Apr 30, 2015

if you are using SQL DB, there is a schema migration script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment