Commit f233c97c authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Started changes for new configuration settings

parent f6295e1a
Loading
Loading
Loading
Loading
+44 −2
Original line number Diff line number Diff line
@@ -22,8 +22,8 @@
 */
package it.inaf.ia2.tsm.datalayer;

import it.inaf.ia2.tsm.datalayer.DatabaseType;
import java.io.Serializable;
import java.util.Objects;
import javax.xml.bind.annotation.XmlAttribute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,7 +37,7 @@ import org.slf4j.LoggerFactory;
public class Credentials implements Serializable {

    private static final long serialVersionUID = 1153912575502196261L;
    private static final Logger log = LoggerFactory.getLogger(Credentials.class);
    private static final Logger LOG = LoggerFactory.getLogger(Credentials.class);

    private String hostname;
    private int port;
@@ -160,4 +160,46 @@ public class Credentials implements Serializable {
                Credentials.class.getCanonicalName(),
                databaseType, hostname, port, username, password, database);
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + Objects.hashCode(this.hostname);
        hash = 97 * hash + this.port;
        hash = 97 * hash + Objects.hashCode(this.username);
        hash = 97 * hash + Objects.hashCode(this.password);
        hash = 97 * hash + Objects.hashCode(this.database);
        hash = 97 * hash + Objects.hashCode(this.databaseType);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Credentials other = (Credentials) obj;
        if (this.port != other.port) {
            return false;
        }
        if (!Objects.equals(this.hostname, other.hostname)) {
            return false;
        }
        if (!Objects.equals(this.username, other.username)) {
            return false;
        }
        if (!Objects.equals(this.password, other.password)) {
            return false;
        }
        if (!Objects.equals(this.database, other.database)) {
            return false;
        }
        return this.databaseType == other.databaseType;
    }
}
+0 −2
Original line number Diff line number Diff line
@@ -5,5 +5,3 @@
# (It is necessary to put this list here because, unfortunately, there is no
# easy way to read an entire resource folder using the ClassLoader).
models = 1,1-IA2,1_1

allow_fictitious_keys = false
+0 −114
Original line number Diff line number Diff line
/* 
 * _____________________________________________________________________________
 * 
 * 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.ia2.tsm.webapp;

import it.inaf.ia2.tsm.webapp.xmlconfig.Configuration;
import it.inaf.ia2.tsm.webapp.xmlconfig.UserConfiguration;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.xml.bind.JAXB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
@Named("config")
@ApplicationScoped
public class ConfigurationData {

    private static final Logger LOG = LoggerFactory.getLogger(ConfigurationData.class);

    private File configFile;
    private Configuration configuration;

    @PostConstruct
    public void init() {
        try {
            Properties prop = new Properties();
            try (InputStream in = getClass().getClassLoader().getResourceAsStream("webapp.properties")) {
                prop.load(in);
            }
            configFile = new File(prop.getProperty("config_file_path"));

            if (!configFile.exists()) {

                LOG.debug("Configuration file doesn't exist: creating a new one at " + configFile.getAbsolutePath());

                configFile.getParentFile().mkdirs();
                configFile.createNewFile();
                configuration = new Configuration();
                updateConfigurationFile();

            } else {

                // Configuration file exists, simply unmarshal it
                configuration = JAXB.unmarshal(configFile, Configuration.class);
            }
        } catch (IOException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public Configuration getData() {
        return configuration;
    }

    public synchronized List<UserConfiguration> cloneUsersConfiguration() {
        // JAXB is exploited for doing deep copy.
        return JAXB.unmarshal(configFile, Configuration.class).getUsers();
    }

    public void updateConfigurationFile() throws IOException {
        JAXB.marshal(configuration, configFile);
    }

    public synchronized void addUser(UserConfiguration user) throws IOException {
        configuration.getUsers().add(user);
        updateConfigurationFile();
    }

    public synchronized void updateUsersList(List<UserConfiguration> users) throws IOException {
        configuration.getUsers().clear();
        for (UserConfiguration user : users) {
            configuration.getUsers().add(user);
        }
        updateConfigurationFile();
    }

    public String getRestPath() {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/faces/rest";
    }

    public String getVersion() {
        return Version.NUMBER;
    }
}
+246 −0
Original line number Diff line number Diff line
/* 
 * _____________________________________________________________________________
 * 
 * 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.ia2.tsm.webapp;

import it.inaf.ia2.tsm.webapp.xmlconfig.TapCredentials;
import it.inaf.ia2.tsm.webapp.xmlconfig.UCDConfiguration;
import it.inaf.ia2.tsm.webapp.xmlconfig.UCDListConfiguration;
import it.inaf.ia2.tsm.webapp.xmlconfig.UsersConfiguration;
import it.inaf.ia2.tsm.webapp.xmlconfig.UserConfiguration;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.xml.bind.JAXB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
 */
@Named("config")
@ApplicationScoped
public class ConfigurationManager {

    private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class);

    private static final String USERS_CONFIG_FILE_NAME = "users.xml";
    private static final String UCD_CONFIG_FILE_NAME = "ucd.xml";

    private File configDirectory;
    private File usersConfigFile;
    private File ucdConfigFile;

    private UsersConfiguration usersConfig;
    private UCDListConfiguration ucdListConfig;

    @PostConstruct
    public void init() {
        try {
            Properties prop = new Properties();
            try (InputStream in = getClass().getClassLoader().getResourceAsStream("webapp.properties")) {
                prop.load(in);
            }
            configDirectory = new File(prop.getProperty("config_directory"));
            usersConfigFile = configDirectory.toPath().resolve(USERS_CONFIG_FILE_NAME).toFile();
            ucdConfigFile = configDirectory.toPath().resolve(UCD_CONFIG_FILE_NAME).toFile();

            if (!configDirectory.exists()) {
                configDirectory.mkdirs();
            }

            if (!usersConfigFile.exists()) {
                usersConfigFile.createNewFile();
                usersConfig = new UsersConfiguration();
                updateUsersConfigurationFile();
            } else {
                usersConfig = JAXB.unmarshal(usersConfigFile, UsersConfiguration.class);
            }

            if (!ucdConfigFile.exists()) {
                ucdConfigFile.createNewFile();
                ucdListConfig = new UCDListConfiguration();
                updateUCDConfigurationFile();
            } else {
                ucdListConfig = JAXB.unmarshal(ucdConfigFile, UCDListConfiguration.class);
            }
        } catch (IOException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public List<UserConfiguration> getUsersConfiguration() {
        return Collections.unmodifiableList(usersConfig.getUsers());
    }

    public List<UCDConfiguration> getUCDConfiguration() {
        return Collections.unmodifiableList(ucdListConfig.getUCDList());
    }

    private void updateUsersConfigurationFile() {
        JAXB.marshal(usersConfig, usersConfigFile);
    }

    private void updateUCDConfigurationFile() {
        JAXB.marshal(ucdListConfig, ucdConfigFile);
    }

    /**
     * Add a new user to the list, but only if none of the existing users have
     * the same username.
     *
     * @return true if the user has been added to the list, false otherwise.
     */
    public synchronized boolean addUser(UserConfiguration user) {
        if (user.getUsername() == null) {
            return false;
        }
        
        for (UserConfiguration u : usersConfig.getUsers()) {
            if (u.getUsername().equals(user.getUsername())) {
                return false;
            }
        }

        usersConfig.getUsers().add(user);
        updateUsersConfigurationFile();
        return true;
    }

    public synchronized boolean deleteUser(String username) {
        Iterator<UserConfiguration> ite = usersConfig.getUsers().iterator();
        while (ite.hasNext()) {
            UserConfiguration userConfig = ite.next();
            if (userConfig.getUsername().equals(username)) {
                ite.remove();
                updateUsersConfigurationFile();
                return true;
            }
        }
        return false;
    }

    /**
     * Add a new UCD to the list, but only if the word has not already been
     * entered.
     *
     * @return true if the UCD has been added to the list, false otherwise.
     */
    public synchronized boolean addUCD(UCDConfiguration ucdConfig) {
        for (UCDConfiguration ucd : ucdListConfig.getUCDList()) {
            if (ucd.getWord().equals(ucdConfig.getWord())) {
                return false;
            }
        }

        ucdListConfig.getUCDList().add(ucdConfig);
        updateUCDConfigurationFile();
        return true;
    }

    /**
     * @return true if the UCD has been deleted from the list, false otherwise.
     */
    public synchronized boolean deleteUCD(UCDConfiguration ucdConfig) {
        Iterator<UCDConfiguration> ite = ucdListConfig.getUCDList().iterator();
        while (ite.hasNext()) {
            UCDConfiguration ucd = ite.next();
            if (ucd.getWord().equals(ucdConfig.getWord())) {
                ite.remove();
                updateUCDConfigurationFile();
                return true;
            }
        }
        return false;
    }

    public List<TapCredentials> getCredentials(String username) {
        for (UserConfiguration user : usersConfig.getUsers()) {
            if (user.getUsername().equals(username)) {
                return Collections.unmodifiableList(user.getCredentialsInfo());
            }
        }
        return null;
    }

    /**
     * Add a new credentials to the list, but only if the credentials has not
     * already been inserted.
     *
     * @return true if the credentials has been added to the list, false
     * 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;
            }
        }
        return false;
    }

    /**
     * @return true if the credentials has been deleted from the list, false
     * otherwise.
     */
    public synchronized boolean deleteCredentials(String username, TapCredentials credentials) {
        for (UserConfiguration user : usersConfig.getUsers()) {
            if (user.getUsername().equals(username)) {
                Iterator<TapCredentials> ite = user.getCredentialsInfo().iterator();
                while (ite.hasNext()) {
                    TapCredentials tapCredentials = ite.next();
                    if (tapCredentials.equals(credentials)) {
                        ite.remove();
                        updateUsersConfigurationFile();
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public String getRestPath() {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/faces/rest";
    }

    public String getVersion() {
        return Version.NUMBER;
    }
}
+19 −18
Original line number Diff line number Diff line
@@ -22,6 +22,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 java.io.IOException;
@@ -52,7 +53,7 @@ public class CredentialsEditing implements Serializable {
    private User user;

    @Inject
    private ConfigurationData config;
    private ConfigurationManager config;

    @Inject
    private SchemaSelectionBean schemaSelectionBean;
@@ -80,7 +81,7 @@ public class CredentialsEditing implements Serializable {
        currentEditingRow = index;
    }

    public void editSeparateCredentials(SeparateCredentials sc, int index) {
    public void editSeparateCredentials(SeparatedCredentials sc, int index) {
        this.sourceCredentials = sc.getSourceCredentials();
        this.tapSchemaCredentials = sc.getTapSchemaCredentials();
        currentEditingRow = index;
@@ -118,25 +119,25 @@ public class CredentialsEditing implements Serializable {
    }

    public void removeCredentials(int index) throws IOException {
        getSavedCredentials().remove(index);
        config.updateConfigurationFile();
//        getSavedCredentials().remove(index);
//        config.updateUsersConfigurationFile();
    }

    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) {
            SeparateCredentials sc = new SeparateCredentials(sourceCredentials, tapSchemaCredentials);
            getSavedCredentials().add(currentEditingRow, sc);
        } else {
            getSavedCredentials().add(currentEditingRow, sourceCredentials);
        }

        config.updateConfigurationFile();
//
//        // 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 boolean isSeparateCredentials() {
Loading