Index: src/main/java/org/modeshape/web/jcr/rest/client/domain/NodeType.java =================================================================== --- src/main/java/org/modeshape/web/jcr/rest/client/domain/NodeType.java (revision 2225) +++ src/main/java/org/modeshape/web/jcr/rest/client/domain/NodeType.java (working copy) @@ -62,6 +62,8 @@ private List propertyDefinitons = null; private List childNodeDefinitons = null; + + private List superTypes = null; // =========================================================================================================================== // Constructors @@ -161,6 +163,11 @@ childNodeDefinitionNodeType.setParentNodeType(this); } + public void addSuperNodeType(NodeType superNodeType) { + if (this.superTypes == null) this.superTypes = new ArrayList(); + superTypes.add(superNodeType); + } + @SuppressWarnings("unchecked") public List getPropertyDefinitions() { return (List) (this.propertyDefinitons != null ? this.propertyDefinitons : Collections.emptyList()); @@ -171,6 +178,11 @@ return (List) (this.childNodeDefinitons != null ? this.childNodeDefinitons : Collections.emptyList()); } + @SuppressWarnings("unchecked") + public List getSuperNodeTypes() { + return (List) (this.superTypes != null ? this.superTypes : Collections.emptyList()); + } + public NodeType getParentNodeType() { return this.parentNodeType; } @@ -179,7 +191,6 @@ this.parentNodeType = parent; } - /** * {@inheritDoc} * Index: src/main/java/org/modeshape/web/jcr/rest/client/json/NodeTypeNode.java =================================================================== --- src/main/java/org/modeshape/web/jcr/rest/client/json/NodeTypeNode.java (revision 2225) +++ src/main/java/org/modeshape/web/jcr/rest/client/json/NodeTypeNode.java (working copy) @@ -24,12 +24,15 @@ package org.modeshape.web.jcr.rest.client.json; import java.net.URL; -import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Set; import net.jcip.annotations.Immutable; @@ -55,9 +58,6 @@ @Immutable public final class NodeTypeNode extends JsonNode { - /* the path used to get to the node types */ -// private static final String GET_NODE_TYPES_URL = "/jcr:system/jcr:nodeTypes"; - // =========================================================================================================================== // Fields // =========================================================================================================================== @@ -70,7 +70,7 @@ private final String depth; private MapnodeTypeMap = new HashMap(); - + // =========================================================================================================================== // Constructors // =========================================================================================================================== @@ -91,23 +91,6 @@ this.depth = nodeDepth; } -// /** -// * Use this constructor if wanting a specific node type -// * @param workspace the workspace being used (never null) -// * @param relative_path is the relative location after the workspace -// * @param nodeTypeName is the node type to be returned -// * @throws Exception if there is a problem creating the folder node -// */ -// public NodeTypeNode( Workspace workspace, String relative_path) throws Exception { -// super(relative_path); -// -// CheckArg.isNotNull(workspace, "workspace"); -// CheckArg.isNotNull(relative_path, "relative_path"); -// -// this.workspace = workspace; -// this.depth = 2; -// } - // =========================================================================================================================== // Methods // =========================================================================================================================== @@ -146,14 +129,6 @@ // path needs to be encoded url.append(JsonUtils.encode(path)); - // if wanting a specific node type, need to append the node type name and the depth in order to get its properties -// if (this.nodeTypeName != null) { -// // url.append("/").append(JsonUtils.encode(this.nodeTypeName)).append(JsonUtils.encode("?depth=" + this.depth)); -// url.append("/").append((this.nodeTypeName)); -// } else if (depth >= 0) { -// url.append(("?depth=" + this.depth)); -// } - if (this.depth != null) { url.append(this.depth); } @@ -168,20 +143,18 @@ */ public Collection getNodeTypes( String jsonResponse ) throws Exception { CheckArg.isNotNull(jsonResponse, "jsonResponse"); - Collection nodetypes = new ArrayList(); - JSONObject body = new JSONObject(jsonResponse); - processBody(body, nodetypes, null); - return nodetypes; + + JSONObject body = new JSONObject(jsonResponse); + NodeType parent = createNodeType(null, body, null); + + processBody(body, parent); + return nodeTypeMap.values(); } @SuppressWarnings("unchecked") - protected void processBody(JSONObject body, Collection nodetypes, NodeType parentNodeType) throws Exception { - NodeType parent = parentNodeType; - - parent = createNodeType(null, body, null); - nodetypes.add(parent); - this.nodeTypeMap.put(parent.getName(), parent); - + protected void processBody(JSONObject body, NodeType parentNodeType) throws Exception { + NodeType parent = parentNodeType; + if (body.has("children")) { Object obj = body.get("children"); @@ -196,8 +169,7 @@ if (child instanceof JSONObject) { JSONObject jsonchild = (JSONObject) child; - NodeType nodeType = createNodeType(key, jsonchild, parent); - nodetypes.add(nodeType); + NodeType nodeType = createNodeType(key, jsonchild, parent); processNodeType(nodeType, jsonchild, parent); } else if (child instanceof JSONArray) { @@ -204,8 +176,7 @@ JSONArray childarray = (JSONArray) child; for (int idx=0; idx superTypes = new HashSet(3); + + Properties properties = new Properties(); // keys are the repository names for (Iterator itr = jsonProperties.keys(); itr.hasNext();) { @@ -289,24 +269,26 @@ Object obj = jsonProperties.get(key); if (obj != null) { if (obj instanceof JSONObject) { - // JSONObject child = (JSONObject) obj; - throw new Exception("Program Error: didnt handle object type: " + obj.getClass().getName()); -// processBody(child, nodetypes, nodeType); } + + if (key.equalsIgnoreCase("jcr:supertypes")) { + + JSONArray superArray = jsonProperties.getJSONArray(key); + + for (int i=0; i it=superTypes.iterator(); it.hasNext();) { + childnodeType.addSuperNodeType(it.next()); + } + + CheckArg.isNotNull(childnodeType, "childNodeType ends up in null state for childkey: " + childkey); + this.nodeTypeMap.put(childnodeType.getName(), childnodeType); + return childnodeType; } @@ -344,6 +337,8 @@ CheckArg.isNotNull(jsonResponse, "jsonResponse"); JSONObject jsonChild = new JSONObject(jsonResponse); + System.out.println("JSON: " + jsonResponse); + Properties properties = new Properties(); NodeType nodetype = null; @@ -363,5 +358,39 @@ return null; } + + /** + * The Super Type weak reference is used because there's no guaranteed order for loading + * all the node types, therefore, a referenced super type might not have been loaded + * at the time of loading its child. Therefore, the findSuperTypeMap + * is used to find the super type after the fact. + * + */ + class WeakSuperTypeReference extends NodeType { + private MapfindSuperTypeMap = null; + + public WeakSuperTypeReference( String name, + Workspace workspace, + Properties properties, + MapnodeTypeMap) { + super(name, workspace, properties); + findSuperTypeMap = nodeTypeMap; + } + + @SuppressWarnings("unchecked") + @Override + public List getPropertyDefinitions() { + NodeType superType = findSuperTypeMap.get(getName()); + return (List) (superType != null ? superType.getPropertyDefinitions() : Collections.emptyList()); + } + + @SuppressWarnings("unchecked") + @Override + public List getChildNodeDefinitions() { + NodeType superType = findSuperTypeMap.get(getName()); + return (List) (superType != null ? superType.getChildNodeDefinitions() : Collections.emptyList()); + } + + } } Index: src/test/java/org/modeshape/web/jcr/rest/client/json/JsonRestClientTest.java =================================================================== --- src/test/java/org/modeshape/web/jcr/rest/client/json/JsonRestClientTest.java (revision 2225) +++ src/test/java/org/modeshape/web/jcr/rest/client/json/JsonRestClientTest.java (working copy) @@ -103,10 +103,6 @@ assertThat(repositories.iterator().next(), is(REPOSITORY1)); } - // Test is not currently working as a unit test, cause it throws an exception when using the default cargo setup - // but does work when pointed at a local jbossas server - // TODO: determine how to add/setup the local cargo server with cnd files - @Ignore @Test public void shouldGetNodeTypes() throws Exception { Workspace ws = new Workspace(WORKSPACE_NAME, REPOSITORY1); @@ -111,26 +107,10 @@ public void shouldGetNodeTypes() throws Exception { Workspace ws = new Workspace(WORKSPACE_NAME, REPOSITORY1); Collection results = this.restClient.getNodeTypes(ws, "jcr:system", "?depth=5"); - // this is currently the number returned from the default jbossas installation - // assertThat(results.size(), is(2)); - - for (Iterator it=results.iterator(); it.hasNext();) { - NodeType nt = it.next(); - System.out.println("NODETYPE: " + nt.getName()); - List children = nt.getChildren(); - if (children != null) { - for (Iterator itc=children.iterator(); itc.hasNext();){ - NodeType ntc = (NodeType) itc.next(); - System.out.println("NODETYPECHILD: " + ntc.getName()); - } - } - - } + assertThat(results.size(), is(14)); } - // Test is not currently working as a unit test, cause it throws an exception when using the default cargo setup - // but does work when pointed at a local jbossas server - // TODO: determine how to add/setup the local cargo server with cnd files + // Test is not currently working as a unit test @Ignore @Test public void shouldGetNodeType() throws Exception { @@ -135,7 +115,7 @@ @Test public void shouldGetNodeType() throws Exception { Workspace ws = new Workspace(WORKSPACE_NAME , REPOSITORY1); - NodeType nt = this.restClient.getNodeType(ws, "mode:system/nodeTypes/nt:base", "?depth=2"); + NodeType nt = this.restClient.getNodeType(ws, "jcr:system/nodeTypes/mode:system", "?depth=2"); assertThat(nt, is(notNullValue())); } Index: src/test/java/org/modeshape/web/jcr/rest/client/domain/WorkspaceTest.java =================================================================== --- src/test/java/org/modeshape/web/jcr/rest/client/domain/WorkspaceTest.java (revision 2225) +++ src/test/java/org/modeshape/web/jcr/rest/client/domain/WorkspaceTest.java (working copy) @@ -47,12 +47,8 @@ private static final Server SERVER1 = new Server("file:/tmp/temp.txt/resources", "user", "pswd"); - // private static final Server SERVER2 = new Server("http:www.redhat.com/resources", "user", "pswd"); - private static final Repository REPOSITORY1 = new Repository(NAME1, SERVER1); - // private static final Repository REPOSITORY2 = new Repository(NAME2, SERVER2); - private static final Workspace WORKSPACE1 = new Workspace(NAME1, REPOSITORY1); private static final Workspace WORKSPACE2 = new Workspace(NAME2, REPOSITORY1); Index: src/test/java/org/modeshape/web/jcr/rest/client/domain/NodeTypeTest.java =================================================================== --- src/test/java/org/modeshape/web/jcr/rest/client/domain/NodeTypeTest.java (revision 2225) +++ src/test/java/org/modeshape/web/jcr/rest/client/domain/NodeTypeTest.java (working copy) @@ -28,6 +28,7 @@ import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertThat; import java.util.HashSet; +import java.util.Properties; import java.util.Set; import org.junit.Test; @@ -46,13 +47,15 @@ private static final Server SERVER1 = new Server("file:/tmp/temp.txt/resources", "user", "pswd"); - private static final Server SERVER2 = new Server("http:www.redhat.com/resources", "user", "pswd"); - private static final Repository REPOSITORY1 = new Repository(NAME1, SERVER1); - private static final Repository REPOSITORY2 = new Repository(NAME2, SERVER2); - private static final Workspace WORKSPACE1 = new Workspace(NAME1, REPOSITORY1); + + private static final Workspace WORKSPACE2 = new Workspace(NAME2, REPOSITORY1); + + private static final NodeType NODETYPE1 = new NodeType(NAME1, WORKSPACE1, new Properties()); + + // =========================================================================================================================== // Tests @@ -60,7 +63,7 @@ @Test public void shouldBeEqualIfHavingSameProperies() { - assertThat(WORKSPACE1, equalTo(new Workspace(WORKSPACE1.getName(), WORKSPACE1.getRepository()))); + assertThat(NODETYPE1, equalTo(new NodeType(NODETYPE1.getName(), WORKSPACE1, new Properties()))); } @Test @@ -65,9 +68,9 @@ @Test public void shouldHashToSameValueIfEquals() { - Set set = new HashSet(); - set.add(WORKSPACE1); - set.add(new Workspace(WORKSPACE1.getName(), WORKSPACE1.getRepository())); + Set set = new HashSet(); + set.add(NODETYPE1); + set.add(new NodeType(NODETYPE1.getName(), WORKSPACE1, null)); assertThat(set.size(), equalTo(1)); } @@ -73,22 +76,22 @@ @Test( expected = java.lang.AssertionError.class ) public void shouldNotAllowNullName() { - new Workspace(null, REPOSITORY1); + new NodeType(null, WORKSPACE1, null); } @Test( expected = java.lang.AssertionError.class ) - public void shouldNotAllowNullRepository() { - new Workspace(NAME1, null); + public void shouldNotAllowNullWorkspace() { + new NodeType(NAME1, null, null); } @Test - public void shouldNotBeEqualIfSameNameButDifferentRepository() { - assertThat(WORKSPACE1, is(not(equalTo(new Workspace(WORKSPACE1.getName(), REPOSITORY2))))); + public void shouldNotBeEqualIfSameNameButDifferentWorkspace() { + assertThat(NODETYPE1, is(not(equalTo(new NodeType(NODETYPE1.getName(), WORKSPACE2, null))))); } @Test - public void shouldNotBeEqualIfSameRepositoryButDifferentName() { - assertThat(WORKSPACE1, is(not(equalTo(new Workspace(NAME2, WORKSPACE1.getRepository()))))); + public void shouldNotBeEqualIfSameWorkspaceButDifferentName() { + assertThat(NODETYPE1, is(not(equalTo(new NodeType(NAME2, WORKSPACE1, null))))); } }