Commit a36982f5 by everdarkgreen

JsonNode: (draft2) node value update

parent 054deba7
......@@ -32,6 +32,11 @@
<version>1.1-rev-1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package kz.arta.synergy.astdev.custom_bp;
import org.codehaus.jackson.annotate.JsonProperty;
/**
*
* @author everdarkgreen
*/
public class Card {
@JsonProperty("form-uuid")
private String formUUID;
@JsonProperty("formCode")
private String formCode;
@JsonProperty("name")
private String name;
@JsonProperty("editable")
private boolean editable;
@JsonProperty("data-uuid")
private String dataUUID;
@JsonProperty("mandatory")
private boolean mandatory;
/**
* @return the formUUID
*/
public String getFormUUID() {
return formUUID;
}
/**
* @return the formCode
*/
public String getFormCode() {
return formCode;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the editable
*/
public boolean isEditable() {
return editable;
}
/**
* @return the dataUUID
*/
public String getDataUUID() {
return dataUUID;
}
/**
* @return the mandatory
*/
public boolean isMandatory() {
return mandatory;
}
}
......@@ -25,6 +25,7 @@ import java.util.Iterator;
import java.util.List;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.naming.directory.InvalidAttributesException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
......@@ -81,7 +82,7 @@ public class Main implements MessageListener {
}
}
// Получение данных исходной формы
// Получение данных исходной формы (той, по которой запущен маршрут)
URL sourceFormURL = new URL("http://127.0.0.1:8080/Synergy/rest/api/asforms/data/" + dataUUID);
String sourceFormData = synergyApiGetString(sourceFormURL);
......@@ -89,27 +90,48 @@ public class Main implements MessageListener {
String userID = getComponentValueByID(sourceFormData, "user1", fieldType.USER);
// По ID пользователя DataUUID целевой карточки
String userCardDataUUID = findDataUuidByUserID(userID, "04f7809d-f44c-4a2d-950d-6aa8e6c3fea1");
String userCardDataUUID = getCardDataUUID(userID, "04f7809d-f44c-4a2d-950d-6aa8e6c3fea1");
// Получение данных целевой карточки по dataUUID
URL targetFormURL = new URL("http://127.0.0.1:8080/Synergy/rest/api/asforms/data/" + userCardDataUUID);
String targetFormData = synergyApiGetString(sourceFormURL);
String targetFormData = synergyApiGetString(targetFormURL);
// Получение значений текущих дат исходной формы
String b2b1, b4b1, b5b1;
String b5b1;
String t4b1, t6b1, t2b1;
String t10b1, t12b1, t8b1;
String t2b1;
String t8b1;
String t18b1, t20b1, t16b1;
String t26b1, t24b1, t22b1;
String t32b1, t30b1, t28b1;
// String t18b1, t20b1, t16b1;
// String t26b1, t24b1, t22b1;
// String t32b1, t30b1, t28b1;
b2b1 = getComponentValueByID(sourceFormData, "b2-b1-b1", fieldType.DATE);
b4b1 = getComponentValueByID(sourceFormData, "b4-b1", fieldType.DATE);
b5b1 = getComponentValueByID(sourceFormData, "b5-b1", fieldType.TEXTBOX);
// first table
String b2b1 = getComponentValueByID(sourceFormData, "b2-b1", fieldType.DATE);
String b4b1 = getComponentValueByID(sourceFormData, "b4-b1", fieldType.DATE);
if (!b2b1.isEmpty() && !b4b1.isEmpty()) {
setComponentValueByID(targetFormData, "b2-b1-b1", "key", b2b1);
setComponentValueByID(targetFormData, "b4-b1-b1", "key", b4b1);
unlockRoute();
return;
}
t4b1 = getComponentValueByID(sourceFormData, "b2-b1", fieldType.DATE);
// Second table
// String t4b1 = getComponentValueByID(sourceFormData, "t2-b1", fieldType.DATE);
// String t6b1 = getComponentValueByID(sourceFormData, "b6-b1", fieldType.DATE);
// String t10b1 = getComponentValueByID(sourceFormData, "t10-b1", fieldType.DATE);
// String t12b1 = getComponentValueByID(sourceFormData, "t12-b1", fieldType.DATE);
//
// if (!t4b1.isEmpty() && !t6b1.isEmpty() && !t10b1.isEmpty() && !t12b1.isEmpty()) {
// setComponentValueByID(targetFormData, "start-b1", "key", b2b1);
// setComponentValueByID(targetFormData, "start-b1", "key", b2b1);
// setComponentValueByID(targetFormData, "start-b1", "key", b2b1);
// setComponentValueByID(targetFormData, "start-b1", "key", b2b1);
//
// unlockRoute();
// return;
// }
// Разблокировка маршрута
unlockRoute();
......@@ -119,13 +141,33 @@ public class Main implements MessageListener {
}
}
private void setComponentValueByID (String formJson, String oldFieldName, String newFieldName) {
private void setComponentValueByID (String targetJsonAsString, String componentID, String fieldName, String newFieldValue) throws InvalidAttributesException {
ObjectMapper mapper = new ObjectMapper();
String result = new String();
try {
JsonNode rootNode = mapper.readTree(formJson);
JsonNode rootNode = mapper.readTree(targetJsonAsString);
ArrayNode rootDN = (ArrayNode) rootNode.get("data");
Iterator<JsonNode> rootDNIterator = rootDN.getElements();
// searching for field value in root 'data' node and setting new value if found
JsonNode node = searchInNode2(rootDNIterator, componentID, fieldName);
// setting node value
if (node != null) {
setValueInNode(node, componentID, newFieldValue, componentID);
return;
}
Iterator<JsonNode> rootDNIterator2 = rootDN.getElements(); // 'reset' iterator (create new one)
while (rootDNIterator2.hasNext()) {
ArrayNode childNode = (ArrayNode) rootDNIterator2.next().get("data"); // get next 'data'
Iterator<JsonNode> childDNIterator = childNode.getElements();
node = searchInNode2(childDNIterator, componentID, fieldName);
}
if (node != null) {
setValueInNode(node, componentID, newFieldValue, componentID);
}
} catch (Exception exc) {
LOGGER.error("GMP: Invalid card data");
......@@ -144,7 +186,8 @@ public class Main implements MessageListener {
}
}
private static String findDataUuidByUserID(String userID, String formUUID) {
// get user's card data uuid by formuuid
private static String getCardDataUUID (String userID, String formUUID) {
ObjectMapper mapper = new ObjectMapper();
try {
URL url = new URL("http://127.0.0.1:8080/Synergy/rest/api/personalrecord/forms/" + userID);
......@@ -163,18 +206,16 @@ public class Main implements MessageListener {
return new String();
}
private static void replaceValueInNode(Iterator<JsonNode> iter, String fieldName, String newFieldValue) {
private static void setValueInNode (JsonNode node, String componentID, String newFieldValue, String componentFieldName) throws InvalidAttributesException {
if (node == null) {
throw new InvalidAttributesException("JsonNode cannot be null");
}
try {
while (iter.hasNext()) {
JsonNode n = iter.next();
if (n.has("id")) {
if (fieldName.equals(n.get("id").asText())) {
((ObjectNode) n).put("key", newFieldValue);
}
}
}
ObjectNode objectNode = (ObjectNode) node;
objectNode.put(componentFieldName, newFieldValue);
} catch (Exception exc) {
LOGGER.error(exc.getMessage(), exc);
throw exc;
}
}
......@@ -201,6 +242,21 @@ public class Main implements MessageListener {
return result;
}
private static JsonNode searchInNode2(Iterator<JsonNode> iter, String componentID, String componentFieldName) {
while (iter.hasNext()) {
// search data through content of child 'data' nodes
JsonNode n = iter.next();
if (n.has("id") && n.has(componentFieldName)) {
if (componentID.equals(n.get("id").asText())) {
return n;
}
}
}
return null;
}
private static String getComponentValueByID(String formJson, String fieldName, fieldType ft) {
ObjectMapper mapper = new ObjectMapper();
String result = new String();
......@@ -219,7 +275,7 @@ public class Main implements MessageListener {
}
// root 'data' value does not contain the expected value, search in second (last possible) 'data'
Iterator<JsonNode> rootDNIterator2 = rootDN.getElements();
Iterator<JsonNode> rootDNIterator2 = rootDN.getElements(); // 'reset' iterator (create new one)
while (rootDNIterator2.hasNext()) {
ArrayNode childNode = (ArrayNode) rootDNIterator2.next().get("data");
Iterator<JsonNode> childDNIterator = childNode.getElements();
......
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