Program: 85918
Project: 85918
Buggy source:
@RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override public PartRevision createPartRevision(PartRevisionKey revisionKey,String pDescription,String pWorkflowModelId,ACLUserEntry[] pACLUserEntries,ACLUserGroupEntry[] pACLUserGroupEntries,Map<String,Collection<String>> userRoleMapping,Map<String,Collection<String>> groupRoleMapping) throws UserNotFoundException, AccessRightException, WorkspaceNotFoundException, PartRevisionNotFoundException, NotAllowedException, FileAlreadyExistsException, CreationException, RoleNotFoundException, WorkflowModelNotFoundException, PartRevisionAlreadyExistsException, UserGroupNotFoundException {
  User user=userManager.checkWorkspaceWriteAccess(revisionKey.getPartMaster().getWorkspace());
  Locale locale=new Locale(user.getLanguage());
  PartRevisionDAO partRevisionDAO=new PartRevisionDAO(locale,em);
  PartRevision originalPartR=partRevisionDAO.loadPartR(revisionKey);
  if (originalPartR.isCheckedOut()) {
    throw new NotAllowedException(locale,"String_Node_Str");
  }
  if (originalPartR.getNumberOfIterations() == 0) {
    throw new NotAllowedException(locale,"String_Node_Str");
  }
  PartRevision partR=originalPartR.getPartMaster().createNextRevision(user);
  PartIteration lastPartI=originalPartR.getLastIteration();
  PartIteration firstPartI=partR.createNextIteration(user);
  if (lastPartI != null) {
    BinaryResourceDAO binDAO=new BinaryResourceDAO(locale,em);
    for (    BinaryResource sourceFile : lastPartI.getAttachedFiles()) {
      String fileName=sourceFile.getName();
      long length=sourceFile.getContentLength();
      Date lastModified=sourceFile.getLastModified();
      String fullName=partR.getWorkspaceId() + "String_Node_Str" + partR.getPartNumber()+ "String_Node_Str"+ partR.getVersion()+ "String_Node_Str"+ fileName;
      BinaryResource targetFile=new BinaryResource(fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      firstPartI.addAttachedFile(targetFile);
      try {
        dataManager.copyData(sourceFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
    List<PartUsageLink> newComponents=new LinkedList<>();
    List<PartUsageLink> oldComponents=lastPartI.getComponents();
    for (    PartUsageLink usage : lastPartI.getComponents()) {
      PartUsageLink newUsage=usage.clone();
      em.persist(newUsage);
      newComponents.add(newUsage);
    }
    firstPartI.setComponents(newComponents);
    em.flush();
    PathToPathLinkDAO pathToPathLinkDAO=new PathToPathLinkDAO(locale,em);
    pathToPathLinkDAO.cloneAndUpgradePathToPathLinks(oldComponents,newComponents);
    for (    Geometry sourceFile : lastPartI.getGeometries()) {
      String fileName=sourceFile.getName();
      long length=sourceFile.getContentLength();
      int quality=sourceFile.getQuality();
      Date lastModified=sourceFile.getLastModified();
      String fullName=partR.getWorkspaceId() + "String_Node_Str" + partR.getPartNumber()+ "String_Node_Str"+ partR.getVersion()+ "String_Node_Str"+ fileName;
      Geometry targetFile=new Geometry(quality,fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      firstPartI.addGeometry(targetFile);
      try {
        dataManager.copyData(sourceFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
    BinaryResource nativeCADFile=lastPartI.getNativeCADFile();
    if (nativeCADFile != null) {
      String fileName=nativeCADFile.getName();
      long length=nativeCADFile.getContentLength();
      Date lastModified=nativeCADFile.getLastModified();
      String fullName=partR.getWorkspaceId() + "String_Node_Str" + partR.getPartNumber()+ "String_Node_Str"+ partR.getVersion()+ "String_Node_Str"+ fileName;
      BinaryResource targetFile=new BinaryResource(fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      firstPartI.setNativeCADFile(targetFile);
      try {
        dataManager.copyData(nativeCADFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
    Set<DocumentLink> links=new HashSet<>();
    for (    DocumentLink link : lastPartI.getLinkedDocuments()) {
      DocumentLink newLink=link.clone();
      links.add(newLink);
    }
    firstPartI.setLinkedDocuments(links);
    List<InstanceAttribute> attrs=new ArrayList<>();
    for (    InstanceAttribute attr : lastPartI.getInstanceAttributes()) {
      InstanceAttribute clonedAttribute=attr.clone();
      attrs.add(clonedAttribute);
    }
    firstPartI.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(originalPartR.getWorkspaceId(),roleName));
      Set<User> users=new HashSet<>();
      roleUserMap.put(role,users);
      for (      String login : userLogins) {
        User u=userDAO.loadUser(new UserKey(originalPartR.getWorkspaceId(),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(originalPartR.getWorkspaceId(),roleName));
      Set<UserGroup> groups=new HashSet<>();
      roleGroupMap.put(role,groups);
      for (      String groupId : groupIds) {
        UserGroup g=groupDAO.loadUserGroup(new UserGroupKey(originalPartR.getWorkspaceId(),groupId));
        groups.add(g);
      }
    }
    WorkflowModel workflowModel=new WorkflowModelDAO(locale,em).loadWorkflowModel(new WorkflowModelKey(user.getWorkspaceId(),pWorkflowModelId));
    Workflow workflow=workflowModel.createWorkflow(roleUserMap,roleGroupMap);
    partR.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,partR);
  }
  partR.setDescription(pDescription);
  if ((pACLUserEntries != null && pACLUserEntries.length > 0) || (pACLUserGroupEntries != null && pACLUserGroupEntries.length > 0)) {
    ACL acl=new ACL();
    if (pACLUserEntries != null) {
      for (      ACLUserEntry entry : pACLUserEntries) {
        acl.addEntry(em.getReference(User.class,new UserKey(user.getWorkspaceId(),entry.getPrincipalLogin())),entry.getPermission());
      }
    }
    if (pACLUserGroupEntries != null) {
      for (      ACLUserGroupEntry entry : pACLUserGroupEntries) {
        acl.addEntry(em.getReference(UserGroup.class,new UserGroupKey(user.getWorkspaceId(),entry.getPrincipalId())),entry.getPermission());
      }
    }
    partR.setACL(acl);
  }
  Date now=new Date();
  partR.setCreationDate(now);
  partR.setCheckOutUser(user);
  partR.setCheckOutDate(now);
  firstPartI.setCreationDate(now);
  firstPartI.setModificationDate(now);
  partRevisionDAO.createPartR(partR);
  return partR;
}

Fixed version (ground truth):
@RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override public PartRevision createPartRevision(PartRevisionKey revisionKey,String pDescription,String pWorkflowModelId,ACLUserEntry[] pACLUserEntries,ACLUserGroupEntry[] pACLUserGroupEntries,Map<String,Collection<String>> userRoleMapping,Map<String,Collection<String>> groupRoleMapping) throws UserNotFoundException, AccessRightException, WorkspaceNotFoundException, PartRevisionNotFoundException, NotAllowedException, FileAlreadyExistsException, CreationException, RoleNotFoundException, WorkflowModelNotFoundException, PartRevisionAlreadyExistsException, UserGroupNotFoundException {
  User user=userManager.checkWorkspaceWriteAccess(revisionKey.getPartMaster().getWorkspace());
  Locale locale=new Locale(user.getLanguage());
  PartRevisionDAO partRevisionDAO=new PartRevisionDAO(locale,em);
  PartRevision originalPartR=partRevisionDAO.loadPartR(revisionKey);
  if (originalPartR.isCheckedOut()) {
    throw new NotAllowedException(locale,"String_Node_Str");
  }
  if (originalPartR.getNumberOfIterations() == 0) {
    throw new NotAllowedException(locale,"String_Node_Str");
  }
  PartRevision partR=originalPartR.getPartMaster().createNextRevision(user);
  PartIteration lastPartI=originalPartR.getLastIteration();
  PartIteration firstPartI=partR.createNextIteration(user);
  if (lastPartI != null) {
    BinaryResourceDAO binDAO=new BinaryResourceDAO(locale,em);
    for (    BinaryResource sourceFile : lastPartI.getAttachedFiles()) {
      String fileName=sourceFile.getName();
      long length=sourceFile.getContentLength();
      Date lastModified=sourceFile.getLastModified();
      String fullName=partR.getWorkspaceId() + "String_Node_Str" + partR.getPartNumber()+ "String_Node_Str"+ partR.getVersion()+ "String_Node_Str"+ fileName;
      BinaryResource targetFile=new BinaryResource(fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      firstPartI.addAttachedFile(targetFile);
      try {
        dataManager.copyData(sourceFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
    List<PartUsageLink> newComponents=new LinkedList<>();
    List<PartUsageLink> oldComponents=lastPartI.getComponents();
    for (    PartUsageLink usage : lastPartI.getComponents()) {
      PartUsageLink newUsage=usage.clone();
      em.persist(newUsage);
      newComponents.add(newUsage);
    }
    firstPartI.setComponents(newComponents);
    em.flush();
    PathToPathLinkDAO pathToPathLinkDAO=new PathToPathLinkDAO(locale,em);
    pathToPathLinkDAO.cloneAndUpgradePathToPathLinks(oldComponents,newComponents);
    for (    Geometry sourceFile : lastPartI.getGeometries()) {
      String fileName=sourceFile.getName();
      long length=sourceFile.getContentLength();
      int quality=sourceFile.getQuality();
      Date lastModified=sourceFile.getLastModified();
      String fullName=partR.getWorkspaceId() + "String_Node_Str" + partR.getPartNumber()+ "String_Node_Str"+ partR.getVersion()+ "String_Node_Str"+ fileName;
      Geometry targetFile=new Geometry(quality,fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      firstPartI.addGeometry(targetFile);
      try {
        dataManager.copyData(sourceFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
    BinaryResource nativeCADFile=lastPartI.getNativeCADFile();
    if (nativeCADFile != null) {
      String fileName=nativeCADFile.getName();
      long length=nativeCADFile.getContentLength();
      Date lastModified=nativeCADFile.getLastModified();
      String fullName=partR.getWorkspaceId() + "String_Node_Str" + partR.getPartNumber()+ "String_Node_Str"+ partR.getVersion()+ "String_Node_Str"+ fileName;
      BinaryResource targetFile=new BinaryResource(fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      firstPartI.setNativeCADFile(targetFile);
      try {
        dataManager.copyData(nativeCADFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
    Set<DocumentLink> links=new HashSet<>();
    for (    DocumentLink link : lastPartI.getLinkedDocuments()) {
      DocumentLink newLink=link.clone();
      links.add(newLink);
    }
    firstPartI.setLinkedDocuments(links);
    List<InstanceAttribute> attrs=new ArrayList<>();
    for (    InstanceAttribute attr : lastPartI.getInstanceAttributes()) {
      InstanceAttribute clonedAttribute=attr.clone();
      attrs.add(clonedAttribute);
    }
    firstPartI.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(originalPartR.getWorkspaceId(),roleName));
      Set<User> users=new HashSet<>();
      roleUserMap.put(role,users);
      for (      String login : userLogins) {
        User u=userDAO.loadUser(new UserKey(originalPartR.getWorkspaceId(),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(originalPartR.getWorkspaceId(),roleName));
      Set<UserGroup> groups=new HashSet<>();
      roleGroupMap.put(role,groups);
      for (      String groupId : groupIds) {
        UserGroup g=groupDAO.loadUserGroup(new UserGroupKey(originalPartR.getWorkspaceId(),groupId));
        groups.add(g);
      }
    }
    WorkflowModel workflowModel=new WorkflowModelDAO(locale,em).loadWorkflowModel(new WorkflowModelKey(user.getWorkspaceId(),pWorkflowModelId));
    Workflow workflow=workflowModel.createWorkflow(roleUserMap,roleGroupMap);
    partR.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();
    }
    mailer.sendApproval(runningTasks,partR);
  }
  partR.setDescription(pDescription);
  if ((pACLUserEntries != null && pACLUserEntries.length > 0) || (pACLUserGroupEntries != null && pACLUserGroupEntries.length > 0)) {
    ACL acl=new ACL();
    if (pACLUserEntries != null) {
      for (      ACLUserEntry entry : pACLUserEntries) {
        acl.addEntry(em.getReference(User.class,new UserKey(user.getWorkspaceId(),entry.getPrincipalLogin())),entry.getPermission());
      }
    }
    if (pACLUserGroupEntries != null) {
      for (      ACLUserGroupEntry entry : pACLUserGroupEntries) {
        acl.addEntry(em.getReference(UserGroup.class,new UserGroupKey(user.getWorkspaceId(),entry.getPrincipalId())),entry.getPermission());
      }
    }
    partR.setACL(acl);
  }
  Date now=new Date();
  partR.setCreationDate(now);
  partR.setCheckOutUser(user);
  partR.setCheckOutDate(now);
  firstPartI.setCreationDate(now);
  firstPartI.setModificationDate(now);
  partRevisionDAO.createPartR(partR);
  if (runningTasks != null) {
    mailer.sendApproval(runningTasks,partR);
  }
  return partR;
}