检索并将 `jcr:mixinTypes` 赋值为多值属性在 Magnolia 中。

huangapple go评论78阅读模式
英文:

Retrieve and assign jcr:mixinTypes a multi-valued property in magnolia

问题

我在尝试获取Magnolia中的多值属性时遇到错误,因为我将其分配给了String类型。我应该如何正确读取单值和多值属性(因为两者都存在)?

> javax.jcr.ValueFormatException:属性
> /wcmstest/de/search/jcr:mixinTypes是多值属性,因此其值只能作为数组检索
> 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):

这是我正在使用的代码:

HashMap<String, Object> map = new LinkedHashMap<>();
PropertyIterator pi;
try {
    pi = node.getProperties();
    // 获取根节点的属性
    while (pi.hasNext()) {
        Property p = pi.nextProperty();
        String name = p.getName();
        String val = p.getString();

        map.put(name, val);
    } // 结束根节点属性的循环
} catch (RepositoryException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
英文:

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&lt;String, Object&gt; map = new LinkedHashMap&lt;&gt;();
 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();
 }

答案1

得分: 1

感谢 @reporter 和一些尝试和错误。这里是一个可行的解决方案

HashMap<String, Object> map = new LinkedHashMap<>();
   
PropertyIterator pi;
try {
    pi = node.getProperties();
    //获取根节点的属性
    while(pi.hasNext()) {
       Property p = pi.nextProperty();
       String name = p.getName();
       String val;
       //确定属性是否为多值
       if (p.isMultiple()) {
            //多值属性		     
            val = p.getValues().toString();
            
        } else {
            //单值属性
            val = p.getValue().getString();
            
        }
        map.put(name, val);
      
    }//结束根节点属性的循环
} catch (RepositoryException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
英文:

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

           HashMap&lt;String, Object&gt; map = new LinkedHashMap&lt;&gt;();
		   
		   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();
			}

huangapple
  • 本文由 发表于 2020年1月30日 18:28:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/59983882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定