7 Replies Latest reply on Sep 21, 2009 11:25 AM by John Ament

    Programmatically set mixin type

    John Ament Master

      I was reviewing chapter 9 of the docs, specifically 9.3.5, custom node type registration. Looking through it, I don't see a way to programmatically add a mixin to a node. Anyone?

        • 1. Re: Programmatically set mixin type
          Randall Hauch Master

          You just include the mixin type(s) in the list of supertypes, and then set via NodeTypeTemplate.setDeclaredSupertypeNames(...). See http://www.jboss.org/file-access/default/members/dna/freezone/docs/0.6/api/index.html. Yes, you need to know the existing supertypes, and this isn't really a big deal for creating new node types. However, it does make it a bit harder to change an existing node type, tho that has lots of other challenges in JCR.

          I suspect JCR 2.0 patterned this after the semantics of CND and how a node type's supertypes can be other primary types and mixin types.

          • 2. Re: Programmatically set mixin type
            John Ament Master

            I think I'm still a bit confused. When do I use a NodeDefinitionTemplate and a NodeTypeTemplate? Let's say I have a Folder type, which can be a child of root, or a child of itself, and can have children that are any of my random child types.

            Since folder is the only thing that can be child of root, is that therefor the NodeTypeTemplate, and then everything else is NodeDefinitionTemplate?

            • 3. Re: Programmatically set mixin type
              Brian Carothers Apprentice

              When you're defining new custom node types for registration, you use a NodeTypeTemplate. If the custom node type has child node definitions, you use a NodeDefinitionTemplate for each child node definition.

              For example, let's say you're defining a new custom node type "city" to represent cities and each city can have one or more sports teams as child nodes named "sportsTeam" (can you tell it's American football season?). You would do something like this:

              NodeTypeTemplate cityType = ...;
              cityType.setName("city");
              
              NodeDefinitionTemplate sportsTeamChildNode = ...;
              sportsTeamChildNode.setName("sportsTeam");
              sportsTeamChildNode.setSameNameSiblings(true); // Since a single city can have more than one sports team
              sportsTeamChildNode.setRequiredPrimaryTypes(new String[] { /* the required type or types */ });
              
              cityType.getNodeDefinitionTemplates().add(sportsTeamChildNode);
              
              // Then register the type
              


              Let's say for a minute that you want to make all "city" nodes referenceable. Then you would have added this line before registering the node type:
              cityType.setDeclaredSupertypeNames(new String[] { "mix:referenceable" });


              Or, alternatively, let's say you wanted to make the "city" node type a mixin type instead of a primary type. Then you would have added this line before registering the node type:
              cityType.setMixin(true);





              • 4. Re: Programmatically set mixin type
                John Ament Master

                right, but what about the exact situation i described, where a node can have its own type as a child node?

                basically, to work around versioning support, i created a folder type, and that folder type should be able to contain other folders. there's a top level "folder" as well as the folder that contains the versions of the other types: e.g. content page, homepage, custom page, etc. the other pages seem to work, as do the folders, but i wanted to see how you expect the folders to end being mapped.

                • 5. Re: Programmatically set mixin type
                  Brian Carothers Apprentice

                  In the example above, you would add this line:

                  sportsTeamChildNode.setRequiredPrimaryTypes(new String[] { "city" });
                  
                  


                  You could also set the default primary type on the child node.

                  Having said that, I don't think that we explicitly test this scenario, so please let us know if you run into any problems.

                  • 6. Re: Programmatically set mixin type
                    John Ament Master

                    Hmm, maybe I'm doing it wrong.

                    In my code, i'm doing something like

                    root.addNode("someName","m:folder")

                    It recognizes it as a referenceable, but it's saying the type is nt:unstructured, even though I've registered m:folder in my code.

                    • 7. Re: Programmatically set mixin type
                      John Ament Master

                      nevermind, ignroe what i just said. it's working right.