Thanks for your answer @priya.rajagopal !
I’m working with 1.5 version. And yes, by register I meant CB document.
I have checked the links that you wrote in your comment, but I haven’t found a solution for my problem. I’m going to try to explain it better because I don’t know how manage with it.
The thing is that I’m creating a conflict resolution system like a Control Version System that the user can decide when there is some conflict between a local change and a remote one with the same document which one change has to be applied.
The different cases that I think that could create a conflict are:
- Local User and Remote User makes a change in the same document.
- Local User removes a document that Remote User has made a change on it.
- Remote User removes a document that Local User has made a change on it.
I have the system implemented and partially working because the problem that I’m having is about conflict detection.
Here is a resume of my code:
void init()
{
...
database.addChangeListener(new Database.ChangeListener()
{
@Override
public void changed(Database.ChangeEvent changeEvent)
{
checkConflicts(changeEvent.getChanges());
}
});
}
Map<String, Object> mergeRevisions(List<SavedRevision> conflicts)
{
Map<String, Object> data = new HashMap<>();
...
//CODE ASKING FOR FIELDS CHANGE BETWEEN REVISIONS TO THE USER AND MERGING
//WHAT USER DECIDED INTO data MAP
...
return data;
}
void checkConflicts(List<DocumentChange> result)
{
try
{
for (DocumentChange dChange : result)
{
final Document document = database.getDocument(dChange.getDocumentId());
final List<SavedRevision> conflicts = document.getConflictingRevisions();
if (dChange.isConflict())
{
Map<String, Object> userChoosenProps = new HashMap<>();
// If conflicts size is 1, it's because it's related to a delete revision
if(conflicts.size() == 1)
{
String kMaintainStr = "Maintain";
String kRemoveStr = "Remove";
List<String> differentOptions = new ArrayList<>();
differentOptions.add(kMaintainStr);
differentOptions.add(kRemoveStr);
ChoiceDialog<String> dialog = new ChoiceDialog<>(kMaintainStr, differentOptions);
dialog.setTitle("Conflict with register deleted");
dialog.setHeaderText(String.format("%s", conflicts.get(0).getUserProperties().values().toString()));
dialog.setContentText("Do you want to remove this entry or maintain the showed version?");
dialog.initStyle(StageStyle.UNDECORATED);
// Traditional way to get the response value.
Optional<String> resultDialog = dialog.showAndWait();
if (resultDialog.isPresent())
{
if(resultDialog.get().compareTo(kMaintainStr) == 0)
{
//USER WANTS TO KEEP THE MODIFIED VERSION
userChoosenProps = conflicts.get(0).getUserProperties();
}
else
{
//USER WANTS TO KEEP THE REMOVED VERSION
UnsavedRevision rev = conflicts.get(0).createRevision();
rev.setIsDeletion(true);
rev.save(true);
return;
}
}
}
else
{
userChoosenProps = mergeRevisions(conflicts);
}
final Map<String, Object> mergedProps = userChoosenProps;
database.runInTransaction(new TransactionalTask()
{
@Override
public boolean run()
{
try
{
SavedRevision current = document.getCurrentRevision();
for (SavedRevision rev : conflicts)
{
UnsavedRevision newRev = rev.createRevision();
if (rev.getId().equals(current.getId()))
{
newRev.setProperties(mergedProps);
}
else
{
newRev.setIsDeletion(true);
}
newRev.save(true);
revisionConflictsAlreadyResolved.add(newRev.getId());
}
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
});
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
I’m listening for changes directly to the database to try to figure out when a conflict occurs and be able to show a UI to the user to solve manually the problem. The thing is with the detection of a conflict when there is a change on the database because when I’m solving a local - remote change for the same document that I need to mark for deletion one of the revision, automatically is calling that there is a conflict with the revisions that I have just solved.
When I’m resolving with the merged properties by the user, then a change on the database is thrown saying that there is a conflict with the new revision and I don’t understand why… I suppose that I’m doing something wrong, but I don’t have any idea what it is…
I don’t know if I have explained well, if not tell me and I’ll try to be more clear.
Thanks.