Search in sources :

Example 56 with PreparedStatement

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

the class TT_PersistenceProxy method testCassandraAccess.

public boolean testCassandraAccess() {
    String ip = "127.0.0.1";
    String keyspace = "RestCommSMSC";
    try {
        Cluster cluster = Cluster.builder().addContactPoint(ip).build();
        try {
            Metadata metadata = cluster.getMetadata();
            for (Host host : metadata.getAllHosts()) {
                logger.info(String.format("Datacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack()));
            }
            Session session = cluster.connect();
            session.execute("USE \"" + keyspace + "\"");
            // testing if a keyspace is acceptable
            PreparedStatement ps = session.prepare("DROP TABLE \"TEST_TABLE\";");
            BoundStatement boundStatement = new BoundStatement(ps);
            boundStatement.bind();
            try {
                session.execute(boundStatement);
            } catch (Exception e) {
                int g1 = 0;
                g1++;
            }
            ps = session.prepare("CREATE TABLE \"TEST_TABLE\" ( id uuid primary key ) ;");
            boundStatement = new BoundStatement(ps);
            boundStatement.bind();
            session.execute(boundStatement);
            // deleting of current tables
            ps = session.prepare("DROP TABLE \"" + Schema.FAMILY_SMPP_SMS_ROUTING_RULE + "\";");
            boundStatement = new BoundStatement(ps);
            boundStatement.bind();
            try {
                session.execute(boundStatement);
            } catch (Exception e) {
                int g1 = 0;
                g1++;
            }
            return true;
        } finally {
            cluster.close();
        // cluster.shutdown();
        }
    } catch (Exception e) {
        return false;
    }
// try {
// 
// Cluster cluster = Cluster.builder().addContactPoint(ip).build();
// Metadata metadata = cluster.getMetadata();
// 
// for (Host host : metadata.getAllHosts()) {
// logger.info(String.format("Datacenter: %s; Host: %s; Rack: %s\n", host.getDatacenter(), host.getAddress(), host.getRack()));
// }
// 
// Session session = cluster.connect();
// 
// session.execute("USE \"" + keyspace + "\"");
// 
// PreparedStatement ps = session.prepare("select * from \"" + Schema.FAMILY_SMS_ROUTING_RULE + "\" limit 1;");
// BoundStatement boundStatement = new BoundStatement(ps);
// boundStatement.bind();
// session.execute(boundStatement);
// 
// return true;
// } catch (Exception e) {
// return false;
// }
}
Also used : Metadata(com.datastax.driver.core.Metadata) Cluster(com.datastax.driver.core.Cluster) Host(com.datastax.driver.core.Host) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Session(com.datastax.driver.core.Session)

Example 57 with PreparedStatement

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

the class CassandraToolForm method getData.

private void getData() {
    try {
        String tName = this.dbOperations.getTableName(new Date());
        String tbFam;
        int option = 0;
        if (this.rbLive.isSelected()) {
            tbFam = Schema.FAMILY_SLOT_MESSAGES_TABLE;
        } else if (this.rbArchive.isSelected()) {
            tbFam = Schema.FAMILY_MESSAGES;
        } else if (this.rbMsgId.isSelected()) {
            tbFam = Schema.FAMILY_MES_ID;
            option = 1;
        } else {
            tbFam = Schema.FAMILY_DLV_MES_ID;
            option = 2;
        }
        String s1 = "SELECT * FROM \"" + tbFam + tName + "\";";
        Session session = this.dbOperations.getSession();
        PreparedStatement ps = session.prepare(s1);
        BoundStatement boundStatement = new BoundStatement(ps);
        ResultSet res = session.execute(boundStatement);
        model.getDataVector().clear();
        model.rowsRemoved(new TableModelEvent(model));
        DateFormat df = new SimpleDateFormat("MM.dd HH:mm:ss");
        if (option == 0) {
            SortedMap<Long, ArrayList<SmsSet>> result = new TreeMap<Long, ArrayList<SmsSet>>();
            for (Row row : res) {
                SmsSet smsSet = this.dbOperations.createSms(row, null);
                if (smsSet != null) {
                    ArrayList<SmsSet> al = result.get(smsSet.getSms(0).getMessageId());
                    if (al == null) {
                        al = new ArrayList<SmsSet>();
                        result.put(smsSet.getSms(0).getMessageId(), al);
                    }
                    al.add(smsSet);
                }
            }
            for (ArrayList<SmsSet> al : result.values()) {
                for (SmsSet smsSet : al) {
                    ListSelectionModel l = tResult.getSelectionModel();
                    Vector newRow = new Vector();
                    newRow.add(smsSet.getTargetId());
                    newRow.add(smsSet.getSms(0).getDbId());
                    String reportDate = df.format(this.dbOperations.c2_getTimeForDueSlot(smsSet.getSms(0).getDueSlot()));
                    newRow.add(smsSet.getSms(0).getDueSlot() + " - " + reportDate);
                    newRow.add(smsSet.getSms(0).getMessageId());
                    newRow.add(smsSet.getInSystem());
                    newRow.add(smsSet.getSms(0).getDeliveryCount());
                    newRow.add(smsSet.getStatus());
                    Date dt = smsSet.getSms(0).getValidityPeriod();
                    if (dt != null)
                        reportDate = df.format(dt);
                    else
                        reportDate = "";
                    newRow.add(reportDate);
                    dt = smsSet.getSms(0).getScheduleDeliveryTime();
                    if (dt != null)
                        reportDate = df.format(dt);
                    else
                        reportDate = "";
                    newRow.add(reportDate);
                    newRow.add(smsSet.getSms(0).getShortMessageText());
                    model.getDataVector().add(0, newRow);
                    model.newRowsAdded(new TableModelEvent(model));
                }
            }
        } else if (option == 1) {
            for (Row row : res) {
                ListSelectionModel l = tResult.getSelectionModel();
                Vector newRow = new Vector();
                newRow.add(row.getString(Schema.COLUMN_ADDR_DST_DIGITS));
                newRow.add(row.getUUID(Schema.COLUMN_ID));
                newRow.add("");
                newRow.add(row.getLong(Schema.COLUMN_MESSAGE_ID));
                newRow.add("");
                newRow.add("");
                newRow.add("");
                newRow.add("");
                newRow.add("");
                newRow.add("");
                model.getDataVector().add(0, newRow);
                model.newRowsAdded(new TableModelEvent(model));
            }
        } else {
            for (Row row : res) {
                ListSelectionModel l = tResult.getSelectionModel();
                Vector newRow = new Vector();
                newRow.add(row.getString(Schema.COLUMN_REMOTE_MESSAGE_ID));
                newRow.add("");
                newRow.add(row.getString(Schema.COLUMN_DEST_ID));
                newRow.add(row.getLong(Schema.COLUMN_MESSAGE_ID));
                newRow.add("");
                newRow.add("");
                newRow.add("");
                newRow.add("");
                newRow.add("");
                newRow.add("");
                model.getDataVector().add(0, newRow);
                model.newRowsAdded(new TableModelEvent(model));
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(getJFrame(), "Can not get data from cassandra database:\n" + e.getMessage());
    }
}
Also used : TableModelEvent(javax.swing.event.TableModelEvent) ArrayList(java.util.ArrayList) ListSelectionModel(javax.swing.ListSelectionModel) PreparedStatement(com.datastax.driver.core.PreparedStatement) TreeMap(java.util.TreeMap) Date(java.util.Date) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) BoundStatement(com.datastax.driver.core.BoundStatement) SimpleDateFormat(java.text.SimpleDateFormat) Vector(java.util.Vector) SmsSet(org.mobicents.smsc.library.SmsSet) Session(com.datastax.driver.core.Session)

Example 58 with PreparedStatement

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

the class NN_DBOper method createRecord.

// SCHEMA 1 ************************
public void createRecord(long dueSlot, Sms sms) throws PersistenceException {
    PreparedStatementCollection2 psc = getStatementCollection(sms.getSubmitDate());
    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.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);
        ps = psc.createRecordSlots;
        boundStatement = new BoundStatement(ps);
        boundStatement.setLong(Schema.COLUMN_DUE_SLOT, dueSlot);
        boundStatement.setString(Schema.COLUMN_TARGET_ID, sms.getSmsSet().getTargetId());
        res = session.execute(boundStatement);
        ps = psc.createRecordDests;
        boundStatement = new BoundStatement(ps);
        boundStatement.setString(Schema.COLUMN_TARGET_ID, sms.getSmsSet().getTargetId());
        boundStatement.setUUID(Schema.COLUMN_ID, sms.getDbId());
        boundStatement.setBool(Schema.COLUMN_SENT, false);
        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 59 with PreparedStatement

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

the class NN_DBOper method deleteIdFromDests.

public void deleteIdFromDests(Sms sms) throws PersistenceException {
    PreparedStatementCollection2 psc = getStatementCollection(sms.getSubmitDate());
    PreparedStatement ps = psc.updateRecordDests;
    BoundStatement boundStatement = new BoundStatement(ps);
    boundStatement.bind(sms.getSmsSet().getTargetId(), sms.getDbId());
    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 60 with PreparedStatement

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

the class NN_DBOper method getTargetIdListForDueSlot.

public List<NN_LoadedTargetId> getTargetIdListForDueSlot(Date[] dtt, long dueSlot, long newDueSlot, int maxRecordCount) throws PersistenceException {
    List<NN_LoadedTargetId> lst = new ArrayList<NN_LoadedTargetId>();
    for (Date dt : dtt) {
        PreparedStatementCollection2 psc = getStatementCollection(dt);
        try {
            // reading a set of TARGET_ID
            String sa = "SELECT \"" + Schema.COLUMN_TARGET_ID + "\" FROM \"" + Schema.FAMILY_SLOTS + psc.tName + "\" where \"" + Schema.COLUMN_DUE_SLOT + "\"=? limit " + maxRecordCount + " allow filtering;";
            PreparedStatement ps = session.prepare(sa);
            BoundStatement boundStatement = new BoundStatement(ps);
            boundStatement.bind(dueSlot);
            ResultSet res = session.execute(boundStatement);
            for (Row row : res) {
                String s = row.getString(0);
                lst.add(new NN_LoadedTargetId(s, dt, newDueSlot));
            }
        } catch (Exception e1) {
            String msg = "Failed reading a set of TARGET_ID from SLOTS for dueSlot " + dueSlot + " !";
            throw new PersistenceException(msg, e1);
        }
        // deleting TARGET_ID form SLOTS table and adding new one for later time
        String s1 = null;
        try {
            for (NN_LoadedTargetId ti : lst) {
                s1 = ti.getTargetId();
                PreparedStatement ps = psc.deleteRecordSlots;
                BoundStatement boundStatement = new BoundStatement(ps);
                boundStatement.bind(dueSlot, ti.getTargetId());
                ResultSet res = session.execute(boundStatement);
                ps = psc.createRecordSlots;
                boundStatement = new BoundStatement(ps);
                boundStatement.setLong(Schema.COLUMN_DUE_SLOT, newDueSlot);
                boundStatement.setString(Schema.COLUMN_TARGET_ID, ti.getTargetId());
                res = session.execute(boundStatement);
            }
        } catch (Exception e1) {
            logger.error("Failed removing a TARGET_ID from SLOTS " + s1 + " !", e1);
        }
        if (lst.size() > 0)
            break;
    }
    return lst;
}
Also used : ArrayList(java.util.ArrayList) 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) Date(java.util.Date) 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