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;
import java.util.List;
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 {
@JsonProperty("data")
private List<AsNode> data;
// @JsonProperty("formatVersion")
// private String formatVersion;
// @JsonProperty("manualTags")
// private ObjectNode manualTags;
@JsonProperty("formatVersion")
private String formatVersion;
@JsonProperty("manualTags")
private ObjectNode manualTags;
public String getId() {
return id;
......@@ -55,10 +55,10 @@ public class AsNode {
public String getKey() {
return key;
}
//
// public void setKey(String key) {
// this.key = key;
// }
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
......@@ -76,20 +76,20 @@ public class AsNode {
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;
// }
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;
}
}
\ No newline at end of file
......@@ -16,6 +16,9 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
......@@ -44,10 +47,10 @@ public class Main implements MessageListener {
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static enum fieldType {
private static enum FieldType {
DATE, TEXTBOX, USER
}
private static enum fields {
private static enum ObjectType {
ID, TYPE, LABEL, KEY, VALUE
}
......@@ -72,15 +75,16 @@ public class Main implements MessageListener {
*/
@Override
public void onMessage(Message message) {
// <editor-fold defaultstate="collapsed" desc=" DESCRIPTION ">
if (!(message instanceof TextMessage)) {
return;
}
try {
JsonFactory factory = new JsonFactory();
JsonParser parser = factory.createParser(((TextMessage) message).getText());
JsonToken token = null;
while ((token = parser.nextToken()) != null) {
if (token == JsonToken.FIELD_NAME) {
String fieldName = parser.getText();
......@@ -102,17 +106,13 @@ public class Main implements MessageListener {
}
}
// </editor-fold>
// Configure ObjectMapper
OBJECT_MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// SOURCE FORM: Получение данных исходной формы (той, по которой запущен маршрут)
URL sourceFormURL = new URL(SYNERGY_ADDRESS + "/rest/api/asforms/data/" + SOURCE_FORM_DATA_UUID);
String sourceFormDataAsString = synergyApiGetString(sourceFormURL);
JsonNode SourceRootNode = OBJECT_MAPPER.readTree(sourceFormDataAsString); // read the whole tree
if (SourceRootNode.isNull()) {
throw new AttributeModificationException("GMP: Passed data has no JSON content");
......@@ -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
// 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")
TARGET_FORM_DATA_UUID = getCardDataUUID(userID, TARGET_FORM_UUID);
// Получение данных целевой карточки по dataUUID
URL targetFormURL = new URL(SYNERGY_ADDRESS + "/rest/api/asforms/data/" + TARGET_FORM_DATA_UUID);
String targetFormDataAsString = synergyApiGetString(targetFormURL);
JsonNode TargetRootNode = OBJECT_MAPPER.readTree(targetFormDataAsString);
if (TargetRootNode.isNull()) {
throw new AttributeModificationException("GMP: Passed data has no JSON content");
......@@ -160,21 +152,25 @@ public class Main implements MessageListener {
// String t32b1, t30b1, t28b1;
// first table
String b2b1 = getComponentValueByID(this.sourceFormData, "b2-b1-b1", fields.VALUE);
String b4b1 = getComponentValueByID(this.sourceFormData, "b4-b1-b1", fields.VALUE);
String b2b1 = getComponentValueByID(this.sourceFormData, "b2-b1-b1", ObjectType.KEY);
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);
updateFieldValue(this.targetFormData, "start-b1", ObjectType.VALUE, dataValueFromKey(b2b1));
updateFieldValue(this.targetFormData, "finish-b1", ObjectType.VALUE, dataValueFromKey(b4b1));
if (!b2b1.isEmpty() && !b4b1.isEmpty()) {
updateFieldValue(this.targetFormData, "start-b1", fields.VALUE, b2b1);
updateFieldValue(this.targetFormData, "finish-b1", fields.VALUE, b4b1);
updateTargetForm();
}
// 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);
// 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);
......@@ -196,7 +192,6 @@ public class Main implements MessageListener {
}
}
private void updateTargetForm() throws IOException, Exception {
// SERIALIZATION
OBJECT_MAPPER.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
......@@ -229,6 +224,12 @@ public class Main implements MessageListener {
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
* @param targetJsonAsString
......@@ -238,7 +239,7 @@ public class Main implements MessageListener {
* @throws AttributeModificationException
* @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 {
......@@ -257,7 +258,8 @@ public class Main implements MessageListener {
targetNode.setLabel(newFieldValue);
break;
case KEY:
throw new AttributeModificationException("GMP: Modification of \"key\" attribute is forbidden according to specification");
targetNode.setKey(newFieldValue);
break;
case VALUE:
targetNode.setValue(newFieldValue);
break;
......@@ -267,7 +269,7 @@ public class Main implements MessageListener {
throw e;
}
}
/**
* Returns user's card data UUID by form's UUID
* @param userID - User identifier
......@@ -302,7 +304,7 @@ public class Main implements MessageListener {
* @throws NameNotFoundException
* @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;
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