use of com.ramussoft.common.persistent.Transaction in project ramus by Vitaliy-Yakovchuk.
the class AbstractJournaledEngine method setPersistentAttribute.
private void setPersistentAttribute(Element element, Attribute attribute, Object newObject, AttributeConverter converter) {
long elementId = (element == null) ? -1 : element.getId();
long attributeId = attribute.getId();
List<Persistent>[] object = converter.toPersistens(newObject, elementId, attributeId, this);
for (int i = 0; i < object.length; i++) {
object[i] = new ArrayList<Persistent>(object[i]);
}
for (List<Persistent> list : object) {
for (Persistent p : list) {
PersistentRow row = getPersistentMetadata(p.getClass());
PersistentWrapper wrapper = getWrapper(p.getClass());
for (PersistentField field : row.getFields()) {
if (field.isAutoset()) {
if (field.getType() == PersistentField.ATTRIBUTE) {
wrapper.setField(p, field.getName(), attributeId);
} else if (field.getType() == PersistentField.ELEMENT) {
wrapper.setField(p, field.getName(), elementId);
} else if (field.getType() == PersistentField.ID) {
}
/*
* else throw new NullPointerException(
* "No way to autoset field: " + field.getName() +
* " for " + row.getClassName());
*/
}
}
}
}
long activeBranch = getActiveBranch();
List<Persistent>[] old = getBinaryBranchAttribute(elementId, attributeId, activeBranch);
Transaction transaction = new Transaction();
for (PersistentPare pare : findChanges(old, object)) {
if (pare.newPersistent == null) {
transaction.getDelete().add(pare.oldPersistent);
} else if (pare.oldPersistent == null) {
pare.newPersistent.setValueBranchId(activeBranch);
transaction.getSave().add(pare.newPersistent);
} else {
transaction.getUpdate().add(pare.newPersistent);
transaction.getOldUpdate().add(pare.oldPersistent);
}
}
setAttribute(elementId, attributeId, transaction);
}
use of com.ramussoft.common.persistent.Transaction 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.common.persistent.Transaction in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method setElementQualifier.
@Override
public void setElementQualifier(final long elementId, final long qualifierId) {
throwExaptionIfNotCan(getAccessor().canDeleteElements(new long[] { elementId }), "Can not update element's qualifier.");
throwExaptionIfNotCan(getAccessor().canCreateElement(qualifierId), "Can not update element's qualifier.");
Element element = getElement(elementId);
Qualifier current = getQualifier(element.getQualifierId());
Qualifier newQualifier = getQualifier(qualifierId);
final List<Attribute> attrs = new ArrayList<Attribute>();
addNotPresentAttributes(current.getAttributes(), newQualifier.getAttributes(), attrs);
addNotPresentAttributes(current.getSystemAttributes(), newQualifier.getSystemAttributes(), attrs);
template.execute(new JDBCCallback() {
@Override
public Object execute(Connection connection) throws SQLException {
Transaction[] transactions = getAttributesWhatWillBeDeleted(elementId, attrs);
for (Transaction transaction : transactions) {
executeTransaction(transaction, connection);
}
PreparedStatement ps = connection.prepareStatement("UPDATE " + prefix + "elements SET QUALIFIER_ID=? WHERE ELEMENT_ID=?");
ps.setLong(1, qualifierId);
ps.setLong(2, elementId);
ps.execute();
ps.close();
return null;
}
});
}
use of com.ramussoft.common.persistent.Transaction in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method deleteElement.
@Override
public void deleteElement(final long id) {
throwExaptionIfNotCan(getAccessor().canDeleteElements(new long[] { id }), "Can not delete element.");
template.execute(new JDBCCallback() {
@Override
public Object execute(Connection connection) throws SQLException {
Transaction[] transactions = getAttributeWhatWillBeDeleted(id);
for (Transaction transaction : transactions) {
executeTransaction(transaction, connection);
}
PreparedStatement ps;
ps = connection.prepareStatement("DELETE FROM " + prefix + "formula_dependences WHERE source_element_id=?");
ps.setLong(1, id);
ps.execute();
ps.close();
ps = connection.prepareStatement("DELETE FROM " + prefix + "formulas WHERE element_id=?");
ps.setLong(1, id);
ps.execute();
ps.close();
ps = connection.prepareStatement("UPDATE " + prefix + "elements SET removed_branch_id=? WHERE ELEMENT_ID=?");
long branch = getActiveBranchId();
ps.setLong(1, branch);
ps.setLong(2, id);
ps.execute();
ps.close();
return null;
}
});
}
use of com.ramussoft.common.persistent.Transaction in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method getAttributesWhatWillBeDeleted.
@Override
public Transaction[] getAttributesWhatWillBeDeleted(long elementId, List<Attribute> attrs) {
throwExaptionIfNotCan(getAccessor().canDeleteElements(new long[] { elementId }), "Can not get attributes what will be deleted, as you can not delete element.");
Transaction[] res = new Transaction[attrs.size()];
for (int i = 0; i < attrs.size(); i++) {
Attribute attribute = attrs.get(i);
res[i] = getAttributeWhatWillBeDeleted(elementId, attribute, false);
}
return res;
}
Aggregations