Program: 85944
Project: 85944
Buggy source:
@RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override public PartRevision createPartRevision(PartRevisionKey revisionKey,String pDescription,String pWorkflowModelId,ACLUserEntry[] pACLUserEntries,ACLUserGroupEntry[] pACLUserGroupEntries,Map<String,String> roleMappings) throws UserNotFoundException, AccessRightException, WorkspaceNotFoundException, PartRevisionNotFoundException, NotAllowedException, FileAlreadyExistsException, CreationException, RoleNotFoundException, WorkflowModelNotFoundException, PartRevisionAlreadyExistsException {
  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();
      newComponents.add(newUsage);
    }
    firstPartI.setComponents(newComponents);
    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);
    RoleDAO roleDAO=new RoleDAO(locale,em);
    Map<Role,User> roleUserMap=new HashMap<>();
    for (    Object o : roleMappings.entrySet()) {
      Map.Entry pairs=(Map.Entry)o;
      String roleName=(String)pairs.getKey();
      String userLogin=(String)pairs.getValue();
      User worker=userDAO.loadUser(new UserKey(originalPartR.getWorkspaceId(),userLogin));
      Role role=roleDAO.loadRole(new RoleKey(originalPartR.getWorkspaceId(),roleName));
      roleUserMap.put(role,worker);
    }
    WorkflowModel workflowModel=new WorkflowModelDAO(locale,em).loadWorkflowModel(new WorkflowModelKey(user.getWorkspaceId(),pWorkflowModelId));
    Workflow workflow=workflowModel.createWorkflow(roleUserMap);
    partR.setWorkflow(workflow);
    for (    Task task : workflow.getTasks()) {
      if (null == task.getWorker()) {
        throw new NotAllowedException(locale,"String_Node_Str");
      }
    }
    Collection<Task> 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);
  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,String> roleMappings) throws UserNotFoundException, AccessRightException, WorkspaceNotFoundException, PartRevisionNotFoundException, NotAllowedException, FileAlreadyExistsException, CreationException, RoleNotFoundException, WorkflowModelNotFoundException, PartRevisionAlreadyExistsException {
  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);
    RoleDAO roleDAO=new RoleDAO(locale,em);
    Map<Role,User> roleUserMap=new HashMap<>();
    for (    Object o : roleMappings.entrySet()) {
      Map.Entry pairs=(Map.Entry)o;
      String roleName=(String)pairs.getKey();
      String userLogin=(String)pairs.getValue();
      User worker=userDAO.loadUser(new UserKey(originalPartR.getWorkspaceId(),userLogin));
      Role role=roleDAO.loadRole(new RoleKey(originalPartR.getWorkspaceId(),roleName));
      roleUserMap.put(role,worker);
    }
    WorkflowModel workflowModel=new WorkflowModelDAO(locale,em).loadWorkflowModel(new WorkflowModelKey(user.getWorkspaceId(),pWorkflowModelId));
    Workflow workflow=workflowModel.createWorkflow(roleUserMap);
    partR.setWorkflow(workflow);
    for (    Task task : workflow.getTasks()) {
      if (null == task.getWorker()) {
        throw new NotAllowedException(locale,"String_Node_Str");
      }
    }
    Collection<Task> 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);
  return partR;
}