jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.workflow.api; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.BooleanNode; |
| 21 | import com.fasterxml.jackson.databind.node.IntNode; |
| 22 | import com.fasterxml.jackson.databind.node.MissingNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | import com.fasterxml.jackson.databind.node.TextNode; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import java.lang.annotation.Annotation; |
| 29 | import java.lang.reflect.Field; |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.HashMap; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | import java.util.Objects; |
| 35 | |
| 36 | /** |
| 37 | * Class for injecting json data model on the work-let execution context. |
| 38 | */ |
| 39 | public class JsonDataModelInjector { |
| 40 | |
| 41 | private static final Logger log = LoggerFactory.getLogger(JsonDataModelInjector.class); |
| 42 | |
| 43 | /** |
| 44 | * Injects data model to work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 45 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 46 | * @param worklet work-let to be injected |
| 47 | * @param context workflow context |
| 48 | * @throws WorkflowException workflow exception |
| 49 | */ |
| 50 | public void inject(Worklet worklet, WorkflowContext context) throws WorkflowException { |
| 51 | |
| 52 | handle(worklet, context, this::injectModel); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Inhales data model from work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 57 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 58 | * @param worklet work-let to be inhaled |
| 59 | * @param context workflow context |
| 60 | * @throws WorkflowException workflow exception |
| 61 | */ |
| 62 | public void inhale(Worklet worklet, WorkflowContext context) throws WorkflowException { |
| 63 | |
| 64 | handle(worklet, context, this::inhaleModel); |
| 65 | } |
| 66 | |
| 67 | private void handle(Worklet worklet, WorkflowContext context, DataModelFieldBehavior func) |
| 68 | throws WorkflowException { |
| 69 | Class cl = worklet.getClass(); |
| 70 | List<Field> fields = getInheritedFields(cl); |
| 71 | if (Objects.isNull(fields)) { |
| 72 | log.error("Invalid fields on {}", cl); |
| 73 | return; |
| 74 | } |
| 75 | |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 76 | for (Field field : fields) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 77 | Annotation[] annotations = field.getAnnotations(); |
| 78 | if (Objects.isNull(annotations)) { |
| 79 | continue; |
| 80 | } |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 81 | for (Annotation annotation : annotations) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 82 | if (!(annotation instanceof JsonDataModel)) { |
| 83 | continue; |
| 84 | } |
| 85 | JsonDataModel model = (JsonDataModel) annotation; |
| 86 | func.apply(worklet, context, field, model); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private static List<Field> getInheritedFields(Class<?> type) { |
| 92 | List<Field> fields = new ArrayList<Field>(); |
| 93 | |
| 94 | Class<?> cl = type; |
| 95 | while (cl != null && cl != Object.class) { |
| 96 | for (Field field : cl.getDeclaredFields()) { |
| 97 | if (!field.isSynthetic()) { |
| 98 | fields.add(field); |
| 99 | } |
| 100 | } |
| 101 | cl = cl.getSuperclass(); |
| 102 | } |
| 103 | return fields; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Functional interface for json data model annotated field behavior. |
| 108 | */ |
| 109 | @FunctionalInterface |
| 110 | public interface DataModelFieldBehavior { |
| 111 | void apply(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 112 | throws WorkflowException; |
| 113 | } |
| 114 | |
| 115 | private static Map<Class, DataModelFieldBehavior> injectTypeMap = new HashMap<>(); |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 116 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 117 | static { |
| 118 | injectTypeMap.put(String.class, JsonDataModelInjector::injectText); |
| 119 | injectTypeMap.put(Integer.class, JsonDataModelInjector::injectInteger); |
| 120 | injectTypeMap.put(Boolean.class, JsonDataModelInjector::injectBoolean); |
| 121 | injectTypeMap.put(JsonNode.class, JsonDataModelInjector::injectJsonNode); |
| 122 | injectTypeMap.put(ArrayNode.class, JsonDataModelInjector::injectArrayNode); |
| 123 | injectTypeMap.put(ObjectNode.class, JsonDataModelInjector::injectObjectNode); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Injects data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 128 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 129 | * @param worklet work-let |
| 130 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 131 | * @param field the field of work-let |
| 132 | * @param model data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 133 | * @throws WorkflowException workflow exception |
| 134 | */ |
| 135 | private void injectModel(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 136 | throws WorkflowException { |
| 137 | |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 138 | DataModelFieldBehavior behavior = injectTypeMap.get(field.getType()); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 139 | if (Objects.isNull(behavior)) { |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 140 | throw new WorkflowException("Not supported type(" + field.getType() + ")"); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 141 | } |
| 142 | behavior.apply(worklet, context, field, model); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Injects text data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 147 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 148 | * @param worklet work-let |
| 149 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 150 | * @param field the field of work-let |
| 151 | * @param model text data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 152 | * @throws WorkflowException workflow exception |
| 153 | */ |
| 154 | private static void injectText(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 155 | throws WorkflowException { |
| 156 | |
| 157 | String text = ((JsonDataModelTree) context.data()).textAt(model.path()); |
| 158 | if (Objects.isNull(text)) { |
| 159 | if (model.optional()) { |
| 160 | return; |
| 161 | } |
| 162 | throw new WorkflowException("Invalid text data model on (" + model.path() + ")"); |
| 163 | } |
| 164 | |
| 165 | if (!(Objects.equals(field.getType(), String.class))) { |
| 166 | throw new WorkflowException("Target field (" + field + ") is not String"); |
| 167 | } |
| 168 | |
| 169 | try { |
| 170 | field.setAccessible(true); |
| 171 | field.set(worklet, text); |
| 172 | } catch (IllegalAccessException e) { |
| 173 | throw new WorkflowException(e); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Injects integer data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 179 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 180 | * @param worklet work-let |
| 181 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 182 | * @param field the field of work-let |
| 183 | * @param model integer data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 184 | * @throws WorkflowException workflow exception |
| 185 | */ |
| 186 | private static void injectInteger(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 187 | throws WorkflowException { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 188 | Integer number = ((JsonDataModelTree) context.data()).intAt(model.path()); |
| 189 | if (Objects.isNull(number)) { |
| 190 | if (model.optional()) { |
| 191 | return; |
| 192 | } |
| 193 | throw new WorkflowException("Invalid number data model on (" + model.path() + ")"); |
| 194 | } |
| 195 | |
| 196 | if (!(Objects.equals(field.getType(), Integer.class))) { |
| 197 | throw new WorkflowException("Target field (" + field + ") is not Integer"); |
| 198 | } |
| 199 | |
| 200 | try { |
| 201 | field.setAccessible(true); |
| 202 | field.set(worklet, number); |
| 203 | } catch (IllegalAccessException e) { |
| 204 | throw new WorkflowException(e); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Injects boolean data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 210 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 211 | * @param worklet work-let |
| 212 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 213 | * @param field the field of work-let |
| 214 | * @param model boolean data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 215 | * @throws WorkflowException workflow exception |
| 216 | */ |
| 217 | private static void injectBoolean(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 218 | throws WorkflowException { |
| 219 | |
| 220 | Boolean bool = ((JsonDataModelTree) context.data()).booleanAt(model.path()); |
| 221 | if (Objects.isNull(bool)) { |
| 222 | if (model.optional()) { |
| 223 | return; |
| 224 | } |
| 225 | throw new WorkflowException("Invalid boolean data model on (" + model.path() + ")"); |
| 226 | } |
| 227 | |
| 228 | if (!(Objects.equals(field.getType(), Boolean.class))) { |
| 229 | throw new WorkflowException("Target field (" + field + ") is not Boolean"); |
| 230 | } |
| 231 | |
| 232 | try { |
| 233 | field.setAccessible(true); |
| 234 | field.set(worklet, bool); |
| 235 | } catch (IllegalAccessException e) { |
| 236 | throw new WorkflowException(e); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Injects json node data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 242 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 243 | * @param worklet work-let |
| 244 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 245 | * @param field the field of work-let |
| 246 | * @param model json node data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 247 | * @throws WorkflowException workflow exception |
| 248 | */ |
| 249 | private static void injectJsonNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 250 | throws WorkflowException { |
| 251 | |
| 252 | JsonNode jsonNode = ((JsonDataModelTree) context.data()).nodeAt(model.path()); |
| 253 | if (Objects.isNull(jsonNode)) { |
| 254 | if (model.optional()) { |
| 255 | return; |
| 256 | } |
| 257 | throw new WorkflowException("Invalid json node data model on (" + model.path() + ")"); |
| 258 | } |
| 259 | |
| 260 | if (!(Objects.equals(field.getType(), JsonNode.class))) { |
| 261 | throw new WorkflowException("Target field (" + field + ") is not JsonNode"); |
| 262 | } |
| 263 | |
| 264 | try { |
| 265 | field.setAccessible(true); |
| 266 | field.set(worklet, jsonNode); |
| 267 | } catch (IllegalAccessException e) { |
| 268 | throw new WorkflowException(e); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Injects json array node data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 274 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 275 | * @param worklet work-let |
| 276 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 277 | * @param field the field of work-let |
| 278 | * @param model json array node data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 279 | * @throws WorkflowException workflow exception |
| 280 | */ |
| 281 | private static void injectArrayNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 282 | throws WorkflowException { |
| 283 | |
| 284 | ArrayNode arrayNode = ((JsonDataModelTree) context.data()).arrayAt(model.path()); |
| 285 | if (Objects.isNull(arrayNode)) { |
| 286 | if (model.optional()) { |
| 287 | return; |
| 288 | } |
| 289 | throw new WorkflowException("Invalid array node data model on (" + model.path() + ")"); |
| 290 | } |
| 291 | |
| 292 | if (!(Objects.equals(field.getType(), ArrayNode.class))) { |
| 293 | throw new WorkflowException("Target field (" + field + ") is not ArrayNode"); |
| 294 | } |
| 295 | |
| 296 | try { |
| 297 | field.setAccessible(true); |
| 298 | field.set(worklet, arrayNode); |
| 299 | } catch (IllegalAccessException e) { |
| 300 | throw new WorkflowException(e); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Injects json object node data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 306 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 307 | * @param worklet work-let |
| 308 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 309 | * @param field the field of work-let |
| 310 | * @param model json object node data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 311 | * @throws WorkflowException workflow exception |
| 312 | */ |
| 313 | private static void injectObjectNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 314 | throws WorkflowException { |
| 315 | |
| 316 | ObjectNode objNode = ((JsonDataModelTree) context.data()).objectAt(model.path()); |
| 317 | if (Objects.isNull(objNode)) { |
| 318 | if (model.optional()) { |
| 319 | return; |
| 320 | } |
| 321 | throw new WorkflowException("Invalid object node data model on (" + model.path() + ")"); |
| 322 | } |
| 323 | |
| 324 | if (!(Objects.equals(field.getType(), ObjectNode.class))) { |
| 325 | throw new WorkflowException("Target field (" + field + ") is not ObjectNode"); |
| 326 | } |
| 327 | |
| 328 | try { |
| 329 | field.setAccessible(true); |
| 330 | field.set(worklet, objNode); |
| 331 | } catch (IllegalAccessException e) { |
| 332 | throw new WorkflowException(e); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | private static Map<Class, DataModelFieldBehavior> inhaleTypeMap = new HashMap<>(); |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 337 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 338 | static { |
| 339 | inhaleTypeMap.put(String.class, JsonDataModelInjector::inhaleText); |
| 340 | inhaleTypeMap.put(Integer.class, JsonDataModelInjector::inhaleInteger); |
| 341 | inhaleTypeMap.put(Boolean.class, JsonDataModelInjector::inhaleBoolean); |
| 342 | inhaleTypeMap.put(JsonNode.class, JsonDataModelInjector::inhaleJsonNode); |
| 343 | inhaleTypeMap.put(ArrayNode.class, JsonDataModelInjector::inhaleArrayNode); |
| 344 | inhaleTypeMap.put(ObjectNode.class, JsonDataModelInjector::inhaleObjectNode); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Inhales data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 349 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 350 | * @param worklet work-let |
| 351 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 352 | * @param field the field of work-let |
| 353 | * @param model data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 354 | * @throws WorkflowException workflow exception |
| 355 | */ |
| 356 | private void inhaleModel(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 357 | throws WorkflowException { |
| 358 | |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 359 | DataModelFieldBehavior behavior = inhaleTypeMap.get(field.getType()); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 360 | if (Objects.isNull(behavior)) { |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 361 | throw new WorkflowException("Not supported type(" + field.getType() + ")"); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 362 | } |
| 363 | behavior.apply(worklet, context, field, model); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Inhales text data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 368 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 369 | * @param worklet work-let |
| 370 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 371 | * @param field the field of work-let |
| 372 | * @param model text data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 373 | * @throws WorkflowException workflow exception |
| 374 | */ |
| 375 | private static void inhaleText(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 376 | throws WorkflowException { |
| 377 | |
| 378 | if (!(Objects.equals(field.getType(), String.class))) { |
| 379 | throw new WorkflowException("Target field (" + field + ") is not String"); |
| 380 | } |
| 381 | |
| 382 | String text; |
| 383 | try { |
| 384 | field.setAccessible(true); |
| 385 | text = (String) field.get(worklet); |
| 386 | } catch (IllegalAccessException e) { |
| 387 | throw new WorkflowException(e); |
| 388 | } |
| 389 | |
| 390 | if (Objects.isNull(text)) { |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | JsonDataModelTree tree = (JsonDataModelTree) context.data(); |
| 395 | JsonNode jsonNode = tree.nodeAt(model.path()); |
| 396 | |
| 397 | if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) { |
| 398 | tree.setAt(model.path(), text); |
| 399 | } else if (!(jsonNode instanceof TextNode)) { |
| 400 | throw new WorkflowException("Invalid text data model on (" + model.path() + ")"); |
| 401 | } else { |
| 402 | tree.remove(model.path()); |
| 403 | tree.setAt(model.path(), text); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Inhales integer data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 409 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 410 | * @param worklet work-let |
| 411 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 412 | * @param field the field of work-let |
| 413 | * @param model integer data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 414 | * @throws WorkflowException workflow exception |
| 415 | */ |
| 416 | private static void inhaleInteger(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 417 | throws WorkflowException { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 418 | if (!(Objects.equals(field.getType(), Integer.class))) { |
| 419 | throw new WorkflowException("Target field (" + field + ") is not Integer"); |
| 420 | } |
| 421 | |
| 422 | Integer number; |
| 423 | try { |
| 424 | field.setAccessible(true); |
| 425 | number = (Integer) field.get(worklet); |
| 426 | } catch (IllegalAccessException e) { |
| 427 | throw new WorkflowException(e); |
| 428 | } |
| 429 | |
| 430 | if (Objects.isNull(number)) { |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | JsonDataModelTree tree = (JsonDataModelTree) context.data(); |
| 435 | JsonNode jsonNode = tree.nodeAt(model.path()); |
| 436 | |
| 437 | if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) { |
| 438 | tree.setAt(model.path(), number); |
| 439 | } else if (!(jsonNode instanceof IntNode)) { |
| 440 | throw new WorkflowException("Invalid integer data model on (" + model.path() + ")"); |
| 441 | } else { |
| 442 | tree.remove(model.path()); |
| 443 | tree.setAt(model.path(), number); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Inhales boolean data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 449 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 450 | * @param worklet work-let |
| 451 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 452 | * @param field the field of work-let |
| 453 | * @param model boolean data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 454 | * @throws WorkflowException workflow exception |
| 455 | */ |
| 456 | private static void inhaleBoolean(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 457 | throws WorkflowException { |
| 458 | |
| 459 | if (!(Objects.equals(field.getType(), Boolean.class))) { |
| 460 | throw new WorkflowException("Target field (" + field + ") is not Boolean"); |
| 461 | } |
| 462 | |
| 463 | Boolean bool; |
| 464 | try { |
| 465 | field.setAccessible(true); |
| 466 | bool = (Boolean) field.get(worklet); |
| 467 | } catch (IllegalAccessException e) { |
| 468 | throw new WorkflowException(e); |
| 469 | } |
| 470 | |
| 471 | if (Objects.isNull(bool)) { |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | JsonDataModelTree tree = (JsonDataModelTree) context.data(); |
| 476 | JsonNode jsonNode = tree.nodeAt(model.path()); |
| 477 | |
| 478 | if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) { |
| 479 | tree.setAt(model.path(), bool); |
| 480 | } else if (!(jsonNode instanceof BooleanNode)) { |
| 481 | throw new WorkflowException("Invalid boolean data model on (" + model.path() + ")"); |
| 482 | } else { |
| 483 | tree.remove(model.path()); |
| 484 | tree.setAt(model.path(), bool); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Inhales json node data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 490 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 491 | * @param worklet work-let |
| 492 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 493 | * @param field the field of work-let |
| 494 | * @param model json node data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 495 | * @throws WorkflowException workflow exception |
| 496 | */ |
| 497 | private static void inhaleJsonNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 498 | throws WorkflowException { |
| 499 | |
| 500 | if (!(Objects.equals(field.getType(), JsonNode.class))) { |
| 501 | throw new WorkflowException("Target field (" + field + ") is not JsonNode"); |
| 502 | } |
| 503 | |
| 504 | JsonNode tgtJsonNode; |
| 505 | try { |
| 506 | field.setAccessible(true); |
| 507 | tgtJsonNode = (JsonNode) field.get(worklet); |
| 508 | } catch (IllegalAccessException e) { |
| 509 | throw new WorkflowException(e); |
| 510 | } |
| 511 | |
| 512 | if (Objects.isNull(tgtJsonNode)) { |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | JsonDataModelTree tree = (JsonDataModelTree) context.data(); |
| 517 | JsonNode jsonNode = tree.nodeAt(model.path()); |
| 518 | |
| 519 | if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) { |
| 520 | tree.attach(model.path(), new JsonDataModelTree(tgtJsonNode)); |
| 521 | } else if (!(jsonNode instanceof JsonNode)) { |
| 522 | throw new WorkflowException("Invalid json node data model on (" + model.path() + ")"); |
| 523 | } else { |
| 524 | // do nothing |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Inhales json array node data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 530 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 531 | * @param worklet work-let |
| 532 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 533 | * @param field the field of work-let |
| 534 | * @param model json array node data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 535 | * @throws WorkflowException workflow exception |
| 536 | */ |
| 537 | private static void inhaleArrayNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 538 | throws WorkflowException { |
| 539 | if (!(Objects.equals(field.getType(), ArrayNode.class))) { |
| 540 | throw new WorkflowException("Target field (" + field + ") is not ArrayNode"); |
| 541 | } |
| 542 | |
| 543 | ArrayNode tgtArrayNode; |
| 544 | try { |
| 545 | field.setAccessible(true); |
| 546 | tgtArrayNode = (ArrayNode) field.get(worklet); |
| 547 | } catch (IllegalAccessException e) { |
| 548 | throw new WorkflowException(e); |
| 549 | } |
| 550 | |
| 551 | if (Objects.isNull(tgtArrayNode)) { |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | JsonDataModelTree tree = (JsonDataModelTree) context.data(); |
| 556 | JsonNode jsonNode = tree.nodeAt(model.path()); |
| 557 | |
| 558 | if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) { |
| 559 | tree.attach(model.path(), new JsonDataModelTree(tgtArrayNode)); |
| 560 | } else if (!(jsonNode instanceof ArrayNode)) { |
| 561 | throw new WorkflowException("Invalid array node data model on (" + model.path() + ")"); |
| 562 | } else { |
| 563 | // do nothing |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Inhales json object node data model on the filed of work-let. |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 569 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 570 | * @param worklet work-let |
| 571 | * @param context workflow context |
mohamedrahilr | a4545fb | 2019-02-07 18:43:01 +0530 | [diff] [blame] | 572 | * @param field the field of work-let |
| 573 | * @param model json object node data model for the field |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 574 | * @throws WorkflowException workflow exception |
| 575 | */ |
| 576 | private static void inhaleObjectNode(Worklet worklet, WorkflowContext context, Field field, JsonDataModel model) |
| 577 | throws WorkflowException { |
| 578 | if (!(Objects.equals(field.getType(), ObjectNode.class))) { |
| 579 | throw new WorkflowException("Target field (" + field + ") is not ObjectNode"); |
| 580 | } |
| 581 | |
| 582 | ObjectNode tgtObjNode; |
| 583 | try { |
| 584 | field.setAccessible(true); |
| 585 | tgtObjNode = (ObjectNode) field.get(worklet); |
| 586 | } catch (IllegalAccessException e) { |
| 587 | throw new WorkflowException(e); |
| 588 | } |
| 589 | |
| 590 | if (Objects.isNull(tgtObjNode)) { |
| 591 | return; |
| 592 | } |
| 593 | |
| 594 | JsonDataModelTree tree = (JsonDataModelTree) context.data(); |
| 595 | JsonNode jsonNode = tree.nodeAt(model.path()); |
| 596 | |
| 597 | if (Objects.isNull(jsonNode) || jsonNode instanceof MissingNode) { |
| 598 | tree.attach(model.path(), new JsonDataModelTree(tgtObjNode)); |
| 599 | } else if (!(jsonNode instanceof ObjectNode)) { |
| 600 | throw new WorkflowException("Invalid object node data model on (" + model.path() + ")"); |
| 601 | } else { |
| 602 | // do nothing |
| 603 | } |
| 604 | } |
| 605 | } |