Commit 86664330 by Mikhail Gashenko

JsonNode: (draft10) successful update of existing values in table

parent fdd8570f
...@@ -2,7 +2,7 @@ package kz.arta.synergy.astdev.custom_bp; ...@@ -2,7 +2,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; import org.codehaus.jackson.node.ObjectNode;
/** /**
* *
...@@ -23,10 +23,10 @@ public class AsNode { ...@@ -23,10 +23,10 @@ public class AsNode {
@JsonProperty("data") @JsonProperty("data")
private List<AsNode> data; private List<AsNode> data;
// @JsonProperty("formatVersion") @JsonProperty("formatVersion")
// private String formatVersion; private String formatVersion;
// @JsonProperty("manualTags") @JsonProperty("manualTags")
// private ObjectNode manualTags; private ObjectNode manualTags;
public String getId() { public String getId() {
return id; return id;
...@@ -55,10 +55,10 @@ public class AsNode { ...@@ -55,10 +55,10 @@ public class AsNode {
public String getKey() { public String getKey() {
return key; return key;
} }
//
// public void setKey(String key) { public void setKey(String key) {
// this.key = key; this.key = key;
// } }
public String getValue() { public String getValue() {
return value; return value;
...@@ -76,20 +76,20 @@ public class AsNode { ...@@ -76,20 +76,20 @@ public class AsNode {
this.data = data; this.data = data;
} }
// public String getFormatVersion() { public String getFormatVersion() {
// return formatVersion; return formatVersion;
// } }
//
// public void setFormatVersion(String formatVersion) { public void setFormatVersion(String formatVersion) {
// this.formatVersion = formatVersion; this.formatVersion = formatVersion;
// } }
//
// public ObjectNode getManualTags() { public ObjectNode getManualTags() {
// return manualTags; return manualTags;
// } }
//
// public void setManualTags(ObjectNode manualTags) { public void setManualTags(ObjectNode manualTags) {
// this.manualTags = manualTags; this.manualTags = manualTags;
// } }
} }
\ No newline at end of file
...@@ -16,6 +16,9 @@ import java.io.IOException; ...@@ -16,6 +16,9 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import java.util.List;
import javax.ejb.ActivationConfigProperty; import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven; import javax.ejb.MessageDriven;
...@@ -44,10 +47,10 @@ public class Main implements MessageListener { ...@@ -44,10 +47,10 @@ public class Main implements MessageListener {
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static enum fieldType { private static enum FieldType {
DATE, TEXTBOX, USER DATE, TEXTBOX, USER
} }
private static enum fields { private static enum ObjectType {
ID, TYPE, LABEL, KEY, VALUE ID, TYPE, LABEL, KEY, VALUE
} }
...@@ -72,6 +75,7 @@ public class Main implements MessageListener { ...@@ -72,6 +75,7 @@ public class Main implements MessageListener {
*/ */
@Override @Override
public void onMessage(Message message) { public void onMessage(Message message) {
// <editor-fold defaultstate="collapsed" desc=" DESCRIPTION ">
if (!(message instanceof TextMessage)) { if (!(message instanceof TextMessage)) {
return; return;
} }
...@@ -102,17 +106,13 @@ public class Main implements MessageListener { ...@@ -102,17 +106,13 @@ public class Main implements MessageListener {
} }
} }
// </editor-fold>
// Configure ObjectMapper // Configure ObjectMapper
OBJECT_MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); OBJECT_MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// SOURCE FORM: Получение данных исходной формы (той, по которой запущен маршрут) // SOURCE FORM: Получение данных исходной формы (той, по которой запущен маршрут)
URL sourceFormURL = new URL(SYNERGY_ADDRESS + "/rest/api/asforms/data/" + SOURCE_FORM_DATA_UUID); URL sourceFormURL = new URL(SYNERGY_ADDRESS + "/rest/api/asforms/data/" + SOURCE_FORM_DATA_UUID);
String sourceFormDataAsString = synergyApiGetString(sourceFormURL); String sourceFormDataAsString = synergyApiGetString(sourceFormURL);
JsonNode SourceRootNode = OBJECT_MAPPER.readTree(sourceFormDataAsString); // read the whole tree JsonNode SourceRootNode = OBJECT_MAPPER.readTree(sourceFormDataAsString); // read the whole tree
if (SourceRootNode.isNull()) { if (SourceRootNode.isNull()) {
throw new AttributeModificationException("GMP: Passed data has no JSON content"); throw new AttributeModificationException("GMP: Passed data has no JSON content");
...@@ -123,22 +123,14 @@ public class Main implements MessageListener { ...@@ -123,22 +123,14 @@ public class Main implements MessageListener {
} }
sourceFormData = OBJECT_MAPPER.readValue(SourceRootDataNode, new TypeReference<List<AsNode>>() {}); // map content of single root 'data' node sourceFormData = OBJECT_MAPPER.readValue(SourceRootDataNode, new TypeReference<List<AsNode>>() {}); // map content of single root 'data' node
// TARGET FORM: Получение ID пользователя указанного в форме // TARGET FORM: Получение ID пользователя указанного в форме
String userID = getComponentValueByID(this.sourceFormData, "user1", fields.KEY); String userID = getComponentValueByID(this.sourceFormData, "user1", ObjectType.KEY);
// По ID пользователя указанного в карточке и ID формы получаем DataUUID целевой карточки (ID for work: "04f7809d-f44c-4a2d-950d-6aa8e6c3fea1") // По ID пользователя указанного в карточке и ID формы получаем DataUUID целевой карточки (ID for work: "04f7809d-f44c-4a2d-950d-6aa8e6c3fea1")
TARGET_FORM_DATA_UUID = getCardDataUUID(userID, TARGET_FORM_UUID); TARGET_FORM_DATA_UUID = getCardDataUUID(userID, TARGET_FORM_UUID);
// Получение данных целевой карточки по dataUUID // Получение данных целевой карточки по dataUUID
URL targetFormURL = new URL(SYNERGY_ADDRESS + "/rest/api/asforms/data/" + TARGET_FORM_DATA_UUID); URL targetFormURL = new URL(SYNERGY_ADDRESS + "/rest/api/asforms/data/" + TARGET_FORM_DATA_UUID);
String targetFormDataAsString = synergyApiGetString(targetFormURL); String targetFormDataAsString = synergyApiGetString(targetFormURL);
JsonNode TargetRootNode = OBJECT_MAPPER.readTree(targetFormDataAsString); JsonNode TargetRootNode = OBJECT_MAPPER.readTree(targetFormDataAsString);
if (TargetRootNode.isNull()) { if (TargetRootNode.isNull()) {
throw new AttributeModificationException("GMP: Passed data has no JSON content"); throw new AttributeModificationException("GMP: Passed data has no JSON content");
...@@ -160,21 +152,25 @@ public class Main implements MessageListener { ...@@ -160,21 +152,25 @@ public class Main implements MessageListener {
// String t32b1, t30b1, t28b1; // String t32b1, t30b1, t28b1;
// first table // first table
String b2b1 = getComponentValueByID(this.sourceFormData, "b2-b1-b1", fields.VALUE); String b2b1 = getComponentValueByID(this.sourceFormData, "b2-b1-b1", ObjectType.KEY);
String b4b1 = getComponentValueByID(this.sourceFormData, "b4-b1-b1", fields.VALUE); String b4b1 = getComponentValueByID(this.sourceFormData, "b4-b1-b1", ObjectType.KEY);
if (!b2b1.isEmpty() && !b4b1.isEmpty()) { // отображается key
updateFieldValue(this.targetFormData, "start-b1", ObjectType.KEY, b2b1);
updateFieldValue(this.targetFormData, "finish-b1", ObjectType.KEY, b4b1);
if (!b2b1.isEmpty() && !b4b1.isEmpty()) { updateFieldValue(this.targetFormData, "start-b1", ObjectType.VALUE, dataValueFromKey(b2b1));
updateFieldValue(this.targetFormData, "start-b1", fields.VALUE, b2b1); updateFieldValue(this.targetFormData, "finish-b1", ObjectType.VALUE, dataValueFromKey(b4b1));
updateFieldValue(this.targetFormData, "finish-b1", fields.VALUE, b4b1);
updateTargetForm(); updateTargetForm();
} }
// Second table // Second table
// String t4b1 = getComponentValueByID(sourceFormData, "t2-b1", fieldType.DATE); // String t4b1 = getComponentValueByID(sourceFormData, "t2-b1", FieldType.DATE);
// String t6b1 = getComponentValueByID(sourceFormData, "b6-b1", fieldType.DATE); // String t6b1 = getComponentValueByID(sourceFormData, "b6-b1", FieldType.DATE);
// String t10b1 = getComponentValueByID(sourceFormData, "t10-b1", fieldType.DATE); // String t10b1 = getComponentValueByID(sourceFormData, "t10-b1", FieldType.DATE);
// String t12b1 = getComponentValueByID(sourceFormData, "t12-b1", fieldType.DATE); // String t12b1 = getComponentValueByID(sourceFormData, "t12-b1", FieldType.DATE);
// //
// if (!t4b1.isEmpty() && !t6b1.isEmpty() && !t10b1.isEmpty() && !t12b1.isEmpty()) { // if (!t4b1.isEmpty() && !t6b1.isEmpty() && !t10b1.isEmpty() && !t12b1.isEmpty()) {
// setComponentValueByID(targetFormData, "start-b1", "key", b2b1); // setComponentValueByID(targetFormData, "start-b1", "key", b2b1);
...@@ -196,7 +192,6 @@ public class Main implements MessageListener { ...@@ -196,7 +192,6 @@ public class Main implements MessageListener {
} }
} }
private void updateTargetForm() throws IOException, Exception { private void updateTargetForm() throws IOException, Exception {
// SERIALIZATION // SERIALIZATION
OBJECT_MAPPER.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); OBJECT_MAPPER.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
...@@ -229,6 +224,12 @@ public class Main implements MessageListener { ...@@ -229,6 +224,12 @@ public class Main implements MessageListener {
return result; return result;
} }
private static String dataValueFromKey (String keyValue) throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(keyValue);
return new SimpleDateFormat("dd.MM.yyy").format(date);
}
/** /**
* Setting new value of requested field of requested component with specified ID * Setting new value of requested field of requested component with specified ID
* @param targetJsonAsString * @param targetJsonAsString
...@@ -238,7 +239,7 @@ public class Main implements MessageListener { ...@@ -238,7 +239,7 @@ public class Main implements MessageListener {
* @throws AttributeModificationException * @throws AttributeModificationException
* @throws IOException * @throws IOException
*/ */
private void updateFieldValue (List<AsNode> dataNodesList, String componentID, fields fieldName, String newFieldValue) throws AttributeModificationException, IOException { private void updateFieldValue (List<AsNode> dataNodesList, String componentID, ObjectType fieldName, String newFieldValue) throws AttributeModificationException, IOException {
try { try {
...@@ -257,7 +258,8 @@ public class Main implements MessageListener { ...@@ -257,7 +258,8 @@ public class Main implements MessageListener {
targetNode.setLabel(newFieldValue); targetNode.setLabel(newFieldValue);
break; break;
case KEY: case KEY:
throw new AttributeModificationException("GMP: Modification of \"key\" attribute is forbidden according to specification"); targetNode.setKey(newFieldValue);
break;
case VALUE: case VALUE:
targetNode.setValue(newFieldValue); targetNode.setValue(newFieldValue);
break; break;
...@@ -302,7 +304,7 @@ public class Main implements MessageListener { ...@@ -302,7 +304,7 @@ public class Main implements MessageListener {
* @throws NameNotFoundException * @throws NameNotFoundException
* @throws IOException * @throws IOException
*/ */
private static String getComponentValueByID(List<AsNode> dataNodesList, String componentID, fields fieldName) throws Exception { private static String getComponentValueByID(List<AsNode> dataNodesList, String componentID, ObjectType fieldName) throws Exception {
String result = null; String result = null;
try { try {
......
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