/** * Get or create a node with the specified node under the specified parent node. * * @param parent the parent node. may not be null * @param name the name of the child node. may not be null * @return the existing or newly created child node * @throws RepositoryException * @throws IllegalArgumentException if either the parent or name argument is null */ public Node findOrCreateChild( Node parent, String name ) throws RepositoryException { return findOrCreateChild(parent, name, null); }
/** * Get or create a node with the specified node under the specified parent node. * * @param parent the parent node. may not be null * @param name the name of the child node. may not be null * @return the existing or newly created child node * @throws RepositoryException * @throws IllegalArgumentException if either the parent or name argument is null */ public Node findOrCreateChild( Node parent, String name ) throws RepositoryException { return findOrCreateChild(parent, name, null); }
@Test public void shouldFailFindOrCreateChildNodeStringStringNullPath() { try { tools.findOrCreateChild(addressNode, NULL_STRING, DEF_TYPE); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } }
@Test public void shouldCreateChildNodeWithParentName() throws RepositoryException { Node childNode = tools.findOrCreateChild(personNode, "Hobby"); assertNotNull(childNode); assertThat(childNode.getName(), is("Hobby")); }
@Test public void testCreateChildNodeWithParentNameType() throws RepositoryException { Node childNode = tools.findOrCreateChild(personNode, "Hobby", DEF_TYPE); assertNotNull(childNode); assertThat(childNode.getName(), is("Hobby")); }
@Test public void shouldFailFindOrCreateChildNodeStringNullPath() { try { tools.findOrCreateChild(addressNode, NULL_STRING); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } }
@Test public void testFindChildNodeWithParentNameType() throws RepositoryException { Node childNode = tools.findOrCreateChild(personNode, "Address", DEF_TYPE); assertNotNull(childNode); assertThat(childNode.getName(), is("Address")); }
@Test public void shouldFailFindOrCreateChildWithNullParentNameType() { try { tools.findOrCreateChild(NULL_NODE, "childNode", DEF_TYPE); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } }
@Test public void shouldFindChildNodeWithParentName() throws RepositoryException { Node childNode = tools.findOrCreateChild(personNode, "Address"); assertNotNull(childNode); assertThat(childNode.getName(), is("Address")); }
@Test public void shouldFailFindOrCreateChildNodeStringNullNode() { try { tools.findOrCreateChild(NULL_NODE, "childNode"); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } }
/** * Get or Create a node relative to the Parent Node and return the Wrapper JcrObject */ public static <T extends JcrObject> T getOrCreateNode(Node parentNode, String name, String nodeType, Class<T> type, Object... constructorArgs) { T entity = null; try { JcrTools tools = new JcrTools(); //if versionable checkout // if(isVersionable(parentNode)){ // JcrVersionUtil.checkout(parentNode); // } Node n = tools.findOrCreateChild(parentNode, name, nodeType); entity = createJcrObject(n, type, constructorArgs); //save ?? // JcrVersionUtil.checkinRecursively(n); // if(isVersionable(parentNode)){ // JcrVersionUtil.checkin(parentNode); // } } catch (AccessDeniedException e) { log.debug("Access denied", e); throw new AccessControlException(e.getMessage()); } catch (RepositoryException e) { throw new MetadataRepositoryException("Failed to retrieve the Node named" + name, e); } return entity; }
Node contentNode = findOrCreateChild(fileNode, "jcr:content", "nt:resource"); Binary binary = session.getValueFactory().createBinary(stream); contentNode.setProperty("jcr:data", binary);
Node contentNode = findOrCreateChild(fileNode, "jcr:content", "nt:resource"); Binary binary = session.getValueFactory().createBinary(stream); contentNode.setProperty("jcr:data", binary);
/** * Creates a new Entity Node object for a Parent Path, relative Path and node type */ public Node findOrCreateEntityNode(String parentPath, String relPath, Class<? extends JcrEntity<?>> jcrEntityType) { Session session = getSession(); try { Node typesNode = session.getNode(parentPath); JcrTools tools = new JcrTool(); Node entNode = tools.findOrCreateChild(typesNode, relPath, getNodeType(jcrEntityType)); return entNode; } catch (RepositoryException e) { throw new MetadataRepositoryException("Failed to create new entity of type: " + getEntityClass(), e); } }
@Test public void testCreateChildNodeWithoutAncestorAccess() throws Exception { // Set privileges for node where child node needs to be added setPolicy("/Person/Address/", Privilege.JCR_ALL); // Set a NON JCR.READ privilege to ancestoral parent setPolicy("/Person", Privilege.JCR_ADD_CHILD_NODES); Node childNode = tools.findOrCreateChild(session.getRootNode(), "/Person/Address/County", DEF_TYPE); assertNotNull(childNode); assertThat(childNode.getName(), is("County")); }