Search in sources :

Example 11 with JobScheduleRequest

use of org.pentaho.platform.web.http.api.resources.JobScheduleRequest 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 12 with JobScheduleRequest

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

the class ScheduleExportUtil method createJobScheduleRequest.

public static JobScheduleRequest createJobScheduleRequest(Job job) {
    if (job == null) {
        throw new IllegalArgumentException(Messages.getInstance().getString("ScheduleExportUtil.JOB_MUST_NOT_BE_NULL"));
    }
    JobScheduleRequest schedule = new JobScheduleRequest();
    schedule.setJobName(job.getJobName());
    schedule.setDuration(job.getJobTrigger().getDuration());
    schedule.setJobState(job.getState());
    Map<String, Serializable> jobParams = job.getJobParams();
    Object streamProviderObj = jobParams.get(QuartzScheduler.RESERVEDMAPKEY_STREAMPROVIDER);
    RepositoryFileStreamProvider streamProvider = null;
    if (streamProviderObj instanceof RepositoryFileStreamProvider) {
        streamProvider = (RepositoryFileStreamProvider) streamProviderObj;
    } else if (streamProviderObj instanceof String) {
        String inputFilePath = null;
        String outputFilePath = null;
        String inputOutputString = (String) streamProviderObj;
        String[] tokens = inputOutputString.split(":");
        if (!ArrayUtils.isEmpty(tokens) && tokens.length == 2) {
            inputFilePath = tokens[0].split("=")[1].trim();
            outputFilePath = tokens[1].split("=")[1].trim();
            streamProvider = new RepositoryFileStreamProvider(inputFilePath, outputFilePath, true);
        }
    }
    if (streamProvider != null) {
        schedule.setInputFile(streamProvider.getInputFilePath());
        schedule.setOutputFile(streamProvider.getOutputFilePath());
    } else {
        // let's look to see if we can figure out the input and output file
        String directory = (String) jobParams.get("directory");
        String transName = (String) jobParams.get("transformation");
        String jobName = (String) jobParams.get("job");
        String artifact = transName == null ? jobName : transName;
        if (directory != null && artifact != null) {
            String outputFile = RepositoryFilenameUtils.concat(directory, artifact);
            outputFile += "*";
            if (artifact.equals(jobName)) {
                artifact += ".kjb";
            } else {
                artifact += ".ktr";
            }
            String inputFile = RepositoryFilenameUtils.concat(directory, artifact);
            schedule.setInputFile(inputFile);
            schedule.setOutputFile(outputFile);
        }
    }
    for (String key : jobParams.keySet()) {
        Serializable serializable = jobParams.get(key);
        if (RUN_PARAMETERS_KEY.equals(key)) {
            if (schedule.getPdiParameters() == null) {
                schedule.setPdiParameters(new HashMap<String, String>());
            }
            schedule.getPdiParameters().putAll((Map<String, String>) serializable);
        } else {
            JobScheduleParam param = null;
            if (serializable instanceof String) {
                String value = (String) serializable;
                if (QuartzScheduler.RESERVEDMAPKEY_ACTIONCLASS.equals(key)) {
                    schedule.setActionClass(value);
                } else if (IBlockoutManager.TIME_ZONE_PARAM.equals(key)) {
                    schedule.setTimeZone(value);
                }
                param = new JobScheduleParam(key, (String) serializable);
            } else if (serializable instanceof Number) {
                param = new JobScheduleParam(key, (Number) serializable);
            } else if (serializable instanceof Date) {
                param = new JobScheduleParam(key, (Date) serializable);
            } else if (serializable instanceof Boolean) {
                param = new JobScheduleParam(key, (Boolean) serializable);
            }
            if (param != null) {
                schedule.getJobParameters().add(param);
            }
        }
    }
    if (job.getJobTrigger() instanceof SimpleJobTrigger) {
        SimpleJobTrigger jobTrigger = (SimpleJobTrigger) job.getJobTrigger();
        schedule.setSimpleJobTrigger(jobTrigger);
    } else if (job.getJobTrigger() instanceof ComplexJobTrigger) {
        ComplexJobTrigger jobTrigger = (ComplexJobTrigger) job.getJobTrigger();
        // force it to a cron trigger to get the auto-parsing of the complex trigger
        CronJobTrigger cron = new CronJobTrigger();
        cron.setCronString(jobTrigger.getCronString());
        cron.setStartTime(jobTrigger.getStartTime());
        cron.setEndTime(jobTrigger.getEndTime());
        cron.setDuration(jobTrigger.getDuration());
        cron.setUiPassParam(jobTrigger.getUiPassParam());
        schedule.setCronJobTrigger(cron);
    } else if (job.getJobTrigger() instanceof CronJobTrigger) {
        CronJobTrigger jobTrigger = (CronJobTrigger) job.getJobTrigger();
        schedule.setCronJobTrigger(jobTrigger);
    } else {
        // don't know what this is, can't export it
        throw new IllegalArgumentException(Messages.getInstance().getString("PentahoPlatformExporter.UNSUPPORTED_JobTrigger", job.getJobTrigger().getClass().getName()));
    }
    return schedule;
}
Also used : JobScheduleParam(org.pentaho.platform.web.http.api.resources.JobScheduleParam) Serializable(java.io.Serializable) Date(java.util.Date) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) RepositoryFileStreamProvider(org.pentaho.platform.web.http.api.resources.RepositoryFileStreamProvider) JobScheduleRequest(org.pentaho.platform.web.http.api.resources.JobScheduleRequest) CronJobTrigger(org.pentaho.platform.api.scheduler2.CronJobTrigger)

Example 13 with JobScheduleRequest

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

the class ScheduleExportUtilTest method testCreateJobScheduleRequest_StreamProviderJobParam.

@Test
public void testCreateJobScheduleRequest_StreamProviderJobParam() throws Exception {
    String jobName = "JOB";
    String inputPath = "/input/path/to/file.ext";
    String outputPath = "/output/path/location.*";
    Map<String, Serializable> params = new HashMap<>();
    RepositoryFileStreamProvider streamProvider = mock(RepositoryFileStreamProvider.class);
    params.put(QuartzScheduler.RESERVEDMAPKEY_STREAMPROVIDER, streamProvider);
    Job job = mock(Job.class);
    CronJobTrigger trigger = mock(CronJobTrigger.class);
    when(job.getJobTrigger()).thenReturn(trigger);
    when(job.getJobName()).thenReturn(jobName);
    when(job.getJobParams()).thenReturn(params);
    when(streamProvider.getInputFilePath()).thenReturn(inputPath);
    when(streamProvider.getOutputFilePath()).thenReturn(outputPath);
    JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest(job);
    assertEquals(inputPath, jobScheduleRequest.getInputFile());
    assertEquals(outputPath, jobScheduleRequest.getOutputFile());
    assertEquals(0, jobScheduleRequest.getJobParameters().size());
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) RepositoryFileStreamProvider(org.pentaho.platform.web.http.api.resources.RepositoryFileStreamProvider) Job(org.pentaho.platform.api.scheduler2.Job) JobScheduleRequest(org.pentaho.platform.web.http.api.resources.JobScheduleRequest) CronJobTrigger(org.pentaho.platform.api.scheduler2.CronJobTrigger) Test(org.junit.Test)

Example 14 with JobScheduleRequest

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

the class ScheduleExportUtilTest method testCreateJobScheduleRequest_ComplexJobTrigger.

@Test
public void testCreateJobScheduleRequest_ComplexJobTrigger() throws Exception {
    String jobName = "JOB";
    Date now = new Date();
    Job job = mock(Job.class);
    ComplexJobTrigger trigger = mock(ComplexJobTrigger.class);
    when(job.getJobTrigger()).thenReturn(trigger);
    when(job.getJobName()).thenReturn(jobName);
    when(trigger.getCronString()).thenReturn("0 30 13 ? * 2,3,4,5,6 *");
    when(trigger.getDuration()).thenReturn(-1L);
    when(trigger.getStartTime()).thenReturn(now);
    when(trigger.getEndTime()).thenReturn(now);
    when(trigger.getUiPassParam()).thenReturn("uiPassParm");
    JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest(job);
    assertNotNull(jobScheduleRequest);
    assertEquals(jobName, jobScheduleRequest.getJobName());
    // we should be getting back a cron trigger, not a complex trigger.
    assertNull(jobScheduleRequest.getSimpleJobTrigger());
    assertNull(jobScheduleRequest.getComplexJobTrigger());
    assertNotNull(jobScheduleRequest.getCronJobTrigger());
    assertEquals(trigger.getCronString(), jobScheduleRequest.getCronJobTrigger().getCronString());
    assertEquals(trigger.getDuration(), jobScheduleRequest.getCronJobTrigger().getDuration());
    assertEquals(trigger.getEndTime(), jobScheduleRequest.getCronJobTrigger().getEndTime());
    assertEquals(trigger.getStartTime(), jobScheduleRequest.getCronJobTrigger().getStartTime());
    assertEquals(trigger.getUiPassParam(), jobScheduleRequest.getCronJobTrigger().getUiPassParam());
}
Also used : ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Job(org.pentaho.platform.api.scheduler2.Job) JobScheduleRequest(org.pentaho.platform.web.http.api.resources.JobScheduleRequest) Date(java.util.Date) Test(org.junit.Test)

Example 15 with JobScheduleRequest

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

the class ScheduleExportUtilTest method testCreateJobScheduleRequest_unknownTrigger.

@Test(expected = IllegalArgumentException.class)
public void testCreateJobScheduleRequest_unknownTrigger() throws Exception {
    String jobName = "JOB";
    Job job = mock(Job.class);
    JobTrigger trigger = mock(JobTrigger.class);
    when(job.getJobTrigger()).thenReturn(trigger);
    JobScheduleRequest jobScheduleRequest = ScheduleExportUtil.createJobScheduleRequest(job);
}
Also used : Job(org.pentaho.platform.api.scheduler2.Job) JobScheduleRequest(org.pentaho.platform.web.http.api.resources.JobScheduleRequest) CronJobTrigger(org.pentaho.platform.api.scheduler2.CronJobTrigger) SimpleJobTrigger(org.pentaho.platform.api.scheduler2.SimpleJobTrigger) JobTrigger(org.pentaho.platform.api.scheduler2.JobTrigger) ComplexJobTrigger(org.pentaho.platform.api.scheduler2.ComplexJobTrigger) Test(org.junit.Test)

Aggregations

JobScheduleRequest (org.pentaho.platform.web.http.api.resources.JobScheduleRequest)29 Test (org.junit.Test)24 Job (org.pentaho.platform.api.scheduler2.Job)19 ArrayList (java.util.ArrayList)11 Serializable (java.io.Serializable)10 HashMap (java.util.HashMap)9 CronJobTrigger (org.pentaho.platform.api.scheduler2.CronJobTrigger)9 JobScheduleParam (org.pentaho.platform.web.http.api.resources.JobScheduleParam)8 SimpleJobTrigger (org.pentaho.platform.api.scheduler2.SimpleJobTrigger)7 IOException (java.io.IOException)6 SchedulerResource (org.pentaho.platform.web.http.api.resources.SchedulerResource)5 Date (java.util.Date)4 Response (javax.ws.rs.core.Response)4 Matchers.anyString (org.mockito.Matchers.anyString)4 SchedulerException (org.pentaho.platform.api.scheduler2.SchedulerException)4 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)3 ComplexJobTrigger (org.pentaho.platform.api.scheduler2.ComplexJobTrigger)3 IJobTrigger (org.pentaho.platform.api.scheduler2.IJobTrigger)3 ComplexJobTriggerProxy (org.pentaho.platform.web.http.api.resources.ComplexJobTriggerProxy)3 Map (java.util.Map)2