Search in sources :

Example 1 with DatasetRequest

use of io.hops.hopsworks.persistence.entity.dataset.DatasetRequest in project hopsworks by logicalclocks.

the class RequestService method requestAccess.

@POST
@Path("/access")
@Produces(MediaType.APPLICATION_JSON)
public Response requestAccess(RequestDTO requestDTO, @Context SecurityContext sc) throws DatasetException, ProjectException {
    RESTApiJsonResponse json = new RESTApiJsonResponse();
    if (requestDTO == null || requestDTO.getInodeId() == null || requestDTO.getProjectId() == null) {
        throw new IllegalArgumentException("requestDTO was not provided or was incomplete!");
    }
    Users user = jWTHelper.getUserPrincipal(sc);
    Inode inode = inodes.findById(requestDTO.getInodeId());
    // requested project
    Project proj = datasetCtrl.getOwningProject(inode);
    Dataset ds = datasetFacade.findByProjectAndInode(proj, inode);
    // requesting project
    Project project = projectFacade.find(requestDTO.getProjectId());
    Dataset dsInRequesting = datasetFacade.findByProjectAndInode(project, inode);
    if (dsInRequesting != null) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.DESTINATION_EXISTS, Level.INFO);
    }
    ProjectTeam projectTeam = projectTeamFacade.findByPrimaryKey(project, user);
    ProjectTeam projTeam = projectTeamFacade.findByPrimaryKey(proj, user);
    if (projTeam != null && proj.equals(project)) {
        throw new ProjectException(RESTCodes.ProjectErrorCode.TEAM_MEMBER_ALREADY_EXISTS, Level.FINE);
    }
    DatasetRequest dsRequest = datasetRequest.findByProjectAndDataset(project, ds);
    // email body
    String msg = "Hi " + proj.getOwner().getFname() + " " + proj.getOwner().getLname() + ", \n\n" + user.getFname() + " " + user.getLname() + " wants access to a dataset in a project you own. \n\n" + "Dataset name: " + ds.getInode().getInodePK().getName() + "\n" + "Project name: " + proj.getName() + "\n";
    if (!Strings.isNullOrEmpty(requestDTO.getMessageContent())) {
        msg += "Attached message: " + requestDTO.getMessageContent() + "\n";
    }
    msg += "After logging in to Hopsworks go to : /project/" + proj.getId() + "/datasets " + " if you want to share this dataset. \n";
    // or the prior request is from a data owner do nothing.
    if (dsRequest != null && (dsRequest.getProjectTeam().getTeamRole().equals(projectTeam.getTeamRole()) || dsRequest.getProjectTeam().getTeamRole().equals(AllowedProjectRoles.DATA_OWNER))) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.DATASET_REQUEST_EXISTS, Level.FINE);
    } else if (dsRequest != null && projectTeam.getTeamRole().equals(AllowedProjectRoles.DATA_OWNER)) {
        dsRequest.setProjectTeam(projectTeam);
        dsRequest.setMessageContent(requestDTO.getMessageContent());
        datasetRequest.merge(dsRequest);
    } else {
        Users to = userFacade.findByEmail(proj.getOwner().getEmail());
        String message = "Hi " + to.getFname() + "<br>" + "I would like to request access to a dataset in a project you own. <br>" + "Project name: " + proj.getName() + "<br>" + "Dataset name: " + ds.getInode().getInodePK().getName() + "<br>" + "To be shared with my project: " + project.getName() + ".<br>" + "Thank you in advance.";
        String preview = user.getFname() + " would like to have access to a dataset in a project you own.";
        String subject = Settings.MESSAGE_DS_REQ_SUBJECT;
        String path = "project/" + proj.getId() + "/datasets";
        // to, from, msg, requested path
        Message newMsg = new Message(user, to, null, message, true, false);
        newMsg.setPath(path);
        newMsg.setSubject(subject);
        newMsg.setPreview(preview);
        messageBean.send(newMsg);
        dsRequest = new DatasetRequest(ds, projectTeam, requestDTO.getMessageContent(), newMsg);
        try {
            datasetRequest.persistDataset(dsRequest);
        } catch (Exception ex) {
            messageBean.remove(newMsg);
            throw new DatasetException(RESTCodes.DatasetErrorCode.DATASET_REQUEST_ERROR, Level.WARNING, ex.getMessage(), null, ex);
        }
    }
    try {
        emailBean.sendEmail(proj.getOwner().getEmail(), RecipientType.TO, "Access request for dataset " + ds.getInode().getInodePK().getName(), msg);
    } catch (MessagingException ex) {
        json.setErrorMsg("Could not send e-mail to " + project.getOwner().getEmail());
        datasetRequest.remove(dsRequest);
        return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(json).build();
    }
    json.setSuccessMessage("Request sent successfully.");
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(json).build();
}
Also used : Message(io.hops.hopsworks.persistence.entity.message.Message) MessagingException(javax.mail.MessagingException) Dataset(io.hops.hopsworks.persistence.entity.dataset.Dataset) Users(io.hops.hopsworks.persistence.entity.user.Users) MessagingException(javax.mail.MessagingException) ProjectException(io.hops.hopsworks.exceptions.ProjectException) DatasetException(io.hops.hopsworks.exceptions.DatasetException) DatasetException(io.hops.hopsworks.exceptions.DatasetException) ProjectException(io.hops.hopsworks.exceptions.ProjectException) Project(io.hops.hopsworks.persistence.entity.project.Project) ProjectTeam(io.hops.hopsworks.persistence.entity.project.team.ProjectTeam) Inode(io.hops.hopsworks.persistence.entity.hdfs.inode.Inode) DatasetRequest(io.hops.hopsworks.persistence.entity.dataset.DatasetRequest) RESTApiJsonResponse(io.hops.hopsworks.api.util.RESTApiJsonResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 2 with DatasetRequest

use of io.hops.hopsworks.persistence.entity.dataset.DatasetRequest in project hopsworks by logicalclocks.

the class MessageService method moveToTrash.

@PUT
@Path("moveToTrash/{msgId}")
@Produces(MediaType.APPLICATION_JSON)
public Response moveToTrash(@PathParam("msgId") Integer msgId, @Context SecurityContext sc) throws RequestException {
    Users user = jWTHelper.getUserPrincipal(sc);
    Message msg = msgFacade.find(msgId);
    if (msg == null) {
        throw new RequestException(RESTCodes.RequestErrorCode.MESSAGE_NOT_FOUND, Level.FINE);
    }
    // Delete Dataset request from the database
    if (!Strings.isNullOrEmpty(msg.getSubject())) {
        DatasetRequest dsReq = dsReqFacade.findByMessageId(msg);
        if (dsReq != null) {
            dsReqFacade.remove(dsReq);
        }
    }
    // check if the user is the owner of the message
    checkMsgUser(msg, user);
    msg.setDeleted(true);
    msgFacade.update(msg);
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).build();
}
Also used : Message(io.hops.hopsworks.persistence.entity.message.Message) DatasetRequest(io.hops.hopsworks.persistence.entity.dataset.DatasetRequest) Users(io.hops.hopsworks.persistence.entity.user.Users) RequestException(io.hops.hopsworks.exceptions.RequestException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 3 with DatasetRequest

use of io.hops.hopsworks.persistence.entity.dataset.DatasetRequest in project hopsworks by logicalclocks.

the class MessageService method markAsRead.

@PUT
@Path("markAsRead/{msgId}")
@Produces(MediaType.APPLICATION_JSON)
public Response markAsRead(@PathParam("msgId") Integer msgId, @Context SecurityContext sc) throws RequestException {
    Users user = jWTHelper.getUserPrincipal(sc);
    Message msg = msgFacade.find(msgId);
    // Delete Dataset request from the database
    if (!Strings.isNullOrEmpty(msg.getSubject())) {
        DatasetRequest dsReq = dsReqFacade.findByMessageId(msg);
        if (dsReq != null) {
            dsReqFacade.remove(dsReq);
        }
    }
    // check if the user is the owner of the message
    checkMsgUser(msg, user);
    msg.setUnread(false);
    msgFacade.update(msg);
    return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).build();
}
Also used : Message(io.hops.hopsworks.persistence.entity.message.Message) DatasetRequest(io.hops.hopsworks.persistence.entity.dataset.DatasetRequest) Users(io.hops.hopsworks.persistence.entity.user.Users) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 4 with DatasetRequest

use of io.hops.hopsworks.persistence.entity.dataset.DatasetRequest in project hopsworks by logicalclocks.

the class DatasetController method shareInternal.

private DatasetSharedWith shareInternal(Project targetProject, Dataset ds, Users user, DatasetAccessPermission permission) throws DatasetException {
    DatasetSharedWith datasetSharedWith = datasetSharedWithFacade.findByProjectAndDataset(targetProject, ds);
    if (datasetSharedWith != null) {
        throw new DatasetException(RESTCodes.DatasetErrorCode.DESTINATION_EXISTS, Level.FINE, "Dataset already in " + targetProject.getName());
    }
    // Create the new Dataset entry
    datasetSharedWith = new DatasetSharedWith(targetProject, ds, permission, false, user);
    // if the dataset is not requested or is requested by a data scientist set status to pending.
    DatasetRequest dsReq = datasetRequest.findByProjectAndDataset(targetProject, ds);
    if (ds.isPublicDs()) {
        if (targetProject.equals(ds.getProject())) {
            throw new DatasetException(RESTCodes.DatasetErrorCode.DATASET_ALREADY_IN_PROJECT, Level.FINE, "Dataset already in project.");
        }
        datasetSharedWith.setAccepted(true);
        datasetSharedWith.setAcceptedBy(user);
        addMembersToGroup(datasetSharedWith);
    } else if (dsReq != null && dsReq.getProjectTeam().getTeamRole().equals(AllowedRoles.DATA_OWNER)) {
        // dataset is private and requested by a data owner
        datasetSharedWith.setAccepted(true);
        datasetSharedWith.setAcceptedBy(dsReq.getProjectTeam().getUser());
        addMembersToGroup(datasetSharedWith);
    }
    datasetSharedWithFacade.save(datasetSharedWith);
    if (dsReq != null) {
        // the dataset is shared so remove the request.
        datasetRequest.remove(dsReq);
    }
    activityFacade.persistActivity(ActivityFacade.SHARED_DATA + ds.getName() + " with project " + targetProject.getName(), ds.getProject(), user, ActivityFlag.DATASET);
    return datasetSharedWith;
}
Also used : DatasetRequest(io.hops.hopsworks.persistence.entity.dataset.DatasetRequest) DatasetSharedWith(io.hops.hopsworks.persistence.entity.dataset.DatasetSharedWith) DatasetException(io.hops.hopsworks.exceptions.DatasetException)

Aggregations

DatasetRequest (io.hops.hopsworks.persistence.entity.dataset.DatasetRequest)4 Message (io.hops.hopsworks.persistence.entity.message.Message)3 Users (io.hops.hopsworks.persistence.entity.user.Users)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 DatasetException (io.hops.hopsworks.exceptions.DatasetException)2 PUT (javax.ws.rs.PUT)2 RESTApiJsonResponse (io.hops.hopsworks.api.util.RESTApiJsonResponse)1 ProjectException (io.hops.hopsworks.exceptions.ProjectException)1 RequestException (io.hops.hopsworks.exceptions.RequestException)1 Dataset (io.hops.hopsworks.persistence.entity.dataset.Dataset)1 DatasetSharedWith (io.hops.hopsworks.persistence.entity.dataset.DatasetSharedWith)1 Inode (io.hops.hopsworks.persistence.entity.hdfs.inode.Inode)1 Project (io.hops.hopsworks.persistence.entity.project.Project)1 ProjectTeam (io.hops.hopsworks.persistence.entity.project.team.ProjectTeam)1 MessagingException (javax.mail.MessagingException)1 POST (javax.ws.rs.POST)1