Search in sources :

Example 66 with LogicalData

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

the class WebDataDirResource method child.

@Override
public Resource child(String childName) throws NotAuthorizedException {
    Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "child({0}) for {1}", new Object[] { childName, getPath() });
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            LogicalData childLD = getCatalogue().getLogicalDataByParentRefAndName(getLogicalData().getUid(), childName, connection);
            connection.commit();
            connection.close();
            if (childLD != null) {
                if (childLD.getType().equals(Constants.LOGICAL_FOLDER)) {
                    return new WebDataDirResource(childLD, Path.path(getPath(), childName), getCatalogue(), authList);
                } else {
                    return new WebDataFileResource(childLD, Path.path(getPath(), childName), getCatalogue(), authList);
                }
            } else {
                return null;
            }
        } catch (SQLException e) {
            Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e);
            if (connection != null && !connection.isClosed()) {
                connection.rollback();
                connection.close();
            }
            return null;
        }
    } catch (SQLException e1) {
        Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e1);
        return null;
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 67 with LogicalData

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

the class WebDataDirResource method getChildren.

@Override
public List<? extends Resource> getChildren() throws NotAuthorizedException {
    // Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "getChildren() for {0}", getPath());
    try {
        try (Connection connection = getCatalogue().getConnection()) {
            try {
                List<Resource> children = new ArrayList<>();
                Collection<LogicalData> childrenLD = getCatalogue().getChildrenByParentRef(getLogicalData().getUid(), connection);
                if (childrenLD != null) {
                    for (LogicalData childLD : childrenLD) {
                        if (childLD.getType().equals(Constants.LOGICAL_FOLDER)) {
                            children.add(new WebDataDirResource(childLD, Path.path(getPath(), childLD.getName()), getCatalogue(), authList));
                        } else {
                            children.add(new WebDataFileResource(childLD, Path.path(getPath(), childLD.getName()), getCatalogue(), authList));
                        }
                    }
                }
                getCatalogue().addViewForRes(getLogicalData().getUid(), connection);
                connection.commit();
                connection.close();
                return children;
            } catch (Exception e) {
                if (connection != null && !connection.isClosed()) {
                    connection.rollback();
                    connection.close();
                }
                throw e;
            }
        }
    } catch (Exception e) {
        Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e);
        return null;
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Connection(java.sql.Connection) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 68 with LogicalData

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

the class Assimilator method assimilate.

private void assimilate(List<StorageSite> sites) throws SQLException, MalformedURLException, VlException, NoSuchAlgorithmException, Exception {
    Connection c = getConnection();
    StorageSiteClient ssClient;
    for (StorageSite site : sites) {
        String username = site.getCredential().getStorageSiteUsername();
        String password = site.getCredential().getStorageSitePassword();
        String ssURI = site.getResourceURI();
        long ssID = getStorageSiteID(c, ssURI);
        if (ssID == -1) {
            long credentialsID = addCredentials(c, username, password);
            ssID = addStorageSite(c, site, credentialsID, false);
        }
        URI importedURI = new URI(site.getResourceURI());
        String importedFolderName = "ImportedFrom-" + importedURI.getScheme() + "-" + importedURI.getHost();
        // Add imported folder
        LogicalData imported = getLogicalDataByPath(Path.path(importedFolderName), c);
        Long importedUid;
        if (imported == null) {
            importedUid = addRegisteredFolder(importedFolderName, c);
        } else {
            importedUid = imported.getUid();
        }
        // 
        ssClient = new StorageSiteClient(username, password, ssURI);
        VNode dir = ssClient.getStorageSiteClient().getNode(new VRL(ssURI));
        // VFSClient client = (VFSClient) ssClient.getStorageSiteClient();
        // VDir dir = client.openDir(new VRL(ssURI));
        // build folders first
        nl.uva.vlet.vdriver.vrs.http.HTTPNode k = (nl.uva.vlet.vdriver.vrs.http.HTTPNode) dir;
        // k.getInputStream()
        add((VComposite) dir, dir.getPath(), c, ssID, false);
        VNode[] nodes = ((VComposite) dir).getNodes();
        for (VNode n : nodes) {
            if (!n.isComposite()) {
                VFile f = (VFile) n;
                String fileName = n.getName();
                VRL currentPath = new VRL(f.getPath().replaceFirst(dir.getPath(), ""));
                LogicalData parent = getLogicalDataByPath(Path.path(currentPath.getPath()), c);
                Long parentRef;
                if (parent != null) {
                    parentRef = parent.getUid();
                } else {
                    parentRef = importedUid;
                }
                LogicalData registered = getLogicalDataByParentRefAndName(parentRef, fileName, c);
                System.err.println(currentPath);
                if (registered == null) {
                    addFile(c, f, parentRef, ssID);
                }
            }
        }
    }
    c.commit();
    c.close();
}
Also used : URI(java.net.URI) VFile(nl.uva.vlet.vfs.VFile) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) VRL(nl.uva.vlet.vrl.VRL) StorageSite(nl.uva.cs.lobcder.resources.StorageSite) VNode(nl.uva.vlet.vrs.VNode) VComposite(nl.uva.vlet.vrs.VComposite)

Example 69 with LogicalData

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

the class Assimilator method add.

public void add(VComposite dir, String base, Connection connection, long ssid, boolean addFiles) throws MalformedURLException, VlException, SQLException, NoSuchAlgorithmException {
    VNode[] nodes = dir.getNodes();
    for (VNode f : nodes) {
        VRL currentPath = new VRL(f.getPath().replaceFirst(base, ""));
        LogicalData register = getLogicalDataByPath(Path.path(currentPath.getPath()), connection);
        LogicalData parent = getLogicalDataByPath(Path.path(currentPath.getPath()).getParent(), connection);
        Long parentRef = new Long(1);
        if (parent == null) {
            parentRef = new Long(1);
        } else {
            parentRef = parent.getUid();
        }
        if (f.isComposite()) {
            if (register == null) {
                LogicalData entry = new LogicalData();
                if (f instanceof VDir) {
                    VDir d = (VDir) f;
                    entry.setCreateDate(d.getModificationTime());
                    entry.setModifiedDate(d.getModificationTime());
                } else {
                    entry.setCreateDate(System.currentTimeMillis());
                    entry.setModifiedDate(System.currentTimeMillis());
                }
                entry.setName(f.getName());
                entry.setOwner(importingOwner);
                entry.setParentRef(parentRef);
                register = registerDirLogicalData(entry, connection);
            }
            add((VComposite) f, base, connection, ssid, addFiles);
        } else if (addFiles) {
            if (register == null) {
                System.err.println(f.getVRL());
                addFile(connection, (VFile) f, parentRef, ssid);
            }
        }
    }
}
Also used : VRL(nl.uva.vlet.vrl.VRL) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) VNode(nl.uva.vlet.vrs.VNode) VDir(nl.uva.vlet.vfs.VDir) VFile(nl.uva.vlet.vfs.VFile)

Example 70 with LogicalData

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

the class Assimilator method getLogicalDataByPath.

public LogicalData getLogicalDataByPath(Path logicalResourceName, @Nonnull Connection connection) throws SQLException {
    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 " + "FROM ldata_table WHERE ldata_table.parentRef = ? AND ldata_table.ldName = ?")) {
                    preparedStatement1.setLong(1, parent);
                    preparedStatement1.setString(2, p);
                    ResultSet rs = preparedStatement1.executeQuery();
                    if (rs.next()) {
                        LogicalData 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));
                        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)

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