Search in sources :

Example 1 with ReplicationMessage

use of com.dexels.replication.api.ReplicationMessage in project navajo by Dexels.

the class JsonTmlConverterImpl method toReplicationNavajo.

/* (non-Javadoc)
	 * @see com.dexels.navajo.document.json.impl.JsonTmlConferter#toNavajo(com.dexels.replication.api.ReplicationMessage, java.lang.String, java.lang.String, java.lang.String)
	 */
@Override
public Navajo toReplicationNavajo(ReplicationMessage message, String tenant, String table, Optional<String> datasource) {
    try {
        Navajo n = NavajoFactory.getInstance().createNavajo();
        Message msg = createTransactionMessage(message, tenant, table, datasource, n);
        n.addMessage(msg);
        // Add PK values.
        Message pM = createPrimaryKeyMessage(message, n);
        msg.addMessage(pM);
        // Add column values.
        Message cV = createColumnsMessage(message, n);
        msg.addMessage(cV);
        // }
        return n;
    } catch (Exception e) {
        logger.error("Error: ", e);
    }
    return null;
}
Also used : Message(com.dexels.navajo.document.Message) ReplicationMessage(com.dexels.replication.api.ReplicationMessage) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Navajo(com.dexels.navajo.document.Navajo)

Example 2 with ReplicationMessage

use of com.dexels.replication.api.ReplicationMessage in project navajo by Dexels.

the class TestProtobufConversion method testConversion.

@Test
public void testConversion() throws JsonGenerationException, JsonMappingException, IOException {
    ReplicationMessage createReplicationMessage = createReplicationMessage();
    // convert to protobuf
    byte[] serializedMessage = ReplicationFactory.getInstance().serialize(createReplicationMessage);
    System.out.println(new String(serializedMessage));
    assertNotNull(serializedMessage);
    // parse protobuf
    ReplicationMessage rmsg = ReplicationFactory.getInstance().parseBytes(Optional.empty(), serializedMessage);
    System.err.println("Rescribe: " + new String(rmsg.toBytes(new JSONReplicationMessageParserImpl())));
    Navajo rr = JsonTmlFactory.getInstance().toReplicationNavajo(rmsg, "Tenant", "Table", Optional.of("Datasource"));
    rr.write(System.out);
    // Check
    Assert.assertEquals(6, rr.getMessage("Transaction/Columns").getArraySize());
}
Also used : JSONReplicationMessageParserImpl(com.dexels.replication.impl.json.JSONReplicationMessageParserImpl) Navajo(com.dexels.navajo.document.Navajo) ReplicationMessage(com.dexels.replication.api.ReplicationMessage) Test(org.junit.Test)

Example 3 with ReplicationMessage

use of com.dexels.replication.api.ReplicationMessage in project navajo by Dexels.

the class JsonTmlConverterImpl method createColumnsMessage.

private Message createColumnsMessage(ReplicationMessage message, Navajo n) {
    Message cV = NavajoFactory.getInstance().createMessage(n, "Columns", Message.MSG_TYPE_ARRAY);
    for (String columnName : message.columnNames()) {
        Message vM = NavajoFactory.getInstance().createMessage(n, "Columns");
        cV.addElement(vM);
        Object value = message.columnValue(columnName);
        Property colPropName = NavajoFactory.getInstance().createProperty(n, "Name", Property.STRING_PROPERTY, columnName, 0, "", Property.DIR_OUT);
        Property colPropValue = NavajoFactory.getInstance().createProperty(n, "Value", Property.STRING_PROPERTY, "", 0, "", Property.DIR_OUT);
        colPropValue.setAnyValue(value);
        vM.addProperty(colPropName);
        vM.addProperty(colPropValue);
    }
    return cV;
}
Also used : Message(com.dexels.navajo.document.Message) ReplicationMessage(com.dexels.replication.api.ReplicationMessage) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Property(com.dexels.navajo.document.Property)

Example 4 with ReplicationMessage

use of com.dexels.replication.api.ReplicationMessage in project navajo by Dexels.

the class JsonTmlConverterImpl method createTransactionMessage.

private Message createTransactionMessage(ReplicationMessage message, String tenant, String table, Optional<String> datasource, Navajo n) {
    String transactionId = message.transactionId();
    Message msg = NavajoFactory.getInstance().createMessage(n, "Transaction");
    Property transactionIdProp = NavajoFactory.getInstance().createProperty(n, "TransactionId", Property.STRING_PROPERTY, transactionId, 0, "", Property.DIR_OUT);
    Property timestamp = NavajoFactory.getInstance().createProperty(n, "Timestamp", Property.LONG_PROPERTY, message.timestamp() + "", 0, "", Property.DIR_OUT);
    Property dbOperation = NavajoFactory.getInstance().createProperty(n, "Operation", Property.STRING_PROPERTY, message.operation().toString(), 0, "", Property.DIR_OUT);
    Property tenantProp = NavajoFactory.getInstance().createProperty(n, "Tenant", Property.STRING_PROPERTY, tenant, 0, "", Property.DIR_OUT);
    Property tableProp = NavajoFactory.getInstance().createProperty(n, "SourceTable", Property.STRING_PROPERTY, table, 0, "", Property.DIR_OUT);
    Property status = NavajoFactory.getInstance().createProperty(n, "Status", Property.STRING_PROPERTY, "PENDING", 0, "", Property.DIR_OUT);
    msg.addProperty(transactionIdProp);
    msg.addProperty(tenantProp);
    msg.addProperty(tableProp);
    if (datasource.isPresent()) {
        Property source = NavajoFactory.getInstance().createProperty(n, "DataSource", Property.STRING_PROPERTY, datasource.get(), 0, "", Property.DIR_OUT);
        msg.addProperty(source);
    }
    msg.addProperty(timestamp);
    msg.addProperty(dbOperation);
    msg.addProperty(status);
    return msg;
}
Also used : Message(com.dexels.navajo.document.Message) ReplicationMessage(com.dexels.replication.api.ReplicationMessage) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Property(com.dexels.navajo.document.Property)

Example 5 with ReplicationMessage

use of com.dexels.replication.api.ReplicationMessage in project navajo by Dexels.

the class JsonTmlConverterImpl method createPrimaryKeyMessage.

private Message createPrimaryKeyMessage(ReplicationMessage message, Navajo n) {
    Message pM = NavajoFactory.getInstance().createMessage(n, "PrimaryKey");
    pM.setType(Message.MSG_TYPE_ARRAY);
    for (String primaryKey : message.primaryKeys()) {
        Message vM = NavajoFactory.getInstance().createMessage(n, "PrimaryKey");
        pM.addElement(vM);
        Object value = message.columnValue(primaryKey);
        Property colPropName = NavajoFactory.getInstance().createProperty(n, "Name", Property.STRING_PROPERTY, primaryKey, 0, "", Property.DIR_OUT);
        Property colPropValue = NavajoFactory.getInstance().createProperty(n, "Value", Property.STRING_PROPERTY, "", 0, "", Property.DIR_OUT);
        colPropValue.setAnyValue(value);
        vM.addProperty(colPropName);
        vM.addProperty(colPropValue);
    }
    return pM;
}
Also used : Message(com.dexels.navajo.document.Message) ReplicationMessage(com.dexels.replication.api.ReplicationMessage) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Property(com.dexels.navajo.document.Property)

Aggregations

ReplicationMessage (com.dexels.replication.api.ReplicationMessage)8 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)6 Message (com.dexels.navajo.document.Message)6 Navajo (com.dexels.navajo.document.Navajo)5 Property (com.dexels.navajo.document.Property)4 Test (org.junit.Test)3 JSONReplicationMessageParserImpl (com.dexels.replication.impl.json.JSONReplicationMessageParserImpl)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1