Commit 600694cc by Mikhail Gashenko

JsonNode: (draft8) added custom serializer!

parent a53a4f91
...@@ -42,10 +42,22 @@ public class APIFormsServiceSave { ...@@ -42,10 +42,22 @@ public class APIFormsServiceSave {
// json с данными по форме (параметр формы) // json с данными по форме (параметр формы)
private String data; private String data;
public APIFormsServiceSave(String login, String password, String address) { /**
* Сохранение данных по форме
* @param login - Login
* @param password - Password
* @param address - Address
* @param formUUID - Target form UUID
* @param uuid - Target form data UUID
* @param data - Form data
*/
public APIFormsServiceSave(String login, String password, String address, String formUUID, String uuid, String data) {
this.login = login; this.login = login;
this.password = password; this.password = password;
this.address = address; this.address = address;
this.formUUID = formUUID;
this.uuid = uuid;
this.data = data;
encoded = Base64.encode((login + ":" + password).getBytes()); encoded = Base64.encode((login + ":" + password).getBytes());
} }
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package kz.arta.synergy.astdev.custom_bp; package kz.arta.synergy.astdev.custom_bp;
import java.util.List; import java.util.List;
...@@ -14,16 +9,17 @@ import org.codehaus.jackson.node.ObjectNode; ...@@ -14,16 +9,17 @@ import org.codehaus.jackson.node.ObjectNode;
* @author drpsy * @author drpsy
*/ */
public class AsNode { public class AsNode {
@JsonProperty("id") @JsonProperty("id")
private String id; private String id;
@JsonProperty("type") @JsonProperty("type")
private String type; private String type;
@JsonProperty("label") @JsonProperty("label")
private String label; private String label;
@JsonProperty("key")
private String key;
@JsonProperty("value") @JsonProperty("value")
private String value; private String value;
@JsonProperty("key")
private String key;
@JsonProperty("data") @JsonProperty("data")
private List<AsNode> data; private List<AsNode> data;
@JsonProperty("formatVersion") @JsonProperty("formatVersion")
...@@ -34,7 +30,6 @@ public class AsNode { ...@@ -34,7 +30,6 @@ public class AsNode {
public String getId() { public String getId() {
return id; return id;
} }
public void setId(String id) { public void setId(String id) {
this.id = id; this.id = id;
} }
...@@ -42,7 +37,6 @@ public class AsNode { ...@@ -42,7 +37,6 @@ public class AsNode {
public String getType() { public String getType() {
return type; return type;
} }
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
} }
...@@ -50,7 +44,6 @@ public class AsNode { ...@@ -50,7 +44,6 @@ public class AsNode {
public String getLabel() { public String getLabel() {
return label; return label;
} }
public void setLabel(String label) { public void setLabel(String label) {
this.label = label; this.label = label;
} }
...@@ -58,7 +51,6 @@ public class AsNode { ...@@ -58,7 +51,6 @@ 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;
} }
...@@ -66,7 +58,6 @@ public class AsNode { ...@@ -66,7 +58,6 @@ public class AsNode {
public String getValue() { public String getValue() {
return value; return value;
} }
public void setValue(String value) { public void setValue(String value) {
this.value = value; this.value = value;
} }
...@@ -74,7 +65,6 @@ public class AsNode { ...@@ -74,7 +65,6 @@ public class AsNode {
public List<AsNode> getData() { public List<AsNode> getData() {
return data; return data;
} }
public void setData(List<AsNode> data) { public void setData(List<AsNode> data) {
this.data = data; this.data = data;
} }
...@@ -82,7 +72,6 @@ public class AsNode { ...@@ -82,7 +72,6 @@ public class AsNode {
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;
} }
...@@ -90,9 +79,8 @@ public class AsNode { ...@@ -90,9 +79,8 @@ public class AsNode {
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;
} }
} }
package kz.arta.synergy.astdev.custom_bp;
import java.io.IOException;
import java.util.List;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
/**
*
* @author drpsy
*/
public class AsNodeSerializer extends JsonSerializer<List<AsNode>> {
@Override
public void serialize(List<AsNode> t, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException {
jg.writeStartArray();
for (AsNode asNode : t) {
jg.writeStartObject();
jg.writeObjectField("data", asNode);
jg.writeEndObject();
}
jg.writeEndArray();
}
}
package kz.arta.synergy.astdev.custom_bp; package kz.arta.synergy.astdev.custom_bp;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.core.JsonToken;
...@@ -22,7 +21,10 @@ import javax.ejb.ActivationConfigProperty; ...@@ -22,7 +21,10 @@ import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven; import javax.ejb.MessageDriven;
import javax.naming.NameNotFoundException; import javax.naming.NameNotFoundException;
import javax.naming.directory.AttributeModificationException; import javax.naming.directory.AttributeModificationException;
import javax.xml.bind.DatatypeConverter;
import kz.arta.synergy.api.rest.sample.asforms.APIFormsServiceSave;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.type.TypeReference; import org.codehaus.jackson.type.TypeReference;
/** /**
...@@ -39,6 +41,7 @@ import org.codehaus.jackson.type.TypeReference; ...@@ -39,6 +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 enum fieldType { private static enum fieldType {
DATE, TEXTBOX, USER DATE, TEXTBOX, USER
...@@ -47,13 +50,21 @@ public class Main implements MessageListener { ...@@ -47,13 +50,21 @@ public class Main implements MessageListener {
ID, TYPE, LABEL, KEY, VALUE ID, TYPE, LABEL, KEY, VALUE
} }
private String dataUUID = null; // идентификатор данных по форме записи реестра
private String executionID = null; // идентификатор блокирующего процесса private String executionID = null; // идентификатор блокирующего процесса
private String documentID = null; // идентификатор документа реестра private String documentID = null; // идентификатор документа реестра
private static final String LOGIN = "111111";
private static final String PASSWORD = "1";
private final String TARGETFORMUUID = "fb44d99d-1579-4ac3-884a-01a6d4f71f9c";
private final String SYNERGYADDRESS = "http://127.0.0.1:8080/Synergy";
private String SOURCEFORMDATAUUID = null; // идентификатор данных по форме записи реестра
private String TARGETFORMDATAUUID = null;
@JsonSerialize(using = AsNodeSerializer.class)
private List<AsNode> sourceFormData = null; private List<AsNode> sourceFormData = null;
@JsonSerialize(using = AsNodeSerializer.class)
private List<AsNode> targetFormData = null; private List<AsNode> targetFormData = null;
/** /**
* *
* @param message * @param message
...@@ -76,7 +87,7 @@ public class Main implements MessageListener { ...@@ -76,7 +87,7 @@ public class Main implements MessageListener {
String value = parser.getText(); String value = parser.getText();
switch (fieldName) { switch (fieldName) {
case "dataUUID": case "dataUUID":
dataUUID = value; SOURCEFORMDATAUUID = value;
break; break;
case "executionID": case "executionID":
executionID = value; executionID = value;
...@@ -91,10 +102,10 @@ public class Main implements MessageListener { ...@@ -91,10 +102,10 @@ public class Main implements MessageListener {
} }
// Получение данных исходной формы (той, по которой запущен маршрут) // Получение данных исходной формы (той, по которой запущен маршрут)
URL sourceFormURL = new URL("http://127.0.0.1:8080/Synergy/rest/api/asforms/data/" + dataUUID); URL sourceFormURL = new URL(SYNERGYADDRESS + "/rest/api/asforms/data/" + SOURCEFORMDATAUUID);
String sourceFormDataAsString = synergyApiGetString(sourceFormURL); String sourceFormDataAsString = synergyApiGetString(sourceFormURL);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode SourceRootNode = objectMapper.readTree(sourceFormDataAsString); // read the whole tree JsonNode SourceRootNode = OBJECTMAPPER.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");
} }
...@@ -102,18 +113,16 @@ public class Main implements MessageListener { ...@@ -102,18 +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 = OBJECTMAPPER.readValue(SourceRootDataNode, new TypeReference<List<AsNode>>() {}); // map content of single root 'data' node
// Получение ID пользователя указанного в форме // Получение ID пользователя указанного в форме
String userID = getComponentValueByID(sourceFormDataAsString, "user1", fields.KEY); // TODO: Pass JsonNode, not String String userID = getComponentValueByID(this.sourceFormData, "user1", fields.KEY); // TODO: Pass JsonNode, not String
// По 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, "fb44d99d-1579-4ac3-884a-01a6d4f71f9c"); String userCardDataUUID = getCardDataUUID(userID, TARGETFORMUUID);
// Получение данных целевой карточки по dataUUID // Получение данных целевой карточки по dataUUID
URL targetFormURL = new URL("http://127.0.0.1:8080/Synergy/rest/api/asforms/data/" + userCardDataUUID); URL targetFormURL = new URL(SYNERGYADDRESS + "/rest/api/asforms/data/" + userCardDataUUID);
String targetFormDataAsString = synergyApiGetString(targetFormURL); TARGETFORMDATAUUID = synergyApiGetString(targetFormURL);
JsonNode TargetRootNode = objectMapper.readTree(targetFormDataAsString); JsonNode TargetRootNode = OBJECTMAPPER.readTree(TARGETFORMDATAUUID);
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");
} }
...@@ -121,7 +130,7 @@ public class Main implements MessageListener { ...@@ -121,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 = OBJECTMAPPER.readValue(TargetRootDataNode, new TypeReference<List<AsNode>>() {});
// Получение значений текущих дат исходной формы // Получение значений текущих дат исходной формы
String b5b1; String b5b1;
...@@ -141,6 +150,8 @@ public class Main implements MessageListener { ...@@ -141,6 +150,8 @@ 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();
unlockRoute(); unlockRoute();
return; return;
} }
...@@ -169,6 +180,15 @@ public class Main implements MessageListener { ...@@ -169,6 +180,15 @@ public class Main implements MessageListener {
} }
} }
private void updateUserCard() throws IOException {
// SERIALIZATION
OBJECTMAPPER.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
String serialData = OBJECTMAPPER.writeValueAsString(targetFormData);
APIFormsServiceSave saver = new APIFormsServiceSave(LOGIN, PASSWORD, SYNERGYADDRESS, TARGETFORMUUID, TARGETFORMDATAUUID, serialData);
}
/** /**
* Returns node which has specifies ID * Returns node which has specifies ID
* @param nodesList - List of nodes * @param nodesList - List of nodes
...@@ -313,9 +333,6 @@ public class Main implements MessageListener { ...@@ -313,9 +333,6 @@ public class Main implements MessageListener {
} }
private static String synergyApiGetString(final URL requestURL) { private static String synergyApiGetString(final URL requestURL) {
String login = "111111";
String password = "1";
try { try {
String output; String output;
...@@ -323,7 +340,7 @@ public class Main implements MessageListener { ...@@ -323,7 +340,7 @@ public class Main implements MessageListener {
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json; charset=utf-8"); conn.setRequestProperty("Accept", "application/json; charset=utf-8");
String encoded = Base64.encode((login + ":" + password).getBytes()); String encoded = DatatypeConverter.printBase64Binary((LOGIN + ":" + PASSWORD).getBytes()); // String encoded = Base64.encode((this.LOGIN + ":" + this.PASSWORD).getBytes());
conn.setRequestProperty("Authorization", "Basic " + encoded); conn.setRequestProperty("Authorization", "Basic " + encoded);
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
......
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