Program: 86129
Project: 86129
Buggy source:
@RolesAllowed("String_Node_Str") public MasterDocument updateDoc(DocumentKey pKey,String pRevisionNote,InstanceAttribute[] pAttributes,DocumentKey[] pLinkKeys) throws WorkspaceNotFoundException, NotAllowedException, MasterDocumentNotFoundException, AccessRightException, UserNotFoundException, UserNotActiveException {
  User user=checkWorkspaceWriteAccess(pKey.getWorkspaceId());
  MasterDocumentDAO mdocDAO=new MasterDocumentDAO(new Locale(user.getLanguage()),em);
  MasterDocument mdoc=mdocDAO.loadMDoc(new MasterDocumentKey(pKey.getWorkspaceId(),pKey.getMasterDocumentId(),pKey.getMasterDocumentVersion()));
  if (mdoc.isCheckedOut() && mdoc.getCheckOutUser().equals(user) && mdoc.getLastIteration().getKey().equals(pKey)) {
    Document doc=mdoc.getLastIteration();
    Set<DocumentToDocumentLink> links=new HashSet<DocumentToDocumentLink>();
    for (    DocumentKey key : pLinkKeys) {
      links.add(new DocumentToDocumentLink(doc,key));
    }
    Set<DocumentToDocumentLink> linksToRemove=new HashSet<DocumentToDocumentLink>(doc.getLinkedDocuments());
    linksToRemove.removeAll(links);
    DocumentToDocumentLinkDAO linkDAO=new DocumentToDocumentLinkDAO(em);
    for (    DocumentToDocumentLink linkToRemove : linksToRemove) {
      linkDAO.removeLink(linkToRemove);
    }
    doc.setInstanceAttributes(Arrays.asList(pAttributes));
    doc.setRevisionNote(pRevisionNote);
    doc.setLinkedDocuments(links);
    return mdoc;
  }
 else {
    throw new NotAllowedException(new Locale(user.getLanguage()),"String_Node_Str");
  }
}

Fixed version (ground truth):
@RolesAllowed("String_Node_Str") public MasterDocument updateDoc(DocumentKey pKey,String pRevisionNote,InstanceAttribute[] pAttributes,DocumentKey[] pLinkKeys) throws WorkspaceNotFoundException, NotAllowedException, MasterDocumentNotFoundException, AccessRightException, UserNotFoundException, UserNotActiveException {
  User user=checkWorkspaceWriteAccess(pKey.getWorkspaceId());
  MasterDocumentDAO mdocDAO=new MasterDocumentDAO(new Locale(user.getLanguage()),em);
  MasterDocument mdoc=mdocDAO.loadMDoc(new MasterDocumentKey(pKey.getWorkspaceId(),pKey.getMasterDocumentId(),pKey.getMasterDocumentVersion()));
  if (mdoc.isCheckedOut() && mdoc.getCheckOutUser().equals(user) && mdoc.getLastIteration().getKey().equals(pKey)) {
    Document doc=mdoc.getLastIteration();
    Set<DocumentToDocumentLink> links=new HashSet<DocumentToDocumentLink>();
    for (    DocumentKey key : pLinkKeys) {
      links.add(new DocumentToDocumentLink(doc,key));
    }
    Set<DocumentToDocumentLink> linksToRemove=new HashSet<DocumentToDocumentLink>(doc.getLinkedDocuments());
    linksToRemove.removeAll(links);
    DocumentToDocumentLinkDAO linkDAO=new DocumentToDocumentLinkDAO(em);
    for (    DocumentToDocumentLink linkToRemove : linksToRemove) {
      linkDAO.removeLink(linkToRemove);
    }
    Map<String,InstanceAttribute> attrs=new HashMap<String,InstanceAttribute>();
    for (    InstanceAttribute attr : pAttributes) {
      attr.setDocument(doc);
      attrs.put(attr.getName(),attr);
    }
    List<String> keysToRemove=new LinkedList<String>();
    for (    Map.Entry<String,InstanceAttribute> entry : doc.getInstanceAttributes().entrySet()) {
      if (!attrs.containsKey(entry.getKey())) {
        keysToRemove.add(entry.getKey());
      }
    }
    for (    String key : keysToRemove) {
      doc.getInstanceAttributes().remove(key);
    }
    for (    InstanceAttribute attr : pAttributes) {
      InstanceAttribute attrToUpdate=doc.getInstanceAttributes().get(attr.getName());
      if (attrToUpdate != null) {
        attrToUpdate.setValue(attr.getValue());
      }
 else {
        attr.setDocument(doc);
        doc.getInstanceAttributes().put(attr.getName(),attr);
      }
    }
    doc.setRevisionNote(pRevisionNote);
    doc.setLinkedDocuments(links);
    return mdoc;
  }
 else {
    throw new NotAllowedException(new Locale(user.getLanguage()),"String_Node_Str");
  }
}