Commit 59b4b3dc authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Credentials insertion and management changes

parent 5e2bad95
......@@ -29,7 +29,6 @@ import it.inaf.ia2.tsm.model.PropertyModel;
import it.inaf.ia2.tsm.model.TableModel;
import it.inaf.ia2.tsm.model.TapSchemaModel;
import it.inaf.ia2.tsm.model.TapSchemaModels;
import it.inaf.ia2.tsm.model.Tasman;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
......@@ -45,8 +44,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The main implementation of {@link TapSchema}.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class TapSchema implements EntitiesContainer<Schema>, Serializable {
......
......@@ -41,9 +41,9 @@ public interface DBBroker {
List<String> getAllSchemaNames() throws SQLException;
List<String> getAllTAPSchemaNames(List<String> allSchemas) throws SQLException;
String detectVersion(String tapSchemaName) throws SQLException;
List<String> getExposedSchemas(String tapSchemaName) throws SQLException;
List<String> getAllTablesNames(String schemaName) throws SQLException;
......
......@@ -101,6 +101,15 @@ public class ConfigurationManager {
return Collections.unmodifiableList(usersConfig.getUsers());
}
public UserConfiguration getUserConfiguration(String username) {
for (UserConfiguration user : usersConfig.getUsers()) {
if (username.equals(user.getUsername())) {
return user;
}
}
return null;
}
public List<UCDConfiguration> getUCDConfiguration() {
return Collections.unmodifiableList(ucdListConfig.getUCDList());
}
......@@ -197,9 +206,17 @@ public class ConfigurationManager {
}
public List<TapCredentials> getCredentials(String username) {
List<TapCredentials> credentials = getUserCredentials(username);
if (credentials == null) {
return null;
}
return Collections.unmodifiableList(credentials);
}
private List<TapCredentials> getUserCredentials(String username) {
for (UserConfiguration user : usersConfig.getUsers()) {
if (user.getUsername().equals(username)) {
return Collections.unmodifiableList(user.getCredentialsInfo());
return user.getCredentialsInfo();
}
}
return null;
......@@ -213,18 +230,11 @@ public class ConfigurationManager {
* otherwise.
*/
public synchronized boolean addCredentials(String username, TapCredentials credentials) {
for (UserConfiguration user : usersConfig.getUsers()) {
if (user.getUsername().equals(username)) {
for (TapCredentials tapCredentials : user.getCredentialsInfo()) {
if (tapCredentials.equals(credentials)) {
return false;
}
}
user.getCredentialsInfo().add(credentials);
updateUsersConfigurationFile();
return false;
}
List<TapCredentials> userCredentials = getUserCredentials(username);
if (userCredentials != null) {
userCredentials.add(credentials);
updateUsersConfigurationFile();
return true;
}
return false;
}
......@@ -234,11 +244,21 @@ public class ConfigurationManager {
* otherwise.
*/
public synchronized boolean deleteCredentials(String username, int credentialsIndex) {
for (UserConfiguration user : usersConfig.getUsers()) {
if (user.getUsername().equals(username)) {
user.getCredentialsInfo().remove(credentialsIndex);
return true;
}
List<TapCredentials> userCredentials = getUserCredentials(username);
if (userCredentials != null && userCredentials.size() > credentialsIndex) {
userCredentials.remove(credentialsIndex);
updateUsersConfigurationFile();
return true;
}
return false;
}
public synchronized boolean updateCredentials(String username, TapCredentials updatedCredentials, int index) {
List<TapCredentials> userCredentials = getUserCredentials(username);
if (userCredentials != null && userCredentials.size() > index) {
userCredentials.set(index, updatedCredentials);
updateUsersConfigurationFile();
return true;
}
return false;
}
......
......@@ -71,6 +71,6 @@ public class ConsistencyChecksBean implements Serializable {
}
public String back() {
return "schemaSelection.xhtml?faces-redirect=true";
return "credentialsEditing.xhtml?faces-redirect=true";
}
}
......@@ -24,9 +24,7 @@ package it.inaf.ia2.tsm.webapp;
import it.inaf.ia2.tsm.webapp.xmlconfig.SeparatedCredentials;
import it.inaf.ia2.tsm.datalayer.Credentials;
import it.inaf.ia2.tsm.datalayer.DBWrapper;
import it.inaf.ia2.tsm.model.TapSchemaModels;
import it.inaf.ia2.tsm.webapp.env.UIModal;
import it.inaf.ia2.tsm.webapp.xmlconfig.JoinedCredentials;
import it.inaf.ia2.tsm.webapp.xmlconfig.TapCredentials;
import java.io.IOException;
......@@ -60,12 +58,11 @@ public class CredentialsEditing implements Serializable {
private ConfigurationManager config;
@Inject
private SchemaSelectionBean schemaSelectionBean;
private TapSchemaLoader tapSchemaLoader;
private Integer credentialsInEditing;
private Integer credentialsToRemove;
private UIModal.StatusObserver credentialsDialogStatusObserver;
private boolean credentialsDialogOpened;
private List<String> tapSchemaVersions;
......@@ -79,12 +76,6 @@ public class CredentialsEditing implements Serializable {
@PostConstruct
public void init() {
this.credentialsDialogStatusObserver = new UIModal.StatusObserver() {
@Override
public void statusChanged(boolean isOpen) {
credentialsDialogOpened = isOpen;
}
};
this.tapSchemaVersions = TapSchemaModels.getAvailableVersions();
}
......@@ -117,7 +108,8 @@ public class CredentialsEditing implements Serializable {
hasObscore = credentials.isHasObscore();
}
public void addNewCredentials() {
public void addNewCredentialsInEditing() {
credentialsInEditing = null;
separateCredentials = false;
sourceCredentials = new Credentials();
tapSchemaCredentials = new Credentials();
......@@ -126,63 +118,73 @@ public class CredentialsEditing implements Serializable {
hasObscore = false;
}
// public void editCredentials(Credentials credentials, int index) {
// this.sourceCredentials = credentials;
// separateCredentials = false;
// currentEditingRow = index;
// }
//
// public void editSeparateCredentials(SeparatedCredentials sc, int index) {
// this.sourceCredentials = sc.getSourceCredentials();
// this.tapSchemaCredentials = sc.getTapSchemaCredentials();
// currentEditingRow = index;
// separateCredentials = true;
// }
public String loginWithJoinedCredentials(JoinedCredentials credentials) {
return loginWithDBWrapper(new DBWrapper(credentials.getCredentials()));
public void loadTapSchema(TapCredentials tapCredentials) throws SQLException {
tapSchemaLoader.tryLoadingTapSchema(tapCredentials);
}
public String loginWithSeparatedCredentials(Credentials sourceCredentials, Credentials tapSchemaCredentials) {
return loginWithDBWrapper(new DBWrapper(sourceCredentials, tapSchemaCredentials));
public void openDeleteCredentialsConfirmation(int index) throws IOException {
credentialsToRemove = index;
}
private String loginWithDBWrapper(DBWrapper dbWrapper) {
try {
dbWrapper.testConnections();
schemaSelectionBean.setDbWrapper(dbWrapper);
return "schemaSelection.xhtml?faces-redirect=true";
} catch (SQLException e) {
LOG.error("Exception caught", e);
FacesContext.getCurrentInstance().addMessage("main", new FacesMessage("Connection error: " + e.getMessage()));
return null;
public void confirmCredentialsDeletion() {
if (credentialsToRemove != null) {
config.deleteCredentials(user.getUsername(), credentialsToRemove);
}
credentialsToRemove = null;
}
public void removeCredentials(int index) throws IOException {
// getSavedCredentials().remove(index);
// config.updateUsersConfigurationFile();
public boolean validateNotNull(FacesContext ctx, String value, String componentId, String errorMessage) {
if (value == null || value.isEmpty()) {
ctx.addMessage(componentId, new FacesMessage(errorMessage));
return false;
}
return true;
}
public void saveCredentialsEdited() throws IOException {
//
// // If is editing existing, remove old for creating a new one
// if (currentEditingRow < getSavedCredentials().size()) {
// getSavedCredentials().remove(currentEditingRow);
// }
//
// if (separateCredentials) {
// SeparatedCredentials sc = new SeparatedCredentials(sourceCredentials, tapSchemaCredentials);
// getSavedCredentials().add(currentEditingRow, sc);
// } else {
// getSavedCredentials().add(currentEditingRow, sourceCredentials);
// }
//
// config.updateUsersConfigurationFile();
}
public UIModal.StatusObserver getCredentialsDialogStatus() {
return credentialsDialogStatusObserver;
/**
* We need to validate manually to avoid problem with JSF AJAX partial
* updates.
*/
FacesContext ctx = FacesContext.getCurrentInstance();
/**
* Single & operator used to perform all validation.
*/
boolean validationOk
= validateNotNull(ctx, sourceCredentials.getHostname(), "main:source_hostname", "Hostname is required")
& validateNotNull(ctx, sourceCredentials.getUsername(), "main:source_username", "Username is required")
& validateNotNull(ctx, tapSchemaName, "main:tap_schema_name", "TAP_SCHEMA name is required");
if (separateCredentials) {
validationOk = validationOk
& validateNotNull(ctx, tapSchemaCredentials.getHostname(), "main:tap_schema_hostname", "Hostname is required")
& validateNotNull(ctx, tapSchemaCredentials.getHostname(), "main:tap_schema_username", "Username is required");
}
if (!validationOk) {
return;
}
TapCredentials editedCredentials;
if (separateCredentials) {
editedCredentials = new SeparatedCredentials(sourceCredentials, tapSchemaCredentials);
} else {
editedCredentials = new JoinedCredentials(sourceCredentials);
}
editedCredentials.setHasObscore(hasObscore);
editedCredentials.setTapSchemaName(tapSchemaName);
editedCredentials.setTapSchemaVersion(tapSchemaVersion);
if (credentialsInEditing == null) {
// New credentials
config.addCredentials(user.getUsername(), editedCredentials);
} else {
// Existing credentials
config.updateCredentials(user.getUsername(), editedCredentials, credentialsInEditing);
}
}
public List<String> getTapSchemaVersions() {
......@@ -201,33 +203,6 @@ public class CredentialsEditing implements Serializable {
this.separateCredentials = separateCredentials;
}
// public void separateCredentialsChanged() {
// if (separateCredentials) {
// Credentials credentials = getJoinedCredentialsInEditing().getCredentials();
// SeparatedCredentials separatedCredentials = new SeparatedCredentials();
// separatedCredentials.setSourceCredentials(credentials);
// credentialsInEditing = separatedCredentials;
// } else {
// Credentials credentials = getSeparatedCredentialsInEditing().getSourceCredentials();
// JoinedCredentials joinedCredentials = new JoinedCredentials();
// joinedCredentials.setCredentials(credentials);
// credentialsInEditing = joinedCredentials;
// }
// }
//
// public JoinedCredentials getJoinedCredentialsInEditing() {
// if (credentialsInEditing instanceof JoinedCredentials) {
// return (JoinedCredentials) credentialsInEditing;
// }
// return null;
// }
//
// public SeparatedCredentials getSeparatedCredentialsInEditing() {
// if (credentialsInEditing instanceof SeparatedCredentials) {
// return (SeparatedCredentials) credentialsInEditing;
// }
// return null;
// }
public boolean isCredentialsDialogOpened() {
return credentialsDialogOpened;
}
......
......@@ -25,7 +25,6 @@ package it.inaf.ia2.tsm.webapp;
import it.inaf.ia2.tsm.ChildEntity;
import it.inaf.ia2.tsm.Column;
import it.inaf.ia2.tsm.webapp.env.CustomPartialResponseWriter;
import it.inaf.ia2.tsm.webapp.env.JSUpdateHandler;
import it.inaf.ia2.tsm.EntitiesContainer;
import it.inaf.ia2.tsm.Key;
import it.inaf.ia2.tsm.KeyColumn;
......@@ -58,7 +57,7 @@ public class TapSchemaEditingBean implements Serializable {
private static final Logger LOG = LoggerFactory.getLogger(TapSchemaEditingBean.class);
@Inject
SchemaSelectionBean schemaSelection;
private TapSchemaLoader tapSchemaLoader;
private TapSchema tapSchema;
private Schema selectedSchema;
......@@ -247,7 +246,7 @@ public class TapSchemaEditingBean implements Serializable {
}
public String back() {
return "schemaSelection.xhtml?faces-redirect=true";
return "credentialsEditing.xhtml?faces-redirect=true";
}
public void undoRemove(ChildEntity entity) throws SQLException {
......@@ -310,13 +309,7 @@ public class TapSchemaEditingBean implements Serializable {
}
// New UCD is set and we can notify the client to close the UCD Search modal dialog.
CustomPartialResponseWriter.getCurrentInstance().addCustomJSUpdate(new JSUpdateHandler() {
@Override
public String getUpdate() {
return "true";
}
});
CustomPartialResponseWriter.getCurrentInstance().addCustomJSUpdate(String.valueOf(true));
}
}
......@@ -338,13 +331,7 @@ public class TapSchemaEditingBean implements Serializable {
if (key.equals("unit")) {
voUnitValidator = new VOUnitValidator(entity.getValue(key, String.class));
}
CustomPartialResponseWriter.getCurrentInstance().addCustomJSUpdate(new JSUpdateHandler() {
@Override
public String getUpdate() {
return isChanged + "";
}
});
CustomPartialResponseWriter.getCurrentInstance().addCustomJSUpdate(String.valueOf(isChanged));
}
public void removeColumn(String name) {
......@@ -371,17 +358,10 @@ public class TapSchemaEditingBean implements Serializable {
public void reload() {
if (schemaSelection.getSelectedRadioOption().equals("edit")) {
schemaSelection.edit();
if (tapSchema.exists()) {
tapSchemaLoader.edit();
} else {
if (tapSchema.exists()) {
schemaSelection.setSelectedRadioOption("edit");
schemaSelection.setSelectedTAPSchema(tapSchema.getName());
schemaSelection.selectedTAPSchemaChanged();
schemaSelection.edit();
} else {
schemaSelection.create();
}
tapSchemaLoader.create();
}
}
......
/*
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2016 Istituto Nazionale di Astrofisica
* 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
......@@ -26,19 +26,12 @@ 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.model.TapSchemaModel;
import it.inaf.ia2.tsm.model.TapSchemaModels;
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 java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.deltaspike.core.api.scope.WindowScoped;
......@@ -49,162 +42,70 @@ import org.slf4j.LoggerFactory;
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
@Named("schemaSelection")
@Named("tapSchemaLoader")
@WindowScoped
public class SchemaSelectionBean implements Serializable {
private static final long serialVersionUID = -5745720427701334323L;
private static final Logger LOG = LoggerFactory.getLogger(SchemaSelectionBean.class);
public class TapSchemaLoader implements Serializable {
private static final long serialVersionUID = 3203810003976020854L;
private static final Logger LOG = LoggerFactory.getLogger(TapSchemaLoader.class);
@Inject
TapSchemaEditingBean tapSchemaEditingBean;
private ConsistencyChecksBean consistencyChecksBean;
@Inject
ConsistencyChecksBean consistencyChecksBean;
private DBWrapper dbWrapper;
private String selectedRadioOption;
TapSchemaEditingBean tapSchemaEditingBean;
// For editing
private List<String> allTAPSchemas;
private String selectedTAPSchema;
private String exposedSchemas;
private TapCredentials tapCredentials;
private DBWrapper dbWrapper;
// For creation
private String tapSchemaName;
private List<String> versions;
private String version;
private List<String> allSchemas;
private List<String> selectedSchemas;
private boolean loading;
private TapSchema loadedTapSchema;
private String loadingError;
@PostConstruct
public void init() {
selectedRadioOption = "edit";
exposedSchemas = "";
versions = new ArrayList<>();
Iterator<TapSchemaModel> ite = TapSchemaModels.getIterator();
while (ite.hasNext()) {
TapSchemaModel model = ite.next();
versions.add(model.getVersion());
}
Collections.sort(versions);
}
public void onPageLoad() {
FacesContext fc = FacesContext.getCurrentInstance();
final boolean ajaxRequest = fc.getPartialViewContext().isAjaxRequest();
final boolean validationFailed = fc.isValidationFailed();
if (!ajaxRequest && !validationFailed) {
// Loading all schemas of the source database
try {
DBBroker broker = DBBrokerFactory.getDBBroker(dbWrapper.getSourceDataSourceWrapper());
allSchemas = broker.getAllSchemaNames();
setSelectedSchemas(new ArrayList<String>());
} catch (SQLException e) {
throw new RuntimeException(e);
}
public void tryLoadingTapSchema(TapCredentials tapCredentials) throws SQLException {
this.tapCredentials = tapCredentials;
// Loading all schemas of the TAP_SCHEMA database
try {
DBBroker broker = DBBrokerFactory.getDBBroker(dbWrapper.getTapSchemaDataSourceWrapper());
allTAPSchemas = broker.getAllTAPSchemaNames(allSchemas);
if (!allTAPSchemas.isEmpty()) {
this.selectedTAPSchema = allTAPSchemas.get(0);
loadExposedSchemas();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
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());
}
}
private void loadExposedSchemas() throws SQLException {
// Testing connections
dbWrapper.testConnections();
// Searching for TAP_SCHEMA name
DBBroker broker = DBBrokerFactory.getDBBroker(dbWrapper.getTapSchemaDataSourceWrapper());
List<String> schemas = broker.getExposedSchemas(selectedTAPSchema);
exposedSchemas = "";
for (int i = 0; i < schemas.size(); i++) {
exposedSchemas += schemas.get(i);
if (i < schemas.size() - 1) {
exposedSchemas += ", ";
boolean tapSchemaExists = false;
for (String schemaName : broker.getAllSchemaNames()) {
if (schemaName.equals(tapCredentials.getTapSchemaName())) {
tapSchemaExists = true;
break;
}
}
}
public List<String> getAllTAPSchemas() {
return allTAPSchemas;
}
public List<String> getAllSchemas() {
return allSchemas;
}
public String getExposedSchemas() {
return exposedSchemas;
}
public String getSelectedRadioOption() {
return selectedRadioOption;
}
public void setSelectedRadioOption(String selectedRadioOption) {
this.selectedRadioOption = selectedRadioOption;
}