Newer
Older
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2016 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package it.inaf.oats.ia2.tapschemamanager.api;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Column;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Key;
import it.inaf.oats.ia2.tapschemamanager.api.contract.KeyColumn;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Status;
import it.inaf.oats.ia2.tapschemamanager.api.contract.Table;
import it.inaf.oats.ia2.tapschemamanager.api.contract.TapSchema;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The main implementation of {@link Column}.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class ColumnImpl extends ChildEntityImpl<Table> implements Column {
private static final long serialVersionUID = 9175956487892235521L;
private static final Logger log = LoggerFactory.getLogger(ColumnImpl.class);
private boolean isPrimaryKey;
private boolean foreignKeySearched;
private Key foreignKey;
private Table parentTable;
private ColumnImpl() {
// for serialization
super();
}
protected ColumnImpl(DBWrapper dbWrapper, TapSchema tapSchema, Table table, String columnName, boolean indexed, boolean isPrimaryKey, String datatype, Integer size, Integer arraySize) {
super(dbWrapper, tapSchema);
this.isPrimaryKey = isPrimaryKey;
parentTable = table;
addProperty(TABLE_NAME_KEY, new FixedEntityProperty<>(table.getCompleteName()));
addProperty(COLUMN_NAME_KEY, new FixedEntityProperty<>(columnName));
addProperty(DATATYPE_KEY, new FixedEntityProperty<>(datatype));
addProperty(SIZE_KEY, new FixedEntityProperty<>(size));
addProperty(ARRAYSIZE_KEY, new FixedEntityProperty<>(arraySize));
addProperty(INDEXED_KEY, new FixedEntityProperty<>(indexed));
// Updatables
addProperty(DESCRIPTION_KEY, new EditableProperty<String>());
addProperty(UTYPE_KEY, new EditableProperty<String>());
addProperty(UNIT_KEY, new EditableProperty<String>());
addProperty(UCD_KEY, new EditableProperty<String>());
addProperty(PRINCIPAL_KEY, new EditableProperty<>(isPrimaryKey, false));
addProperty(STD_KEY, new EditableProperty<>(false, false));
addProperty(COLUMN_INDEX_KEY, new EditableProperty<Integer>());
addProperty(COLUMN_ID_KEY, new EditableProperty<Long>());
setStatus(Status.LOADED);
}
/**
* {@inheritDoc}
*/
@Override
public Key getForeignKey() {
if (!foreignKeySearched) { // lazy loading (but the foreignKey value can be null, so we use this boolean)
String tableCompleteName = getParent().getCompleteName();
String columnName = getValue(COLUMN_NAME_KEY, String.class);
keysloop: // we want to loop on all schema keys, also hidden ones.
for (Key key : ((TapSchemaImpl) tapSchema).getAllKeys()) {
if (key.getFromTableCompleteName().equals(tableCompleteName)) {
for (KeyColumn keyColumn : key.getKeyColumns()) {
if (keyColumn.getFromColumn().equals(columnName)) {
foreignKey = key;
break keysloop;
}
}
}
}
foreignKeySearched = true;
}
return foreignKey;
}
/**
* {@inheritDoc}
*/
@Override
public String getTableCompleteName() {
return getValue(TABLE_NAME_KEY, String.class);
}
/**
* {@inheritDoc}
*/
@Override
public String getName() {
return getValue(COLUMN_NAME_KEY, String.class);
}
@Override
public boolean getIndexed() {
return getValue(INDEXED_KEY, Boolean.class);
}
/**
* {@inheritDoc}
*
* This information is not stored into the TAP_SCHEMA, so it will be
* reloaded from the source schema each time.
*/
@Override
public boolean isPrimaryKey() {
return isPrimaryKey;
}
/**
* {@inheritDoc}
*/
@Override
public String getDatatype() {
return getValue(DATATYPE_KEY, String.class);
}
/**
* {@inheritDoc}
*/
@Override
public Integer getArraySize() {
return getValue(ARRAYSIZE_KEY, Integer.class);
}
/**
* {@inheritDoc}
*/
@Override
public Integer getSize() {
return getValue(SIZE_KEY, Integer.class);
}
/**
* {@inheritDoc}
*/
@Override
public String getDescription() {
return getValue(DESCRIPTION_KEY, String.class);
}
@Override
public void setDescription(String description) {
setValue(DESCRIPTION_KEY, description);
}
/**
* {@inheritDoc}
*/
@Override
public String getUtype() {
return getValue(UTYPE_KEY, String.class);
}
@Override
public void setUtype(String utype) {
setValue(UTYPE_KEY, utype);
}
/**
* {@inheritDoc}
*/
@Override
public String getUnit() {
return getValue(UNIT_KEY, String.class);
}
@Override
public void setUnit(String unit) {
setValue(UNIT_KEY, unit);
}
/**
* {@inheritDoc}
*/
@Override
public String getUCD() {
return getValue(UCD_KEY, String.class);
}
@Override
public void setUCD(String ucd) {
setValue(UCD_KEY, ucd);
}
/**
* {@inheritDoc}
*/
@Override
public boolean getPrincipal() {
return getValue(PRINCIPAL_KEY, Boolean.class);
}
@Override
public void setPrincipal(boolean principal) {
setValue(PRINCIPAL_KEY, principal);
}
/**
* {@inheritDoc}
*/
@Override
public boolean getStd() {
return getValue(STD_KEY, Boolean.class);
}
@Override
public void setStd(boolean std) {
setValue(STD_KEY, std);
}
/**
* {@inheritDoc}
*/
@Override
public Integer getColumnIndex() {
return getValue(COLUMN_INDEX_KEY, Integer.class);
}
@Override
public void setColumnIndex(Integer columnIndex) {
setValue(COLUMN_INDEX_KEY, columnIndex);
}
/**
* {@inheritDoc}
*/
@Override
public Long getColumnID() {
return getValue(COLUMN_ID_KEY, Long.class);
}
@Override
public void setColumnID(Long columnID) {
setValue(COLUMN_ID_KEY, columnID);
}
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + Objects.hashCode(getTableCompleteName());
hash = 29 * hash + Objects.hashCode(getName());
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ColumnImpl other = (ColumnImpl) obj;
if (!Objects.equals(this.getTableCompleteName(), other.getTableCompleteName())) {
return false;
}
if (!Objects.equals(this.getName(), other.getName())) {
return false;
}
return true;
}
/**
* {@inheritDoc}
*/
@Override
public Table getParent() {
return parentTable;
}
}