Commit 9b3b8a29 by Mikhail Gashenko

JsonNode: (draft5) new node value search and update with ObjectMapper

parent a91e1e7d
...@@ -7,6 +7,7 @@ package kz.arta.synergy.astdev.custom_bp; ...@@ -7,6 +7,7 @@ package kz.arta.synergy.astdev.custom_bp;
import java.util.List; import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty; import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.node.ObjectNode;
/** /**
* *
...@@ -25,6 +26,10 @@ public class AsNode { ...@@ -25,6 +26,10 @@ public class AsNode {
private String value; private String value;
@JsonProperty("data") @JsonProperty("data")
private List<AsNode> data; private List<AsNode> data;
@JsonProperty("formatVersion")
private String formatVersion;
@JsonProperty("manualTags")
private ObjectNode manualTags;
public String getId() { public String getId() {
return id; return id;
...@@ -74,4 +79,20 @@ public class AsNode { ...@@ -74,4 +79,20 @@ public class AsNode {
this.data = data; this.data = data;
} }
public String getFormatVersion() {
return formatVersion;
}
public void setFormatVersion(String formatVersion) {
this.formatVersion = formatVersion;
}
public ObjectNode getManualTags() {
return manualTags;
}
public void setManualTags(ObjectNode manualTags) {
this.manualTags = manualTags;
}
} }
...@@ -29,6 +29,10 @@ import org.codehaus.jackson.node.ArrayNode; ...@@ -29,6 +29,10 @@ import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode; import org.codehaus.jackson.node.ObjectNode;
import org.codehaus.jackson.type.TypeReference; import org.codehaus.jackson.type.TypeReference;
/**
*
* @author drpsy
*/
@MessageDriven(name = "CustomBP", activationConfig = { @MessageDriven(name = "CustomBP", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:jboss/queues/Integration/CustomBP"), @ActivationConfigProperty(propertyName = "destination", propertyValue = "java:jboss/queues/Integration/CustomBP"),
...@@ -52,6 +56,10 @@ public class Main implements MessageListener { ...@@ -52,6 +56,10 @@ public class Main implements MessageListener {
private String executionID = null; // идентификатор блокирующего процесса private String executionID = null; // идентификатор блокирующего процесса
private String documentID = null; // идентификатор документа реестра private String documentID = null; // идентификатор документа реестра
/**
*
* @param message
*/
@Override @Override
public void onMessage(Message message) { public void onMessage(Message message) {
if (!(message instanceof TextMessage)) { if (!(message instanceof TextMessage)) {
...@@ -88,11 +96,13 @@ public class Main implements MessageListener { ...@@ -88,11 +96,13 @@ public class Main implements MessageListener {
URL sourceFormURL = new URL("http://127.0.0.1:8080/Synergy/rest/api/asforms/data/" + dataUUID); URL sourceFormURL = new URL("http://127.0.0.1:8080/Synergy/rest/api/asforms/data/" + dataUUID);
String sourceFormData = synergyApiGetString(sourceFormURL); String sourceFormData = synergyApiGetString(sourceFormURL);
// Получение ID пользователя // Получение ID пользователя указанного в форме
String userID = getComponentValueByID(sourceFormData, "user1", fields.KEY); String userID = getComponentValueByID(sourceFormData, "user1", fields.KEY);
// По ID пользователя DataUUID целевой карточки // По ID пользователя указанного в карточке и ID формы получаем DataUUID целевой карточки
String userCardDataUUID = getCardDataUUID(userID, "04f7809d-f44c-4a2d-950d-6aa8e6c3fea1"); // String userCardDataUUID = getCardDataUUID(userID, "04f7809d-f44c-4a2d-950d-6aa8e6c3fea1");
String userCardDataUUID = getCardDataUUID(userID, "fb44d99d-1579-4ac3-884a-01a6d4f71f9c");
// Получение данных целевой карточки по dataUUID // Получение данных целевой карточки по dataUUID
URL targetFormURL = new URL("http://127.0.0.1:8080/Synergy/rest/api/asforms/data/" + userCardDataUUID); URL targetFormURL = new URL("http://127.0.0.1:8080/Synergy/rest/api/asforms/data/" + userCardDataUUID);
String targetFormData = synergyApiGetString(targetFormURL); String targetFormData = synergyApiGetString(targetFormURL);
...@@ -154,18 +164,32 @@ public class Main implements MessageListener { ...@@ -154,18 +164,32 @@ public class Main implements MessageListener {
*/ */
private static AsNode findNode(List<AsNode> nodesList, String componentID) { private static AsNode findNode(List<AsNode> nodesList, String componentID) {
AsNode result = null; AsNode result = null;
for (AsNode n : nodesList) { for (AsNode n : nodesList) {
if (componentID.equals(n.getId())) { if (componentID.equals(n.getId())) {
result = n; result = n;
break; break;
} }
if(!n.getData().isEmpty()) { if(n.getData() != null) {
findNode(n.getData(), componentID); result = findNode(n.getData(), componentID);
if (result != null) {
break;
} }
} }
}
return result; return result;
} }
/**
* Setting new value of requested field of requested component with specified ID
* @param targetJsonAsString
* @param componentID
* @param fieldName
* @param newFieldValue
* @throws AttributeModificationException
* @throws IOException
*/
private void updateFieldValue (String targetJsonAsString, String componentID, fields fieldName, String newFieldValue) throws AttributeModificationException, IOException { private void updateFieldValue (String targetJsonAsString, String componentID, fields fieldName, String newFieldValue) throws AttributeModificationException, IOException {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
try { try {
...@@ -199,8 +223,6 @@ public class Main implements MessageListener { ...@@ -199,8 +223,6 @@ public class Main implements MessageListener {
case VALUE: case VALUE:
targetNode.setValue(newFieldValue); targetNode.setValue(newFieldValue);
break; break;
default: // TODO
break;
} }
} }
} catch (AttributeModificationException e) { } catch (AttributeModificationException e) {
...@@ -233,7 +255,16 @@ public class Main implements MessageListener { ...@@ -233,7 +255,16 @@ public class Main implements MessageListener {
return new String(); return new String();
} }
private static String getComponentValueByID(String targetJsonAsString, String componentID, fields fieldName) throws NameNotFoundException, IOException { /**
* Returns value of of specified field of component with specified ID
* @param targetJsonAsString
* @param componentID
* @param fieldName
* @return
* @throws NameNotFoundException
* @throws IOException
*/
private static String getComponentValueByID(String targetJsonAsString, String componentID, fields fieldName) throws Exception {
// String b2b1 = getComponentValueByID(sourceFormData, "b2-b1-b1", fieldType.DATE); // String b2b1 = getComponentValueByID(sourceFormData, "b2-b1-b1", fieldType.DATE);
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
String result = null; String result = null;
...@@ -241,17 +272,17 @@ public class Main implements MessageListener { ...@@ -241,17 +272,17 @@ public class Main implements MessageListener {
try { try {
JsonNode rootNode = objectMapper.readTree(targetJsonAsString); // read the whole tree JsonNode rootNode = objectMapper.readTree(targetJsonAsString); // read the whole tree
if (rootNode.isNull()) { if (rootNode.isNull()) {
throw new NameNotFoundException("GMP: Passed data has no JSON content"); throw new Exception("GMP: Passed data has no JSON content");
} }
JsonNode rootDataNode = rootNode.get("data"); // read single root 'data' node JsonNode rootDataNode = rootNode.get("data"); // read single root 'data' node
if (rootDataNode == null) { if (rootDataNode == null) {
throw new NameNotFoundException("GMP: Invalid form data, root \"data\" node does not exists"); throw new Exception("GMP: Invalid form data, root \"data\" node does not exists");
} }
List<AsNode> dataNodesList = objectMapper.readValue(rootDataNode, new TypeReference<List<AsNode>>() {}); // map content of single root 'data' node List<AsNode> dataNodesList = objectMapper.readValue(rootDataNode, new TypeReference<List<AsNode>>() {}); // map content of single root 'data' node
AsNode targetNode = findNode(dataNodesList, componentID); AsNode targetNode = findNode(dataNodesList, componentID);
if (targetNode == null) { if (targetNode == null) {
throw new NameNotFoundException("GMP: Node with specified ID is not found in passed JSON"); throw new Exception("GMP: Node with specified ID is not found in passed JSON");
} else { } else {
switch (fieldName) { switch (fieldName) {
case ID: case ID:
...@@ -272,13 +303,12 @@ public class Main implements MessageListener { ...@@ -272,13 +303,12 @@ public class Main implements MessageListener {
} }
} }
return result; return result;
} catch (NameNotFoundException e) { } catch (Exception e) {
throw e; throw e;
} }
} }
/** /**
* Route unlocking * Route unlocking
*/ */
...@@ -287,43 +317,12 @@ public class Main implements MessageListener { ...@@ -287,43 +317,12 @@ public class Main implements MessageListener {
String address = "http://127.0.0.1:8080/Synergy"; String address = "http://127.0.0.1:8080/Synergy";
String signal = "got_agree"; String signal = "got_agree";
URL url = new URL(address + "/rest/api/processes/signal?signal=" + signal + "&executionID=" + this.executionID + "&param1=resolution&value1=signal_is_" + signal); URL url = new URL(address + "/rest/api/processes/signal?signal=" + signal + "&executionID=" + this.executionID + "&param1=resolution&value1=signal_is_" + signal);
synergyApiGet(url); synergyApiGetString(url);
} catch (Exception exc) { } catch (Exception exc) {
LOGGER.error(exc.getMessage(), exc); LOGGER.error(exc.getMessage(), exc);
} }
} }
private static JsonParser synergyApiGet(final URL requestURL) {
String login = "111111";
String password = "1";
try {
String output;
HttpURLConnection conn = (HttpURLConnection) requestURL.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json; charset=utf-8");
String encoded = Base64.encode((login + ":" + password).getBytes());
conn.setRequestProperty("Authorization", "Basic " + encoded);
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
StringBuilder result = new StringBuilder();
while ((output = br.readLine()) != null) {
result.append(output);
}
conn.disconnect();
JsonFactory factory = new JsonFactory();
return factory.createParser(result.toString());
} catch (Exception exc) {
LOGGER.error(exc.getMessage(), exc);
}
return null;
}
private static String synergyApiGetString(final URL requestURL) { private static String synergyApiGetString(final URL requestURL) {
String login = "111111"; String login = "111111";
String password = "1"; String password = "1";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment