001/*
002 * (C) Copyright 2007-2018 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Munch
018 */
019package org.nuxeo.ecm.core.scheduler;
020
021import java.io.Serializable;
022import java.util.Map;
023import java.util.TimeZone;
024
025import org.quartz.CronScheduleBuilder;
026import org.quartz.CronTrigger;
027import org.quartz.JobBuilder;
028import org.quartz.JobDataMap;
029import org.quartz.ScheduleBuilder;
030import org.quartz.TriggerBuilder;
031
032/**
033 * Default implementation of {@link EventJobFactory} instantiating an {@link EventJob} with a {@link CronTrigger}.
034 *
035 * @since 10.2
036 */
037public class DefaultEventJobFactory implements EventJobFactory {
038    @Override
039    public JobBuilder buildJob(Schedule schedule, Map<String, Serializable> parameters) {
040        JobDataMap map = new JobDataMap();
041        if (parameters != null) {
042            map.putAll(parameters);
043        }
044
045        return JobBuilder.newJob(getJobClass())
046                .withIdentity(schedule.getId(), "nuxeo")
047                .usingJobData(map)
048                .usingJobData("eventId", schedule.getEventId())
049                .usingJobData("eventCategory", schedule.getEventCategory())
050                .usingJobData("username", schedule.getUsername());
051    }
052
053    @Override
054    public TriggerBuilder<?> buildTrigger(Schedule schedule) {
055        return TriggerBuilder.newTrigger()
056                .withIdentity(schedule.getId(), "nuxeo")
057                .withSchedule(buildSchedule(schedule));
058    }
059
060    @Override
061    public ScheduleBuilder<?> buildSchedule(Schedule schedule) {
062        CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule(schedule.getCronExpression());
063        if (schedule.getTimeZone() != null) {
064            builder.inTimeZone(TimeZone.getTimeZone(schedule.getTimeZone()));
065        }
066        return builder;
067    }
068
069    protected Class<? extends EventJob> getJobClass() {
070        return EventJob.class;
071    }
072}