|
| 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 | + |
| 20 | +package org.elasticsearch.client.dataframe.transforms; |
| 21 | + |
| 22 | +import org.elasticsearch.client.dataframe.transforms.pivot.PivotConfig; |
| 23 | +import org.elasticsearch.common.ParseField; |
| 24 | +import org.elasticsearch.common.Strings; |
| 25 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; |
| 34 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; |
| 35 | + |
| 36 | +public class DataFrameTransformConfig implements ToXContentObject { |
| 37 | + |
| 38 | + public static final ParseField ID = new ParseField("id"); |
| 39 | + public static final ParseField SOURCE = new ParseField("source"); |
| 40 | + public static final ParseField DEST = new ParseField("dest"); |
| 41 | + public static final ParseField QUERY = new ParseField("query"); |
| 42 | + // types of transforms |
| 43 | + public static final ParseField PIVOT_TRANSFORM = new ParseField("pivot"); |
| 44 | + |
| 45 | + private final String id; |
| 46 | + private final String source; |
| 47 | + private final String dest; |
| 48 | + private final QueryConfig queryConfig; |
| 49 | + private final PivotConfig pivotConfig; |
| 50 | + |
| 51 | + private static final ConstructingObjectParser<DataFrameTransformConfig, String> PARSER = |
| 52 | + new ConstructingObjectParser<>("data_frame_transform", true, |
| 53 | + (args) -> { |
| 54 | + String id = (String) args[0]; |
| 55 | + String source = (String) args[1]; |
| 56 | + String dest = (String) args[2]; |
| 57 | + QueryConfig queryConfig = (QueryConfig) args[3]; |
| 58 | + PivotConfig pivotConfig = (PivotConfig) args[4]; |
| 59 | + return new DataFrameTransformConfig(id, source, dest, queryConfig, pivotConfig); |
| 60 | + }); |
| 61 | + |
| 62 | + static { |
| 63 | + PARSER.declareString(constructorArg(), ID); |
| 64 | + PARSER.declareString(constructorArg(), SOURCE); |
| 65 | + PARSER.declareString(constructorArg(), DEST); |
| 66 | + PARSER.declareObject(optionalConstructorArg(), (p, c) -> QueryConfig.fromXContent(p), QUERY); |
| 67 | + PARSER.declareObject(optionalConstructorArg(), (p, c) -> PivotConfig.fromXContent(p), PIVOT_TRANSFORM); |
| 68 | + } |
| 69 | + |
| 70 | + public static DataFrameTransformConfig fromXContent(final XContentParser parser) { |
| 71 | + return PARSER.apply(parser, null); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + public DataFrameTransformConfig(final String id, |
| 76 | + final String source, |
| 77 | + final String dest, |
| 78 | + final QueryConfig queryConfig, |
| 79 | + final PivotConfig pivotConfig) { |
| 80 | + this.id = Objects.requireNonNull(id); |
| 81 | + this.source = Objects.requireNonNull(source); |
| 82 | + this.dest = Objects.requireNonNull(dest); |
| 83 | + this.queryConfig = queryConfig; |
| 84 | + this.pivotConfig = pivotConfig; |
| 85 | + } |
| 86 | + |
| 87 | + public String getId() { |
| 88 | + return id; |
| 89 | + } |
| 90 | + |
| 91 | + public String getSource() { |
| 92 | + return source; |
| 93 | + } |
| 94 | + |
| 95 | + public String getDestination() { |
| 96 | + return dest; |
| 97 | + } |
| 98 | + |
| 99 | + public PivotConfig getPivotConfig() { |
| 100 | + return pivotConfig; |
| 101 | + } |
| 102 | + |
| 103 | + public QueryConfig getQueryConfig() { |
| 104 | + return queryConfig; |
| 105 | + } |
| 106 | + |
| 107 | + public boolean isValid() { |
| 108 | + if (queryConfig != null && queryConfig.isValid() == false) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + if (pivotConfig == null || pivotConfig.isValid() == false) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException { |
| 121 | + builder.startObject(); |
| 122 | + builder.field(ID.getPreferredName(), id); |
| 123 | + builder.field(SOURCE.getPreferredName(), source); |
| 124 | + builder.field(DEST.getPreferredName(), dest); |
| 125 | + if (queryConfig != null) { |
| 126 | + builder.field(QUERY.getPreferredName(), queryConfig); |
| 127 | + } |
| 128 | + if (pivotConfig != null) { |
| 129 | + builder.field(PIVOT_TRANSFORM.getPreferredName(), pivotConfig); |
| 130 | + } |
| 131 | + builder.endObject(); |
| 132 | + return builder; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public boolean equals(Object other) { |
| 137 | + if (this == other) { |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + if (other == null || getClass() != other.getClass()) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + final DataFrameTransformConfig that = (DataFrameTransformConfig) other; |
| 146 | + |
| 147 | + return Objects.equals(this.id, that.id) |
| 148 | + && Objects.equals(this.source, that.source) |
| 149 | + && Objects.equals(this.dest, that.dest) |
| 150 | + && Objects.equals(this.queryConfig, that.queryConfig) |
| 151 | + && Objects.equals(this.pivotConfig, that.pivotConfig); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public int hashCode() { |
| 156 | + return Objects.hash(id, source, dest, queryConfig, pivotConfig); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public String toString() { |
| 161 | + return Strings.toString(this, true, true); |
| 162 | + } |
| 163 | +} |
0 commit comments