Search in sources :

Example 1 with LogicalData

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

the class JDBCatalogue method getLogicalDataByPath.

public LogicalData getLogicalDataByPath(Path logicalResourceName, @Nonnull Connection connection) throws SQLException, UnsupportedEncodingException {
    LogicalData res = null;
    try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT uid FROM ldata_table WHERE ldata_table.parentRef = ? AND ldata_table.ldName = ?")) {
        long parent = 1;
        String[] parts = logicalResourceName.getParts();
        if (parts.length == 0) {
            parts = new String[] { "" };
        }
        for (int i = 0; i != parts.length; ++i) {
            String p = parts[i];
            if (i == (parts.length - 1)) {
                try (PreparedStatement preparedStatement1 = connection.prepareStatement("SELECT uid, ownerId, datatype, createDate, modifiedDate, ldLength, " + "contentTypesStr, pdriGroupRef, isSupervised, checksum, lastValidationDate, " + "lockTokenId, lockScope, lockType, lockedByUser, lockDepth, lockTimeout, " + "description, locationPreference, status, accessDate, ttlSec " + // + ", resourceUri "
                "FROM ldata_table " + // + "JOIN storage_site_table ON storageSiteId = storageSiteRef "
                "WHERE ldata_table.parentRef = ? AND ldata_table.ldName = ?")) {
                    preparedStatement1.setLong(1, parent);
                    preparedStatement1.setString(2, p);
                    ResultSet rs = preparedStatement1.executeQuery();
                    if (rs.next()) {
                        res = new LogicalData();
                        res.setUid(rs.getLong(1));
                        res.setParentRef(parent);
                        res.setOwner(rs.getString(2));
                        res.setType(rs.getString(3));
                        res.setName(p);
                        res.setCreateDate(rs.getTimestamp(4).getTime());
                        res.setModifiedDate(rs.getTimestamp(5).getTime());
                        res.setLength(rs.getLong(6));
                        res.setContentTypesAsString(rs.getString(7));
                        res.setPdriGroupId(rs.getLong(8));
                        res.setSupervised(rs.getBoolean(9));
                        res.setChecksum(rs.getString(10));
                        res.setLastValidationDate(rs.getLong(11));
                        res.setLockTokenID(rs.getString(12));
                        res.setLockScope(rs.getString(13));
                        res.setLockType(rs.getString(14));
                        res.setLockedByUser(rs.getString(15));
                        res.setLockDepth(rs.getString(16));
                        res.setLockTimeout(rs.getLong(17));
                        res.setDescription(rs.getString(18));
                        // res.setDataLocationPreference(rs.getString(19));
                        res.setStatus(rs.getString(20));
                        Timestamp ts = rs.getTimestamp(21);
                        // Object ts = rs.getObject(21);
                        res.setLastAccessDate(ts != null ? ts.getTime() : null);
                        int ttl = rs.getInt(22);
                        res.setTtlSec(rs.wasNull() ? null : ttl);
                        return res;
                    } else {
                        return null;
                    }
                }
            } else {
                preparedStatement.setLong(1, parent);
                preparedStatement.setString(2, p);
                ResultSet rs = preparedStatement.executeQuery();
                if (rs.next()) {
                    parent = rs.getLong(1);
                } else {
                    return null;
                }
            }
        }
        return null;
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData)

Example 2 with LogicalData

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

the class JDBCatalogue method copyFolder.

public void copyFolder(LogicalData toCopy, LogicalData newParent, String newName, MyPrincipal principal, Connection connection) throws SQLException {
    try {
        Permissions toCopyPerm = getPermissions(toCopy.getUid(), toCopy.getOwner(), connection);
        Permissions newParentPerm = getPermissions(newParent.getUid(), newParent.getOwner(), connection);
        Permissions permissionsForNew = new Permissions(principal, newParentPerm);
        if (toCopy.isFolder() && principal.canWrite(newParentPerm)) {
            // ignore folder if there is a problem
            LogicalData newFolderEntry = toCopy.clone();
            newFolderEntry.setName(newName);
            newFolderEntry.setParentRef(newParent.getUid());
            newFolderEntry.setType(Constants.LOGICAL_FOLDER);
            newFolderEntry.setCreateDate(System.currentTimeMillis());
            newFolderEntry.setModifiedDate(System.currentTimeMillis());
            newFolderEntry.setOwner(principal.getUserId());
            newFolderEntry = registerDirLogicalData(newFolderEntry, connection);
            setPermissions(newFolderEntry.getUid(), permissionsForNew, connection);
            if (principal.canRead(toCopyPerm)) {
                try (CallableStatement cs = connection.prepareCall("{CALL copyFolderContentProc(?, ?, ?, ?, ?, ?)}")) {
                    cs.setString(1, principal.getUserId());
                    cs.setString(2, principal.getRolesStr());
                    cs.setString(3, permissionsForNew.getReadStr());
                    cs.setString(4, permissionsForNew.getWriteStr());
                    cs.setLong(5, toCopy.getUid());
                    cs.setLong(6, newFolderEntry.getUid());
                    cs.execute();
                    try (PreparedStatement ps1 = connection.prepareStatement("SELECT uid, ownerId, ldName FROM ldata_table WHERE datatype='logical.folder' AND parentRef = ?")) {
                        ps1.setLong(1, toCopy.getUid());
                        ResultSet rs = ps1.executeQuery();
                        while (rs.next()) {
                            LogicalData element = new LogicalData();
                            element.setUid(rs.getLong(1));
                            element.setOwner(rs.getString(2));
                            element.setType("logical.folder");
                            element.setName(rs.getString(3));
                            element.setParentRef(toCopy.getUid());
                            copyFolder(element, newFolderEntry, element.getName(), principal, connection);
                        }
                    }
                }
            }
        }
    } catch (CloneNotSupportedException e) {
        throw new RuntimeException(e);
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Permissions(nl.uva.cs.lobcder.auth.Permissions)

Example 3 with LogicalData

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

the class JDBCatalogue method getLogicalDataByName.

public List<LogicalData> getLogicalDataByName(Path fileName, Connection connection) throws SQLException, UnsupportedEncodingException {
    Path decodedLogicalFileName = Path.path(java.net.URLDecoder.decode(fileName.toString(), "UTF-8"));
    try (PreparedStatement ps = connection.prepareStatement("SELECT uid, parentRef, " + "ownerId, datatype, ldName, createDate, modifiedDate, ldLength, " + "contentTypesStr, pdriGroupRef, isSupervised, checksum, " + "lastValidationDate, lockTokenID, lockScope, lockType, " + "lockedByUser, lockDepth, lockTimeout, description, " + "locationPreference, status" + // + ", resourceUri "
    "FROM ldata_table " + // + "JOIN storage_site_table ON storageSiteId = storageSiteRef"
    "WHERE ldata_table.ldName like '" + decodedLogicalFileName.toString() + "'")) {
        // ps.setString(1, fileName.toString());
        ResultSet rs = ps.executeQuery();
        List<LogicalData> results = new ArrayList<>();
        while (rs.next()) {
            LogicalData res = new LogicalData();
            res.setUid(rs.getLong(1));
            res.setParentRef(rs.getLong(2));
            res.setOwner(rs.getString(3));
            res.setType(rs.getString(4));
            res.setName(rs.getString(5));
            res.setCreateDate(rs.getTimestamp(6).getTime());
            res.setModifiedDate(rs.getTimestamp(7).getTime());
            res.setLength(rs.getLong(8));
            res.setContentTypesAsString(rs.getString(9));
            res.setPdriGroupId(rs.getLong(10));
            res.setSupervised(rs.getBoolean(11));
            res.setChecksum(rs.getString(12));
            res.setLastValidationDate(rs.getLong(13));
            res.setLockTokenID(rs.getString(14));
            res.setLockScope(rs.getString(15));
            res.setLockType(rs.getString(16));
            res.setLockedByUser(rs.getString(17));
            res.setLockDepth(rs.getString(18));
            res.setLockTimeout(rs.getLong(19));
            res.setDescription(rs.getString(20));
            // res.setDataLocationPreference(rs.getString(21));
            res.setStatus(rs.getString(22));
            // Array locationPerf = rs.getArray(23);
            // res.setDataLocationPreferences(getDataLocationPreferace(connection, res.getUid()));
            results.add(res);
        }
        return results;
    }
}
Also used : Path(io.milton.common.Path) LogicalData(nl.uva.cs.lobcder.resources.LogicalData)

Example 4 with LogicalData

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

the class JDBCatalogue method getLogicalDataByUid.

public LogicalData getLogicalDataByUid(Long UID, @Nonnull Connection connection) throws SQLException {
    LogicalData res = null;
    try (PreparedStatement ps = connection.prepareStatement("SELECT parentRef, ownerId, datatype, ldName, " + "createDate, modifiedDate, ldLength, contentTypesStr, pdriGroupRef, " + "isSupervised, checksum, lastValidationDate, lockTokenId, lockScope, " + "lockType, lockedByUser, lockDepth, lockTimeout, description, " + "status, accessDate, ttlSec " + // + ", resourceUri "
    "FROM ldata_table " + // + "JOIN storage_site_table ON storageSiteId = storageSiteRef"
    "WHERE ldata_table.uid = ?")) {
        ps.setLong(1, UID);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            res = new LogicalData();
            res.setUid(UID);
            res.setParentRef(rs.getLong(1));
            res.setOwner(rs.getString(2));
            res.setType(rs.getString(3));
            res.setName(rs.getString(4));
            res.setCreateDate(rs.getTimestamp(5).getTime());
            res.setModifiedDate(rs.getTimestamp(6).getTime());
            res.setLength(rs.getLong(7));
            res.setContentTypesAsString(rs.getString(8));
            res.setPdriGroupId(rs.getLong(9));
            res.setSupervised(rs.getBoolean(10));
            res.setChecksum(rs.getString(11));
            res.setLastValidationDate(rs.getLong(12));
            res.setLockTokenID(rs.getString(13));
            res.setLockScope(rs.getString(14));
            res.setLockType(rs.getString(15));
            res.setLockedByUser(rs.getString(16));
            res.setLockDepth(rs.getString(17));
            res.setLockTimeout(rs.getLong(18));
            res.setDescription(rs.getString(19));
            // res.setDataLocationPreference(rs.getString(20));
            res.setStatus(rs.getString(20));
            res.setLastAccessDate(rs.getTimestamp(21) != null ? rs.getTimestamp(21).getTime() : null);
            int ttl = rs.getInt(22);
            res.setTtlSec(rs.wasNull() ? null : ttl);
            // Array pref = rs.getArray(24);
            return res;
        } else {
            return null;
        }
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData)

Example 5 with LogicalData

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

the class JDBCatalogue method removeFolderContent.

public boolean removeFolderContent(LogicalData toRemove, MyPrincipal principal, Connection connection) throws SQLException {
    boolean flag = true;
    Permissions toRemovePerm = getPermissions(toRemove.getUid(), toRemove.getOwner(), connection);
    if (principal.canRead(toRemovePerm) && principal.canWrite(toRemovePerm)) {
        try (PreparedStatement ps1 = connection.prepareStatement("DELETE FROM ldata_table WHERE datatype  = 'logical.file' AND parentRef = ?")) {
            ps1.setLong(1, toRemove.getUid());
            ps1.executeUpdate();
            try (PreparedStatement ps2 = connection.prepareStatement("SELECT uid, ownerId FROM ldata_table WHERE datatype  = 'logical.folder' AND parentRef = ?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) {
                ps2.setLong(1, toRemove.getUid());
                ResultSet rs = ps2.executeQuery();
                while (rs.next()) {
                    LogicalData element = new LogicalData();
                    element.setUid(rs.getLong(1));
                    element.setParentRef(toRemove.getUid());
                    element.setType(Constants.LOGICAL_FOLDER);
                    element.setOwner(rs.getString(2));
                    if (removeFolderContent(element, principal, connection)) {
                        rs.deleteRow();
                    } else {
                        flag = false;
                    }
                }
            }
        }
    } else {
        return false;
    }
    return flag;
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Permissions(nl.uva.cs.lobcder.auth.Permissions)

Aggregations

LogicalData (nl.uva.cs.lobcder.resources.LogicalData)71 Connection (java.sql.Connection)29 SQLException (java.sql.SQLException)29 Permissions (nl.uva.cs.lobcder.auth.Permissions)29 MyPrincipal (nl.uva.cs.lobcder.auth.MyPrincipal)20 PreparedStatement (java.sql.PreparedStatement)11 ResultSet (java.sql.ResultSet)10 Path (io.milton.common.Path)7 ArrayList (java.util.ArrayList)7 BadRequestException (io.milton.http.exceptions.BadRequestException)6 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)5 PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)5 ConflictException (io.milton.http.exceptions.ConflictException)4 URISyntaxException (java.net.URISyntaxException)4 Stack (java.util.Stack)4 Path (javax.ws.rs.Path)4 LogicalDataWrapped (nl.uva.cs.lobcder.rest.wrappers.LogicalDataWrapped)4 VRL (nl.uva.vlet.vrl.VRL)4 PreConditionFailedException (io.milton.http.exceptions.PreConditionFailedException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3