Skip to content
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
Assignees

Comments

@efreet007
Copy link

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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
Copy link
Author

Great, I am waiting for this feature

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

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
Copy link
Member

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
Copy link
Member

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

@eladh
Copy link

eladh commented Mar 2, 2015

thanks :-)

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

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

@bartoszwalacik
Copy link
Member

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants