/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.jackrabbit.oak.jcr.util;

import org.apache.jackrabbit.mk.model.Scalar;
import org.apache.jackrabbit.mk.model.ScalarImpl;

import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import java.util.ArrayList;
import java.util.List;

/**
 * Utility class for converting between internal value representation and JCR
 * values.
 * todo: needs refactoring. see OAK-16.
 */
public final class ValueConverter {
    private ValueConverter() {}

    public static Scalar toScalar(Value value) throws RepositoryException {
        switch (value.getType()) {
            case PropertyType.STRING: {
                return ScalarImpl.stringScalar(value.getString());
            }
            case PropertyType.DOUBLE: {
                return ScalarImpl.doubleScalar(value.getDouble());
            }
            case PropertyType.LONG: {
                return ScalarImpl.longScalar(value.getLong());
            }
            case PropertyType.BOOLEAN: {
                return ScalarImpl.booleanScalar(value.getBoolean());
            }
            case PropertyType.DECIMAL:
            case PropertyType.BINARY:
            case PropertyType.DATE:
            case PropertyType.NAME:
            case PropertyType.PATH:
            case PropertyType.REFERENCE:
            case PropertyType.WEAKREFERENCE:
            case PropertyType.URI:
            default: {
                throw new UnsupportedRepositoryOperationException("toScalar"); // todo implement toScalar
            }
        }
    }
    
    public static List<Scalar> toScalar(Value[] values) throws RepositoryException {
        List<Scalar> scalars = new ArrayList<Scalar>();
        for (Value value : values) {
            if (value != null) {
                scalars.add(toScalar(value));
            }
        }
        return scalars;
    }

    public static Value toValue(ValueFactory valueFactory, Scalar scalar)
            throws UnsupportedRepositoryOperationException {

        switch (scalar.getType()) {
            case BOOLEAN:
                return valueFactory.createValue(scalar.getBoolean());
            case LONG:
                return valueFactory.createValue(scalar.getLong());
            case DOUBLE:
                return valueFactory.createValue(scalar.getDouble());
            case STRING:
                return valueFactory.createValue(scalar.getString());
            default:
                throw new UnsupportedRepositoryOperationException("toValue"); // todo implement toValue
        }
    }

    public static Value[] toValues(ValueFactory valueFactory, Iterable<Scalar> scalars)
            throws UnsupportedRepositoryOperationException {
        List<Value> values = new ArrayList<Value>();
        for (Scalar scalar : scalars) {
            values.add(toValue(valueFactory, scalar));
        }
        return values.toArray(new Value[values.size()]);
    }

}

@@ -1384,16 +1384,1188 @@
 r() %7B%7D%0A%0A
+    public static Scalar toScalar(String value, int propertyType) throws RepositoryException %7B%0A        switch (propertyType) %7B%0A            case PropertyType.STRING: %7B%0A                return ScalarImpl.stringScalar(value);%0A            %7D%0A            case PropertyType.DOUBLE: %7B%0A                return ScalarImpl.doubleScalar(Double.parseDouble(value));%0A            %7D%0A            case PropertyType.LONG: %7B%0A                return ScalarImpl.longScalar(Long.parseLong(value));%0A            %7D%0A            case PropertyType.BOOLEAN: %7B%0A                return ScalarImpl.booleanScalar(Boolean.parseBoolean(value));%0A            %7D%0A            case PropertyType.BINARY: %7B%0A                return ScalarImpl.binaryScalar(value);%0A            %7D%0A            case PropertyType.DECIMAL:%0A            case PropertyType.DATE:%0A            case PropertyType.NAME:%0A            case PropertyType.PATH:%0A            case PropertyType.REFERENCE:%0A            case PropertyType.WEAKREFERENCE:%0A            case PropertyType.URI:%0A            default: %7B%0A                // todo implement toScalar%0A                throw new UnsupportedRepositoryOperationException(%22toScalar%22);%0A            %7D%0A        %7D%0A    %7D%0A%0A
     publ

add ValueConverter#toScalar(String, type)