0
votes

I'm playing with a JCR repository - Jackrabbit specifically - for storing data for my current project. Everything is going fine, though documentation is unfortunately a bit hard to find at times. What I'm struggling with right now is setting up unit-test data for some tests. I've gotten an in-memory Jackrabbit running, and am importing my Nodetypes.cnd and data.xml files into it correctly so that I have seed data to test against. I can't however work out how to import from the data.xml file where the node has multiple properties with the same name.

Specifically, because of the way the data is structured I have a Rules node that links to other Rules nodes - where one rule actually works in terms of other rules. I've implemented this by having a property on the Rules node that is "rule (PATH) multiple", so simply a list of the node paths to the rules nodes that are linked. I can easily add one link in my xml by writing

<rule1 name="Rule #1" description="This is Rule number 1" rule="/rules/rule2" />

which correctly creates a rule called "rule1" that has a link to "rule2". What I want to to be able to create this rule using the session.importXML() method where it has more than one link... I've tried

<rule1 name="Rule #1" description="This is Rule number 1" rule="/rules/rule2,/rules/rule3" />
<rule1 name="Rule #1" description="This is Rule number 1" rule="/rules/rule2" rule="/rules/rule3" />
<rule1 Rule #1" description="This is Rule number 1">
    <@rule>/rules/rule2</@rule>
    <@rule>/rules/rule3</@rule>
</rule1>

And none of these have worked. In fact - obviously enough - the second and third ones don't even parse as XML.

Is there a way to do this? Or do I need to change my import process to work differently here?

The import is literally done with

    InputStream xml = getClass().getResourceAsStream("/jcr/data.xml");
    session.importXML(session.getRootNode().getPath(), xml, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);

So absolutely nothing special about that at all.

1

1 Answers

1
votes

I am assuming the "data.xml" is a document-view XML that you want to import to the repository. Jackrabbit does not define how to import/export a multi-valued property with multiple values from/to a document view of a repository. It seems from the JCR 2.0 spec, the interpretation of a multi-valued property is dependent on the implementation. However Jackrabbit does support the import of multi-valued properties from a system-view of a repository. Same for export.

You can download or export the system-view of the repository using Session.exportSystemView() API or manually create one for your test repository.

Here is an example. Note - "car:engines (string) multiple".

<car='http://www.modeshape.org/examples/cars/1.0'>
[car:Car] > nt:unstructured, mix:created, mix:referenceable
  - car:maker (string)
  - car:model (string)
  - car:year (string) < '(19|20)[0-9]{2}'  // any 4 digit number starting with '19' or '20'
  - car:msrp (string) < '[$][0-9]{1,3}[,]?[0-9]{3}([.][0-9]{2})?'   // of the form "$X,XXX.ZZ", "$XX,XXX.ZZ" or "$XXX,XXX.ZZ"
                                                       // where '.ZZ' is optional
  - car:userRating (long) < '[1,5]'                        // any value from 1 to 5 (inclusive)
  - car:valueRating (long) < '[1,5]'                       // any value from 1 to 5 (inclusive)
  - car:mpgCity (long) < '(0,]'                            // any value greater than 0
  - car:mpgHighway (long) < '(0,]'                         // any value greater than 0
  - car:lengthInInches (double) < '(0,]'                   // any value greater than 0
  - car:wheelbaseInInches (double) < '(0,]'                // any value greater than 0
  - car:alternateModels (reference)  < 'car:Car'
  - car:engines (string) multiple

Following is an example system-view XML of a repository containing the above CND. Note the multi-valued property "car:engines" is represented as a list element.

<sv:property sv:name="car:engines" sv:type="String" sv:multiple="true">
    <sv:value>1.8L</sv:value>
    <sv:value>1.5L</sv:value>
</sv:property>

This system-view XML can be imported to another repository using Session.importXML() API as you are doing.

<?xml version="1.0" encoding="UTF-8"?>
<sv:node sv:name="Hybrid" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:fn_old="http://www.w3.org/2004/10/xpath-functions"  xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:car="http://www.modeshape.org/examples/cars/1.0" xmlns:rep="internal" xmlns:nt="http://www.jcp.org/jcr/nt/1.0">
    <sv:property sv:name="jcr:primaryType" sv:type="Name">
        <sv:value>nt:unstructured</sv:value>
    </sv:property>
    <sv:property sv:name="jcr:mixinTypes" sv:type="Name" sv:multiple="true">
        <sv:value>mix:referenceable</sv:value>
    </sv:property>
    <sv:property sv:name="jcr:uuid" sv:type="String">
        <sv:value>7e999653-e558-4131-8889-af1e16872f4d</sv:value>
    </sv:property>
    <sv:node sv:name="Toyota Prius">
        <sv:property sv:name="jcr:primaryType" sv:type="Name">
            <sv:value>car:Car</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:uuid" sv:type="String">
            <sv:value>0111cbd0-8f9b-4588-8ded-d66907174bcd</sv:value>
        </sv:property>
        <sv:property sv:name="car:engines" sv:type="String" sv:multiple="true">
            <sv:value>1.8L</sv:value>
            <sv:value>1.5L</sv:value>
        </sv:property>
        <sv:property sv:name="car:maker" sv:type="String">
            <sv:value>Toyota</sv:value>
        </sv:property>
        <sv:property sv:name="car:model" sv:type="String">
            <sv:value>Prius</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgCity" sv:type="Long">
            <sv:value>48</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgHighway" sv:type="Long">
            <sv:value>45</sv:value>
        </sv:property>
        <sv:property sv:name="car:msrp" sv:type="String">
            <sv:value>$21,500</sv:value>
        </sv:property>
        <sv:property sv:name="car:userRating" sv:type="Long">
            <sv:value>4</sv:value>
        </sv:property>
        <sv:property sv:name="car:valueRating" sv:type="Long">
            <sv:value>5</sv:value>
        </sv:property>
        <sv:property sv:name="car:year" sv:type="String">
            <sv:value>2008</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:created" sv:type="Date">
            <sv:value>2011-06-13T23:45:59.175-04:00</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:createdBy" sv:type="String">
            <sv:value>admin</sv:value>
        </sv:property>
        <sv:node sv:name="carImage">
            <sv:property sv:name="jcr:primaryType" sv:type="Name">
                <sv:value>nt:file</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:created" sv:type="Date">
                <sv:value>2011-06-13T23:45:59.181-04:00</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:createdBy" sv:type="String">
                <sv:value>admin</sv:value>
            </sv:property>
            <sv:node sv:name="jcr:content">
                <sv:property sv:name="jcr:primaryType" sv:type="Name">
                    <sv:value>nt:resource</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:uuid" sv:type="String">
                    <sv:value>7de51be4-1466-414b-a3ff-d840095e61bf</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:data" sv:type="Binary">
                    <sv:value/>
                </sv:property>
                <sv:property sv:name="jcr:encoding" sv:type="String">
                    <sv:value>binary</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModified" sv:type="Date">
                    <sv:value>2011-05-18T16:14:30.000-04:00</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModifiedBy" sv:type="String">
                    <sv:value>admin</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:mimeType" sv:type="String">
                    <sv:value>image/jpeg</sv:value>
                </sv:property>
            </sv:node>
        </sv:node>
        <sv:node sv:name="carImage">
            <sv:property sv:name="jcr:primaryType" sv:type="Name">
                <sv:value>nt:file</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:created" sv:type="Date">
                <sv:value>2011-06-13T23:45:59.322-04:00</sv:value>
            </sv:property>
            <sv:property sv:name="jcr:createdBy" sv:type="String">
                <sv:value>admin</sv:value>
            </sv:property>
            <sv:node sv:name="jcr:content">
                <sv:property sv:name="jcr:primaryType" sv:type="Name">
                    <sv:value>nt:resource</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:uuid" sv:type="String">
                    <sv:value>6ad955e2-6e75-4833-904c-6ee3bf7c18b6</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:data" sv:type="Binary">
                    <sv:value/>
                </sv:property>
                <sv:property sv:name="jcr:encoding" sv:type="String">
                    <sv:value>binary</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModified" sv:type="Date">
                    <sv:value>2011-05-18T16:14:30.000-04:00</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:lastModifiedBy" sv:type="String">
                    <sv:value>admin</sv:value>
                </sv:property>
                <sv:property sv:name="jcr:mimeType" sv:type="String">
                    <sv:value>image/jpeg</sv:value>
                </sv:property>
            </sv:node>
        </sv:node>
    </sv:node>
    <sv:node sv:name="Toyota Highlander">
        <sv:property sv:name="jcr:primaryType" sv:type="Name">
            <sv:value>car:Car</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:uuid" sv:type="String">
            <sv:value>99b2afd4-74e8-4a45-a6d2-623bfc66387a</sv:value>
        </sv:property>
        <sv:property sv:name="car:maker" sv:type="String">
            <sv:value>Toyota</sv:value>
        </sv:property>
        <sv:property sv:name="car:model" sv:type="String">
            <sv:value>Highlander</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgCity" sv:type="Long">
            <sv:value>27</sv:value>
        </sv:property>
        <sv:property sv:name="car:mpgHighway" sv:type="Long">
            <sv:value>25</sv:value>
        </sv:property>
        <sv:property sv:name="car:msrp" sv:type="String">
            <sv:value>$34,200</sv:value>
        </sv:property>
        <sv:property sv:name="car:userRating" sv:type="Long">
            <sv:value>4</sv:value>
        </sv:property>
        <sv:property sv:name="car:valueRating" sv:type="Long">
            <sv:value>5</sv:value>
        </sv:property>
        <sv:property sv:name="car:year" sv:type="String">
            <sv:value>2008</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:created" sv:type="Date">
            <sv:value>2011-06-13T23:45:59.191-04:00</sv:value>
        </sv:property>
        <sv:property sv:name="jcr:createdBy" sv:type="String">
            <sv:value>admin</sv:value>
        </sv:property>
    </sv:node>
</sv:node>