Search in sources :

Example 16 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.

the class NN_DBOper method createRecord_sch2.

public void createRecord_sch2(Sms sms) throws PersistenceException {
    PreparedStatementCollection3 psc = getStatementCollection3(sms.getSubmitDate());
    long dueSlot = getNextDueSlotForTargetId(psc, sms.getSmsSet().getTargetId());
    try {
        PreparedStatement ps = psc.createRecordData;
        BoundStatement boundStatement = new BoundStatement(ps);
        boundStatement.setString(Schema.COLUMN_ADDR_DST_DIGITS, sms.getSmsSet().getDestAddr());
        boundStatement.setInt(Schema.COLUMN_ADDR_DST_TON, sms.getSmsSet().getDestAddrTon());
        boundStatement.setInt(Schema.COLUMN_ADDR_DST_NPI, sms.getSmsSet().getDestAddrNpi());
        boundStatement.setUUID(Schema.COLUMN_ID, sms.getDbId());
        boundStatement.setString(Schema.COLUMN_TARGET_ID, sms.getSmsSet().getTargetId());
        boundStatement.setLong(Schema.COLUMN_DUE_SLOT, dueSlot);
        if (sms.getSourceAddr() != null) {
            boundStatement.setString(Schema.COLUMN_ADDR_SRC_DIGITS, sms.getSourceAddr());
        }
        boundStatement.setLong(Schema.COLUMN_IN_SYSTEM, 0L);
        boundStatement.setInt(Schema.COLUMN_ADDR_SRC_TON, sms.getSourceAddrTon());
        boundStatement.setInt(Schema.COLUMN_ADDR_SRC_NPI, sms.getSourceAddrNpi());
        boundStatement.setInt(Schema.COLUMN_DUE_DELAY, sms.getSmsSet().getDueDelay());
        if (sms.getSmsSet().getStatus() != null)
            boundStatement.setInt(Schema.COLUMN_SM_STATUS, sms.getSmsSet().getStatus().getCode());
        boundStatement.setBool(Schema.COLUMN_ALERTING_SUPPORTED, sms.getSmsSet().isAlertingSupported());
        boundStatement.setLong(Schema.COLUMN_MESSAGE_ID, sms.getMessageId());
        boundStatement.setInt(Schema.COLUMN_MO_MESSAGE_REF, sms.getMoMessageRef());
        if (sms.getOrigEsmeName() != null) {
            boundStatement.setString(Schema.COLUMN_ORIG_ESME_NAME, sms.getOrigEsmeName());
        }
        if (sms.getOrigSystemId() != null) {
            boundStatement.setString(Schema.COLUMN_ORIG_SYSTEM_ID, sms.getOrigSystemId());
        }
        if (sms.getSubmitDate() != null) {
            DBOperations.setBoundStatementDate(boundStatement, Schema.COLUMN_SUBMIT_DATE, sms.getSubmitDate());
        }
        if (sms.getServiceType() != null) {
            boundStatement.setString(Schema.COLUMN_SERVICE_TYPE, sms.getServiceType());
        }
        boundStatement.setInt(Schema.COLUMN_ESM_CLASS, sms.getEsmClass());
        boundStatement.setInt(Schema.COLUMN_PROTOCOL_ID, sms.getProtocolId());
        boundStatement.setInt(Schema.COLUMN_PRIORITY, sms.getPriority());
        boundStatement.setInt(Schema.COLUMN_REGISTERED_DELIVERY, sms.getRegisteredDelivery());
        boundStatement.setInt(Schema.COLUMN_REPLACE, sms.getReplaceIfPresent());
        boundStatement.setInt(Schema.COLUMN_DATA_CODING, sms.getDataCoding());
        boundStatement.setInt(Schema.COLUMN_DEFAULT_MSG_ID, sms.getDefaultMsgId());
        if (sms.getShortMessage() != null) {
            boundStatement.setBytes(Schema.COLUMN_MESSAGE, ByteBuffer.wrap(sms.getShortMessage()));
        }
        if (sms.getScheduleDeliveryTime() != null) {
            DBOperations.setBoundStatementDate(boundStatement, Schema.COLUMN_SCHEDULE_DELIVERY_TIME, sms.getScheduleDeliveryTime());
        }
        if (sms.getValidityPeriod() != null) {
            DBOperations.setBoundStatementDate(boundStatement, Schema.COLUMN_VALIDITY_PERIOD, sms.getValidityPeriod());
        }
        boundStatement.setInt(Schema.COLUMN_DELIVERY_COUNT, sms.getDeliveryCount());
        if (sms.getTlvSet().getOptionalParameterCount() > 0) {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                XMLObjectWriter writer = XMLObjectWriter.newInstance(baos);
                writer.setIndentation("\t");
                writer.write(sms.getTlvSet(), TLV_SET, TlvSet.class);
                writer.close();
                byte[] rawData = baos.toByteArray();
                String serializedEvent = new String(rawData);
                boundStatement.setString(Schema.COLUMN_OPTIONAL_PARAMETERS, serializedEvent);
            } catch (XMLStreamException e) {
                String msg = "XMLStreamException when serializing optional parameters for '" + sms.getDbId() + "'!";
                throw new PersistenceException(msg, e);
            }
        }
        ResultSet res = session.execute(boundStatement);
    } catch (Exception e1) {
        String msg = "Failed createRecord !";
        throw new PersistenceException(msg, e1);
    }
}
Also used : XMLStreamException(javolution.xml.stream.XMLStreamException) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) XMLObjectWriter(javolution.xml.XMLObjectWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BoundStatement(com.datastax.driver.core.BoundStatement) XMLStreamException(javolution.xml.stream.XMLStreamException) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException)

Example 17 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.

the class NN_DBOper method doGetStatementCollection3.

private synchronized PreparedStatementCollection3 doGetStatementCollection3(String tName) throws PersistenceException {
    PreparedStatementCollection3 psc = dataTableRead3.get(tName);
    if (psc != null)
        return psc;
    try {
        try {
            // checking if a datatable exists
            String s1 = "SELECT * FROM \"" + Schema.FAMILY_SLOT_MESSAGES_TABLE + tName + "\";";
            PreparedStatement ps = session.prepare(s1);
        } catch (InvalidQueryException e) {
            // datatable does not exist
            // SLOT_MESSAGES_TABLE
            StringBuilder sb = new StringBuilder();
            sb.append("CREATE TABLE \"" + Schema.FAMILY_SLOT_MESSAGES_TABLE);
            sb.append(tName);
            sb.append("\" (");
            appendField(sb, Schema.COLUMN_ID, "uuid");
            appendField(sb, Schema.COLUMN_TARGET_ID, "ascii");
            appendField(sb, Schema.COLUMN_DUE_SLOT, "bigint");
            appendField(sb, Schema.COLUMN_IN_SYSTEM, "bigint");
            appendField(sb, Schema.COLUMN_ADDR_DST_DIGITS, "ascii");
            appendField(sb, Schema.COLUMN_ADDR_DST_TON, "int");
            appendField(sb, Schema.COLUMN_ADDR_DST_NPI, "int");
            appendField(sb, Schema.COLUMN_ADDR_SRC_DIGITS, "ascii");
            appendField(sb, Schema.COLUMN_ADDR_SRC_TON, "int");
            appendField(sb, Schema.COLUMN_ADDR_SRC_NPI, "int");
            appendField(sb, Schema.COLUMN_DUE_DELAY, "int");
            appendField(sb, Schema.COLUMN_ALERTING_SUPPORTED, "boolean");
            appendField(sb, Schema.COLUMN_MESSAGE_ID, "bigint");
            appendField(sb, Schema.COLUMN_MO_MESSAGE_REF, "int");
            appendField(sb, Schema.COLUMN_ORIG_ESME_NAME, "text");
            appendField(sb, Schema.COLUMN_ORIG_SYSTEM_ID, "text");
            appendField(sb, Schema.COLUMN_DEST_CLUSTER_NAME, "text");
            appendField(sb, Schema.COLUMN_DEST_ESME_NAME, "text");
            appendField(sb, Schema.COLUMN_DEST_SYSTEM_ID, "text");
            appendField(sb, Schema.COLUMN_SUBMIT_DATE, "timestamp");
            appendField(sb, Schema.COLUMN_DELIVERY_DATE, "timestamp");
            appendField(sb, Schema.COLUMN_SERVICE_TYPE, "text");
            appendField(sb, Schema.COLUMN_ESM_CLASS, "int");
            appendField(sb, Schema.COLUMN_PROTOCOL_ID, "int");
            appendField(sb, Schema.COLUMN_PRIORITY, "int");
            appendField(sb, Schema.COLUMN_REGISTERED_DELIVERY, "int");
            appendField(sb, Schema.COLUMN_REPLACE, "int");
            appendField(sb, Schema.COLUMN_DATA_CODING, "int");
            appendField(sb, Schema.COLUMN_DEFAULT_MSG_ID, "int");
            appendField(sb, Schema.COLUMN_MESSAGE, "blob");
            appendField(sb, Schema.COLUMN_OPTIONAL_PARAMETERS, "text");
            appendField(sb, Schema.COLUMN_SCHEDULE_DELIVERY_TIME, "timestamp");
            appendField(sb, Schema.COLUMN_VALIDITY_PERIOD, "timestamp");
            appendField(sb, Schema.COLUMN_IMSI, "ascii");
            appendField(sb, Schema.COLUMN_NNN_DIGITS, "ascii");
            appendField(sb, Schema.COLUMN_NNN_AN, "int");
            appendField(sb, Schema.COLUMN_NNN_NP, "int");
            appendField(sb, Schema.COLUMN_SM_STATUS, "int");
            appendField(sb, Schema.COLUMN_SM_TYPE, "int");
            appendField(sb, Schema.COLUMN_DELIVERY_COUNT, "int");
            sb.append("PRIMARY KEY ((\"");
            sb.append(Schema.COLUMN_DUE_SLOT);
            sb.append("\"), \"");
            sb.append(Schema.COLUMN_TARGET_ID);
            sb.append("\", \"");
            sb.append(Schema.COLUMN_ID);
            sb.append("\"");
            sb.append("));");
            String s2 = sb.toString();
            PreparedStatement ps = session.prepare(s2);
            BoundStatement boundStatement = new BoundStatement(ps);
            ResultSet res = session.execute(boundStatement);
        }
    } catch (Exception e1) {
        String msg = "Failed to access or create table " + tName + "!";
        throw new PersistenceException(msg, e1);
    }
    psc = new PreparedStatementCollection3(tName);
    dataTableRead3.putEntry(tName, psc);
    return psc;
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) XMLStreamException(javolution.xml.stream.XMLStreamException) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException)

Example 18 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.

the class NN_DBOper method appendIndex.

private void appendIndex(String tName, String fieldName) {
    StringBuilder sb = new StringBuilder();
    sb.append("CREATE INDEX ON \"");
    sb.append(tName);
    sb.append("\" (\"");
    sb.append(fieldName);
    sb.append("\");");
    String s2 = sb.toString();
    PreparedStatement ps = session.prepare(s2);
    BoundStatement boundStatement = new BoundStatement(ps);
    ResultSet res = session.execute(boundStatement);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 19 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.

the class NN_DBOper method getSmsSetForTargetId.

public SmsSet getSmsSetForTargetId(Date[] dtt, NN_LoadedTargetId ti) throws PersistenceException {
    SmsSet smsSet = null;
    for (Date dt : dtt) {
        PreparedStatementCollection2 psc = getStatementCollection(dt);
        ArrayList<UUID> lst2 = new ArrayList<UUID>();
        try {
            PreparedStatement ps = psc.getIdListDests;
            BoundStatement boundStatement = new BoundStatement(ps);
            boundStatement.bind(ti.getTargetId());
            ResultSet res = session.execute(boundStatement);
            for (Row row : res) {
                UUID id = row.getUUID(0);
                boolean sent = row.getBool(1);
                if (!sent)
                    lst2.add(id);
            }
        } catch (Exception e1) {
            String msg = "Failed reading a set of ID from DESTS for targetId " + ti.getTargetId() + " !";
            throw new PersistenceException(msg, e1);
        }
        try {
            for (UUID id : lst2) {
                PreparedStatement ps = psc.getSms;
                BoundStatement boundStatement = new BoundStatement(ps);
                boundStatement.bind(ti.getTargetId(), id);
                ResultSet res = session.execute(boundStatement);
                for (Row row : res) {
                    Sms sms = createSms(row, smsSet);
                    smsSet = sms.getSmsSet();
                }
            }
        } catch (Exception e1) {
            String msg = "Failed reading SmsSet for " + ti.getTargetId() + " !";
            throw new PersistenceException(msg, e1);
        }
    }
    if (smsSet != null)
        smsSet.resortSms();
    else {
        // no message records - removing TARGET_ID from SLOTS
        PreparedStatementCollection2 psc = getStatementCollection(ti.getDtx());
        PreparedStatement ps = psc.deleteRecordSlots;
        BoundStatement boundStatement = new BoundStatement(ps);
        boundStatement.bind(ti.getDueSlot(), ti.getTargetId());
        ResultSet res = session.execute(boundStatement);
    }
    return smsSet;
}
Also used : ArrayList(java.util.ArrayList) PreparedStatement(com.datastax.driver.core.PreparedStatement) Date(java.util.Date) XMLStreamException(javolution.xml.stream.XMLStreamException) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) ResultSet(com.datastax.driver.core.ResultSet) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) Sms(org.mobicents.smsc.library.Sms) Row(com.datastax.driver.core.Row) UUID(java.util.UUID) BoundStatement(com.datastax.driver.core.BoundStatement) SmsSet(org.mobicents.smsc.library.SmsSet)

Example 20 with PreparedStatement

use of com.datastax.driver.core.PreparedStatement in project smscgateway by RestComm.

the class NN_DBOper method getDueSlotForTargetId.

private long getDueSlotForTargetId(PreparedStatementCollection3 psc, String targetId) throws PersistenceException {
    try {
        PreparedStatement ps = psc.getDueSlotForTargetId;
        BoundStatement boundStatement = new BoundStatement(ps);
        boundStatement.bind(targetId);
        ResultSet res = session.execute(boundStatement);
        long l = 0;
        for (Row row : res) {
            l = row.getLong(0);
            break;
        }
        return l;
    } catch (Exception e1) {
        String msg = "Failed to execute getDueSlotForTargetId() !";
        throw new PersistenceException(msg, e1);
    }
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) PreparedStatement(com.datastax.driver.core.PreparedStatement) Row(com.datastax.driver.core.Row) BoundStatement(com.datastax.driver.core.BoundStatement) XMLStreamException(javolution.xml.stream.XMLStreamException) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException)

Aggregations

PreparedStatement (com.datastax.driver.core.PreparedStatement)113 ResultSet (com.datastax.driver.core.ResultSet)60 BoundStatement (com.datastax.driver.core.BoundStatement)59 Session (com.datastax.driver.core.Session)39 Test (org.junit.Test)30 Row (com.datastax.driver.core.Row)27 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)27 XMLStreamException (javolution.xml.stream.XMLStreamException)25 PersistenceException (org.mobicents.smsc.cassandra.PersistenceException)15 Cluster (com.datastax.driver.core.Cluster)9 Date (java.util.Date)9 IInvokableInstance (org.apache.cassandra.distributed.api.IInvokableInstance)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)7 QueryProcessor (org.apache.cassandra.cql3.QueryProcessor)7 GOSSIP (org.apache.cassandra.distributed.api.Feature.GOSSIP)7 NATIVE_PROTOCOL (org.apache.cassandra.distributed.api.Feature.NATIVE_PROTOCOL)7 NETWORK (org.apache.cassandra.distributed.api.Feature.NETWORK)7 ICluster (org.apache.cassandra.distributed.api.ICluster)7