|
| 1 | +/* |
| 2 | + * Copyright 2020-Present The Serverless Workflow Specification Authors |
| 3 | + * <p> |
| 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 | + * <p> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 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 | + */ |
| 17 | +package io.serverlessworkflow.api.deserializers; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.core.JsonParser; |
| 20 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 21 | +import com.fasterxml.jackson.databind.JsonNode; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
| 24 | +import io.serverlessworkflow.api.end.End; |
| 25 | +import io.serverlessworkflow.api.interfaces.WorkflowPropertySource; |
| 26 | +import io.serverlessworkflow.api.produce.ProduceEvent; |
| 27 | +import io.serverlessworkflow.api.start.Start; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +public class EndDefinitionDeserializer extends StdDeserializer<End> { |
| 36 | + |
| 37 | + private static final long serialVersionUID = 510l; |
| 38 | + private static Logger logger = LoggerFactory.getLogger(EndDefinitionDeserializer.class); |
| 39 | + |
| 40 | + private WorkflowPropertySource context; |
| 41 | + |
| 42 | + public EndDefinitionDeserializer() { |
| 43 | + this(End.class); |
| 44 | + } |
| 45 | + |
| 46 | + public EndDefinitionDeserializer(Class<?> vc) { |
| 47 | + super(vc); |
| 48 | + } |
| 49 | + |
| 50 | + public EndDefinitionDeserializer(WorkflowPropertySource context) { |
| 51 | + this(Start.class); |
| 52 | + this.context = context; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public End deserialize(JsonParser jp, |
| 57 | + DeserializationContext ctxt) throws IOException { |
| 58 | + |
| 59 | + ObjectMapper mapper = (ObjectMapper) jp.getCodec(); |
| 60 | + JsonNode node = jp.getCodec().readTree(jp); |
| 61 | + |
| 62 | + End end = new End(); |
| 63 | + |
| 64 | + if (node.isBoolean()) { |
| 65 | + end.setProduceEvents(null); |
| 66 | + end.setCompensate(false); |
| 67 | + end.setTerminate(false); |
| 68 | + return node.asBoolean() ? end : null; |
| 69 | + } else { |
| 70 | + if(node.get("produceEvents") != null) { |
| 71 | + List<ProduceEvent> produceEvents= new ArrayList<>(); |
| 72 | + for (final JsonNode nodeEle : node.get("produceEvents")) { |
| 73 | + produceEvents.add(mapper.treeToValue(nodeEle, ProduceEvent.class)); |
| 74 | + } |
| 75 | + end.setProduceEvents(produceEvents); |
| 76 | + } |
| 77 | + |
| 78 | + if(node.get("terminate") != null) { |
| 79 | + end.setTerminate(node.get("terminate").asBoolean()); |
| 80 | + } else { |
| 81 | + end.setTerminate(false); |
| 82 | + } |
| 83 | + |
| 84 | + if(node.get("compensate") != null) { |
| 85 | + end.setCompensate(node.get("compensate").asBoolean()); |
| 86 | + } else { |
| 87 | + end.setCompensate(false); |
| 88 | + } |
| 89 | + |
| 90 | + return end; |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | +} |
0 commit comments