View Javadoc
1   package fr.ifremer.adagio.synchro.meta;
2   
3   /*
4    * #%L
5    * SIH-Adagio :: Synchronization
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import org.hibernate.tool.hbm2ddl.ColumnMetadata;
27  
28  import com.google.common.base.Preconditions;
29  
30  /**
31   * Overrides of the {@link ColumnMetadata} with some improvements:
32   * <ul>
33   * <li>Obtains owner table name {@link #getTableName()};</li>
34   * <li>Obtains linked column (for FK only) {@link #getParentJoin()} as a join metadata: used to synchronize data in
35   * {@link DataSynchroService};</li>
36   * <li>Obtains if a column is protected for data import {@link #isProtected()}: used by {@link SynchroServiceUtils} for
37   * schemas check.</li>
38   * </ul>
39   * 
40   * @author Benoit Lavenier <benoit.lavenier@e-is.pro>
41   * @since 3.5.2
42   * 
43   */
44  public class SynchroColumnMetadata {
45  
46  	protected final ColumnMetadata delegate;
47  
48  	protected final boolean isProtected;
49  
50  	protected final String tableName;
51  
52  	protected final int columnIndex;
53  
54  	protected SynchroJoinMetadata parentJoin = null;
55  
56  	public SynchroColumnMetadata(
57  			ColumnMetadata delegate,
58  			String tableName,
59  			int columnIndex,
60  			boolean isProtected) {
61  
62  		Preconditions.checkNotNull(delegate);
63  
64  		this.delegate = delegate;
65  		this.tableName = tableName;
66  		this.columnIndex = columnIndex;
67  		this.isProtected = isProtected;
68  	}
69  
70  	public SynchroColumnMetadata(
71  			SynchroColumnMetadata beanToCopy) {
72  
73  		Preconditions.checkNotNull(beanToCopy);
74  
75  		this.delegate = beanToCopy.delegate;
76  		this.tableName = beanToCopy.tableName;
77  		this.columnIndex = beanToCopy.columnIndex;
78  		this.isProtected = beanToCopy.isProtected;
79  	}
80  
81  	/**
82  	 * Obtains if a column is protected for data import.<br/>
83  	 * This is used by {@link SynchroServiceUtils} for schemas check.
84  	 * 
85  	 * @return true if the column is protected
86  	 */
87  	public boolean isProtected() {
88  		return isProtected;
89  	}
90  
91  	/**
92  	 * Obtains owner table name
93  	 * 
94  	 * @return The owner table name
95  	 */
96  	public String getTableName() {
97  		return tableName;
98  	}
99  
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 }