package com.j_spaces.examples.spacestore;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;
import com.j_spaces.core.EntryHolder;
import com.j_spaces.core.sadapter.SAException;
import com.j_spaces.sadapter.GenericPA.GenericPersistentAdapter;
import com.j_spaces.sadapter.GenericPA.IBasicPersistence;
import com.j_spaces.sadapter.GenericPA.IQueryblePersistentAdapter;
import com.j_spaces.sadapter.GenericPA.PAObjectTypes;
import com.j_spaces.sadapter.GenericPA.PersistentContext;
import com.j_spaces.sadapter.GenericPA.PersistentEntry;
import com.j_spaces.sadapter.GenericPA.PersistentMatchTemplate;
import com.j_spaces.sadapter.GenericPA.Record;
public class SpaceStore implements IBasicPersistence {
String myTableName = "MyTable";
String myClassName = TestData.class.getName();
Connection dbConnection;
public void init(String url, String user, String password,
boolean isColdMode, Properties properties) throws SAException {
try {
Class.forName("org.hsqldb.jdbcDriver").newInstance();
System.out.println("Connection to HSQL database - URL:" + url); dbConnection = DriverManager.getConnection(url, "sa", "");
System.out.println();
System.out.println(" ********* SPACE STORE STARTED ********* ");
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}
}
public void initContext(PersistentContext context) throws SAException {
}
public void closeContext(PersistentContext context) throws SAException {
}
public void insert(PersistentContext context, PersistentEntry entry)
throws SAException {
if (entry.m_ClassName == null)
return;
try {
if (entry.m_ClassName.equals(myClassName)) {
Vector fields = getFields("Insert", entry);
String insertSQL = "INSERT INTO " + myTableName
+ " VALUES(?,?,?,?,?,?)";
PreparedStatement psInsert = dbConnection
.prepareStatement(insertSQL);
/*
* Field:0 - _DATE , TimeStamp Field:1 - ID INTEGER Field:2 -
* ITEM VARCHAR Field:3 - PRICE DOUBLE Field:4 - QUANTITY DOUBLE
*/
psInsert.setString(1, (String) entry.getUID());
psInsert.setTimestamp(2, (Timestamp) fields.get(0));
psInsert.setInt(3, (Integer) fields.get(1));
psInsert.setString(4, (String) fields.get(2));
psInsert.setDouble(5, (Double) fields.get(3));
psInsert.setDouble(6, (Double) fields.get(4));
int r = psInsert.executeUpdate();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public int delete(PersistentContext context, String className, Object uid)
throws SAException {
try {
if (className.equals(myClassName)) {
System.out.println("delete:" + className + " UID:" + uid);
String deleteSQL = "DELETE FROM " + myTableName
+ " WHERE __UID='" + uid + "'";
Statement st = dbConnection.createStatement();
st.executeUpdate(deleteSQL);
st.close();
return 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public void update(PersistentContext context, PersistentEntry entry)
throws SAException {
getFields("Update", entry);
}
public void createClassTable(PersistentContext context, String className,
String[] fieldNames, String[] fieldTypes, String uidFieldName,
String uidFieldType, boolean[] indexIndicators,
boolean isFifoSupported) throws SAException {
try {
if (className.equals(myClassName)) {
System.out.println("createClassTable - ClassName:" + className);
/*
* Field:0 - _DATE , DATE Field:1 - ID INTEGER Field:2 - ITEM
* VARCHAR Field:3 - PRICE DOUBLE Field:4 - QUANTITY DOUBLE
*/
String createSQL = "CREATE TABLE "
+ myTableName
+ "(__UID VARCHAR(3000) , _DATE date , ID INTEGER ,
ITEM VARCHAR(3000), PRICE DOUBLE,QUANTITY DOUBLE)";
Statement st = dbConnection.createStatement();
st.executeUpdate(createSQL);
st.executeUpdate("CREATE INDEX __UIDIndex ON " + myTableName
+ " (__UID )");
st.executeUpdate("CREATE INDEX _DATEIndex ON " + myTableName
+ " (_DATE )");
st.executeUpdate("CREATE INDEX IDIndex ON " + myTableName
+ " (ID)");
st.executeUpdate("CREATE INDEX QUANTITYIndex ON " + myTableName
+ " (QUANTITY )");
st.executeUpdate("CREATE INDEX PRICEIndex ON " + myTableName
+ " (PRICE)");
st.close();
System.out.println("createClassTable - ClassName:" + className
+ " OK!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void dropClassTable(PersistentContext context, String className)
throws SAException {
try {
if (className == null)
return;
if (className.equals(myClassName)) {
Statement st = dbConnection.createStatement();
st.executeUpdate("DROP TABLE " + myTableName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Iterator getEntries(PersistentContext context, String className,
Object[] uids) throws SAException {
Iterator iter = null;
Vector vec = new Vector();
return vec.iterator();
}
public PersistentEntry getEntry(PersistentContext context,
String className, Object uid) throws SAException {
return null;
}
public void shutDown() throws SAException {
}
public void createIndex(PersistentContext context, String className,
String fieldType, int fieldPosition) throws SAException {
}
public void dropIndex(PersistentContext context, String className,
String indexName) throws SAException {
}
public void flush(PersistentContext arg0) throws SAException {
}
public int getCount(PersistentContext context, String className)
throws SAException {
return 0;
}
private Vector getFields(String operation, PersistentEntry entry) {
Vector fields = new Vector();
System.out.println("Operation:" + operation + " Entry "
+ entry.getUID() + " Class:" + entry.getClazzName());
int fldCount = entry.getFieldsValues().length;
fldCount = fldCount - 7;
for (int i = 0; i < fldCount; i++) {
System.out.println("Field:" + i + " " + entry.getFieldsValues()[i]);
fields.add(entry.getFieldsValues()[i]);
}
return fields;
}
private Vector getTemplateFields(String operation, PersistentMatchTemplate entry) {
Vector fields = new Vector();
System.out.println("Operation:" + operation + " Class:" + entry.getClazzName());
int fldCount = entry.getFieldValues().length;
for (int i = 0; i < fldCount; i++) {
System.out.println("Field:" + i + " " + entry.getFieldValues()[i]);
fields.add(entry.getFieldValues()[i]);
}
return fields;
}
}
Add Comment