001/*
002 * (C) Copyright 2007-2018 Nuxeo SA (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 Guillaume
018 *     Florent Munch
019 */
020package org.nuxeo.ecm.core.scheduler;
021
022import org.nuxeo.common.xmap.annotation.XNode;
023import org.nuxeo.common.xmap.annotation.XObject;
024import org.nuxeo.ecm.core.api.NuxeoException;
025
026/**
027 * ScheduleImpl extension definition.
028 */
029@XObject("schedule")
030public class ScheduleImpl implements Schedule {
031
032    private static final long serialVersionUID = 1L;
033
034    @XNode("@id")
035    public String id;
036
037    /**
038     * @since 10.2
039     */
040    @XNode("@jobFactoryClass")
041    public Class<? extends EventJobFactory> jobFactoryClass = DefaultEventJobFactory.class;
042
043    @XNode("event")
044    public String eventId;
045
046    // BBB compat with old descriptors. use <event> now for consistency with
047    // EventListenerDescriptor
048    @XNode("eventId")
049    public void setEventId(String eventId) {
050        this.eventId = eventId;
051    }
052
053    @XNode("eventCategory")
054    public String eventCategory;
055
056    @XNode("cronExpression")
057    public String cronExpression;
058
059    /**
060     * @since 10.2
061     */
062    @XNode("timezone")
063    public String timeZone;
064
065    @XNode("username")
066    public String username;
067
068    /**
069     * @since 5.7.3
070     */
071    @XNode("@enabled")
072    public boolean enabled = true;
073
074    @Override
075    public String getId() {
076        return id;
077    }
078
079    @Override
080    public EventJobFactory getJobFactory() {
081        try {
082            return jobFactoryClass.getDeclaredConstructor().newInstance();
083        } catch (ReflectiveOperationException e) {
084            throw new NuxeoException("Failed to instantiate job factory " + jobFactoryClass, e);
085        }
086    }
087
088    @Override
089    public String getEventId() {
090        return eventId;
091    }
092
093    @Override
094    public String getEventCategory() {
095        return eventCategory;
096    }
097
098    @Override
099    public String getCronExpression() {
100        return cronExpression;
101    }
102
103    @Override
104    public String getUsername() {
105        return username;
106    }
107
108    @Override
109    public String toString() {
110        return "Schedule " + id + " (cron=" + cronExpression + ')';
111    }
112
113    @Override
114    public boolean equals(Object obj) {
115        if (obj == null) {
116            return false;
117        }
118        if (!(obj instanceof Schedule)) {
119            return false;
120        }
121        return id.equals(((Schedule) obj).getId());
122    }
123
124    @Override
125    public int hashCode() {
126        return id.hashCode();
127    }
128
129    @Override
130    public boolean isEnabled() {
131        return enabled;
132    }
133
134
135    /**
136     * @since 10.2
137     */
138    @Override
139    public String getTimeZone() {
140        return timeZone;
141    }
142}