Search code examples
javareflectionnodesjcr

Is it possible to convert a List to a JCR Value?


So I am using reflection to create a new instance of a List. I then want to set that list to a Node. The problem is Nodes only take primitives, Strings, Value, and Value[]. I get a ClassCastException when I try to do this:

 Value[] valueArray = (Value[])Array.newInstance(elementType,size);

I would like to do :

node.setProperty(name,valueArray);

Has anyone come across a way to do this properly? Or places that would lead me in the right direction? Is this even possible? Thanks for looking.


Solution

  • So why don't you just have your own implementation of Value, for instance like this:

    class MyValue implements Value {
        private Object value;
        private int type;
    
        public MyValue(Object value) throws RepositoryException {
            if (value == null)
                throw new RepositoryException("Value can not be null");
            this.value = value;
            if (value instanceof Boolean) {
                type = PropertyType.BOOLEAN;
            } else if (value instanceof Calendar) {
                type = PropertyType.DATE;
            } else if (value instanceof Double) {
                type = PropertyType.DOUBLE;
            } else if (value instanceof Long) {
                type = PropertyType.LONG;
            } else if (value instanceof String) {
                type = PropertyType.STRING;
            } else {
                throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
            }
        }
    
        @Override
        public Binary getBinary() throws RepositoryException {
            throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
        }
    
        @Override
        public boolean getBoolean() throws ValueFormatException,
                RepositoryException {
            if (type == PropertyType.BOOLEAN)
                return (Boolean)value;
            throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
        }
    
        @Override
        public Calendar getDate() throws ValueFormatException, RepositoryException {
            if (type == PropertyType.DATE)
                return (Calendar)value;
            throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
        }
    
        @Override
        public BigDecimal getDecimal() throws ValueFormatException,
                RepositoryException {
            if (type == PropertyType.DECIMAL)
                return (BigDecimal)value;
            throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
        }
    
        @Override
        public double getDouble() throws ValueFormatException, RepositoryException {
            if (type == PropertyType.DOUBLE)
                return (Double)value;
            throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
        }
    
        @Override
        public long getLong() throws ValueFormatException, RepositoryException {
            if (type == PropertyType.LONG)
                return (Long)value;
            throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
        }
    
        @Override
        public InputStream getStream() throws RepositoryException {
            throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
        }
    
        @Override
        public String getString() throws ValueFormatException,
                IllegalStateException, RepositoryException {
            if (type == PropertyType.STRING)
                return (String)value;
            throw new RepositoryException("Wrong type: " + value.getClass().getSimpleName());
        }
    
        @Override
        public int getType() {
            return type;
        }
    
        @Override
        public String toString() {
            return value.getClass().getSimpleName() + "(" + value + ")";
        }
    }
    

    Then you array code wouble be like:

        Value[] valueArray = new Value[size];
        valueArray[0] = new MyValue(123L);
        valueArray[1] = new MyValue(123.45);
        valueArray[2] = new MyValue("12345");