001/*
002 *  Copyright 2019 Anyware Services
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 */
016package org.ametys.odf.ose.db.column;
017
018import org.apache.commons.lang3.tuple.Pair;
019
020/**
021 * The definition of a column with a foreign key.
022 * It extends {@link DefaultColumn} and add the notion of foreign key.
023 * For integrity reasons, the AMETYS_* tables haven't the foreign, it's only used to retrieve values from the SRC_* views.
024 */
025public class ForeignKeyColumn extends DefaultColumn
026{
027    /** The foreign key: left is table name and right is column name of the referenced column */
028    protected Pair<String, String> _foreignKey;
029    /** If the foreign key needs to match the year */
030    protected boolean _needYear;
031    
032    /**
033     * Constructor.
034     * @param name The name of the column
035     * @param type The SQL type of the column
036     * @param isNullable <code>true</code> if it's nullable
037     * @param foreignKey The foreign key
038     * @param needYear If the foreign key needs to match the year
039     * @param viewName The name of the column in the associated SRC_* view
040     */
041    public ForeignKeyColumn(String name, String type, boolean isNullable, Pair<String, String> foreignKey, boolean needYear, String viewName)
042    {
043        super(name, type, isNullable, viewName);
044        this._foreignKey = foreignKey;
045        this._needYear = needYear;
046    }
047    
048    /**
049     * Constructor
050     * @param name The name of the column (it will also be used for the associated SRC_* view)
051     * @param type The SQL type of the column
052     * @param isNullable <code>true</code> if it's nullable
053     * @param foreignKey The foreign key
054     * @param needYear If the foreign key needs to match the year
055     */
056    public ForeignKeyColumn(String name, String type, boolean isNullable, Pair<String, String> foreignKey, boolean needYear)
057    {
058        this(name, type, isNullable, foreignKey, needYear, name);
059    }
060    
061    @Override
062    public String getViewDescription()
063    {
064        StringBuilder sb = new StringBuilder();
065        
066        sb.append("(SELECT FKT.ID FROM ");
067        sb.append(_foreignKey.getLeft());
068        sb.append(" FKT WHERE FKT.");
069        sb.append(_foreignKey.getRight());
070        sb.append(" = TBL.");
071        sb.append(_name);
072        if (_needYear)
073        {
074            sb.append(" AND FKT.ANNEE_ID = TBL.ANNEE_ID");
075        }
076        sb.append(") AS ");
077        sb.append(_viewName);
078        
079        return sb.toString();
080    }
081}