|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.ml; |
| 20 | + |
| 21 | +import org.elasticsearch.action.ActionRequest; |
| 22 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 23 | +import org.elasticsearch.client.ml.calendars.Calendar; |
| 24 | +import org.elasticsearch.client.ml.calendars.ScheduledEvent; |
| 25 | +import org.elasticsearch.common.ParseField; |
| 26 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 27 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 28 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Objects; |
| 34 | + |
| 35 | +/** |
| 36 | + * Request to add a ScheduledEvent to a Machine Learning calendar |
| 37 | + */ |
| 38 | +public class PostCalendarEventRequest extends ActionRequest implements ToXContentObject { |
| 39 | + |
| 40 | + private final String calendarId; |
| 41 | + private final List<ScheduledEvent> scheduledEvents; |
| 42 | + |
| 43 | + public static final String INCLUDE_CALENDAR_ID_KEY = "include_calendar_id"; |
| 44 | + public static final ParseField EVENTS = new ParseField("events"); |
| 45 | + |
| 46 | + @SuppressWarnings("unchecked") |
| 47 | + public static final ConstructingObjectParser<PostCalendarEventRequest, Void> PARSER = |
| 48 | + new ConstructingObjectParser<>("post_calendar_event_request", |
| 49 | + a -> new PostCalendarEventRequest((String)a[0], (List<ScheduledEvent>)a[1])); |
| 50 | + |
| 51 | + static { |
| 52 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), Calendar.ID); |
| 53 | + PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), |
| 54 | + (p, c) -> ScheduledEvent.PARSER.apply(p, null), EVENTS); |
| 55 | + } |
| 56 | + public static final MapParams EXCLUDE_CALENDAR_ID_PARAMS = |
| 57 | + new MapParams(Collections.singletonMap(INCLUDE_CALENDAR_ID_KEY, Boolean.toString(false))); |
| 58 | + |
| 59 | + /** |
| 60 | + * Create a new PostCalendarEventRequest with an existing non-null calendarId and a list of Scheduled events |
| 61 | + * |
| 62 | + * @param calendarId The ID of the calendar, must be non-null |
| 63 | + * @param scheduledEvents The non-null, non-empty, list of {@link ScheduledEvent} objects to add to the calendar |
| 64 | + */ |
| 65 | + public PostCalendarEventRequest(String calendarId, List<ScheduledEvent> scheduledEvents) { |
| 66 | + this.calendarId = Objects.requireNonNull(calendarId, "[calendar_id] must not be null."); |
| 67 | + this.scheduledEvents = Objects.requireNonNull(scheduledEvents, "[events] must not be null."); |
| 68 | + if (scheduledEvents.isEmpty()) { |
| 69 | + throw new IllegalArgumentException("At least 1 event is required"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public String getCalendarId() { |
| 74 | + return calendarId; |
| 75 | + } |
| 76 | + |
| 77 | + public List<ScheduledEvent> getScheduledEvents() { |
| 78 | + return scheduledEvents; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public ActionRequestValidationException validate() { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 88 | + builder.startObject(); |
| 89 | + if (params.paramAsBoolean(INCLUDE_CALENDAR_ID_KEY, true)) { |
| 90 | + builder.field(Calendar.ID.getPreferredName(), calendarId); |
| 91 | + } |
| 92 | + builder.field(EVENTS.getPreferredName(), scheduledEvents); |
| 93 | + builder.endObject(); |
| 94 | + return builder; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public int hashCode() { |
| 99 | + return Objects.hash(calendarId, scheduledEvents); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean equals(Object obj) { |
| 104 | + if (this == obj) { |
| 105 | + return true; |
| 106 | + } |
| 107 | + if (obj == null || getClass() != obj.getClass()) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + PostCalendarEventRequest other = (PostCalendarEventRequest) obj; |
| 111 | + return Objects.equals(calendarId, other.calendarId) && Objects.equals(scheduledEvents, other.scheduledEvents); |
| 112 | + } |
| 113 | +} |
0 commit comments