use of com.ramussoft.common.persistent.PersistentWrapper in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method getBinaryAttribute.
@Override
@SuppressWarnings("unchecked")
public List<Persistent>[] getBinaryAttribute(long elementId, long attributeId) {
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>(2);
ArrayList<String> paramFields = new ArrayList<String>(2);
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());
}
long branchId = 0l;
try {
branchId = getBranch(prefix + "attributes_data_metadata", new Object[] { "element_id", "attribute_id" }, new Object[] { elementId, attributeId }, getActiveBranchId());
} catch (SQLException e1) {
e1.printStackTrace();
throw new RuntimeException(e1);
}
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.common.persistent.PersistentWrapper in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method executeTransaction.
private void executeTransaction(final Transaction transaction, Connection connection) throws SQLException {
for (Persistent key : transaction.getUpdate()) {
PersistentRow row = metadata.get(key.getClass());
PersistentWrapper wrapper = wrappers.get(key.getClass());
StringBuffer sb = new StringBuffer("UPDATE " + row.getTableName() + " SET ");
boolean first = true;
List<Object> list = new ArrayList<Object>(5);
for (PersistentField field : row.getDataFields()) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(field.getDatabaseName());
sb.append("=?");
list.add(wrapper.getField(key, field.getName()));
}
sb.append(" WHERE ");
first = true;
for (PersistentField field : row.getKeyFields()) {
if (first) {
first = false;
} else {
sb.append(" AND ");
}
sb.append(field.getDatabaseName());
sb.append("=?");
list.add(wrapper.getField(key, field.getName()));
}
PreparedStatement ps = template.getPreparedStatement(sb.toString(), true);
template.setParams(ps, list.toArray(new Object[list.size()]));
ps.execute();
// ps.close();
}
for (Persistent key : transaction.getDelete()) {
PersistentRow row = metadata.get(key.getClass());
PersistentWrapper wrapper = wrappers.get(key.getClass());
StringBuffer sb = new StringBuffer("DELETE FROM " + row.getTableName() + " WHERE ");
boolean first = true;
first = true;
List<Object> list = new ArrayList<Object>(2);
for (PersistentField field : row.getKeyFields()) {
if (first) {
first = false;
} else {
sb.append("AND ");
}
sb.append(field.getDatabaseName());
sb.append("=?");
list.add(wrapper.getField(key, field.getName()));
}
PreparedStatement ps = template.getPreparedStatement(sb.toString(), // connection.prepareStatement(sb.toString());
true);
template.setParams(ps, list.toArray(new Object[list.size()]));
ps.execute();
// ps.close();
}
for (Persistent key : transaction.getSave()) {
PersistentRow row = metadata.get(key.getClass());
PersistentWrapper wrapper = wrappers.get(key.getClass());
StringBuffer sb = new StringBuffer("INSERT INTO " + row.getTableName() + "(");
boolean first = true;
List<Object> list = new ArrayList<Object>(5);
for (PersistentField field : row.getFields()) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(field.getDatabaseName());
list.add(wrapper.getField(key, field.getName()));
}
sb.append(") VALUES(");
first = true;
for (int i = 0; i < list.size(); i++) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append("?");
}
sb.append(")");
PreparedStatement ps = template.getPreparedStatement(sb.toString(), // connection.prepareStatement(sb.toString());
true);
template.setParams(ps, list.toArray(new Object[list.size()]));
ps.execute();
// ps.close();
}
}
use of com.ramussoft.common.persistent.PersistentWrapper in project ramus by Vitaliy-Yakovchuk.
the class UniversalPersistentFactory method updateInTransaction.
public void updateInTransaction(Object object, final boolean cached, Class clazz) throws SQLException {
PersistentRow row = rowHash.get(clazz);
PersistentWrapper wrapper = rowWrappers.get(clazz);
List<PersistentField> fields = row.getFields();
Object[] objects = new Object[fields.size()];
StringBuffer set = new StringBuffer("UPDATE " + row.getTableName() + " SET ");
StringBuffer key = new StringBuffer(" WHERE ");
boolean firstP = true;
boolean firstS = true;
int k = 0;
for (int i = 0; i < objects.length; i++) {
PersistentField field = fields.get(i);
if (field.isPrimary()) {
if (firstP)
firstP = false;
else
key.append(" AND ");
} else {
if (firstS)
firstS = false;
else
set.append(", ");
}
if (field.isPrimary()) {
key.append(field.getDatabaseName());
key.append("=?");
} else {
set.append(field.getDatabaseName());
set.append("=?");
}
if (!field.isPrimary()) {
objects[k] = wrapper.getField(object, field.getName());
k++;
}
}
for (int i = 0; i < objects.length; i++) if (fields.get(i).isPrimary()) {
objects[k] = wrapper.getField(object, fields.get(i).getName());
k++;
}
String sql = set.toString() + key.toString();
PreparedStatement ps = template.getPreparedStatement(sql, cached);
synchronized (ps) {
template.setParams(ps, objects);
ps.execute();
if (!cached)
ps.close();
}
}
use of com.ramussoft.common.persistent.PersistentWrapper in project ramus by Vitaliy-Yakovchuk.
the class UniversalPersistentFactory method delete.
public void delete(Object object) {
Class clazz = object.getClass();
PersistentRow row = rowHash.get(clazz);
PersistentWrapper wrapper = rowWrappers.get(clazz);
List<PersistentField> fields = row.getKeyFields();
Object[] objects = new Object[fields.size()];
StringBuffer delete = new StringBuffer("DELETE FROM " + row.getTableName() + " WHERE ");
boolean first = true;
for (int i = 0; i < objects.length; i++) {
PersistentField field = fields.get(i);
if (first)
first = false;
else {
delete.append(" AND ");
}
delete.append(field.getDatabaseName());
delete.append("=?");
objects[i] = wrapper.getField(object, field.getName());
}
String sql = delete.toString();
template.update(sql, objects, true);
}
use of com.ramussoft.common.persistent.PersistentWrapper in project ramus by Vitaliy-Yakovchuk.
the class UniversalPersistentFactory method update.
public Object update(Object object, boolean cached, JDBCCallback callback, Class clazz) {
if (object instanceof List)
return updateList((List) object, cached, callback);
PersistentRow row = rowHash.get(clazz);
PersistentWrapper wrapper = rowWrappers.get(clazz);
List<PersistentField> fields = row.getFields();
Object[] objects = new Object[fields.size()];
StringBuffer set = new StringBuffer("UPDATE " + row.getTableName() + " SET ");
StringBuffer key = new StringBuffer(" WHERE ");
boolean firstP = true;
boolean firstS = true;
int k = 0;
for (int i = 0; i < objects.length; i++) {
PersistentField field = fields.get(i);
if (field.isPrimary()) {
if (firstP)
firstP = false;
else
key.append(" AND ");
} else {
if (firstS)
firstS = false;
else
set.append(", ");
}
if (field.isPrimary()) {
key.append(field.getDatabaseName());
key.append("=?");
} else {
set.append(field.getDatabaseName());
set.append("=?");
}
if (!field.isPrimary()) {
objects[k] = wrapper.getField(object, field.getName());
k++;
}
}
for (int i = 0; i < objects.length; i++) if (fields.get(i).isPrimary()) {
objects[k] = wrapper.getField(object, fields.get(i).getName());
k++;
}
String sql = set.toString() + key.toString();
return template.update(sql, objects, cached, callback);
}
Aggregations