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