Source:
<code>public void addContainer(AgentPrincipals agent, PID parentPid, Resource containerType) { Model model = ModelFactory.createDefaultModel(); Resource parentResc = model.getResource(parentPid.getRepositoryPath()); ContentContainerObject child = null; if (Cdr.AdminUnit.equals(containerType)) { aclService.assertHasAccess( "User does not have permissions to create admin units", parentPid, agent.getPrincipals(), Permission.createAdminUnit); child = repoObjFactory.createAdminUnit(model); repoObjFactory.createMemberLink(URI.create(parentResc.getURI()), child.getPid().getRepositoryUri()); } else if (Cdr.Collection.equals(containerType)) { aclService.assertHasAccess( "User does not have permissions to create collections", parentPid, agent.getPrincipals(), Permission.createCollection); child = repoObjFactory.createCollectionObject(model); repoObjFactory.createMemberLink(URI.create(parentResc.getURI()), child.getPid().getRepositoryUri()); } else if (Cdr.Folder.equals(containerType)) { aclService.assertHasAccess( "User does not have permissions to create collections", parentPid, agent.getPrincipals(), Permission.ingest); child = repoObjFactory.createFolderObject(model); repoObjFactory.createMemberLink(URI.create(parentResc.getURI()), child.getPid().getRepositoryUri()); } else if (Cdr.Work.equals(containerType)) { aclService.assertHasAccess( "User does not have permissions to create collections", parentPid, agent.getPrincipals(), Permission.ingest); child = repoObjFactory.createWorkObject(model); <START> repoObjFactory.createMemberLink(URI.create(parentResc.getURI()), child.getPid().getRepositoryUri()); <END> } child.getPremisLog().buildEvent(Premis.Creation) .addImplementorAgent(agent.getUsernameUri()) .addEventDetail("Container added at destination " + parentPid) .write(); }</code><technical_language>suggest this parentObject calling addMember it, includes validation a legitimate child type added. move this out of ifs same. child created (was a valid type) continuing. perform creation adding (and premis) in a transaction creation of object out if invalid child type (like adding AdminUnit a folder)</technical_language>

Target:
public void addContainer(AgentPrincipals agent, PID parentPid, Resource containerType) { FedoraTransaction tx = txManager.startTransaction(); try { ContentContainerObject child = null; if (Cdr.AdminUnit.equals(containerType)) { aclService.assertHasAccess( "User does not have permissions to create admin units", parentPid, agent.getPrincipals(), Permission.createAdminUnit); child = repoObjFactory.createAdminUnit(null); } else if (Cdr.Collection.equals(containerType)) { aclService.assertHasAccess( "User does not have permissions to create collections", parentPid, agent.getPrincipals(), Permission.createCollection); child = repoObjFactory.createCollectionObject(null); } else if (Cdr.Folder.equals(containerType)) { aclService.assertHasAccess( "User does not have permissions to create folders", parentPid, agent.getPrincipals(), Permission.ingest); child = repoObjFactory.createFolderObject(null); } else if (Cdr.Work.equals(containerType)) { aclService.assertHasAccess( "User does not have permissions to create works", parentPid, agent.getPrincipals(), Permission.ingest); child = repoObjFactory.createWorkObject(null); } ContentContainerObject parent = (ContentContainerObject) repoObjLoader.getRepositoryObject(parentPid); parent.addMember(child); child.getPremisLog() .buildEvent(Premis.Creation) .addImplementorAgent(agent.getUsernameUri()) .addEventDetail("Container added at destination " + parentPid) .write(); } catch (Exception e) { tx.cancel(e); } finally { tx.close(); } }