7 Replies Latest reply on Nov 20, 2014 9:21 AM by rhauch

    How do I share a node's content using nt:linkedFile type node ?

    djoshi24

      I have different nodes under /, one per user in my system. One of them user already uploaded a file and shares it with another user. I want the same jcr:content file to get referred by a node under the another user's child node. how do I implement that ?

        • 1. Re: How do I share a node's content using nt:linkedFile type node ?
          hchiorean

          According to the spec:

          The nt:linkedFile node type is similar to nt:file, except that the content node is not stored directly as a child node, but rather is specified by a REFERENCE property. This allows the content node to reside anywhere in the workspace and to be referenced by multiple nt:linkedFile nodes. The content node must be referenceable.

          So when you upload & store a new piece of content, you need to make sure that you also add the [mix:referenceable] mixin to the content node (either via your node type definitions or via the API). Once you store a referenceable content node, you should be able to use the [nt:linkedFile] type for other nodes and set the [jcr:content] property as a reference towards the original content node (just like you would set a normal reference):

          [nt:linkedFile] > nt:hierarchyNode primaryitem jcr:content

            - jcr:content (reference) mandatory

          1 of 1 people found this helpful
          • 2. Re: Re: How do I share a node's content using nt:linkedFile type node ?
            djoshi24

            Thank you for reply.

             

            For storing a document while uploading, I am writing the following code


                           InputStream stream = new BufferedInputStream(new FileInputStream(file));

             

                        Node folder = root.addNode("FolderPdf " + rnd.nextInt(), "nt:folder");

                        Node fileNode = folder.addNode("FilePdf " + rnd.nextInt(), "nt:file");

             

                        Node contentNode = fileNode.addNode("jcr:content", "nt:resource");

                        Binary binary = session.getValueFactory().createBinary(stream);

                        contentNode.setProperty("jcr:data", binary);

             

            I am not getting how to give the reference to existing jcr:content node. Could you please give me piece of code explaining that.

            • 3. Re: Re: How do I share a node's content using nt:linkedFile type node ?
              hchiorean
              I am not getting how to give the reference to existing jcr:content node. Could you please give me piece of code explaining that.

              contentNode.addMixin("mix:referenceable").

               

              And you set the property on the nt:linkedFile nodes like: node.setProperty("jcr:content", contentNode).

              • 4. Re: How do I share a node's content using nt:linkedFile type node ?
                bes82

                Sorry if this is a bit off topic, is mix:referencable something that is impacting performance?

                 

                I'm currently only using that mixin so that I can read jcr:uuid and then use that uuid for soft links on other nodes. Not for assigning nodes to properties as described in your post.

                 

                I'm asking, because I'm using mix:referencable a lot, so If that is something that would cause a lot of (unneccessary) processing overhead when creating nodes, I should refactor my code.

                • 5. Re: How do I share a node's content using nt:linkedFile type node ?
                  rhauch

                  I'm currently only using that mixin so that I can read jcr:uuid and then use that uuid for soft links on other nodes. Not for assigning nodes to properties as described in your post.

                   

                  I'm asking, because I'm using mix:referencable a lot, so If that is something that would cause a lot of (unneccessary) processing overhead when creating nodes, I should refactor my code.

                  Adding a mixin to a node (like "mix:referenceable") is very little overhead - it basically adds the mixin name to the "jcr:mixins" property (which is protected). You're right, though -- there is some.

                   

                  An alternative that uses none of the overhead is to use the "Node.getIdentifier()" method (added in JCR 2.0) that returns ModeShape's internal identifier for the node. This works on every node, even for nodes that are not referenceable. So, if you're just getting an identifier string to store in a property (of type STRING), you can certainly use the node's identifier instead of a referenceable node's UUID. There's even a method on Session to directly look up the node given an identifier string. (Please consider this string to be opaque; ModeShape uses different patterns for the identifiers strings of different kinds of nodes.)

                   

                  Yet another alternative is to use a property of type SIMPLEREFERENCE. This is non-standard and specific to ModeShape, but it allows you to get and set the values of the property via Node references. Yes, you still need the referred node to be "mix:referenceable", but it is easier than having to manually get the identifier for the node before setting the identifier string as the value, and manually resolving the string to a Node when getting the value. A SIMPLEREFERENCE is like a WEAKREFERENCE in that referential integrity is not enforced, but there is no backreference from the referred node to the referring node (so it's less overhead).

                  • 6. Re: How do I share a node's content using nt:linkedFile type node ?
                    bes82

                    Thanks for the reply,

                     

                    I already use Node.getIdentifier(), for creating weak references, the jcr:referencable mixin is only used so that I can use "WHERE jcr:uuid IN" subselect queries. Sorry, the "I'm currently only using that mixin so that I can read jcr:uuid " part was a bit misleading.

                     

                    So if I understand your answer correctly, not much is happening when just adding the mixin, but rather when adding a node having that mixin to a property (which I don't do). Correct?

                    • 7. Re: How do I share a node's content using nt:linkedFile type node ?
                      rhauch

                      So if I understand your answer correctly, not much is happening when just adding the mixin, but rather when adding a node having that mixin to a property (which I don't do). Correct?

                      Correct. Very little happens when that property is a SIMPLEREFERENCE, but more happens with WEAKREFERENCE and REFERENCE.