NodeStateCopyUtils { public static void copyToNode(NodeState state, Node node) throws RepositoryException { copyProps(state, node); Tree src = TreeFactory.createReadOnlyTree(state); for (Tree srcChild : src.getChildren()){ String childName = srcChild.getName(); if (NodeStateUtils.isHidden(childName)){ continue; } NodeState childState = state.getChildNode(childName); Node child = JcrUtils.getOrAddNode(node, childName, primaryType(childState)); copyToNode(childState, child); } }  static void copyToTree(NodeState state, Tree tree); static void copyToNode(NodeState state, Node node);  }
@Test public void copyToJcr() throws Exception{ repository = new Jcr().with(new OpenSecurityProvider()).createRepository(); Tree srcTree = TreeFactory.createTree(builder); srcTree.setOrderableChildren(true); srcTree.setProperty("foo", "x"); srcTree.setProperty("foo", "x"); srcTree.setProperty(JcrConstants.JCR_PRIMARYTYPE, NT_OAK_UNSTRUCTURED, Type.NAME); srcTree.addChild("a").setOrderableChildren(true); srcTree.addChild("a").setProperty("foo", "y"); srcTree.addChild("a").setProperty(JcrConstants.JCR_PRIMARYTYPE, NT_OAK_UNSTRUCTURED, Type.NAME); srcTree.addChild("b").setOrderableChildren(true); srcTree.addChild("b").setProperty("foo", "z"); srcTree.addChild("b").setProperty(JcrConstants.JCR_PRIMARYTYPE, NT_OAK_UNSTRUCTURED, Type.NAME); Session session = repository.login(null, null); Node node = session.getRootNode(); Node test = node.addNode("test", NT_OAK_UNSTRUCTURED); NodeStateCopyUtils.copyToNode(builder.getNodeState(), test); session.save(); test = session.getNode("/test"); assertEquals("y", test.getProperty("a/foo").getString()); assertEquals("z", test.getProperty("b/foo").getString()); }
@Test public void copyToJcrAndHiddenProps() throws Exception{ repository = new Jcr().with(new OpenSecurityProvider()).createRepository(); Tree srcTree = TreeFactory.createTree(builder); srcTree.addChild("a").setProperty("foo", "y"); srcTree.addChild("a").setProperty(JcrConstants.JCR_PRIMARYTYPE, NT_OAK_UNSTRUCTURED, Type.NAME); builder.child(":hidden-node").setProperty("x", "y"); builder.setProperty(":hidden-prop", "y"); Session session = repository.login(null, null); Node node = session.getRootNode(); Node test = node.addNode("test", NT_OAK_UNSTRUCTURED); NodeStateCopyUtils.copyToNode(builder.getNodeState(), test); session.save(); test = session.getNode("/test"); assertEquals("y", test.getProperty("a/foo").getString()); }
@Test public void copyToJcrVariousProps() throws Exception{ repository = new Jcr().with(new OpenSecurityProvider()).createRepository(); Calendar cal = ISO8601.parse(ISO8601.format(Calendar.getInstance())); Tree srcTree = TreeFactory.createTree(builder); srcTree.setOrderableChildren(true); srcTree.setProperty("fooString", "x"); srcTree.setProperty("fooLong", 1L, Type.LONG); srcTree.setProperty("fooPath", "/fooNode", Type.PATH); srcTree.setProperty("fooName", "mix:title", Type.NAME); srcTree.setProperty("fooDouble", 1.0, Type.DOUBLE); srcTree.setProperty("fooDate", ISO8601.format(cal), Type.DATE); srcTree.setProperty("fooBoolean", true, Type.BOOLEAN); srcTree.setProperty("fooStrings", asList("a", "b"), Type.STRINGS); srcTree.setProperty("fooBlob", new ArrayBasedBlob("foo".getBytes()), Type.BINARY); srcTree.setProperty(JcrConstants.JCR_PRIMARYTYPE, NT_OAK_UNSTRUCTURED, Type.NAME); srcTree.setProperty(JcrConstants.JCR_MIXINTYPES, asList("mix:mimeType", "mix:title"), Type.NAMES); Session session = repository.login(null, null); Node node = session.getRootNode(); Node test = node.addNode("test", NT_OAK_UNSTRUCTURED); Node fooNode = node.addNode("fooNode", NT_OAK_UNSTRUCTURED); NodeStateCopyUtils.copyToNode(builder.getNodeState(), test); session.save(); test = session.getNode("/test"); assertEquals("x", test.getProperty("fooString").getString()); assertEquals("/fooNode", test.getProperty("fooPath").getNode().getPath()); assertEquals("mix:title", test.getProperty("fooName").getString()); assertEquals(1, test.getProperty("fooLong").getLong()); assertEquals(cal, test.getProperty("fooDate").getDate()); assertEquals("a", test.getProperty("fooStrings").getValues()[0].getString()); assertEquals("b", test.getProperty("fooStrings").getValues()[1].getString()); InputStream is = test.getProperty("fooBlob").getBinary().getStream(); String streamVal = IOUtils.toString(is, "UTF-8"); assertEquals("foo", streamVal); }