All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.metadata.MultitenancyMetaData Maven / Gradle / Ivy

Go to download

DataNucleus Core provides the primary components of a heterogenous Java persistence solution. It supports persistence API's being layered on top of the core functionality.

There is a newer version: 6.0.9
Show newest version
/**********************************************************************
Copyright (c) 2021 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:
    ...
**********************************************************************/
package org.datanucleus.metadata;

import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.util.StringUtils;

/**
 * MetaData representation for a multitenancy discriminator.
 */
public class MultitenancyMetaData extends MetaData
{
    private static final long serialVersionUID = 1526198088851283681L;

    /** Column name for multitenancy discriminator. */
    protected String columnName = null;

    /** Multitenancy column details. */
    protected ColumnMetaData columnMetaData=null;

    protected Boolean indexed = null;

    /** Detail of any indexing of the multitenancy column (optional). */
    protected IndexMetaData indexMetaData;

    public MultitenancyMetaData()
    {
    }

    /**
     * Copy constructor.
     * @param mtmd MultitenancyMetaData to copy
     */
    public MultitenancyMetaData(final MultitenancyMetaData mtmd)
    {
        super(null, mtmd);
        this.columnName = mtmd.columnName;
        this.indexed = mtmd.indexed;
        if (mtmd.columnMetaData != null)
        {
            setColumnMetaData(new ColumnMetaData(mtmd.columnMetaData));
        }
        if (mtmd.indexMetaData != null)
        {
            setIndexMetaData(new IndexMetaData(mtmd.indexMetaData));
        }
    }

    /**
     * Initialisation method. 
     * This should be called AFTER using the populate method if you are going to use populate. 
     * It creates the internal convenience arrays etc needed for normal operation.
     * @param clr Not used
     */
    public void initialise(ClassLoaderResolver clr)
    {
        // Interpret the "indexed" value to create our IndexMetaData where it wasn't specified that way
        if (indexMetaData == null && indexed == Boolean.TRUE)
        {
            indexMetaData = new IndexMetaData();
            indexMetaData.setUnique(false);
            if (columnMetaData != null)
            {
                indexMetaData.addColumn(columnMetaData.getName());
            }
            indexMetaData.parent = this;
        }

        setInitialised();
    }

    public String getColumnName()
    {
        // Return the column variant if specified, otherwise the name field
        if (columnMetaData != null && columnMetaData.getName() != null)
        {
            return columnMetaData.getName();
        }
        return columnName;
    }

    public MultitenancyMetaData setColumnName(String columnName)
    {
        if (!StringUtils.isWhitespace(columnName))
        {
            if (columnMetaData == null)
            {
                columnMetaData = new ColumnMetaData();
                columnMetaData.setName(columnName);
                columnMetaData.parent = this;
            }
            else
            {
                columnMetaData.setName(columnName);
            }
            this.columnName = columnName;
        }
        else
        {
            this.columnName = null;
        }
        return this;
    }

    /**
     * Accessor for column MetaData.
     * @return Returns the column MetaData.
     */
    public ColumnMetaData getColumnMetaData()
    {
        return columnMetaData;
    }

    /**
     * Method to create a new ColumnMetaData, add it, and return it.
     * @return The Column metadata
     */
    public ColumnMetaData newColumnMetaData()
    {
        ColumnMetaData colmd = new ColumnMetaData();
        colmd.setJdbcType(JdbcType.VARCHAR); // Default to text
        if (columnName != null)
        {
            colmd.setName(columnName);
        }
        setColumnMetaData(colmd);
        return colmd;
    }

    /**
     * Mutator for column MetaData.
     * @param columnMetaData The column MetaData to set.
     */
    public void setColumnMetaData(ColumnMetaData columnMetaData)
    {
        this.columnMetaData = columnMetaData;
        this.columnMetaData.parent = this;
    }

    public MultitenancyMetaData setIndexed(boolean indexed)
    {
        this.indexed = indexed;
        return this;
    }

    /**
     * Accessor for indexMetaData
     * @return Returns the indexMetaData.
     */
    public final IndexMetaData getIndexMetaData()
    {
        return indexMetaData;
    }

    /**
     * Mutator for the index MetaData 
     * @param indexMetaData The indexMetaData to set.
     */
    public final void setIndexMetaData(IndexMetaData indexMetaData)
    {
        this.indexMetaData = indexMetaData;
        this.indexMetaData.parent = this;
    }

    public String toString()
    {
        StringBuilder str = new StringBuilder("MultitenancyMetaData[");
        if (columnName != null)
        {
            str.append("columnName=").append(columnName);
        }
        if (columnMetaData != null)
        {
            str.append(", column=").append(columnMetaData);
        }
        if (indexed != null)
        {
            str.append(", indexed=").append(indexed);
        }
        str.append("]");
        return str.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy