001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.api.model.impl;
023
024import java.io.Serializable;
025import java.lang.reflect.Array;
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.Collection;
029
030import org.nuxeo.common.collections.PrimitiveArrays;
031import org.nuxeo.ecm.core.api.PropertyException;
032import org.nuxeo.ecm.core.api.model.Property;
033import org.nuxeo.ecm.core.api.model.PropertyConversionException;
034import org.nuxeo.ecm.core.schema.types.Field;
035import org.nuxeo.ecm.core.schema.types.JavaTypes;
036import org.nuxeo.ecm.core.schema.types.ListType;
037import org.nuxeo.ecm.core.schema.types.Type;
038import org.nuxeo.ecm.core.schema.types.TypeException;
039
040/**
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043public class ArrayProperty extends ScalarProperty {
044
045    private static final long serialVersionUID = 0L;
046
047    public ArrayProperty(Property parent, Field field, int flags) {
048        super(parent, field, flags);
049    }
050
051    @Override
052    public ListType getType() {
053        return (ListType) super.getType();
054    }
055
056    @Override
057    public boolean isContainer() {
058        return false;
059    }
060
061    @Override
062    public void setValue(Object value) throws PropertyException {
063        // this code manage dirty status for the arrayproperty and its childs values
064        // it checks whether the property changed, or their index changed
065        if (value == null) {
066            childDirty = new boolean[0];
067            super.setValue(null);
068        } else {
069            Object[] oldValues = (Object[]) internalGetValue();
070            boolean[] oldChildDirty = getChildDirty();
071            super.setValue(value);
072            Object[] newValues = (Object[]) internalGetValue();
073            boolean[] newChildDirty = new boolean[newValues != null ? newValues.length : 0];
074            for (int i = 0; i < newChildDirty.length; i++) {
075                Object newValue = newValues[i];
076                if (oldValues == null || i >= oldValues.length) {
077                    newChildDirty[i] = true;
078                } else {
079                    Object oldValue = oldValues[i];
080                    if (!((newValue == null && oldValue == null) || (newValue != null && newValue.equals(oldValue)))) {
081                        newChildDirty[i] = true;
082                    } else {
083                        newChildDirty[i] = false || oldChildDirty[i];
084                    }
085                }
086            }
087            childDirty = newChildDirty;
088        }
089    }
090
091    @Override
092    protected boolean isSameValue(Serializable value1, Serializable value2) {
093        Object[] castedtValue1 = (Object[]) value1;
094        Object[] castedtValue2 = (Object[]) value2;
095        return castedtValue1 == castedtValue2 || (castedtValue1 == null && castedtValue2.length == 0)
096                || (castedtValue2 == null && castedtValue1.length == 0) || Arrays.equals(castedtValue1, castedtValue2);
097    }
098
099    @Override
100    public boolean isNormalized(Object value) {
101        return value == null || value.getClass().isArray();
102    }
103
104    @Override
105    public Serializable normalize(Object value) throws PropertyConversionException {
106        if (value == null) {
107            return null;
108        } else if (value.getClass().isArray()) {
109            return convert(Arrays.asList((Object[]) value));
110        } else if (value instanceof Collection) {
111            return convert((Collection<?>) value);
112        }
113        throw new PropertyConversionException(value.getClass(), Object[].class, getPath());
114    }
115
116    protected Serializable convert(Collection<?> value) throws PropertyConversionException {
117        try {
118            Type fieldType = getType().getFieldType();
119            Collection<Object> col = new ArrayList<>(value.size());
120            for (Object v : value) {
121                if (v == null) {
122                    col.add(null);
123                } else {
124                    col.add(fieldType.convert(v));
125                }
126            }
127            Class<?> klass = JavaTypes.getClass(fieldType);
128            return col.toArray((Object[]) Array.newInstance(klass, col.size()));
129        } catch (TypeException e) {
130            throw new PropertyConversionException("Unable to convert collection value to desired type.", e);
131        }
132    }
133
134    @SuppressWarnings("unchecked")
135    @Override
136    public <T> T convertTo(Serializable value, Class<T> toType) throws PropertyConversionException {
137        if (toType.isArray()) {
138            return (T) PrimitiveArrays.toObjectArray(value);
139        } else if (Collection.class.isAssignableFrom(toType)) {
140            return (T) Arrays.asList((Object[]) value);
141        }
142        throw new PropertyConversionException(value.getClass(), toType);
143    }
144
145    @Override
146    public Object newInstance() {
147        return new Serializable[0];
148    }
149
150    // this boolean array managed the dirty flags for arrayproperty childs
151    private boolean[] childDirty = null;
152
153    protected boolean[] getChildDirty() {
154        if (childDirty == null) {
155            Object[] oldValues = (Object[]) internalGetValue();
156            if (oldValues == null) {
157                childDirty = new boolean[0];
158            } else {
159                childDirty = new boolean[oldValues.length];
160                for (int i = 0; i < childDirty.length; i++) {
161                    childDirty[i] = false;
162                }
163            }
164        }
165        return childDirty;
166    }
167
168    /**
169     * This method provides a way to know if some arrayproperty values are dirty: value or index changed. since 7.2
170     */
171    public boolean isDirty(int index) {
172        if (index > getChildDirty().length) {
173            throw new IndexOutOfBoundsException(
174                    "Index out of bounds: " + index + ". Bounds are: 0 - " + (getChildDirty().length - 1));
175        }
176        return getChildDirty()[index];
177    }
178
179    @Override
180    public void clearDirtyFlags() {
181        // even makes child properties not dirty
182        super.clearDirtyFlags();
183        for (int i = 0; i < getChildDirty().length; i++) {
184            childDirty[i] = false;
185        }
186    }
187
188}