Program: 85919
Project: 85919
Buggy source:
@RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override public PartMaster createPartMaster(String pWorkspaceId,String pNumber,String pName,boolean pStandardPart,String pWorkflowModelId,String pPartRevisionDescription,String templateId,ACLUserEntry[] pACLUserEntries,ACLUserGroupEntry[] pACLUserGroupEntries,Map<String,Collection<String>> userRoleMapping,Map<String,Collection<String>> groupRoleMapping) throws NotAllowedException, UserNotFoundException, WorkspaceNotFoundException, AccessRightException, WorkflowModelNotFoundException, PartMasterAlreadyExistsException, CreationException, PartMasterTemplateNotFoundException, FileAlreadyExistsException, RoleNotFoundException, UserGroupNotFoundException {
  User user=userManager.checkWorkspaceWriteAccess(pWorkspaceId);
  Locale locale=new Locale(user.getLanguage());
  checkNameValidity(pNumber,locale);
  PartMaster pm=new PartMaster(user.getWorkspace(),pNumber,user);
  pm.setName(pName);
  pm.setStandardPart(pStandardPart);
  Date now=new Date();
  pm.setCreationDate(now);
  PartRevision newRevision=pm.createNextRevision(user);
  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(pWorkspaceId,roleName));
      Set<User> users=new HashSet<>();
      roleUserMap.put(role,users);
      for (      String login : userLogins) {
        User u=userDAO.loadUser(new UserKey(pWorkspaceId,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(pWorkspaceId,roleName));
      Set<UserGroup> groups=new HashSet<>();
      roleGroupMap.put(role,groups);
      for (      String groupId : groupIds) {
        UserGroup g=groupDAO.loadUserGroup(new UserGroupKey(pWorkspaceId,groupId));
        groups.add(g);
      }
    }
    WorkflowModel workflowModel=new WorkflowModelDAO(locale,em).loadWorkflowModel(new WorkflowModelKey(user.getWorkspaceId(),pWorkflowModelId));
    Workflow workflow=workflowModel.createWorkflow(roleUserMap,roleGroupMap);
    newRevision.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,newRevision);
  }
  newRevision.setCheckOutUser(user);
  newRevision.setCheckOutDate(now);
  newRevision.setCreationDate(now);
  newRevision.setDescription(pPartRevisionDescription);
  PartIteration ite=newRevision.createNextIteration(user);
  ite.setCreationDate(now);
  if (templateId != null) {
    PartMasterTemplate partMasterTemplate=new PartMasterTemplateDAO(locale,em).loadPartMTemplate(new PartMasterTemplateKey(pWorkspaceId,templateId));
    if (!Tools.validateMask(partMasterTemplate.getMask(),pNumber)) {
      throw new NotAllowedException(locale,"String_Node_Str");
    }
    pm.setType(partMasterTemplate.getPartType());
    pm.setAttributesLocked(partMasterTemplate.isAttributesLocked());
    List<InstanceAttribute> attrs=new ArrayList<>();
    for (    InstanceAttributeTemplate attrTemplate : partMasterTemplate.getAttributeTemplates()) {
      InstanceAttribute attr=attrTemplate.createInstanceAttribute();
      attrs.add(attr);
    }
    ite.setInstanceAttributes(attrs);
    BinaryResourceDAO binDAO=new BinaryResourceDAO(locale,em);
    BinaryResource sourceFile=partMasterTemplate.getAttachedFile();
    if (sourceFile != null) {
      String fileName=sourceFile.getName();
      long length=sourceFile.getContentLength();
      Date lastModified=sourceFile.getLastModified();
      String fullName=pWorkspaceId + "String_Node_Str" + pm.getNumber()+ "String_Node_Str"+ fileName;
      BinaryResource targetFile=new BinaryResource(fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      ite.setNativeCADFile(targetFile);
      try {
        dataManager.copyData(sourceFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
  }
  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());
      }
    }
    newRevision.setACL(acl);
    new ACLDAO(em).createACL(acl);
  }
  new PartMasterDAO(locale,em).createPartM(pm);
  return pm;
}

Fixed version (ground truth):
@RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override public PartMaster createPartMaster(String pWorkspaceId,String pNumber,String pName,boolean pStandardPart,String pWorkflowModelId,String pPartRevisionDescription,String templateId,ACLUserEntry[] pACLUserEntries,ACLUserGroupEntry[] pACLUserGroupEntries,Map<String,Collection<String>> userRoleMapping,Map<String,Collection<String>> groupRoleMapping) throws NotAllowedException, UserNotFoundException, WorkspaceNotFoundException, AccessRightException, WorkflowModelNotFoundException, PartMasterAlreadyExistsException, CreationException, PartMasterTemplateNotFoundException, FileAlreadyExistsException, RoleNotFoundException, UserGroupNotFoundException {
  User user=userManager.checkWorkspaceWriteAccess(pWorkspaceId);
  Locale locale=new Locale(user.getLanguage());
  checkNameValidity(pNumber,locale);
  PartMaster pm=new PartMaster(user.getWorkspace(),pNumber,user);
  pm.setName(pName);
  pm.setStandardPart(pStandardPart);
  Date now=new Date();
  pm.setCreationDate(now);
  PartRevision newRevision=pm.createNextRevision(user);
  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(pWorkspaceId,roleName));
      Set<User> users=new HashSet<>();
      roleUserMap.put(role,users);
      for (      String login : userLogins) {
        User u=userDAO.loadUser(new UserKey(pWorkspaceId,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(pWorkspaceId,roleName));
      Set<UserGroup> groups=new HashSet<>();
      roleGroupMap.put(role,groups);
      for (      String groupId : groupIds) {
        UserGroup g=groupDAO.loadUserGroup(new UserGroupKey(pWorkspaceId,groupId));
        groups.add(g);
      }
    }
    WorkflowModel workflowModel=new WorkflowModelDAO(locale,em).loadWorkflowModel(new WorkflowModelKey(user.getWorkspaceId(),pWorkflowModelId));
    Workflow workflow=workflowModel.createWorkflow(roleUserMap,roleGroupMap);
    newRevision.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();
    }
  }
  newRevision.setCheckOutUser(user);
  newRevision.setCheckOutDate(now);
  newRevision.setCreationDate(now);
  newRevision.setDescription(pPartRevisionDescription);
  PartIteration ite=newRevision.createNextIteration(user);
  ite.setCreationDate(now);
  if (templateId != null) {
    PartMasterTemplate partMasterTemplate=new PartMasterTemplateDAO(locale,em).loadPartMTemplate(new PartMasterTemplateKey(pWorkspaceId,templateId));
    if (!Tools.validateMask(partMasterTemplate.getMask(),pNumber)) {
      throw new NotAllowedException(locale,"String_Node_Str");
    }
    pm.setType(partMasterTemplate.getPartType());
    pm.setAttributesLocked(partMasterTemplate.isAttributesLocked());
    List<InstanceAttribute> attrs=new ArrayList<>();
    for (    InstanceAttributeTemplate attrTemplate : partMasterTemplate.getAttributeTemplates()) {
      InstanceAttribute attr=attrTemplate.createInstanceAttribute();
      attrs.add(attr);
    }
    ite.setInstanceAttributes(attrs);
    BinaryResourceDAO binDAO=new BinaryResourceDAO(locale,em);
    BinaryResource sourceFile=partMasterTemplate.getAttachedFile();
    if (sourceFile != null) {
      String fileName=sourceFile.getName();
      long length=sourceFile.getContentLength();
      Date lastModified=sourceFile.getLastModified();
      String fullName=pWorkspaceId + "String_Node_Str" + pm.getNumber()+ "String_Node_Str"+ fileName;
      BinaryResource targetFile=new BinaryResource(fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      ite.setNativeCADFile(targetFile);
      try {
        dataManager.copyData(sourceFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
  }
  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());
      }
    }
    newRevision.setACL(acl);
    new ACLDAO(em).createACL(acl);
  }
  new PartMasterDAO(locale,em).createPartM(pm);
  if (runningTasks != null) {
    mailer.sendApproval(runningTasks,newRevision);
  }
  return pm;
}