001/*
002 * Copyright (c) 2006-2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Benjamin JALON <bjalon@nuxeo.com>
011 */
012package org.nuxeo.ecm.automation.core.operations.document;
013
014import org.nuxeo.ecm.automation.OperationException;
015import org.nuxeo.ecm.core.schema.types.ListType;
016import org.nuxeo.ecm.core.schema.types.Type;
017
018/**
019 * Abstract Class that exposes some useful method to manage list of values
020 *
021 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
022 * @since 5.7
023 */
024public class AbstractOperationMultiValuedProperty {
025
026    /**
027     * Check if the given field type store a list of values and if the given value is compatible with the given type. We
028     * assume the Type store a list of scalar values, not complex types.
029     */
030    protected void checkFieldType(Type type, Object value) throws OperationException {
031        if (!type.isListType()) {
032            throw new OperationException("Only multivalued String Types can be set using this operation");
033        }
034
035        ListType listType = (ListType) type;
036        Type itemType = listType.getFieldType();
037        if (itemType.isComplexType()) {
038            throw new UnsupportedOperationException("Manage only lists of scalar items");
039        }
040
041        if (!itemType.newInstance().getClass().equals(value.getClass())) {
042            throw new UnsupportedOperationException(String.format("Given type \"%s\" value is not a %s type", value,
043                    itemType.getName()));
044        }
045    }
046}