|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Hydrologic Engineering Center |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package mil.army.usace.hec.cwms.data.api.client.controllers; |
| 26 | + |
| 27 | +import static mil.army.usace.hec.cwms.data.api.client.controllers.CdaEndpointConstants.ACCEPT_HEADER_V1; |
| 28 | +import static mil.army.usace.hec.cwms.data.api.client.controllers.CdaEndpointConstants.ACCEPT_QUERY_HEADER; |
| 29 | + |
| 30 | +import java.util.Objects; |
| 31 | +import mil.army.usace.hec.cwms.data.api.client.model.Entity; |
| 32 | +import mil.army.usace.hec.cwms.http.client.EndpointInput; |
| 33 | +import mil.army.usace.hec.cwms.http.client.HttpRequestBuilder; |
| 34 | + |
| 35 | +public final class EntityEndpointInput { |
| 36 | + |
| 37 | + private EntityEndpointInput() { |
| 38 | + throw new AssertionError("factory class"); |
| 39 | + } |
| 40 | + |
| 41 | + public static GetAll getAll() { |
| 42 | + return new GetAll(); |
| 43 | + } |
| 44 | + |
| 45 | + public static GetOne getOne(String entityId, String officeId) { |
| 46 | + return new GetOne(entityId, officeId); |
| 47 | + } |
| 48 | + |
| 49 | + public static Post post(Entity entity) { |
| 50 | + return new Post(entity); |
| 51 | + } |
| 52 | + |
| 53 | + public static Patch patch(Entity entity) { |
| 54 | + return new Patch(entity); |
| 55 | + } |
| 56 | + |
| 57 | + public static Delete delete(String entityId, String officeId, boolean cascadeDelete) { |
| 58 | + return new Delete(entityId, officeId, cascadeDelete); |
| 59 | + } |
| 60 | + |
| 61 | + public static final class GetAll extends EndpointInput { |
| 62 | + static final String OFFICE_QUERY_PARAMETER = "office"; |
| 63 | + static final String ENTITY_ID_QUERY_PARAMETER = "entity-id"; |
| 64 | + static final String PARENT_ENTITY_ID_QUERY_PARAMETER = "parent-entity-id"; |
| 65 | + static final String CATEGORY_ID_QUERY_PARAMETER = "category-id"; |
| 66 | + static final String LONG_NAME_QUERY_PARAMETER = "long-name"; |
| 67 | + static final String MATCH_NULL_PARENTS_QUERY_PARAMETER = "match-null-parents"; |
| 68 | + |
| 69 | + private String officeId; |
| 70 | + private String entityId; |
| 71 | + private String parentEntityId; |
| 72 | + private String categoryId; |
| 73 | + private String longName; |
| 74 | + private boolean matchNullParents = true; |
| 75 | + |
| 76 | + private GetAll() { |
| 77 | + // factory |
| 78 | + } |
| 79 | + |
| 80 | + public GetAll officeId(String officeId) { |
| 81 | + this.officeId = officeId; |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + public GetAll entityId(String entityId) { |
| 86 | + this.entityId = entityId; |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + public GetAll parentEntityId(String parentEntityId) { |
| 91 | + this.parentEntityId = parentEntityId; |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + public GetAll categoryId(String categoryId) { |
| 96 | + this.categoryId = categoryId; |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + public GetAll longName(String longName) { |
| 101 | + this.longName = longName; |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + public GetAll matchNullParents(boolean matchNullParents) { |
| 106 | + this.matchNullParents = matchNullParents; |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + protected HttpRequestBuilder addInputParameters(HttpRequestBuilder httpRequestBuilder) { |
| 112 | + return httpRequestBuilder |
| 113 | + .addQueryParameter(OFFICE_QUERY_PARAMETER, officeId) |
| 114 | + .addQueryParameter(ENTITY_ID_QUERY_PARAMETER, entityId) |
| 115 | + .addQueryParameter(PARENT_ENTITY_ID_QUERY_PARAMETER, parentEntityId) |
| 116 | + .addQueryParameter(CATEGORY_ID_QUERY_PARAMETER, categoryId) |
| 117 | + .addQueryParameter(LONG_NAME_QUERY_PARAMETER, longName) |
| 118 | + .addQueryParameter(MATCH_NULL_PARENTS_QUERY_PARAMETER, Boolean.toString(matchNullParents)) |
| 119 | + .addQueryHeader(ACCEPT_QUERY_HEADER, ACCEPT_HEADER_V1); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static final class GetOne extends EndpointInput { |
| 124 | + static final String OFFICE_QUERY_PARAMETER = "office"; |
| 125 | + private final String entityId; |
| 126 | + private final String officeId; |
| 127 | + |
| 128 | + private GetOne(String entityId, String officeId) { |
| 129 | + this.entityId = Objects.requireNonNull(entityId, "Id required for getOne entity endpoint"); |
| 130 | + this.officeId = Objects.requireNonNull(officeId, "Office id required for getOne entity endpoint"); |
| 131 | + } |
| 132 | + |
| 133 | + String entityId() { |
| 134 | + return entityId; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected HttpRequestBuilder addInputParameters(HttpRequestBuilder httpRequestBuilder) { |
| 139 | + return httpRequestBuilder.addQueryParameter(OFFICE_QUERY_PARAMETER, officeId) |
| 140 | + .addQueryHeader(ACCEPT_QUERY_HEADER, ACCEPT_HEADER_V1); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public static final class Post extends EndpointInput { |
| 145 | + private final Entity entity; |
| 146 | + |
| 147 | + private Post(Entity entity) { |
| 148 | + this.entity = Objects.requireNonNull(entity, "Cannot access the entity POST endpoint without an entity object"); |
| 149 | + } |
| 150 | + |
| 151 | + Entity entity() { |
| 152 | + return entity; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + protected HttpRequestBuilder addInputParameters(HttpRequestBuilder httpRequestBuilder) { |
| 157 | + return httpRequestBuilder.addQueryHeader(ACCEPT_QUERY_HEADER, ACCEPT_HEADER_V1); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + public static final class Patch extends EndpointInput { |
| 162 | + private final Entity entity; |
| 163 | + |
| 164 | + private Patch(Entity entity) { |
| 165 | + this.entity = Objects.requireNonNull(entity, "Cannot access the entity PATCH endpoint without an entity object"); |
| 166 | + } |
| 167 | + |
| 168 | + Entity entity() { |
| 169 | + return entity; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + protected HttpRequestBuilder addInputParameters(HttpRequestBuilder httpRequestBuilder) { |
| 174 | + return httpRequestBuilder.addQueryHeader(ACCEPT_QUERY_HEADER, ACCEPT_HEADER_V1); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + public static final class Delete extends EndpointInput { |
| 179 | + static final String OFFICE_QUERY_PARAMETER = "office"; |
| 180 | + static final String CASCADE_DELETE_QUERY_PARAMETER = "cascade-delete"; |
| 181 | + private final String entityId; |
| 182 | + private final String officeId; |
| 183 | + private final boolean cascadeDelete; |
| 184 | + |
| 185 | + private Delete(String entityId, String officeId, boolean cascadeDelete) { |
| 186 | + this.entityId = Objects.requireNonNull(entityId, "Cannot access the entity DELETE endpoint without an id"); |
| 187 | + this.officeId = Objects.requireNonNull(officeId, "Cannot access the entity DELETE endpoint without an office id"); |
| 188 | + this.cascadeDelete = cascadeDelete; |
| 189 | + } |
| 190 | + |
| 191 | + String entityId() { |
| 192 | + return entityId; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + protected HttpRequestBuilder addInputParameters(HttpRequestBuilder httpRequestBuilder) { |
| 197 | + return httpRequestBuilder |
| 198 | + .addQueryParameter(OFFICE_QUERY_PARAMETER, officeId) |
| 199 | + .addQueryParameter(CASCADE_DELETE_QUERY_PARAMETER, Boolean.toString(cascadeDelete)) |
| 200 | + .addQueryHeader(ACCEPT_QUERY_HEADER, ACCEPT_HEADER_V1); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments