2

I am getting an error when I try to get a multi-valued property in magnolia since I assign it to a String type. How can I properly read a single and multi-valued property (since both are present)?

javax.jcr.ValueFormatException: property /wcmstest/de/search/jcr:mixinTypes is a multi-valued property, so it's values can only be retrieved as an array at org.apache.jackrabbit.core.PropertyImpl.internalGetValue(PropertyImpl.java:483) at org.apache.jackrabbit.core.PropertyImpl.getValue(PropertyImpl.java:510) at org.apache.jackrabbit.core.PropertyImpl.getString(PropertyImpl.java:520) at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162) at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162) at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162) at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162) at info.magnolia.jcr.wrapper.DelegatePropertyWrapper.getString(DelegatePropertyWrapper.java:162) at ch.uzh.module.versioning.CreateNestedMap.nestedMap(CreateNestedMap.java:32) at ch.uzh.module.versioning.ContextLoggerCommand.execute(ContextLoggerCommand.java:78) at info.magnolia.commands.MgnlCommand.executeSynchronized(MgnlCommand.java:80) at info.magnolia.commands.MgnlCommand.execute(MgnlCommand.java:69) at info.magnolia.commands.chain.ChainBase.execute(ChainBase.java:101) at info.magnolia.commands.DelegateCommand.execute(DelegateCommand.java:66) :

This is the code I am using:

 HashMap<String, Object> map = new LinkedHashMap<>();
 PropertyIterator pi;
 try {
    pi = node.getProperties();
    //Get properties for the root node
    while(pi.hasNext())
    {
        Property p = pi.nextProperty();
        String name = p.getName();
        String val = p.getString();

        map.put(name,val);

    }//end of while for properties of root node
 } catch (RepositoryException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
 }
Ana Sustic
  • 425
  • 2
  • 17
  • 1
    https://stackoverflow.com/questions/42822066/how-to-determine-if-a-jcr-property-is-multivalue ? – Reporter Jan 30 '20 at 10:40
  • Thanks @reporter I will give it a try and store it as array if prop.isMultiple(). I was wondering what type should this array that stores multi-valued properties be? – Ana Sustic Jan 30 '20 at 10:51
  • 1
    I'm working only with the "big sister" of Magnolia calls Adobe Experience Manager -short AEM-. that offers a gui that allowes a view about stored nodes. Called crx. It seems Magnolia offers also such gui. To Your Exapmle: Is the type of your target node a String and hast multiple Values, then you need String[], else String. – Reporter Jan 30 '20 at 11:01
  • 1
    https://helpx.adobe.com/content/dam/help/en/experience-manager/6-2/sites/developing/using/customizing-consoles-touch/_jcr_content/main-pars/procedure_1/proc_par/step/step_par/image_1/custouch5.png a picture from AEM crx gui. – Reporter Jan 30 '20 at 11:06
  • 1
    The jcr api does also offer a method to determine node type. – Reporter Jan 30 '20 at 11:07

1 Answers1

1

Thanks to @reporter and some trial and error. Here is a working solution.

           HashMap<String, Object> map = new LinkedHashMap<>();

           PropertyIterator pi;
            try {
                pi = node.getProperties();
                //Get properties for the root node
                while(pi.hasNext())
                    {
                       Property p = pi.nextProperty();
                       String name = p.getName();
                       String val;
                       //determine if property is multi-value
                       if (p.isMultiple()) {
                            //multi-value property           
                            val=p.getValues().toString();

                        } else {
                            //single-value property
                            val = p.getValue().getString();

                        }
                        map.put(name,val);

                    }//end of while for properties of root node
            } catch (RepositoryException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
Ana Sustic
  • 425
  • 2
  • 17