Commit 1ccf1eb2 by Mikhail Gashenko

JsonNode: (draft9) added custom serializer!

parent 600694cc
package kz.arta.synergy.api.rest.sample.asforms; package kz.arta.synergy.api.rest.sample.asforms;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken; import org.codehaus.jackson.JsonToken;
...@@ -11,6 +10,7 @@ import java.io.InputStream; ...@@ -11,6 +10,7 @@ import java.io.InputStream;
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 javax.xml.bind.DatatypeConverter;
/** /**
* *
...@@ -58,7 +58,7 @@ public class APIFormsServiceSave { ...@@ -58,7 +58,7 @@ public class APIFormsServiceSave {
this.formUUID = formUUID; this.formUUID = formUUID;
this.uuid = uuid; this.uuid = uuid;
this.data = data; this.data = data;
encoded = Base64.encode((login + ":" + password).getBytes()); this.encoded = DatatypeConverter.printBase64Binary((login + ":" + password).getBytes());
} }
public void save() throws Exception { public void save() throws Exception {
...@@ -87,7 +87,7 @@ public class APIFormsServiceSave { ...@@ -87,7 +87,7 @@ public class APIFormsServiceSave {
InputStream is = connection.getInputStream(); InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is)); BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer response = new StringBuffer(); StringBuilder response = new StringBuilder();
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
......
...@@ -41,7 +41,7 @@ import org.codehaus.jackson.type.TypeReference; ...@@ -41,7 +41,7 @@ import org.codehaus.jackson.type.TypeReference;
public class Main implements MessageListener { 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 OBJECTMAPPER = new ObjectMapper(); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static enum fieldType { private static enum fieldType {
DATE, TEXTBOX, USER DATE, TEXTBOX, USER
...@@ -55,10 +55,10 @@ public class Main implements MessageListener { ...@@ -55,10 +55,10 @@ public class Main implements MessageListener {
private static final String LOGIN = "111111"; private static final String LOGIN = "111111";
private static final String PASSWORD = "1"; private static final String PASSWORD = "1";
private final String TARGETFORMUUID = "fb44d99d-1579-4ac3-884a-01a6d4f71f9c"; private final String TARGET_FORM_UUID = "fb44d99d-1579-4ac3-884a-01a6d4f71f9c";
private final String SYNERGYADDRESS = "http://127.0.0.1:8080/Synergy"; private final String SYNERGY_ADDRESS = "http://127.0.0.1:8080/Synergy";
private String SOURCEFORMDATAUUID = null; // идентификатор данных по форме записи реестра private String SOURCE_FORM_DATA_UUID = null; // идентификатор данных по форме записи реестра
private String TARGETFORMDATAUUID = null; private String TARGET_FORM_DATA_UUID = null;
@JsonSerialize(using = AsNodeSerializer.class) @JsonSerialize(using = AsNodeSerializer.class)
private List<AsNode> sourceFormData = null; private List<AsNode> sourceFormData = null;
...@@ -87,7 +87,7 @@ public class Main implements MessageListener { ...@@ -87,7 +87,7 @@ public class Main implements MessageListener {
String value = parser.getText(); String value = parser.getText();
switch (fieldName) { switch (fieldName) {
case "dataUUID": case "dataUUID":
SOURCEFORMDATAUUID = value; SOURCE_FORM_DATA_UUID = value;
break; break;
case "executionID": case "executionID":
executionID = value; executionID = value;
...@@ -101,11 +101,11 @@ public class Main implements MessageListener { ...@@ -101,11 +101,11 @@ public class Main implements MessageListener {
} }
} }
// Получение данных исходной формы (той, по которой запущен маршрут) // SOURCE FORM: Получение данных исходной формы (той, по которой запущен маршрут)
URL sourceFormURL = new URL(SYNERGYADDRESS + "/rest/api/asforms/data/" + SOURCEFORMDATAUUID); URL sourceFormURL = new URL(SYNERGY_ADDRESS + "/rest/api/asforms/data/" + SOURCE_FORM_DATA_UUID);
String sourceFormDataAsString = synergyApiGetString(sourceFormURL); String sourceFormDataAsString = synergyApiGetString(sourceFormURL);
JsonNode SourceRootNode = OBJECTMAPPER.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");
} }
...@@ -113,16 +113,16 @@ public class Main implements MessageListener { ...@@ -113,16 +113,16 @@ public class Main implements MessageListener {
if (SourceRootDataNode == null) { if (SourceRootDataNode == null) {
throw new AttributeModificationException("GMP: Invalid form data, root \"data\" node does not exists"); throw new AttributeModificationException("GMP: Invalid form data, root \"data\" node does not exists");
} }
sourceFormData = OBJECTMAPPER.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
// Получение ID пользователя указанного в форме // TARGET FORM: Получение ID пользователя указанного в форме
String userID = getComponentValueByID(this.sourceFormData, "user1", fields.KEY); // TODO: Pass JsonNode, not String String userID = getComponentValueByID(this.sourceFormData, "user1", fields.KEY);
// По ID пользователя указанного в карточке и ID формы получаем DataUUID целевой карточки (ID for work: "04f7809d-f44c-4a2d-950d-6aa8e6c3fea1") // По ID пользователя указанного в карточке и ID формы получаем DataUUID целевой карточки (ID for work: "04f7809d-f44c-4a2d-950d-6aa8e6c3fea1")
String userCardDataUUID = getCardDataUUID(userID, TARGETFORMUUID); String userCardDataUUID = getCardDataUUID(userID, TARGET_FORM_UUID);
// Получение данных целевой карточки по dataUUID // Получение данных целевой карточки по dataUUID
URL targetFormURL = new URL(SYNERGYADDRESS + "/rest/api/asforms/data/" + userCardDataUUID); URL targetFormURL = new URL(SYNERGY_ADDRESS + "/rest/api/asforms/data/" + userCardDataUUID);
TARGETFORMDATAUUID = synergyApiGetString(targetFormURL); TARGET_FORM_DATA_UUID = synergyApiGetString(targetFormURL);
JsonNode TargetRootNode = OBJECTMAPPER.readTree(TARGETFORMDATAUUID); JsonNode TargetRootNode = OBJECT_MAPPER.readTree(TARGET_FORM_DATA_UUID);
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");
} }
...@@ -130,7 +130,7 @@ public class Main implements MessageListener { ...@@ -130,7 +130,7 @@ public class Main implements MessageListener {
if (TargetRootDataNode == null) { if (TargetRootDataNode == null) {
throw new AttributeModificationException("GMP: Invalid form data, root \"data\" node does not exists"); throw new AttributeModificationException("GMP: Invalid form data, root \"data\" node does not exists");
} }
targetFormData = OBJECTMAPPER.readValue(TargetRootDataNode, new TypeReference<List<AsNode>>() {}); targetFormData = OBJECT_MAPPER.readValue(TargetRootDataNode, new TypeReference<List<AsNode>>() {});
// Получение значений текущих дат исходной формы // Получение значений текущих дат исходной формы
String b5b1; String b5b1;
...@@ -150,7 +150,7 @@ public class Main implements MessageListener { ...@@ -150,7 +150,7 @@ public class Main implements MessageListener {
updateFieldValue(this.targetFormData, "start-b1", fields.KEY, b2b1); updateFieldValue(this.targetFormData, "start-b1", fields.KEY, b2b1);
updateFieldValue(this.targetFormData, "finish-b1", fields.KEY, b4b1); updateFieldValue(this.targetFormData, "finish-b1", fields.KEY, b4b1);
updateUserCard(); updateTargetForm();
unlockRoute(); unlockRoute();
return; return;
...@@ -181,12 +181,12 @@ public class Main implements MessageListener { ...@@ -181,12 +181,12 @@ public class Main implements MessageListener {
} }
private void updateUserCard() throws IOException { private void updateTargetForm() throws IOException {
// SERIALIZATION // SERIALIZATION
OBJECTMAPPER.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); OBJECT_MAPPER.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
String serialData = OBJECTMAPPER.writeValueAsString(targetFormData); String serialData = OBJECT_MAPPER.writeValueAsString(targetFormData);
APIFormsServiceSave saver = new APIFormsServiceSave(LOGIN, PASSWORD, SYNERGYADDRESS, TARGETFORMUUID, TARGETFORMDATAUUID, serialData); APIFormsServiceSave saver = new APIFormsServiceSave(LOGIN, PASSWORD, SYNERGY_ADDRESS, TARGET_FORM_UUID, TARGET_FORM_DATA_UUID, "\"data\": " + serialData);
} }
/** /**
......
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