use of com.ramussoft.jdbc.RowMapper in project ramus by Vitaliy-Yakovchuk.
the class EngineConnection method init.
public void init(String[] args) throws Exception {
try {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
java.sql.Connection jdbcConnection = null;
Properties ps = EngineFactory.getPropeties();
if (ps != null) {
try {
Class.forName(ps.getProperty("driver"));
jdbcConnection = DriverManager.getConnection(ps.getProperty("url"), ps.getProperty("user"), ps.getProperty("password"));
} catch (Exception e) {
e.printStackTrace();
}
}
if (jdbcConnection == null)
jdbcConnection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/ramus_public", "postgres", "postgres");
template = new JDBCTemplate(jdbcConnection);
userFactory = new UserFactoryImpl(template);
JDBCTemplate template = new JDBCTemplate(jdbcConnection);
String password = template.queryForObjects("SELECT \"password\" FROM users WHERE \"login\"=?", new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int index) throws SQLException {
return rs.getString(1);
}
}, new Object[] { "admin" }, true).toString().trim();
connection = new TcpClientConnection("127.0.0.1", Metadata.TCP_PORT) {
@Override
protected void objectReaded(Object object) {
if (tcpClientEngine != null)
tcpClientEngine.call((EvenstHolder) object);
}
};
connection.start();
connection.invoke("login", new Object[] { "admin", password });
tcpClientEngine = new TcpClientEngine((EngineInvocker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { EngineInvocker.class }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return connection.invoke(method.getName(), args);
}
}), connection);
List<Class> interfaces = new ArrayList<Class>();
interfaces.add(Engine.class);
interfaces.add(Journaled.class);
List<PluginProvider> suits = new ArrayList<PluginProvider>();
suits.add(new SimpleAttributePluginSuit());
initAdditionalPluginSuits(suits);
PluginFactory factory = createPluginFactory(suits);
for (Plugin plugin : factory.getPlugins()) if (plugin.getFunctionalInterface() != null)
interfaces.add(plugin.getFunctionalInterface());
final Engine engine1 = (Engine) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces.toArray(new Class[interfaces.size()]), tcpClientEngine);
// new CachedEngine(engine1);
final Engine cachedEngine = engine1;
final Hashtable<Method, Object> hashtable = new Hashtable<Method, Object>();
for (Method m : Engine.class.getMethods()) {
hashtable.put(m, cachedEngine);
}
for (Method m : Cached.class.getMethods()) {
hashtable.put(m, cachedEngine);
}
interfaces.add(Cached.class);
Engine engine = (Engine) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces.toArray(new Class[interfaces.size()]), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object o = hashtable.get(method);
if (o == null)
return tcpClientEngine.invoke(proxy, method, args);
return method.invoke(o, args);
}
});
tcpClientEngine.setEngine(engine);
for (Plugin plugin : factory.getPlugins()) plugin.init(engine, rules);
rules = (AccessRules) createDeligate(AccessRules.class);
this.engine = engine;
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.ramussoft.jdbc.RowMapper in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method getAttributeWhatWillBeDeleted.
private Transaction getAttributeWhatWillBeDeleted(long elementId, Attribute attribute, boolean attributeDeteting) {
final Transaction transaction = new Transaction();
AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
final Class<? extends Persistent>[] classes;
if (elementId >= 0)
classes = plugin.getAttributePersistents();
else
classes = plugin.getAttributePropertyPersistents();
for (int i = 0; i < classes.length; i++) {
final Class<? extends Persistent> clazz = classes[i];
final PersistentRow row = metadata.get(clazz);
final PersistentWrapper wrapper = wrappers.get(clazz);
ArrayList<Object> params = new ArrayList<Object>(2);
ArrayList<String> paramFields = new ArrayList<String>(2);
boolean delete = false;
for (PersistentField field : row.getFields()) {
if (field.isAutoset()) {
if (field.getType() == PersistentField.ELEMENT) {
params.add(elementId);
paramFields.add(field.getDatabaseName());
if (!attributeDeteting)
delete = true;
} else if (field.getType() == PersistentField.ATTRIBUTE) {
params.add(attribute.getId());
paramFields.add(field.getDatabaseName());
if (attributeDeteting)
delete = true;
} else if (field.getType() == PersistentField.VALUE_BRANCH_ID) {
params.add(getActiveBranchId());
paramFields.add(field.getDatabaseName());
if (attributeDeteting)
delete = true;
}
}
}
if (delete) {
StringBuffer sb = new StringBuffer("SELECT * FROM " + row.getTableName() + " WHERE ");
boolean first = true;
for (int x = 0; x < params.size(); x++) {
if (first) {
first = false;
} else {
sb.append(" AND ");
}
sb.append(paramFields.get(x));
sb.append("=?");
}
template.query(sb.toString(), new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
try {
Persistent persistent = clazz.newInstance();
for (PersistentField field : row.getFields()) {
wrapper.setDatabaseField(persistent, field, rs);
}
transaction.getDelete().add(persistent);
return null;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
throw new RuntimeException();
}
}, params.toArray(new Object[params.size()]), true);
}
}
transaction.setRemoveBranchInfo(true);
return transaction;
}
use of com.ramussoft.jdbc.RowMapper in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method setStream.
@SuppressWarnings("unchecked")
@Override
public void setStream(String path, byte[] bytes) {
if (!path.startsWith("/"))
throw new RuntimeException("Path should start with / symbol.");
if (path.startsWith("/elements")) {
if (!canUpdateElement(path)) {
throw new RuntimeException("You can not update path " + path);
}
}
throwExaptionIfNotCan(getAccessor().canUpdateStream(path), "Can not update stream.");
writeStream(path, bytes);
List<String> list = template.query("SELECT * FROM " + prefix + "streams WHERE STREAM_ID=?", new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString("STREAM_ID");
}
}, new Object[] { path }, true);
if (list.size() == 0) {
template.update("INSERT INTO " + prefix + "streams (STREAM_ID) VALUES (?)", new Object[] { path }, true);
}
}
use of com.ramussoft.jdbc.RowMapper in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method getBinaryBranchAttribute.
@Override
public List<Persistent>[] getBinaryBranchAttribute(long elementId, long attributeId, long branchId) {
throwExaptionIfNotCan(getAccessor().canReadElement(elementId, attributeId), "Can not get attribute.");
Attribute attribute = getAttribute(attributeId);
AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
final Class<? extends Persistent>[] classes;
if (elementId >= 0)
classes = plugin.getAttributePersistents();
else
classes = plugin.getAttributePropertyPersistents();
List<Persistent>[] lists = new List[classes.length];
for (int i = 0; i < lists.length; i++) {
final Class<? extends Persistent> clazz = classes[i];
final PersistentRow row = metadata.get(clazz);
final PersistentWrapper wrapper = wrappers.get(clazz);
ArrayList<Object> params = new ArrayList<Object>(3);
ArrayList<String> paramFields = new ArrayList<String>(3);
plugin.fillAttributeQuery(row, attributeId, elementId, params, paramFields, this);
if (elementId >= 0l && attribute.getAttributeType().getTypeName().equals("ElementList") && attribute.getAttributeType().getPluginName().equals("Core")) {
return getEListFixed(row, clazz, wrapper, paramFields, params, getActiveBranchId());
}
params.add(branchId);
paramFields.add("value_branch_id");
StringBuffer sb = new StringBuffer("SELECT * FROM " + row.getTableName());
if (params.size() > 0) {
sb.append(" WHERE ");
}
boolean first = true;
for (int j = 0; j < params.size(); j++) {
if (first) {
first = false;
} else {
sb.append(" AND ");
}
sb.append(paramFields.get(j));
sb.append("=?");
}
List<Persistent> list = template.query(sb.toString(), new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
try {
Persistent persistent = clazz.newInstance();
for (PersistentField field : row.getFields()) {
wrapper.setDatabaseField(persistent, field, rs);
}
return persistent;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
throw new RuntimeException();
}
}, params.toArray(new Object[params.size()]), true);
lists[i] = list;
}
return lists;
}
use of com.ramussoft.jdbc.RowMapper in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method createAttribute.
@Override
public Attribute createAttribute(long attributeId, AttributeType attributeType, boolean system) {
throwExaptionIfNotCan(getAccessor().canCreateAttribute(), "Can not create attribute.");
if (attributeId == -1) {
attributeId = nextValue("attributes_sequence");
long id = template.queryForLong("SELECT MAX(ATTRIBUTE_ID) FROM " + prefix + "attributes;");
while (id >= attributeId) {
attributeId = template.nextVal(prefix + "attributes_sequence");
}
}
Attribute attribute = new Attribute();
attribute.setId(attributeId);
attribute.setAttributeType(attributeType);
attribute.setSystem(system);
long branchId = getActiveBranchId();
Long l = (Long) template.queryForObjects("SELECT COUNT(*) FROM " + prefix + "attributes WHERE ATTRIBUTE_ID=? AND removed_branch_id=?", new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong(1);
}
}, new Object[] { attributeId, branchId }, false);
if (l.longValue() > 0)
template.update("UPDATE " + prefix + "attributes SET ATTRIBUTE_NAME=?, ATTRIBUTE_SYSTEM=?, ATTRIBUTE_TYPE_PLUGIN_NAME=?, ATTRIBUTE_TYPE_NAME=?, ATTRIBUTE_TYPE_COMPARABLE=?, " + "removed_branch_id=? WHERE ATTRIBUTE_ID = ? AND removed_branch_id=?", new Object[] { attribute.getName(), system, attributeType.getPluginName(), attributeType.getTypeName(), attributeType.isComparable(), Integer.MAX_VALUE, attribute.getId(), branchId }, true);
else
template.update("INSERT INTO " + prefix + "attributes(ATTRIBUTE_ID, ATTRIBUTE_NAME, ATTRIBUTE_SYSTEM, ATTRIBUTE_TYPE_PLUGIN_NAME, ATTRIBUTE_TYPE_NAME, ATTRIBUTE_TYPE_COMPARABLE, created_branch_id) " + "VALUES(?, ?, ?, ?, ?, ?, ?)", new Object[] { attribute.getId(), attribute.getName(), system, attributeType.getPluginName(), attributeType.getTypeName(), attributeType.isComparable(), branchId }, true);
return attribute;
}
Aggregations