Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2017 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.ia2.tsm.webapp;
import it.inaf.ia2.tsm.TapSchema;
import it.inaf.ia2.tsm.datalayer.DBBroker;
import it.inaf.ia2.tsm.datalayer.DBBrokerFactory;
import it.inaf.ia2.tsm.datalayer.DBWrapper;
import it.inaf.ia2.tsm.webapp.env.CustomPartialResponseWriter;
import it.inaf.ia2.tsm.webapp.xmlconfig.JoinedCredentials;
import it.inaf.ia2.tsm.webapp.xmlconfig.SeparatedCredentials;
import it.inaf.ia2.tsm.webapp.xmlconfig.TapCredentials;
import java.io.Serializable;
import java.sql.SQLException;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.deltaspike.core.api.scope.WindowScoped;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
@Named("tapSchemaLoader")
@WindowScoped
public class TapSchemaLoader implements Serializable {
private static final long serialVersionUID = 3203810003976020854L;
private static final Logger LOG = LoggerFactory.getLogger(TapSchemaLoader.class);
@Inject
private ConsistencyChecksBean consistencyChecksBean;
@Inject
TapSchemaEditingBean tapSchemaEditingBean;
private TapCredentials tapCredentials;
private DBWrapper dbWrapper;
private boolean loading;
private TapSchema loadedTapSchema;
private String loadingError;
public void tryLoadingTapSchema(TapCredentials tapCredentials) throws SQLException {
this.tapCredentials = tapCredentials;
if (tapCredentials instanceof JoinedCredentials) {
JoinedCredentials joinedCredentials = (JoinedCredentials) tapCredentials;
dbWrapper = new DBWrapper(joinedCredentials.getCredentials());
} else {
SeparatedCredentials separatedCredentials = (SeparatedCredentials) tapCredentials;
dbWrapper = new DBWrapper(separatedCredentials.getSourceCredentials(), separatedCredentials.getTapSchemaCredentials());
}
// Testing connections
dbWrapper.testConnections();
// Searching for TAP_SCHEMA name
DBBroker broker = DBBrokerFactory.getDBBroker(dbWrapper.getTapSchemaDataSourceWrapper());
boolean tapSchemaExists = false;
for (String schemaName : broker.getAllSchemaNames()) {
if (schemaName.equals(tapCredentials.getTapSchemaName())) {
tapSchemaExists = true;
break;
}
}
CustomPartialResponseWriter.getCurrentInstance().addCustomJSUpdate("tap_schema_existence", String.valueOf(tapSchemaExists));
if (tapSchemaExists) {
edit();
}
// Otherwise create TAP_SCHEMA only if user press Ok on confirmation dialog
}
public void edit() {
loadedTapSchema = null;
loading = true;
loadingError = null;
new Thread(new Runnable() {
@Override
public void run() {
try {
loadedTapSchema = new TapSchema(tapCredentials.getTapSchemaVersion(), dbWrapper, tapCredentials.getTapSchemaName(), true);
} catch (Throwable e) {
LOG.error("Exception caught", e);
loadingError = e.getMessage();
if (loadingError == null) {
loadingError = e.getClass().getCanonicalName();
}
}
loading = false;
}
}).start();
}
public void create() {
loadedTapSchema = null;
loading = true;
loadingError = null;
new Thread(new Runnable() {
@Override
public void run() {
try {
loadedTapSchema = new TapSchema(tapCredentials.getTapSchemaVersion(), dbWrapper, tapCredentials.getTapSchemaName(), false);
} catch (Throwable e) {
LOG.error("Exception caught", e);
if (e.getMessage() != null) {
loadingError = e.getMessage();
} else {
loadingError = e.getClass().getCanonicalName();
loadedTapSchema = null;
}
}
loading = false;
}
}).start();
}
public TapCredentials getTapCredentials() {
return tapCredentials;
}
public boolean isLoading() {
return loading;
}
public TapSchema getLoadedTapSchema() {
return loadedTapSchema;
}
public String getLoadingError() {
return loadingError;
}
public String openLoaded() {
if (loadedTapSchema.getConsistencyChecks().isInconsistent()) {
consistencyChecksBean.setDbWrapper(dbWrapper);
consistencyChecksBean.setTapSchema(loadedTapSchema);
return "consistencyChecks.xhtml?faces-redirect=true";
} else {
tapSchemaEditingBean.setTapSchema(loadedTapSchema);
return "tapSchemaEditing.xhtml?faces-redirect=true";
}
}
}