Program: 85917
Project: 85917
Buggy source:
@RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override public DocumentRevision createDocumentMaster(String pParentFolder,String pDocMId,String pTitle,String pDescription,String pDocMTemplateId,String pWorkflowModelId,ACLUserEntry[] pACLUserEntries,ACLUserGroupEntry[] pACLUserGroupEntries,Map<String,Collection<String>> userRoleMapping,Map<String,Collection<String>> groupRoleMapping) throws UserNotFoundException, AccessRightException, WorkspaceNotFoundException, NotAllowedException, FolderNotFoundException, DocumentMasterTemplateNotFoundException, FileAlreadyExistsException, CreationException, DocumentRevisionAlreadyExistsException, RoleNotFoundException, WorkflowModelNotFoundException, DocumentMasterAlreadyExistsException, UserGroupNotFoundException {
  User user=userManager.checkWorkspaceWriteAccess(Folder.parseWorkspaceId(pParentFolder));
  Locale locale=new Locale(user.getLanguage());
  checkNameValidity(pDocMId,locale);
  Folder folder=new FolderDAO(locale,em).loadFolder(pParentFolder);
  checkFolderWritingRight(user,folder);
  DocumentMaster docM;
  DocumentRevision docR;
  DocumentIteration newDoc;
  DocumentMasterDAO docMDAO=new DocumentMasterDAO(locale,em);
  if (pDocMTemplateId == null) {
    docM=new DocumentMaster(user.getWorkspace(),pDocMId,user);
    docM.setType("String_Node_Str");
    docMDAO.createDocM(docM);
    docR=docM.createNextRevision(user);
    newDoc=docR.createNextIteration(user);
  }
 else {
    DocumentMasterTemplate template=new DocumentMasterTemplateDAO(locale,em).loadDocMTemplate(new DocumentMasterTemplateKey(user.getWorkspaceId(),pDocMTemplateId));
    if (!Tools.validateMask(template.getMask(),pDocMId)) {
      throw new NotAllowedException(locale,"String_Node_Str");
    }
    docM=new DocumentMaster(user.getWorkspace(),pDocMId,user);
    docM.setType(template.getDocumentType());
    docM.setAttributesLocked(template.isAttributesLocked());
    docMDAO.createDocM(docM);
    docR=docM.createNextRevision(user);
    newDoc=docR.createNextIteration(user);
    List<InstanceAttribute> attrs=new ArrayList<>();
    for (    InstanceAttributeTemplate attrTemplate : template.getAttributeTemplates()) {
      InstanceAttribute attr=attrTemplate.createInstanceAttribute();
      attrs.add(attr);
    }
    newDoc.setInstanceAttributes(attrs);
    BinaryResourceDAO binDAO=new BinaryResourceDAO(locale,em);
    for (    BinaryResource sourceFile : template.getAttachedFiles()) {
      String fileName=sourceFile.getName();
      long length=sourceFile.getContentLength();
      Date lastModified=sourceFile.getLastModified();
      String fullName=docM.getWorkspaceId() + "String_Node_Str" + docM.getId()+ "String_Node_Str"+ fileName;
      BinaryResource targetFile=new BinaryResource(fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      newDoc.addFile(targetFile);
      try {
        dataManager.copyData(sourceFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
  }
  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(Folder.parseWorkspaceId(pParentFolder),roleName));
      Set<User> users=new HashSet<>();
      roleUserMap.put(role,users);
      for (      String login : userLogins) {
        User u=userDAO.loadUser(new UserKey(Folder.parseWorkspaceId(pParentFolder),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(Folder.parseWorkspaceId(pParentFolder),roleName));
      Set<UserGroup> groups=new HashSet<>();
      roleGroupMap.put(role,groups);
      for (      String groupId : groupIds) {
        UserGroup g=groupDAO.loadUserGroup(new UserGroupKey(Folder.parseWorkspaceId(pParentFolder),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);
  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());
      }
    }
    docR.setACL(acl);
  }
  Date now=new Date();
  docM.setCreationDate(now);
  docR.setCreationDate(now);
  docR.setLocation(folder);
  docR.setCheckOutUser(user);
  docR.setCheckOutDate(now);
  newDoc.setCreationDate(now);
  new DocumentRevisionDAO(locale,em).createDocR(docR);
  return docR;
}

Fixed version (ground truth):
@RolesAllowed(UserGroupMapping.REGULAR_USER_ROLE_ID) @Override public DocumentRevision createDocumentMaster(String pParentFolder,String pDocMId,String pTitle,String pDescription,String pDocMTemplateId,String pWorkflowModelId,ACLUserEntry[] pACLUserEntries,ACLUserGroupEntry[] pACLUserGroupEntries,Map<String,Collection<String>> userRoleMapping,Map<String,Collection<String>> groupRoleMapping) throws UserNotFoundException, AccessRightException, WorkspaceNotFoundException, NotAllowedException, FolderNotFoundException, DocumentMasterTemplateNotFoundException, FileAlreadyExistsException, CreationException, DocumentRevisionAlreadyExistsException, RoleNotFoundException, WorkflowModelNotFoundException, DocumentMasterAlreadyExistsException, UserGroupNotFoundException {
  User user=userManager.checkWorkspaceWriteAccess(Folder.parseWorkspaceId(pParentFolder));
  Locale locale=new Locale(user.getLanguage());
  checkNameValidity(pDocMId,locale);
  Folder folder=new FolderDAO(locale,em).loadFolder(pParentFolder);
  checkFolderWritingRight(user,folder);
  DocumentMaster docM;
  DocumentRevision docR;
  DocumentIteration newDoc;
  DocumentMasterDAO docMDAO=new DocumentMasterDAO(locale,em);
  if (pDocMTemplateId == null) {
    docM=new DocumentMaster(user.getWorkspace(),pDocMId,user);
    docM.setType("String_Node_Str");
    docMDAO.createDocM(docM);
    docR=docM.createNextRevision(user);
    newDoc=docR.createNextIteration(user);
  }
 else {
    DocumentMasterTemplate template=new DocumentMasterTemplateDAO(locale,em).loadDocMTemplate(new DocumentMasterTemplateKey(user.getWorkspaceId(),pDocMTemplateId));
    if (!Tools.validateMask(template.getMask(),pDocMId)) {
      throw new NotAllowedException(locale,"String_Node_Str");
    }
    docM=new DocumentMaster(user.getWorkspace(),pDocMId,user);
    docM.setType(template.getDocumentType());
    docM.setAttributesLocked(template.isAttributesLocked());
    docMDAO.createDocM(docM);
    docR=docM.createNextRevision(user);
    newDoc=docR.createNextIteration(user);
    List<InstanceAttribute> attrs=new ArrayList<>();
    for (    InstanceAttributeTemplate attrTemplate : template.getAttributeTemplates()) {
      InstanceAttribute attr=attrTemplate.createInstanceAttribute();
      attrs.add(attr);
    }
    newDoc.setInstanceAttributes(attrs);
    BinaryResourceDAO binDAO=new BinaryResourceDAO(locale,em);
    for (    BinaryResource sourceFile : template.getAttachedFiles()) {
      String fileName=sourceFile.getName();
      long length=sourceFile.getContentLength();
      Date lastModified=sourceFile.getLastModified();
      String fullName=docM.getWorkspaceId() + "String_Node_Str" + docM.getId()+ "String_Node_Str"+ fileName;
      BinaryResource targetFile=new BinaryResource(fullName,length,lastModified);
      binDAO.createBinaryResource(targetFile);
      newDoc.addFile(targetFile);
      try {
        dataManager.copyData(sourceFile,targetFile);
      }
 catch (      StorageException e) {
        LOGGER.log(Level.INFO,null,e);
      }
    }
  }
  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(Folder.parseWorkspaceId(pParentFolder),roleName));
      Set<User> users=new HashSet<>();
      roleUserMap.put(role,users);
      for (      String login : userLogins) {
        User u=userDAO.loadUser(new UserKey(Folder.parseWorkspaceId(pParentFolder),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(Folder.parseWorkspaceId(pParentFolder),roleName));
      Set<UserGroup> groups=new HashSet<>();
      roleGroupMap.put(role,groups);
      for (      String groupId : groupIds) {
        UserGroup g=groupDAO.loadUserGroup(new UserGroupKey(Folder.parseWorkspaceId(pParentFolder),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);
  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());
      }
    }
    docR.setACL(acl);
  }
  Date now=new Date();
  docM.setCreationDate(now);
  docR.setCreationDate(now);
  docR.setLocation(folder);
  docR.setCheckOutUser(user);
  docR.setCheckOutDate(now);
  newDoc.setCreationDate(now);
  new DocumentRevisionDAO(locale,em).createDocR(docR);
  if (runningTasks != null) {
    mailer.sendApproval(runningTasks,docR);
  }
  return docR;
}