Search in sources :

Example 1 with SchedulerResource

use of org.pentaho.platform.web.http.api.resources.SchedulerResource in project pentaho-platform by pentaho.

the class SolutionImportHandler method importSchedules.

protected void importSchedules(List<JobScheduleRequest> scheduleList) throws PlatformImportException {
    if (CollectionUtils.isNotEmpty(scheduleList)) {
        SchedulerResource schedulerResource = new SchedulerResource();
        schedulerResource.pause();
        for (JobScheduleRequest jobScheduleRequest : scheduleList) {
            boolean jobExists = false;
            List<Job> jobs = getAllJobs(schedulerResource);
            if (jobs != null) {
                // paramRequest to map<String, Serializable>
                Map<String, Serializable> mapParamsRequest = new HashMap<>();
                for (JobScheduleParam paramRequest : jobScheduleRequest.getJobParameters()) {
                    mapParamsRequest.put(paramRequest.getName(), paramRequest.getValue());
                }
                for (Job job : jobs) {
                    if ((mapParamsRequest.get(RESERVEDMAPKEY_LINEAGE_ID) != null) && (mapParamsRequest.get(RESERVEDMAPKEY_LINEAGE_ID).equals(job.getJobParams().get(RESERVEDMAPKEY_LINEAGE_ID)))) {
                        jobExists = true;
                    }
                    if (overwriteFile && jobExists) {
                        JobRequest jobRequest = new JobRequest();
                        jobRequest.setJobId(job.getJobId());
                        schedulerResource.removeJob(jobRequest);
                        jobExists = false;
                        break;
                    }
                }
            }
            if (!jobExists) {
                try {
                    Response response = createSchedulerJob(schedulerResource, jobScheduleRequest);
                    if (response.getStatus() == Response.Status.OK.getStatusCode()) {
                        if (response.getEntity() != null) {
                            // get the schedule job id from the response and add it to the import session
                            ImportSession.getSession().addImportedScheduleJobId(response.getEntity().toString());
                        }
                    }
                } catch (Exception e) {
                    // the space(s)
                    if (jobScheduleRequest.getInputFile().contains(" ") || jobScheduleRequest.getOutputFile().contains(" ")) {
                        log.info("Could not import schedule, attempting to replace spaces with underscores and retrying: " + jobScheduleRequest.getInputFile());
                        File inFile = new File(jobScheduleRequest.getInputFile());
                        File outFile = new File(jobScheduleRequest.getOutputFile());
                        String inputFileName = inFile.getParent() + RepositoryFile.SEPARATOR + inFile.getName().replaceAll(" ", "_");
                        String outputFileName = outFile.getParent() + RepositoryFile.SEPARATOR + outFile.getName().replaceAll(" ", "_");
                        jobScheduleRequest.setInputFile(inputFileName);
                        jobScheduleRequest.setOutputFile(outputFileName);
                        try {
                            if (File.separator != RepositoryFile.SEPARATOR) {
                                // on windows systems, the backslashes will result in the file not being found in the repository
                                jobScheduleRequest.setInputFile(inputFileName.replace(File.separator, RepositoryFile.SEPARATOR));
                                jobScheduleRequest.setOutputFile(outputFileName.replace(File.separator, RepositoryFile.SEPARATOR));
                            }
                            Response response = createSchedulerJob(schedulerResource, jobScheduleRequest);
                            if (response.getStatus() == Response.Status.OK.getStatusCode()) {
                                if (response.getEntity() != null) {
                                    // get the schedule job id from the response and add it to the import session
                                    ImportSession.getSession().addImportedScheduleJobId(response.getEntity().toString());
                                }
                            }
                        } catch (Exception ex) {
                            // log it and keep going. we should stop processing all schedules just because one fails.
                            log.error(Messages.getInstance().getString("SolutionImportHandler.ERROR_0001_ERROR_CREATING_SCHEDULE", e.getMessage()), ex);
                        }
                    } else {
                        // log it and keep going. we should stop processing all schedules just because one fails.
                        log.error(Messages.getInstance().getString("SolutionImportHandler.ERROR_0001_ERROR_CREATING_SCHEDULE", e.getMessage()));
                    }
                }
            } else {
                log.info(Messages.getInstance().getString("DefaultImportHandler.ERROR_0009_OVERWRITE_CONTENT", jobScheduleRequest.toString()));
            }
        }
        schedulerResource.start();
    }
}
Also used : JobScheduleParam(org.pentaho.platform.web.http.api.resources.JobScheduleParam) Serializable(java.io.Serializable) HashMap(java.util.HashMap) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) Response(javax.ws.rs.core.Response) JobRequest(org.pentaho.platform.web.http.api.resources.JobRequest) SchedulerResource(org.pentaho.platform.web.http.api.resources.SchedulerResource) Job(org.pentaho.platform.api.scheduler2.Job) JobScheduleRequest(org.pentaho.platform.web.http.api.resources.JobScheduleRequest) ManifestFile(org.pentaho.platform.plugin.services.importexport.ImportSession.ManifestFile) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File)

Example 2 with SchedulerResource

use of org.pentaho.platform.web.http.api.resources.SchedulerResource in project pentaho-platform by pentaho.

the class SolutionImportHandlerTest method mockSchedulerPause.

private void mockSchedulerPause() {
    SchedulerResource schedulerResource = new SchedulerResource();
    new NonStrictExpectations(SchedulerResource.class) {

        {
            schedulerResource.pause();
            times = 1;
            schedulerResource.start();
            times = 1;
            schedulerResource.getAllJobs();
            result = null;
        }
    };
}
Also used : SchedulerResource(org.pentaho.platform.web.http.api.resources.SchedulerResource) NonStrictExpectations(mockit.NonStrictExpectations)

Aggregations

SchedulerResource (org.pentaho.platform.web.http.api.resources.SchedulerResource)2 File (java.io.File)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 Response (javax.ws.rs.core.Response)1 NonStrictExpectations (mockit.NonStrictExpectations)1 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)1 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)1 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)1 AlreadyExistsException (org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException)1 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)1 Job (org.pentaho.platform.api.scheduler2.Job)1 ManifestFile (org.pentaho.platform.plugin.services.importexport.ImportSession.ManifestFile)1 JobRequest (org.pentaho.platform.web.http.api.resources.JobRequest)1 JobScheduleParam (org.pentaho.platform.web.http.api.resources.JobScheduleParam)1 JobScheduleRequest (org.pentaho.platform.web.http.api.resources.JobScheduleRequest)1