Search in sources :

Example 1 with StorageSite

use of nl.uva.cs.lobcder.resources.StorageSite in project lobcder by skoulouzis.

the class JDBCatalogue method insertOrUpdateStorageSites.

public void insertOrUpdateStorageSites(Collection<StorageSite> sites, Connection connection, Boolean includePrivate) throws SQLException {
    Collection<Credential> credentials = getCredentials(connection);
    Collection<StorageSite> existingSites = getStorageSites(connection, Boolean.FALSE, includePrivate);
    existingSites.addAll(getStorageSites(connection, Boolean.TRUE, includePrivate));
    Collection<String> updatedSites = new ArrayList<>();
    for (StorageSite s : sites) {
        long credentialID = -1;
        for (Credential c : credentials) {
            if (c.getStorageSiteUsername().equals(s.getCredential().getStorageSiteUsername()) && c.getStorageSitePassword().equals(s.getCredential().getStorageSitePassword())) {
                credentialID = c.getId();
                break;
            }
        }
        if (credentialID == -1) {
            try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO credential_table (username, " + "password) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS)) {
                preparedStatement.setString(1, s.getCredential().getStorageSiteUsername());
                preparedStatement.setString(2, s.getCredential().getStorageSitePassword());
                preparedStatement.executeUpdate();
                ResultSet rs = preparedStatement.getGeneratedKeys();
                rs.next();
                credentialID = rs.getLong(1);
                s.getCredential().setId(credentialID);
                credentials.add(s.getCredential());
            }
        }
        for (StorageSite es : existingSites) {
            if (es.getResourceURI().equals(s.getResourceURI())) {
                // Long id = es.getStorageSiteId();
                try (PreparedStatement preparedStatement = connection.prepareStatement("UPDATE storage_site_table SET " + "resourceUri = ?, " + "credentialRef = ?, " + "currentNum = ?, " + "currentSize = ?, " + "quotaNum = ?, " + "quotaSize = ?, " + "isCache = ?, " + "encrypt = ? " + "WHERE storageSiteId = ?")) {
                    preparedStatement.setString(1, s.getResourceURI());
                    preparedStatement.setLong(2, credentialID);
                    preparedStatement.setLong(3, s.getCurrentNum());
                    preparedStatement.setLong(4, s.getCurrentSize());
                    preparedStatement.setLong(5, s.getQuotaNum());
                    preparedStatement.setLong(6, s.getQuotaSize());
                    preparedStatement.setBoolean(7, s.isCache());
                    preparedStatement.setBoolean(8, s.isEncrypt());
                    preparedStatement.setLong(9, es.getStorageSiteId());
                    preparedStatement.executeUpdate();
                }
                updatedSites.add(es.getResourceURI());
            }
        }
        if (!updatedSites.contains(s.getResourceURI())) {
            try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO storage_site_table " + "(resourceUri, credentialRef, currentNum, " + "currentSize, quotaNum, quotaSize, isCache, extra, " + "encrypt) " + "VALUES (?, ?, ?, ?, ?, ?, ?, NULL, ?)", Statement.RETURN_GENERATED_KEYS)) {
                preparedStatement.setString(1, s.getResourceURI());
                preparedStatement.setLong(2, credentialID);
                preparedStatement.setLong(3, s.getCurrentNum());
                preparedStatement.setLong(4, s.getCurrentSize());
                preparedStatement.setLong(5, s.getQuotaNum());
                preparedStatement.setLong(6, s.getQuotaSize());
                preparedStatement.setBoolean(7, s.isCache());
                preparedStatement.setBoolean(8, s.isEncrypt());
                preparedStatement.executeUpdate();
                ResultSet rs = preparedStatement.getGeneratedKeys();
                rs.next();
                s.setStorageSiteId(rs.getLong(1));
                existingSites.add(s);
            }
        }
    }
}
Also used : Credential(nl.uva.cs.lobcder.resources.Credential) StorageSite(nl.uva.cs.lobcder.resources.StorageSite)

Example 2 with StorageSite

use of nl.uva.cs.lobcder.resources.StorageSite in project lobcder by skoulouzis.

the class Assimilator method main.

public static void main(String[] args) throws SQLException, MalformedURLException, VlException, NoSuchAlgorithmException, Exception {
    try {
        importingOwner = "admin";
        List<StorageSite> sites = new ArrayList<>();
        Credential credential = new Credential();
        credential.setStorageSiteUsername("fake");
        credential.setStorageSitePassword("fake");
        // StorageSite ss1 = new StorageSite();
        // ss1.setCredential(credential);
        // ss1.setResourceURI("swift://somewhere:8443/auth/v1.0/uploadContainer/");
        // ss1.setCurrentNum(Long.valueOf("-1"));
        // ss1.setCurrentSize(Long.valueOf("-1"));
        // ss1.setEncrypt(false);
        // ss1.setQuotaNum(Long.valueOf("-1"));
        // ss1.setQuotaSize(Long.valueOf("-1"));
        // sites.add(ss1);
        // 
        // 
        // StorageSite ss2 = new StorageSite();
        // ss2.setCredential(credential);
        // ss2.setResourceURI("file:///" + System.getProperty("user.home") + "/Downloads");
        // ss2.setCurrentNum(Long.valueOf("-1"));
        // ss2.setCurrentSize(Long.valueOf("-1"));
        // ss2.setEncrypt(false);
        // ss2.setQuotaNum(Long.valueOf("-1"));
        // ss2.setQuotaSize(Long.valueOf("-1"));
        // sites.add(ss2);
        // StorageSite ss3 = new StorageSite();
        // ss3.setCredential(credential);
        // ss3.setResourceURI("file:///" + System.getProperty("user.home") + "/Downloads/lobcderUsageData");
        // //            ss3.setResourceURI("srm://tbn18.nikhef.nl:8446/dpm/nikhef.nl/home/biomed/lobcder");
        // ss3.setCurrentNum(Long.valueOf("-1"));
        // ss3.setCurrentSize(Long.valueOf("-1"));
        // ss3.setEncrypt(false);
        // ss3.setQuotaNum(Long.valueOf("-1"));
        // ss3.setQuotaSize(Long.valueOf("-1"));
        // sites.add(ss3);
        StorageSite ss4 = new StorageSite();
        ss4.setCredential(credential);
        ss4.setResourceURI("http://data.sdss3.org/sas/dr12/apogee/spectro/redux/r5/apo1m/calibration/56357/plots/");
        ss4.setCurrentNum(Long.valueOf("-1"));
        ss4.setCurrentSize(Long.valueOf("-1"));
        ss4.setEncrypt(false);
        ss4.setQuotaNum(Long.valueOf("-1"));
        ss4.setQuotaSize(Long.valueOf("-1"));
        ss4.setPrivateSite(false);
        ss4.setReadOnly(true);
        sites.add(ss4);
        Assimilator a = new Assimilator();
        a.assimilate(sites);
    } catch (Exception ex) {
        ex.printStackTrace();
        Logger.getLogger(Assimilator.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        VRS.exit();
        System.exit(0);
    }
}
Also used : Credential(nl.uva.cs.lobcder.resources.Credential) StorageSite(nl.uva.cs.lobcder.resources.StorageSite) ArrayList(java.util.ArrayList) MalformedURLException(java.net.MalformedURLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) VlException(nl.uva.vlet.exception.VlException)

Example 3 with StorageSite

use of nl.uva.cs.lobcder.resources.StorageSite in project lobcder by skoulouzis.

the class StorageSitesService method set.

@Path("set/")
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public void set(JAXBElement<StorageSiteWrapperList> jbSites) throws SQLException {
    MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
    if (mp.isAdmin()) {
        try (Connection connection = getCatalogue().getConnection()) {
            StorageSiteWrapperList sitesWL = jbSites.getValue();
            List<StorageSiteWrapper> sswl = sitesWL.getSites();
            if (sswl != null && sswl.size() > 0) {
                Collection<StorageSite> sites = new ArrayList<>();
                for (StorageSiteWrapper ssw : sswl) {
                    StorageSite site = new StorageSite();
                    Credential cred = new Credential();
                    cred.setStorageSitePassword(ssw.getCredential().getStorageSitePassword());
                    cred.setStorageSiteUsername(ssw.getCredential().getStorageSiteUsername());
                    site.setCredential(cred);
                    site.setCurrentNum(ssw.getCurrentNum());
                    site.setCurrentSize(ssw.getCurrentSize());
                    site.setResourceURI(ssw.getResourceURI());
                    site.setEncrypt(ssw.isEncrypt());
                    site.setCache(ssw.isCache());
                    site.setQuotaNum(ssw.getQuotaNum());
                    site.setQuotaSize(ssw.getQuotaSize());
                    sites.add(site);
                }
                getCatalogue().insertOrUpdateStorageSites(sites, connection);
                connection.commit();
            }
        }
    }
}
Also used : StorageSiteWrapper(nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapper) StorageSiteWrapperList(nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapperList) MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) Credential(nl.uva.cs.lobcder.resources.Credential) StorageSite(nl.uva.cs.lobcder.resources.StorageSite) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 4 with StorageSite

use of nl.uva.cs.lobcder.resources.StorageSite in project lobcder by skoulouzis.

the class StorageSitesService method queryStorageSites.

private List<StorageSiteWrapper> queryStorageSites(@Nonnull Connection cn) throws SQLException {
    MultivaluedMap<String, String> queryParameters = info.getQueryParameters();
    List<String> ids = queryParameters.get("id");
    if (ids != null && ids.size() > 0 && ids.get(0).equals("all")) {
        Collection<StorageSite> sites = getCatalogue().getStorageSites(cn, Boolean.FALSE);
        Collection<StorageSite> cachesites = getCatalogue().getStorageSites(cn, Boolean.TRUE);
        List<StorageSiteWrapper> sitesWarpper = new ArrayList<>();
        for (StorageSite s : sites) {
            StorageSiteWrapper sw = new StorageSiteWrapper();
            CredentialWrapped cw = new CredentialWrapped();
            cw.setStorageSitePassword(s.getCredential().getStorageSitePassword());
            // cw.setStorageSitePassword("************");
            cw.setStorageSiteUsername(s.getCredential().getStorageSiteUsername());
            sw.setCredential(cw);
            sw.setCurrentNum(s.getCurrentNum());
            sw.setEncrypt(s.isEncrypt());
            sw.setQuotaNum(s.getQuotaNum());
            sw.setQuotaSize(s.getQuotaSize());
            sw.setResourceURI(s.getResourceURI());
            sw.setStorageSiteId(s.getStorageSiteId());
            sw.setCache(false);
            sw.setCurrentSize(s.getCurrentSize());
            sitesWarpper.add(sw);
        }
        for (StorageSite s : cachesites) {
            StorageSiteWrapper sw = new StorageSiteWrapper();
            CredentialWrapped cw = new CredentialWrapped();
            cw.setStorageSitePassword(s.getCredential().getStorageSitePassword());
            // cw.setStorageSitePassword("************");
            cw.setStorageSiteUsername(s.getCredential().getStorageSiteUsername());
            sw.setCredential(cw);
            sw.setCurrentNum(s.getCurrentNum());
            sw.setEncrypt(s.isEncrypt());
            sw.setQuotaNum(s.getQuotaNum());
            sw.setQuotaSize(s.getQuotaSize());
            sw.setResourceURI(s.getResourceURI());
            sw.setStorageSiteId(s.getStorageSiteId());
            sw.setCache(true);
            sw.setCurrentSize(s.getCurrentSize());
            sitesWarpper.add(sw);
        }
        return sitesWarpper;
    }
    return null;
}
Also used : StorageSiteWrapper(nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapper) StorageSite(nl.uva.cs.lobcder.resources.StorageSite) ArrayList(java.util.ArrayList) CredentialWrapped(nl.uva.cs.lobcder.rest.wrappers.CredentialWrapped)

Example 5 with StorageSite

use of nl.uva.cs.lobcder.resources.StorageSite in project lobcder by skoulouzis.

the class JDBCatalogue method insertOrUpdateStorageSites.

public void insertOrUpdateStorageSites(Collection<StorageSite> sites, Connection connection) throws SQLException {
    Collection<Credential> credentials = getCredentials(connection);
    Collection<StorageSite> existingSites = getStorageSites(connection, Boolean.FALSE);
    existingSites.addAll(getStorageSites(connection, Boolean.TRUE));
    Collection<String> updatedSites = new ArrayList<>();
    for (StorageSite s : sites) {
        long credentialID = -1;
        for (Credential c : credentials) {
            if (c.getStorageSiteUsername().equals(s.getCredential().getStorageSiteUsername()) && c.getStorageSitePassword().equals(s.getCredential().getStorageSitePassword())) {
                credentialID = c.getId();
                break;
            }
        }
        if (credentialID == -1) {
            try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO credential_table (username, " + "password) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS)) {
                preparedStatement.setString(1, s.getCredential().getStorageSiteUsername());
                preparedStatement.setString(2, s.getCredential().getStorageSitePassword());
                preparedStatement.executeUpdate();
                ResultSet rs = preparedStatement.getGeneratedKeys();
                rs.next();
                credentialID = rs.getLong(1);
                s.getCredential().setId(credentialID);
                credentials.add(s.getCredential());
            }
        }
        for (StorageSite es : existingSites) {
            if (es.getResourceURI().equals(s.getResourceURI())) {
                // Long id = es.getStorageSiteId();
                try (PreparedStatement preparedStatement = connection.prepareStatement("UPDATE storage_site_table SET " + "`resourceUri` = ?, " + "`credentialRef` = ?, " + "`currentNum` = ?, " + "`currentSize` = ?, " + "`quotaNum` = ?, " + "`quotaSize` = ?, " + "`isCache` = ?, " + "`encrypt` = ? " + "WHERE storageSiteId = ?;")) {
                    preparedStatement.setString(1, s.getResourceURI());
                    preparedStatement.setLong(2, credentialID);
                    preparedStatement.setLong(3, s.getCurrentNum());
                    preparedStatement.setLong(4, s.getCurrentSize());
                    preparedStatement.setLong(5, s.getQuotaNum());
                    preparedStatement.setLong(6, s.getQuotaSize());
                    preparedStatement.setBoolean(7, s.isCache());
                    preparedStatement.setBoolean(8, s.isEncrypt());
                    preparedStatement.setLong(9, es.getStorageSiteId());
                    preparedStatement.executeUpdate();
                }
                updatedSites.add(es.getResourceURI());
            }
        }
        if (!updatedSites.contains(s.getResourceURI())) {
            try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO storage_site_table " + "(resourceUri, credentialRef, currentNum, " + "currentSize, quotaNum, quotaSize, isCache, extra, " + "encrypt) " + "VALUES (?, ?, ?, ?, ?, ?, ?, NULL, ?)", Statement.RETURN_GENERATED_KEYS)) {
                preparedStatement.setString(1, s.getResourceURI());
                preparedStatement.setLong(2, credentialID);
                preparedStatement.setLong(3, s.getCurrentNum());
                preparedStatement.setLong(4, s.getCurrentSize());
                preparedStatement.setLong(5, s.getQuotaNum());
                preparedStatement.setLong(6, s.getQuotaSize());
                preparedStatement.setBoolean(7, s.isCache());
                preparedStatement.setBoolean(8, s.isEncrypt());
                preparedStatement.executeUpdate();
                ResultSet rs = preparedStatement.getGeneratedKeys();
                rs.next();
                s.setStorageSiteId(rs.getLong(1));
                existingSites.add(s);
            }
        }
    }
}
Also used : Credential(nl.uva.cs.lobcder.resources.Credential) StorageSite(nl.uva.cs.lobcder.resources.StorageSite)

Aggregations

StorageSite (nl.uva.cs.lobcder.resources.StorageSite)12 ArrayList (java.util.ArrayList)6 Credential (nl.uva.cs.lobcder.resources.Credential)6 HashMap (java.util.HashMap)3 StorageSiteWrapper (nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapper)3 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 CredentialWrapped (nl.uva.cs.lobcder.rest.wrappers.CredentialWrapped)2 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Connection (java.sql.Connection)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 Consumes (javax.ws.rs.Consumes)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 MyPrincipal (nl.uva.cs.lobcder.auth.MyPrincipal)1 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)1 StorageSiteWrapperList (nl.uva.cs.lobcder.rest.wrappers.StorageSiteWrapperList)1