Program: 85916
Project: 85916
Buggy source:
@RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override public DocumentRevision[] createDocumentRevision(DocumentRevisionKey pOriginalDocRPK,String pTitle,String pDescription,String pWorkflowModelId,ACLUserEntry[] pACLUserEntries,ACLUserGroupEntry[] pACLUserGroupEntries,Map<String,Collection<String>> userRoleMapping,Map<String,Collection<String>> groupRoleMapping) throws UserNotFoundException, AccessRightException, WorkspaceNotFoundException, NotAllowedException, DocumentRevisionAlreadyExistsException, CreationException, WorkflowModelNotFoundException, RoleNotFoundException, DocumentRevisionNotFoundException, FileAlreadyExistsException, UserGroupNotFoundException {
  User user=userManager.checkWorkspaceWriteAccess(pOriginalDocRPK.getDocumentMaster().getWorkspace());
  Locale locale=new Locale(user.getLanguage());
  DocumentRevisionDAO docRDAO=new DocumentRevisionDAO(locale,em);
  DocumentRevision originalDocR=docRDAO.loadDocR(pOriginalDocRPK);
  DocumentMaster docM=originalDocR.getDocumentMaster();
  Folder folder=originalDocR.getLocation();
  checkFolderWritingRight(user,folder);
  if (originalDocR.isCheckedOut()) {
    throw new NotAllowedException(locale,"String_Node_Str");
  }
  if (originalDocR.getNumberOfIterations() == 0) {
    throw new NotAllowedException(locale,"String_Node_Str");
  }
  DocumentRevision docR=docM.createNextRevision(user);
  DocumentIteration lastDoc=originalDocR.getLastIteration();
  DocumentIteration firstIte=docR.createNextIteration(user);
  if (lastDoc != null) {
    BinaryResourceDAO binDAO=new BinaryResourceDAO(locale,em);
    for (    BinaryResource sourceFile : lastDoc.getAttachedFiles()) {
      String fileName=sourceFile.getName();
      long length=sourceFile.getContentLength();
      Date lastModified=sourceFile.getLastModified();
      String fullName=docR.getWorkspaceId() + "String_Node_Str" + docR.getId()+ "String_Node_Str"+ docR.getVersion()+ "String_Node_Str"+ fileName;
      BinaryResource targetFile=new BinaryResource(fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      firstIte.addFile(targetFile);
      try {
        dataManager.copyData(sourceFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
    Set<DocumentLink> links=new HashSet<>();
    for (    DocumentLink link : lastDoc.getLinkedDocuments()) {
      DocumentLink newLink=link.clone();
      links.add(newLink);
    }
    firstIte.setLinkedDocuments(links);
    List<InstanceAttribute> attrs=new ArrayList<>();
    for (    InstanceAttribute attr : lastDoc.getInstanceAttributes()) {
      InstanceAttribute clonedAttribute=attr.clone();
      attrs.add(clonedAttribute);
    }
    firstIte.setInstanceAttributes(attrs);
  }
  if (pWorkflowModelId != null) {
    UserDAO userDAO=new UserDAO(locale,em);
    UserGroupDAO groupDAO=new UserGroupDAO(locale,em);
    RoleDAO roleDAO=new RoleDAO(locale,em);
    Map<Role,Collection<User>> roleUserMap=new HashMap<>();
    for (    Map.Entry<String,Collection<String>> pair : userRoleMapping.entrySet()) {
      String roleName=pair.getKey();
      Collection<String> userLogins=pair.getValue();
      Role role=roleDAO.loadRole(new RoleKey(pOriginalDocRPK.getDocumentMaster().getWorkspace(),roleName));
      Set<User> users=new HashSet<>();
      roleUserMap.put(role,users);
      for (      String login : userLogins) {
        User u=userDAO.loadUser(new UserKey(pOriginalDocRPK.getDocumentMaster().getWorkspace(),login));
        users.add(u);
      }
    }
    Map<Role,Collection<UserGroup>> roleGroupMap=new HashMap<>();
    for (    Map.Entry<String,Collection<String>> pair : groupRoleMapping.entrySet()) {
      String roleName=pair.getKey();
      Collection<String> groupIds=pair.getValue();
      Role role=roleDAO.loadRole(new RoleKey(pOriginalDocRPK.getDocumentMaster().getWorkspace(),roleName));
      Set<UserGroup> groups=new HashSet<>();
      roleGroupMap.put(role,groups);
      for (      String groupId : groupIds) {
        UserGroup g=groupDAO.loadUserGroup(new UserGroupKey(pOriginalDocRPK.getDocumentMaster().getWorkspace(),groupId));
        groups.add(g);
      }
    }
    WorkflowModel workflowModel=new WorkflowModelDAO(locale,em).loadWorkflowModel(new WorkflowModelKey(user.getWorkspaceId(),pWorkflowModelId));
    Workflow workflow=workflowModel.createWorkflow(roleUserMap,roleGroupMap);
    docR.setWorkflow(workflow);
    for (    Task task : workflow.getTasks()) {
      if (!task.hasPotentialWorker()) {
        throw new NotAllowedException(locale,"String_Node_Str");
      }
    }
    Collection<Task> runningTasks=workflow.getRunningTasks();
    for (    Task runningTask : runningTasks) {
      runningTask.start();
    }
    em.flush();
    mailer.sendApproval(runningTasks,docR);
  }
  docR.setTitle(pTitle);
  docR.setDescription(pDescription);
  Map<String,String> userEntries=new HashMap<>();
  Map<String,String> groupEntries=new HashMap<>();
  if (pACLUserEntries != null) {
    for (    ACLUserEntry entry : pACLUserEntries) {
      userEntries.put(entry.getPrincipalLogin(),entry.getPermission().name());
    }
  }
  if (pACLUserGroupEntries != null) {
    for (    ACLUserGroupEntry entry : pACLUserGroupEntries) {
      groupEntries.put(entry.getPrincipal().getId(),entry.getPermission().name());
    }
  }
  if (!userEntries.isEmpty() || !groupEntries.isEmpty()) {
    ACLFactory aclFactory=new ACLFactory(em);
    ACL acl=aclFactory.createACL(docR.getWorkspaceId(),userEntries,groupEntries);
    docR.setACL(acl);
  }
  Date now=new Date();
  docR.setCreationDate(now);
  docR.setLocation(folder);
  docR.setCheckOutUser(user);
  docR.setCheckOutDate(now);
  firstIte.setCreationDate(now);
  firstIte.setModificationDate(now);
  docRDAO.createDocR(docR);
  return new DocumentRevision[]{originalDocR,docR};
}

Fixed version (ground truth):
@RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override public DocumentRevision[] createDocumentRevision(DocumentRevisionKey pOriginalDocRPK,String pTitle,String pDescription,String pWorkflowModelId,ACLUserEntry[] pACLUserEntries,ACLUserGroupEntry[] pACLUserGroupEntries,Map<String,Collection<String>> userRoleMapping,Map<String,Collection<String>> groupRoleMapping) throws UserNotFoundException, AccessRightException, WorkspaceNotFoundException, NotAllowedException, DocumentRevisionAlreadyExistsException, CreationException, WorkflowModelNotFoundException, RoleNotFoundException, DocumentRevisionNotFoundException, FileAlreadyExistsException, UserGroupNotFoundException {
  User user=userManager.checkWorkspaceWriteAccess(pOriginalDocRPK.getDocumentMaster().getWorkspace());
  Locale locale=new Locale(user.getLanguage());
  DocumentRevisionDAO docRDAO=new DocumentRevisionDAO(locale,em);
  DocumentRevision originalDocR=docRDAO.loadDocR(pOriginalDocRPK);
  DocumentMaster docM=originalDocR.getDocumentMaster();
  Folder folder=originalDocR.getLocation();
  checkFolderWritingRight(user,folder);
  if (originalDocR.isCheckedOut()) {
    throw new NotAllowedException(locale,"String_Node_Str");
  }
  if (originalDocR.getNumberOfIterations() == 0) {
    throw new NotAllowedException(locale,"String_Node_Str");
  }
  DocumentRevision docR=docM.createNextRevision(user);
  DocumentIteration lastDoc=originalDocR.getLastIteration();
  DocumentIteration firstIte=docR.createNextIteration(user);
  if (lastDoc != null) {
    BinaryResourceDAO binDAO=new BinaryResourceDAO(locale,em);
    for (    BinaryResource sourceFile : lastDoc.getAttachedFiles()) {
      String fileName=sourceFile.getName();
      long length=sourceFile.getContentLength();
      Date lastModified=sourceFile.getLastModified();
      String fullName=docR.getWorkspaceId() + "String_Node_Str" + docR.getId()+ "String_Node_Str"+ docR.getVersion()+ "String_Node_Str"+ fileName;
      BinaryResource targetFile=new BinaryResource(fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      firstIte.addFile(targetFile);
      try {
        dataManager.copyData(sourceFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
    Set<DocumentLink> links=new HashSet<>();
    for (    DocumentLink link : lastDoc.getLinkedDocuments()) {
      DocumentLink newLink=link.clone();
      links.add(newLink);
    }
    firstIte.setLinkedDocuments(links);
    List<InstanceAttribute> attrs=new ArrayList<>();
    for (    InstanceAttribute attr : lastDoc.getInstanceAttributes()) {
      InstanceAttribute clonedAttribute=attr.clone();
      attrs.add(clonedAttribute);
    }
    firstIte.setInstanceAttributes(attrs);
  }
  Collection<Task> runningTasks=null;
  if (pWorkflowModelId != null) {
    UserDAO userDAO=new UserDAO(locale,em);
    UserGroupDAO groupDAO=new UserGroupDAO(locale,em);
    RoleDAO roleDAO=new RoleDAO(locale,em);
    Map<Role,Collection<User>> roleUserMap=new HashMap<>();
    for (    Map.Entry<String,Collection<String>> pair : userRoleMapping.entrySet()) {
      String roleName=pair.getKey();
      Collection<String> userLogins=pair.getValue();
      Role role=roleDAO.loadRole(new RoleKey(pOriginalDocRPK.getDocumentMaster().getWorkspace(),roleName));
      Set<User> users=new HashSet<>();
      roleUserMap.put(role,users);
      for (      String login : userLogins) {
        User u=userDAO.loadUser(new UserKey(pOriginalDocRPK.getDocumentMaster().getWorkspace(),login));
        users.add(u);
      }
    }
    Map<Role,Collection<UserGroup>> roleGroupMap=new HashMap<>();
    for (    Map.Entry<String,Collection<String>> pair : groupRoleMapping.entrySet()) {
      String roleName=pair.getKey();
      Collection<String> groupIds=pair.getValue();
      Role role=roleDAO.loadRole(new RoleKey(pOriginalDocRPK.getDocumentMaster().getWorkspace(),roleName));
      Set<UserGroup> groups=new HashSet<>();
      roleGroupMap.put(role,groups);
      for (      String groupId : groupIds) {
        UserGroup g=groupDAO.loadUserGroup(new UserGroupKey(pOriginalDocRPK.getDocumentMaster().getWorkspace(),groupId));
        groups.add(g);
      }
    }
    WorkflowModel workflowModel=new WorkflowModelDAO(locale,em).loadWorkflowModel(new WorkflowModelKey(user.getWorkspaceId(),pWorkflowModelId));
    Workflow workflow=workflowModel.createWorkflow(roleUserMap,roleGroupMap);
    docR.setWorkflow(workflow);
    for (    Task task : workflow.getTasks()) {
      if (!task.hasPotentialWorker()) {
        throw new NotAllowedException(locale,"String_Node_Str");
      }
    }
    runningTasks=workflow.getRunningTasks();
    for (    Task runningTask : runningTasks) {
      runningTask.start();
    }
  }
  docR.setTitle(pTitle);
  docR.setDescription(pDescription);
  Map<String,String> userEntries=new HashMap<>();
  Map<String,String> groupEntries=new HashMap<>();
  if (pACLUserEntries != null) {
    for (    ACLUserEntry entry : pACLUserEntries) {
      userEntries.put(entry.getPrincipalLogin(),entry.getPermission().name());
    }
  }
  if (pACLUserGroupEntries != null) {
    for (    ACLUserGroupEntry entry : pACLUserGroupEntries) {
      groupEntries.put(entry.getPrincipal().getId(),entry.getPermission().name());
    }
  }
  if (!userEntries.isEmpty() || !groupEntries.isEmpty()) {
    ACLFactory aclFactory=new ACLFactory(em);
    ACL acl=aclFactory.createACL(docR.getWorkspaceId(),userEntries,groupEntries);
    docR.setACL(acl);
  }
  Date now=new Date();
  docR.setCreationDate(now);
  docR.setLocation(folder);
  docR.setCheckOutUser(user);
  docR.setCheckOutDate(now);
  firstIte.setCreationDate(now);
  firstIte.setModificationDate(now);
  docRDAO.createDocR(docR);
  if (runningTasks != null) {
    mailer.sendApproval(runningTasks,docR);
  }
  return new DocumentRevision[]{originalDocR,docR};
}