001/*
002 * Copyright (c) 2006-2011 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 *     Florent Guillaume
011 */
012
013package org.nuxeo.ecm.core.storage.sql;
014
015import java.io.Serializable;
016
017/**
018 * A {@link CollectionProperty} gives access to a collection value stored in an underlying {@link Fragment}.
019 *
020 * @author Florent Guillaume
021 */
022public class CollectionProperty extends BaseProperty {
023
024    /** The {@link Fragment} holding the information. */
025    private final Fragment fragment;
026
027    /** The key in the row if it is a SimpleFragment otherwise null */
028    private final String key;
029
030    /**
031     * Creates a {@link CollectionProperty}.
032     */
033    public CollectionProperty(String name, PropertyType type, boolean readonly, CollectionFragment fragment) {
034        super(name, type, readonly);
035        this.fragment = fragment;
036        this.key = null;
037    }
038
039    /**
040     * Creates a {@link CollectionProperty}.
041     */
042    public CollectionProperty(String name, PropertyType type, boolean readonly, SimpleFragment fragment, String key) {
043        super(name, type, readonly);
044        this.fragment = fragment;
045        this.key = key;
046    }
047
048    // ----- getters -----
049
050    public Serializable[] getValue() {
051        Serializable[] value = null;
052        if (hasCollectionFragment()) {
053            value = ((CollectionFragment) fragment).get();
054        } else {
055            value = (Serializable[]) ((SimpleFragment) fragment).get(key);
056            if (value == null) {
057                value = type.getEmptyArray();
058            }
059        }
060        return value;
061    }
062
063    public String[] getStrings() {
064        switch (type) {
065        case ARRAY_STRING:
066            Serializable[] res = getValue();
067            if (res.length == 0) {
068                // special case because we may have an empty Serializable[]
069                res = new String[0];
070            }
071            return (String[]) res;
072        default:
073            throw new UnsupportedOperationException("Not implemented: " + type);
074        }
075    }
076
077    // ----- setters -----
078
079    public void setValue(Object[] value) {
080        checkWritable();
081        try {
082            if (hasCollectionFragment()) {
083                ((CollectionFragment) fragment).set(type.normalize(value));
084            } else {
085                ((SimpleFragment) fragment).put(key, type.normalize(value));
086            }
087        } catch (IllegalArgumentException e) {
088            throw new IllegalArgumentException("item of list property '" + name + "': " + e.getMessage());
089        }
090        // mark fragment dirty!
091    }
092
093    private boolean hasCollectionFragment() {
094        return key == null;
095    }
096
097}