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;
import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.node.ObjectNode;
/**
*
......@@ -25,6 +26,10 @@ public class AsNode {
private String value;
@JsonProperty("data")
private List<AsNode> data;
@JsonProperty("formatVersion")
private String formatVersion;
@JsonProperty("manualTags")
private ObjectNode manualTags;
public String getId() {
return id;
......@@ -73,5 +78,21 @@ public class AsNode {
public void setData(List<AsNode> 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;
import org.codehaus.jackson.node.ObjectNode;
import org.codehaus.jackson.type.TypeReference;
/**
*
* @author drpsy
*/
@MessageDriven(name = "CustomBP", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "java:jboss/queues/Integration/CustomBP"),
......@@ -52,6 +56,10 @@ public class Main implements MessageListener {
private String executionID = null; // идентификатор блокирующего процесса
private String documentID = null; // идентификатор документа реестра
/**
*
* @param message
*/
@Override
public void onMessage(Message message) {
if (!(message instanceof TextMessage)) {
......@@ -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);
String sourceFormData = synergyApiGetString(sourceFormURL);
// Получение ID пользователя
// Получение ID пользователя указанного в форме
String userID = getComponentValueByID(sourceFormData, "user1", fields.KEY);
// По ID пользователя DataUUID целевой карточки
String userCardDataUUID = getCardDataUUID(userID, "04f7809d-f44c-4a2d-950d-6aa8e6c3fea1");
// По ID пользователя указанного в карточке и ID формы получаем DataUUID целевой карточки
// String userCardDataUUID = getCardDataUUID(userID, "04f7809d-f44c-4a2d-950d-6aa8e6c3fea1");
String userCardDataUUID = getCardDataUUID(userID, "fb44d99d-1579-4ac3-884a-01a6d4f71f9c");
// Получение данных целевой карточки по dataUUID
URL targetFormURL = new URL("http://127.0.0.1:8080/Synergy/rest/api/asforms/data/" + userCardDataUUID);
String targetFormData = synergyApiGetString(targetFormURL);
......@@ -154,18 +164,32 @@ public class Main implements MessageListener {
*/
private static AsNode findNode(List<AsNode> nodesList, String componentID) {
AsNode result = null;
for (AsNode n : nodesList) {
if (componentID.equals(n.getId())) {
result = n;
break;
}
if(!n.getData().isEmpty()) {
findNode(n.getData(), componentID);
if(n.getData() != null) {
result = findNode(n.getData(), componentID);
if (result != null) {
break;
}
}
}
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 {
ObjectMapper objectMapper = new ObjectMapper();
try {
......@@ -199,8 +223,6 @@ public class Main implements MessageListener {
case VALUE:
targetNode.setValue(newFieldValue);
break;
default: // TODO
break;
}
}
} catch (AttributeModificationException e) {
......@@ -233,7 +255,16 @@ public class Main implements MessageListener {
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);
ObjectMapper objectMapper = new ObjectMapper();
String result = null;
......@@ -241,17 +272,17 @@ public class Main implements MessageListener {
try {
JsonNode rootNode = objectMapper.readTree(targetJsonAsString); // read the whole tree
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
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
AsNode targetNode = findNode(dataNodesList, componentID);
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 {
switch (fieldName) {
case ID:
......@@ -272,13 +303,12 @@ public class Main implements MessageListener {
}
}
return result;
} catch (NameNotFoundException e) {
} catch (Exception e) {
throw e;
}
}
/**
* Route unlocking
*/
......@@ -287,43 +317,12 @@ public class Main implements MessageListener {
String address = "http://127.0.0.1:8080/Synergy";
String signal = "got_agree";
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) {
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) {
String login = "111111";
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