package com.nexirius.ulc.example1.persistence;
import com.nexirius.framework.application.DialogManager;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.DataModelEnumeration;
import com.nexirius.framework.datamodel.DataModelVector;
import com.nexirius.framework.datamodel.ModelFlag;
import com.nexirius.framework.jdbc.DatabaseTableMapping;
import com.nexirius.framework.jdbc.JdbcConnectionHandler;
import com.nexirius.framework.jdbc.DatabaseFieldTranslator;
import com.nexirius.framework.jdbc.JnexPreparedStatement;
import com.nexirius.ulc.example1.datamodel.ItemArrayModel;
import com.nexirius.ulc.example1.datamodel.ItemModel;
import com.nexirius.ulc.example1.datamodel.ItemSubStructure;
import com.nexirius.util.XFile;
import com.nexirius.util.StringVector;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.sql.ResultSet;
public class PersistenceManager {
public static void init() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
} catch (InstantiationException e) {
e.printStackTrace(); //TODO
} catch (IllegalAccessException e) {
e.printStackTrace(); //TODO
} catch (ClassNotFoundException e) {
e.printStackTrace(); //TODO
}
String dbFileName = "testDatabase.mdb";
String dbDirName = "C:\\jnex\\";
String dbPathName = dbDirName + dbFileName;
XFile dbFile = new XFile(dbPathName);
if (!dbFile.exists()) {
InputStream stream = new BufferedInputStream(dbFile.getClass().getClassLoader().getResourceAsStream(dbFileName));
try {
new XFile(dbDirName).mkdirs();
dbFile.createFrom(stream);
} catch (IOException e) {
e.printStackTrace();
DialogManager.error(e);
}
}
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/jnex/testDatabase.mdb;DriverID=22;READONLY=true}"; // add on to the end
JdbcConnectionHandler.init(new JdbcConnectionHandler(database, null, null));
//DatabaseTableMapping itemMapping = DatabaseTableMapping.createStandardMapping(new ItemModel());
DatabaseTableMapping itemMapping = new DatabaseTableMapping(ItemModel.FIELD_NAME, "PK",
new String[]{ItemModel.FIELD_booleanField,
ItemModel.FIELD_dateField,
ItemModel.FIELD_doubleField,
ItemModel.FIELD_intField,
ItemModel.FIELD_stringField,
ItemModel.FIELD_timeField,
ItemSubStructure.FIELD_NAME + DataModel.FIELD_NAME_SEPARATOR_CHAR + ItemSubStructure.FIELD_s1,
ItemSubStructure.FIELD_NAME + DataModel.FIELD_NAME_SEPARATOR_CHAR + ItemSubStructure.FIELD_s2,
ItemSubStructure.FIELD_NAME + DataModel.FIELD_NAME_SEPARATOR_CHAR + ItemSubStructure.FIELD_bool,
ItemSubStructure.FIELD_NAME + DataModel.FIELD_NAME_SEPARATOR_CHAR + ItemSubStructure.FIELD_combo,
},
new String[]{ItemModel.FIELD_booleanField,
ItemModel.FIELD_dateField,
ItemModel.FIELD_doubleField,
ItemModel.FIELD_intField,
ItemModel.FIELD_stringField,
ItemModel.FIELD_timeField,
ItemSubStructure.FIELD_s1,
ItemSubStructure.FIELD_s2,
ItemSubStructure.FIELD_bool,
ItemSubStructure.FIELD_combo,
}
);
itemMapping.registerTranslator(new DatabaseFieldTranslator() {
public StringVector getFieldList() {
return new StringVector(new String[] {ItemModel.FIELD_comboBoxField});
}
public void setModelValues(DatabaseTableMapping mapping, JnexPreparedStatement statement, int indexOfFirstField, DataModel model) {
statement.setString(indexOfFirstField, ((ItemModel)model).getCode());
}
public void getModelValues(DatabaseTableMapping mapping, ResultSet resultSet, int indexOfFirstField, DataModel model) throws SQLException {
((ItemModel)model).setCode(resultSet.getString(indexOfFirstField));
}
});
JdbcConnectionHandler.instance().registerDatabaseTableMapping(ItemModel.class.getName(), itemMapping);
}
public static void save(ItemArrayModel array) throws Exception {
DatabaseTableMapping m = JdbcConnectionHandler.instance().getDatabaseTableMapping(ItemModel.class.getName());
DataModelEnumeration en = array.getEnumeration();
DataModelVector remove = new DataModelVector();
while (en.hasMore()) {
ItemModel model = (ItemModel) en.next();
if (model.getFlag(ModelFlag.DELETED)) {
if (model.getInstanceName() == null) {
m.delete(model);
}
remove.append(model);
} else {
if (model.getInstanceName() == null) {
m.create(model);
} else {
m.update(model);
}
}
}
for (DataModel model = remove.firstItem(); model != null; model = remove.nextItem()) {
array.removeItem(model);
}
}
public static void load(ItemArrayModel array) throws Exception {
DatabaseTableMapping m = JdbcConnectionHandler.instance().getDatabaseTableMapping(ItemModel.class.getName());
m.search(null, new ItemModel(), array);
}
public static void saveItem(ItemModel model) throws Exception {
DatabaseTableMapping m = JdbcConnectionHandler.instance().getDatabaseTableMapping(ItemModel.class.getName());
if (model.getInstanceName() == null) {
m.create(model);
} else {
m.update(model);
}
}
public static void remove(DataModel model) throws Exception {
DatabaseTableMapping m = JdbcConnectionHandler.instance().getDatabaseTableMapping(ItemModel.class.getName());
m.delete(model);
}
}