Support convert json object to Java BPMN object 75/14975/1
authorLvbo163 <lv.bo163@zte.com.cn>
Mon, 25 Sep 2017 08:39:41 +0000 (16:39 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Mon, 25 Sep 2017 08:39:41 +0000 (16:39 +0800)
Issue-ID: SDC-367

Change-Id: I69d3932536fb46c52cc5f4b53e59776fc9977d27
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/parser/Bpmn4ToscaJsonParser.java [new file with mode: 0644]

diff --git a/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/parser/Bpmn4ToscaJsonParser.java b/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/parser/Bpmn4ToscaJsonParser.java
new file mode 100644 (file)
index 0000000..81b41d9
--- /dev/null
@@ -0,0 +1,71 @@
+/**\r
+ * Copyright (c) 2017 ZTE Corporation.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * and the Apache License 2.0 which both accompany this distribution,\r
+ * and are available at http://www.eclipse.org/legal/epl-v10.html\r
+ * and http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Contributors:\r
+ *     ZTE - initial API and implementation and/or initial documentation\r
+ */\r
+package org.onap.sdc.workflowdesigner.parser;\r
+\r
+import java.io.IOException;\r
+import java.net.MalformedURLException;\r
+import java.net.URI;\r
+import java.util.Iterator;\r
+\r
+import org.onap.sdc.workflowdesigner.model.Element;\r
+import org.onap.sdc.workflowdesigner.model.Process;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import com.fasterxml.jackson.core.JsonParseException;\r
+import com.fasterxml.jackson.databind.DeserializationFeature;\r
+import com.fasterxml.jackson.databind.JsonMappingException;\r
+import com.fasterxml.jackson.databind.JsonNode;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.fasterxml.jackson.databind.SerializationFeature;\r
+\r
+public class Bpmn4ToscaJsonParser {\r
+\r
+       private static Logger log = LoggerFactory.getLogger(Bpmn4ToscaJsonParser.class);\r
+       \r
+       private static ObjectMapper MAPPER = new ObjectMapper();\r
+       \r
+       static {\r
+               MAPPER.enable(SerializationFeature.INDENT_OUTPUT);\r
+               MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);\r
+       }\r
+       \r
+       public Process parse(String processName, URI jsonFileUrl) throws JsonParseException, JsonMappingException, MalformedURLException, IOException {\r
+               Process process = new Process(processName);\r
+\r
+               JsonNode rootNode = MAPPER.readValue(jsonFileUrl.toURL(), JsonNode.class);\r
+\r
+               log.debug("Creating Process models...");\r
+               JsonNode nodes = rootNode.get("nodes");\r
+               if(nodes == null) {\r
+                       return process;\r
+               }\r
+               \r
+               Iterator<JsonNode> iter = nodes.iterator();\r
+               while (iter.hasNext()) {\r
+                       JsonNode jsonNode = (JsonNode) iter.next();\r
+\r
+                       // get element\r
+                       Element element = createElementFromJson(jsonNode);\r
+                       process.getElementList().add(element);\r
+               }\r
+\r
+               return process;\r
+\r
+       }\r
+       \r
+       protected Element createElementFromJson(JsonNode jsonNode) throws JsonParseException, JsonMappingException, IOException {\r
+               String jsonObject = jsonNode.toString();\r
+               return MAPPER.readValue(jsonObject, Element.class);\r
+       }\r
+       \r
+}\r