Search in sources :

Example 1 with JobData

use of org.haiku.haikudepotserver.job.model.JobData in project haikudepotserver by haiku.

the class JobApiImpl method getJob.

@Override
public GetJobResult getJob(GetJobRequest request) {
    Preconditions.checkArgument(null != request);
    Preconditions.checkArgument(!Strings.isNullOrEmpty(request.guid));
    final ObjectContext context = serverRuntime.newContext();
    JobSnapshot job = jobService.tryGetJob(request.guid).orElseThrow(() -> new ObjectNotFoundException(JobSnapshot.class.getSimpleName(), request.guid));
    if (Strings.isNullOrEmpty(job.getOwnerUserNickname())) {
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), null, Permission.JOBS_VIEW)) {
            throw new AccessDeniedException("attempt to access jobs view");
        }
    } else {
        User ownerUser = User.tryGetByNickname(context, job.getOwnerUserNickname()).orElseThrow(() -> new ObjectNotFoundException(User.class.getSimpleName(), job.getOwnerUserNickname()));
        if (!permissionEvaluator.hasPermission(SecurityContextHolder.getContext().getAuthentication(), ownerUser, Permission.USER_VIEWJOBS)) {
            throw new AccessDeniedException("attempt to access jobs view for [" + job + "]");
        }
    }
    // now output the result.
    GetJobResult result = new GetJobResult();
    result.guid = job.getGuid();
    result.jobStatus = JobStatus.valueOf(job.getStatus().name());
    result.jobTypeCode = job.getJobTypeCode();
    result.ownerUserNickname = job.getOwnerUserNickname();
    result.startTimestamp = null == job.getStartTimestamp() ? null : job.getStartTimestamp().getTime();
    result.finishTimestamp = null == job.getFinishTimestamp() ? null : job.getFinishTimestamp().getTime();
    result.queuedTimestamp = null == job.getQueuedTimestamp() ? null : job.getQueuedTimestamp().getTime();
    result.failTimestamp = null == job.getFailTimestamp() ? null : job.getFailTimestamp().getTime();
    result.cancelTimestamp = null == job.getCancelTimestamp() ? null : job.getCancelTimestamp().getTime();
    result.progressPercent = job.getProgressPercent();
    result.generatedDatas = new ArrayList<>();
    for (String guid : job.getGeneratedDataGuids()) {
        JobData jobData = jobService.tryGetData(guid).orElseThrow(() -> new ObjectNotFoundException(JobData.class.getSimpleName(), guid));
        GetJobResult.JobData resultJobData = new GetJobResult.JobData();
        resultJobData.useCode = jobData.getUseCode();
        resultJobData.guid = jobData.getGuid();
        resultJobData.mediaTypeCode = jobData.getMediaTypeCode();
        resultJobData.filename = jobService.deriveDataFilename(guid);
        result.generatedDatas.add(resultJobData);
    }
    return result;
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.haiku.haikudepotserver.dataobjects.User) ObjectNotFoundException(org.haiku.haikudepotserver.api1.support.ObjectNotFoundException) JobSnapshot(org.haiku.haikudepotserver.job.model.JobSnapshot) ObjectContext(org.apache.cayenne.ObjectContext) JobData(org.haiku.haikudepotserver.job.model.JobData)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)1 ObjectNotFoundException (org.haiku.haikudepotserver.api1.support.ObjectNotFoundException)1 User (org.haiku.haikudepotserver.dataobjects.User)1 JobData (org.haiku.haikudepotserver.job.model.JobData)1 JobSnapshot (org.haiku.haikudepotserver.job.model.JobSnapshot)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1