001/*
002 * (C) Copyright 2006-2011 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 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.core.storage.sql;
021
022import java.io.Serializable;
023
024/**
025 * A SimpleProperty gives access to a scalar value stored in an underlying {@link SimpleFragment}.
026 *
027 * @author Florent Guillaume
028 */
029public class SimpleProperty extends BaseProperty {
030
031    /** The {@link SimpleFragment} holding the information. */
032    private final SimpleFragment fragment;
033
034    /** The key in the dataRow */
035    private final String key;
036
037    /**
038     * Creates a SimpleProperty, with specific info about row and key.
039     */
040    public SimpleProperty(String name, PropertyType type, boolean readonly, SimpleFragment fragment, String key) {
041        super(name, type, readonly);
042        this.fragment = fragment;
043        this.key = key;
044    }
045
046    // ----- getters -----
047
048    public Serializable getValue() {
049        return fragment.get(key);
050    }
051
052    public String getString() {
053        switch (type) {
054        case STRING:
055        case BINARY:
056            return (String) fragment.get(key);
057        default:
058            throw new RuntimeException("Not a String property: " + type);
059        }
060    }
061
062    public Long getLong() {
063        switch (type) {
064        case LONG:
065            return (Long) fragment.get(key);
066        default:
067            throw new RuntimeException("Not a Long property: " + type);
068        }
069    }
070
071    // ----- setters -----
072
073    public void setValue(Object value) {
074        checkWritable();
075        fragment.put(key, type.normalize(value));
076        // mark fragment dirty!
077    }
078
079}