001package fr.ifremer.adagio.synchro.meta;
002
003/*
004 * #%L
005 * SIH-Adagio :: Synchronization
006 * $Id:$
007 * $HeadURL:$
008 * %%
009 * Copyright (C) 2012 - 2014 Ifremer
010 * %%
011 * This program is free software: you can redistribute it and/or modify
012 * it under the terms of the GNU Affero General Public License as published by
013 * the Free Software Foundation, either version 3 of the License, or
014 * (at your option) any later version.
015 * 
016 * This program is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019 * GNU General Public License for more details.
020 * 
021 * You should have received a copy of the GNU Affero General Public License
022 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
023 * #L%
024 */
025
026import org.hibernate.tool.hbm2ddl.ColumnMetadata;
027
028import com.google.common.base.Preconditions;
029
030/**
031 * Overrides of the {@link ColumnMetadata} with some improvements:
032 * <ul>
033 * <li>Obtains owner table name {@link #getTableName()};</li>
034 * <li>Obtains linked column (for FK only) {@link #getParentJoin()} as a join metadata: used to synchronize data in
035 * {@link DataSynchroService};</li>
036 * <li>Obtains if a column is protected for data import {@link #isProtected()}: used by {@link SynchroServiceUtils} for
037 * schemas check.</li>
038 * </ul>
039 * 
040 * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
041 * @since 3.5.2
042 * 
043 */
044public class SynchroColumnMetadata {
045
046    protected final ColumnMetadata delegate;
047
048    protected final boolean isProtected;
049
050    protected final String tableName;
051
052    protected final int columnIndex;
053
054    protected SynchroJoinMetadata parentJoin = null;
055
056    public SynchroColumnMetadata(
057            ColumnMetadata delegate,
058            String tableName,
059            int columnIndex,
060            boolean isProtected) {
061
062        Preconditions.checkNotNull(delegate);
063
064        this.delegate = delegate;
065        this.tableName = tableName;
066        this.columnIndex = columnIndex;
067        this.isProtected = isProtected;
068    }
069
070    public SynchroColumnMetadata(
071            SynchroColumnMetadata beanToCopy) {
072
073        Preconditions.checkNotNull(beanToCopy);
074
075        this.delegate = beanToCopy.delegate;
076        this.tableName = beanToCopy.tableName;
077        this.columnIndex = beanToCopy.columnIndex;
078        this.isProtected = beanToCopy.isProtected;
079    }
080
081    /**
082     * Obtains if a column is protected for data import.<br/>
083     * This is used by {@link SynchroServiceUtils} for schemas check.
084     * 
085     * @return true if the column is protected
086     */
087    public boolean isProtected() {
088        return isProtected;
089    }
090
091    /**
092     * Obtains owner table name
093     * 
094     * @return The owner table name
095     */
096    public String getTableName() {
097        return tableName;
098    }
099
100    /**
101     * Obtains linked column (for FK only)<br/>
102     * This is used to synchronize data in {@link DataSynchroService};
103     * 
104     * @return the parent join metadata
105     * @see SynchroJoinMetadata
106     */
107    public SynchroJoinMetadata getParentJoin() {
108        return parentJoin;
109    }
110
111    public void setParentJoin(SynchroJoinMetadata parentJoin) {
112        this.parentJoin = parentJoin;
113    }
114
115    /**
116     * @see ColumnMetadata#hashCode()
117     */
118    public int hashCode() {
119        return delegate.hashCode();
120    }
121
122    /**
123     * @see ColumnMetadata#getName()
124     */
125    public String getName() {
126        return delegate.getName();
127    }
128
129    /**
130     * @see ColumnMetadata#getTypeName()
131     */
132    public String getTypeName() {
133        return delegate.getTypeName();
134    }
135
136    /**
137     * @see ColumnMetadata#getColumnSize()
138     */
139    public int getColumnSize() {
140        return delegate.getColumnSize();
141    }
142
143    /**
144     * @see ColumnMetadata#getDecimalDigits()
145     */
146    public int getDecimalDigits() {
147        return delegate.getDecimalDigits();
148    }
149
150    /**
151     * @see ColumnMetadata#getTypeCode()
152     */
153    public String getNullable() {
154        return delegate.getNullable();
155    }
156
157    public boolean isNullable() {
158        return "YES".equalsIgnoreCase(getNullable());
159    }
160
161    /**
162     * @see ColumnMetadata#toString()
163     */
164    public String toString() {
165        return delegate.toString();
166    }
167
168    /**
169     * @see ColumnMetadata#getTypeCode()
170     */
171    public int getTypeCode() {
172        return delegate.getTypeCode();
173    }
174
175    /**
176     * @see ColumnMetadata#delegate(Object)
177     */
178    public boolean equals(Object obj) {
179        return delegate.equals(obj);
180    }
181
182}