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
022/**
023 * Model of a property (simple or array) of a {@link Node}.
024 */
025public class ModelProperty {
026
027    public final PropertyType propertyType;
028
029    public final String fragmentName;
030
031    public final String fragmentKey;
032
033    public final boolean readonly;
034
035    public final boolean fulltext;
036
037    protected final boolean isIntermediateSegment;
038
039    /**
040     * Creates a model for a scalar property, or the last segment of a complex property.
041     */
042    public ModelProperty(PropertyType propertyType, String fragmentName, String fragmentKey, boolean readonly) {
043        this.propertyType = propertyType;
044        this.fragmentName = fragmentName;
045        this.fragmentKey = fragmentKey;
046        this.readonly = readonly;
047        isIntermediateSegment = false;
048        // TODO use some config to decide this
049        fulltext = (propertyType.equals(PropertyType.STRING) || propertyType.equals(PropertyType.BINARY) || propertyType.equals(PropertyType.ARRAY_STRING))
050                && (fragmentKey == null || !fragmentKey.equals(Model.MAIN_KEY))
051                && !fragmentName.equals(Model.HIER_TABLE_NAME)
052                && !fragmentName.equals(Model.VERSION_TABLE_NAME)
053                && !fragmentName.equals(Model.PROXY_TABLE_NAME)
054                && !fragmentName.equals(Model.FULLTEXT_TABLE_NAME)
055                && !fragmentName.equals(Model.LOCK_TABLE_NAME)
056                && !fragmentName.equals(Model.UID_SCHEMA_NAME)
057                && !fragmentName.equals(Model.MISC_TABLE_NAME);
058    }
059
060    /**
061     * Create a model for an intermediate segment of a complex property.
062     *
063     * @param name the canonical segment name
064     * @since 5.7.3
065     */
066    public ModelProperty(String propertyName) {
067        this.fragmentName = propertyName;
068        propertyType = null;
069        fragmentKey = "";
070        readonly = false;
071        fulltext = false;
072        isIntermediateSegment = true;
073    }
074
075    /**
076     * Gets the segment name for an intermediate segment.
077     *
078     * @return the segment name
079     * @since 5.7.3
080     */
081    public String getIntermediateSegment() {
082        return fragmentName;
083    }
084
085    /**
086     * Checks if this is a pseudo-model for an intermediate segment of a complex property.
087     *
088     * @since 5.7.3
089     */
090    public boolean isIntermediateSegment() {
091        return isIntermediateSegment;
092    }
093
094    @Override
095    public String toString() {
096        return getClass().getSimpleName() + '(' + fragmentName + ", " + fragmentKey + ", " + propertyType
097                + (readonly ? ", RO" : "") + (fulltext ? ", FT" : "") + ')';
098    }
099
100}