Commit 600694cc by Mikhail Gashenko

JsonNode: (draft8) added custom serializer!

parent a53a4f91
......@@ -42,10 +42,22 @@ public class APIFormsServiceSave {
// json с данными по форме (параметр формы)
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.password = password;
this.address = address;
this.formUUID = formUUID;
this.uuid = uuid;
this.data = data;
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;
import java.util.List;
......@@ -14,16 +9,17 @@ import org.codehaus.jackson.node.ObjectNode;
* @author drpsy
*/
public class AsNode {
@JsonProperty("id")
private String id;
@JsonProperty("type")
private String type;
@JsonProperty("label")
private String label;
@JsonProperty("key")
private String key;
@JsonProperty("value")
private String value;
@JsonProperty("key")
private String key;
@JsonProperty("data")
private List<AsNode> data;
@JsonProperty("formatVersion")
......@@ -34,7 +30,6 @@ public class AsNode {
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
......@@ -42,7 +37,6 @@ public class AsNode {
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
......@@ -50,7 +44,6 @@ public class AsNode {
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
......@@ -58,7 +51,6 @@ public class AsNode {
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
......@@ -66,7 +58,6 @@ public class AsNode {
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
......@@ -74,7 +65,6 @@ public class AsNode {
public List<AsNode> getData() {
return data;
}
public void setData(List<AsNode> data) {
this.data = data;
}
......@@ -82,7 +72,6 @@ public class AsNode {
public String getFormatVersion() {
return formatVersion;
}
public void setFormatVersion(String formatVersion) {
this.formatVersion = formatVersion;
}
......@@ -90,7 +79,6 @@ public class AsNode {
public ObjectNode getManualTags() {
return manualTags;
}
public void setManualTags(ObjectNode 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;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
......@@ -22,7 +21,10 @@ import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.naming.NameNotFoundException;
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.annotate.JsonSerialize;
import org.codehaus.jackson.type.TypeReference;
/**
......@@ -39,6 +41,7 @@ import org.codehaus.jackson.type.TypeReference;
public class Main implements MessageListener {
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private static final ObjectMapper OBJECTMAPPER = new ObjectMapper();
private static enum fieldType {
DATE, TEXTBOX, USER
......@@ -47,11 +50,19 @@ public class Main implements MessageListener {
ID, TYPE, LABEL, KEY, VALUE
}
private String dataUUID = null; // идентификатор данных по форме записи реестра
private String executionID = 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;
@JsonSerialize(using = AsNodeSerializer.class)
private List<AsNode> targetFormData = null;
/**
......@@ -76,7 +87,7 @@ public class Main implements MessageListener {
String value = parser.getText();
switch (fieldName) {
case "dataUUID":
dataUUID = value;
SOURCEFORMDATAUUID = value;
break;
case "executionID":
executionID = value;
......@@ -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);
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()) {
throw new AttributeModificationException("GMP: Passed data has no JSON content");
}
......@@ -102,18 +113,16 @@ public class Main implements MessageListener {
if (SourceRootDataNode == null) {
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 пользователя указанного в форме
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")
String userCardDataUUID = getCardDataUUID(userID, "fb44d99d-1579-4ac3-884a-01a6d4f71f9c");
String userCardDataUUID = getCardDataUUID(userID, TARGETFORMUUID);
// Получение данных целевой карточки по dataUUID
URL targetFormURL = new URL("http://127.0.0.1:8080/Synergy/rest/api/asforms/data/" + userCardDataUUID);
String targetFormDataAsString = synergyApiGetString(targetFormURL);
JsonNode TargetRootNode = objectMapper.readTree(targetFormDataAsString);
URL targetFormURL = new URL(SYNERGYADDRESS + "/rest/api/asforms/data/" + userCardDataUUID);
TARGETFORMDATAUUID = synergyApiGetString(targetFormURL);
JsonNode TargetRootNode = OBJECTMAPPER.readTree(TARGETFORMDATAUUID);
if (TargetRootNode.isNull()) {
throw new AttributeModificationException("GMP: Passed data has no JSON content");
}
......@@ -121,7 +130,7 @@ public class Main implements MessageListener {
if (TargetRootDataNode == null) {
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;
......@@ -141,6 +150,8 @@ public class Main implements MessageListener {
updateFieldValue(this.targetFormData, "start-b1", fields.KEY, b2b1);
updateFieldValue(this.targetFormData, "finish-b1", fields.KEY, b4b1);
updateUserCard();
unlockRoute();
return;
}
......@@ -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
* @param nodesList - List of nodes
......@@ -313,9 +333,6 @@ public class Main implements MessageListener {
}
private static String synergyApiGetString(final URL requestURL) {
String login = "111111";
String password = "1";
try {
String output;
......@@ -323,7 +340,7 @@ public class Main implements MessageListener {
conn.setRequestMethod("GET");
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);
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