Commit cb7319e8 by Mikhail Gashenko

JsonNode: (draft10) successful table row add!

parent 86664330
...@@ -47,10 +47,10 @@ public class Main implements MessageListener { ...@@ -47,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 ObjectType {
DATE, TEXTBOX, USER DATE, TEXTBOX, USER
} }
private static enum ObjectType { private static enum FieldType {
ID, TYPE, LABEL, KEY, VALUE ID, TYPE, LABEL, KEY, VALUE
} }
...@@ -124,7 +124,7 @@ public class Main implements MessageListener { ...@@ -124,7 +124,7 @@ 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", ObjectType.KEY); String userID = getComponentValueByID(this.sourceFormData, "user1", FieldType.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
...@@ -152,25 +152,37 @@ public class Main implements MessageListener { ...@@ -152,25 +152,37 @@ 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", ObjectType.KEY); String b2b1 = getComponentValueByID(this.sourceFormData, "b2-b1-b1", FieldType.KEY);
String b4b1 = getComponentValueByID(this.sourceFormData, "b4-b1-b1", ObjectType.KEY); String b4b1 = getComponentValueByID(this.sourceFormData, "b4-b1-b1", FieldType.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 == null || b4b1 == null) { // отображается key
throw new Exception ("Source date fields is empty");
} else {
constructNewTableRow(b2b1, b4b1);
updateTargetForm(); updateTargetForm();
} }
// boolean key_b2b1 = false, key_b4b1 = false, val_b2b1 = false, val_b4b1 = false;
// if (!b2b1.isEmpty() && !b4b1.isEmpty()) { // do not update with empty values
// key_b2b1 = updateFieldValue(this.targetFormData, "start-b1", FieldType.KEY, b2b1);
// key_b4b1 = updateFieldValue(this.targetFormData, "finish-b1", FieldType.KEY, b4b1);
// val_b2b1 = updateFieldValue(this.targetFormData, "start-b1", FieldType.VALUE, dateValueFromKey(b2b1));
// val_b4b1 = updateFieldValue(this.targetFormData, "finish-b1", FieldType.VALUE, dateValueFromKey(b4b1));
// }
//
// if (key_b2b1 && key_b4b1 && val_b2b1 && val_b4b1) {
// updateTargetForm();
// }
// Second table // Second table
// String t4b1 = getComponentValueByID(sourceFormData, "t2-b1", FieldType.DATE); // String t4b1 = getComponentValueByID(sourceFormData, "t2-b1", ObjectType.DATE);
// String t6b1 = getComponentValueByID(sourceFormData, "b6-b1", FieldType.DATE); // String t6b1 = getComponentValueByID(sourceFormData, "b6-b1", ObjectType.DATE);
// String t10b1 = getComponentValueByID(sourceFormData, "t10-b1", FieldType.DATE); // String t10b1 = getComponentValueByID(sourceFormData, "t10-b1", ObjectType.DATE);
// String t12b1 = getComponentValueByID(sourceFormData, "t12-b1", FieldType.DATE); // String t12b1 = getComponentValueByID(sourceFormData, "t12-b1", ObjectType.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);
...@@ -192,6 +204,75 @@ public class Main implements MessageListener { ...@@ -192,6 +204,75 @@ public class Main implements MessageListener {
} }
} }
private void constructNewTableRow(String b2b1, String b4b1) throws Exception {
AsNode tableNode = findNode(targetFormData, "table");
if (tableNode != null) {
List<AsNode> tableDataNodesList = tableNode.getData();
String tablesCounter = Integer.toString(appendabeTableRowsCount(tableDataNodesList, "start", "b") + 1);
AsNode n1 = new AsNode();
AsNode n2 = new AsNode();
AsNode n3 = new AsNode();
AsNode n4 = new AsNode();
AsNode n5 = new AsNode();
AsNode n6 = new AsNode();
AsNode n7 = new AsNode();
n1.setId("start-b" + tablesCounter);
n1.setType("date");
n1.setLabel(" ");
n1.setValue(dateValueFromKey(b2b1));
n1.setKey(b2b1);
n2.setId("finish-b" + tablesCounter);
n2.setType("date");
n2.setLabel(" ");
n2.setValue(dateValueFromKey(b4b1));
n2.setKey(b4b1);
n3.setId("days-b" + tablesCounter);
n3.setType("textbox");
n3.setLabel(" ");
n4.setId("matpom-b" + tablesCounter);
n4.setType("listbox");
n4.setValue("");
n4.setKey("1");
n5.setId("period_start-b" + tablesCounter);
n5.setType("date");
n6.setId("period_finish-b" + tablesCounter);
n6.setType("date");
n7.setId("otozvano-b" + tablesCounter);
n7.setType("texbox");
n7.setLabel(" ");
tableDataNodesList.add(n1);
tableDataNodesList.add(n2);
tableDataNodesList.add(n3);
tableDataNodesList.add(n4);
tableDataNodesList.add(n5);
tableDataNodesList.add(n6);
tableDataNodesList.add(n7);
}
}
private int appendabeTableRowsCount(List<AsNode> lan, String tableID, String tableElementID) {
int counter = 0;
for (;;) {
String searchString = tableID + "-" + tableElementID + Integer.toString(counter + 1);
AsNode n = findNode(lan, searchString);
if (n == null) {
break;
} else {
counter++;
}
}
return counter;
}
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);
...@@ -205,26 +286,26 @@ public class Main implements MessageListener { ...@@ -205,26 +286,26 @@ public class Main implements MessageListener {
/** /**
* Returns node which has specifies ID * Returns node which has specifies ID
* @param nodesList - List of nodes * @param nodesList - List of nodes
* @param componentID - Component ID * @param objectID - Component ID
* @return AsNode if such exists, null otherwise * @return AsNode if such exists, null otherwise
*/ */
private static AsNode findNode(List<AsNode> nodesList, String componentID) { private static AsNode findNode(List<AsNode> nodesList, String objectID) {
AsNode result = null; AsNode result = null;
for (AsNode n : nodesList) { for (AsNode n : nodesList) {
if (result != null) break; // prevent relooping if result has already been found if (result != null) break; // prevent relooping if result has already been found
if (componentID.equals(n.getId())) { if (objectID.equals(n.getId())) {
result = n; result = n;
} else if (n.getData() != null) { } else if (n.getData() != null) {
result = findNode(n.getData(), componentID); result = findNode(n.getData(), objectID);
} }
} }
return result; return result;
} }
private static String dataValueFromKey (String keyValue) throws ParseException { private static String dateValueFromKey (String keyValue) throws ParseException {
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(keyValue); Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(keyValue);
return new SimpleDateFormat("dd.MM.yyy").format(date); return new SimpleDateFormat("dd.MM.yyy").format(date);
...@@ -233,21 +314,19 @@ public class Main implements MessageListener { ...@@ -233,21 +314,19 @@ public class Main implements MessageListener {
/** /**
* 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
* @param componentID * @param objectID
* @param fieldName * @param fieldType
* @param newFieldValue * @param newFieldValue
* @throws AttributeModificationException * @throws AttributeModificationException
* @throws IOException * @throws IOException
*/ */
private void updateFieldValue (List<AsNode> dataNodesList, String componentID, ObjectType fieldName, String newFieldValue) throws AttributeModificationException, IOException { private boolean updateFieldValue (List<AsNode> dataNodesList, String objectID, FieldType fieldType, String newFieldValue) {
try { try {
AsNode targetNode = findNode(dataNodesList, objectID);
AsNode targetNode = findNode(dataNodesList, componentID);
if (targetNode == null) { if (targetNode == null) {
throw new AttributeModificationException("GMP: Node with specified ID is not found in passed JSON"); return false;
} else { } else {
switch (fieldName) { switch (fieldType) {
case ID: case ID:
targetNode.setId(newFieldValue); targetNode.setId(newFieldValue);
break; break;
...@@ -265,7 +344,8 @@ public class Main implements MessageListener { ...@@ -265,7 +344,8 @@ public class Main implements MessageListener {
break; break;
} }
} }
} catch (AttributeModificationException e) { return true;
} catch (Exception e) {
throw e; throw e;
} }
} }
...@@ -298,21 +378,21 @@ public class Main implements MessageListener { ...@@ -298,21 +378,21 @@ public class Main implements MessageListener {
/** /**
* Returns value of of specified field of component with specified ID * Returns value of of specified field of component with specified ID
* @param targetJsonAsString * @param targetJsonAsString
* @param componentID * @param objectID
* @param fieldName * @param fieldType
* @return * @return
* @throws NameNotFoundException * @throws NameNotFoundException
* @throws IOException * @throws IOException
*/ */
private static String getComponentValueByID(List<AsNode> dataNodesList, String componentID, ObjectType fieldName) throws Exception { private static String getComponentValueByID(List<AsNode> dataNodesList, String objectID, FieldType fieldType) throws Exception {
String result = null; String result = null;
try { try {
AsNode targetNode = findNode(dataNodesList, componentID); AsNode targetNode = findNode(dataNodesList, objectID);
if (targetNode == null) { if (targetNode == null) {
throw new Exception("GMP: Node with specified ID is not found in passed JSON"); return null;
} else { } else {
switch (fieldName) { switch (fieldType) {
case ID: case ID:
result = targetNode.getId(); // may be for cheking if component with such ID exists at all result = targetNode.getId(); // may be for cheking if component with such ID exists at all
break; break;
......
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