private void applyToBag(VersionedRepo rp, String cRef, DocumentBagObject dObj) {
   for (Map.Entry<String, String> d : dObj.getDocRefs().entrySet()) {
     boolean found = false;
     for (DocumentBagReference bagRef2 : shadow.getDocuments()) {
       DocumentBagObject dObj2 = rp.getObjectDatabase().getDocumentBag(bagRef2.getBagRef());
       if (dObj2.getDocRefs().containsKey(d.getKey())) {
         if (!dObj2.getDocRefs().get(d.getKey()).equals(d.getValue())) {
           // Here we have to see what we prefer - the target
           // could
           // have moved this document forward and
           // that is why they are different. OR we could have
           // moved it forward. Who wins? How do we know
           // whether
           // this document was modified here? - perhaps we add
           // the
           // commit to the BaseObject
           found = true;
           DocumentObject dob = rp.getObjectDatabase().getDocument(d.getValue());
           if (cRef.equals(dob.getCommitRef())) {
             stagedDocuments.put(d.getKey(), rp.getObjectDatabase().getDocument(d.getValue()));
           }
         }
       }
     }
     if (!found) {
       stagedDocuments.put(d.getKey(), rp.getObjectDatabase().getDocument(d.getValue()));
     }
   }
 }
 private void applyNewSaves(TreeObject newTreeObject, Set<String> refsToSave) {
   for (DocumentBagReference docRef : shadow.getDocuments()) {
     if (refsToSave.contains(docRef.getBagRef())) {
       newTreeObject.getDocuments().add(docRef);
     }
   }
 }
 private void applyDocuments(VersionedRepo rp, TreeObject apply, String cRef) {
   // Documents is a bit easier, simply overwrite for now...
   for (DocumentBagReference bagRef : apply.getDocuments()) {
     DocumentBagObject dObj = rp.getObjectDatabase().getDocumentBag(bagRef.getBagRef());
     applyToBag(rp, cRef, dObj);
   }
 }
 private void workOutCommitChanges(
     VersionedRepo rp,
     Set<DocumentBagObject> needToSave,
     Map<String, DocumentBagObject> maybeReuse,
     Set<String> refsToSave) {
   for (DocumentBagReference docRef : shadow.getDocuments()) {
     refsToSave.add(docRef.getBagRef());
     DocumentBagObject dbo = rp.getObjectDatabase().getDocumentBag(docRef.getBagRef());
     boolean changed = false;
     for (String key : stagedDocuments.keySet()) {
       if (dbo.getDocRefs().containsKey(key)) {
         // We need to be saving this back
         dbo.getDocRefs().remove(key);
         needToSave.add(dbo);
         refsToSave.remove(docRef.getBagRef());
         changed = true;
         break;
       }
     }
     for (String rem : removedDocuments) {
       if (dbo.getDocRefs().containsKey(rem)) {
         dbo.getDocRefs().remove(rem);
         refsToSave.remove(docRef.getBagRef());
         needToSave.add(dbo);
         changed = true;
       }
     }
     if (!changed) {
       if (dbo.getDocRefs().size() < capacity) {
         maybeReuse.put(docRef.getBagRef(), dbo);
         refsToSave.remove(docRef.getBagRef());
       }
     }
   }
 }