use of com.ramussoft.common.persistent.PersistentRow 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.PersistentRow 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.PersistentRow 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.common.persistent.PersistentRow in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method getBinaryElements.
@Override
public Hashtable<Element, List<Persistent>[][]> getBinaryElements(long qualifierId, final long[] attributeIds) {
for (long attributeId : attributeIds) throwExaptionIfNotCan(getAccessor().canReadAttribute(qualifierId, attributeId), "Can not get attribute for qualifier.");
List<Element> elements = getElements(qualifierId);
Hashtable<Element, List<Persistent>[][]> result = new Hashtable<Element, List<Persistent>[][]>();
final Hashtable<Long, List<Persistent>[][]> values = new Hashtable<Long, List<Persistent>[][]>();
int index = 0;
final int[] persistentCount = new int[attributeIds.length];
for (int i = 0; i < attributeIds.length; i++) {
Attribute attribute = getAttribute(attributeIds[i]);
AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
persistentCount[i] = plugin.getAttributePersistents().length;
}
for (long attributeId : attributeIds) {
final int attrIndex = index;
Attribute attribute = getAttribute(attributeId);
AttributePlugin plugin = factory.getAttributePlugin(attribute.getAttributeType());
final Class<? extends Persistent>[] classes;
classes = plugin.getAttributePersistents();
for (int i = 0; i < classes.length; i++) {
final Class<? extends Persistent> clazz = classes[i];
final int listIndex = 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, -1l, params, paramFields, this);
StringBuffer sb = new StringBuffer("SELECT * FROM " + row.getTableName() + " main_table");
StringBuffer where = new StringBuffer(" AND attribute_id=?");
for (int j = 0; j < params.size(); j++) if (paramFields.get(j).startsWith("element") && paramFields.get(j).endsWith("id")) {
where.append(" AND main_table.");
where.append(paramFields.get(j));
where.append("=element_id");
}
for (PersistentField field : row.getFields()) {
if (field.isAutoset()) {
if (field.getType() == PersistentField.ELEMENT) {
where.append(" AND main_table.");
where.append(field.getDatabaseName());
where.append("=element_id");
}
}
}
sb.append(" WHERE (value_branch_id IN (SELECT MAX(branch_id) FROM " + prefix + "attributes_data_metadata WHERE branch_id<=? " + where + ") OR ((SELECT MAX(branch_id) FROM " + prefix + "attributes_data_metadata WHERE branch_id<=? " + where + ") IS NULL AND value_branch_id=0))");
for (int j = 0; j < params.size(); j++) {
sb.append(" AND ");
sb.append(paramFields.get(j));
sb.append("=?");
}
params.add(0, getActiveBranchId());
params.add(1, attributeId);
params.add(2, getActiveBranchId());
params.add(3, attributeId);
template.query(sb.toString(), new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
try {
Persistent persistent = clazz.newInstance();
long id = -1;
for (PersistentField field : row.getFields()) {
wrapper.setDatabaseField(persistent, field, rs);
if ((field.isAutoset()) && (field.getType() == PersistentField.ELEMENT)) {
id = rs.getLong(field.getDatabaseName());
}
}
getLists(values, id, attributeIds.length, persistentCount)[attrIndex][listIndex].add(persistent);
return null;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
throw new RuntimeException();
}
}, params.toArray(new Object[params.size()]), false);
}
index++;
}
for (Element element : elements) {
result.put(element, getLists(values, element.getId(), attributeIds.length, persistentCount));
}
return result;
}
use of com.ramussoft.common.persistent.PersistentRow in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method process.
private void process(long elementId, long attributeId, Persistent persistent) {
Class<? extends Persistent> clazz = persistent.getClass();
PersistentRow row = metadata.get(clazz);
PersistentWrapper wrapper = wrappers.get(clazz);
for (PersistentField field : row.getFields()) {
if (field.isAutoset()) {
if (field.getType() == PersistentField.ELEMENT) {
wrapper.setField(persistent, field.getName(), elementId);
} else if (field.getType() == PersistentField.ATTRIBUTE) {
wrapper.setField(persistent, field.getName(), attributeId);
} else if (field.getType() == PersistentField.ID) {
long id = (Long) wrapper.getField(persistent, field.getName());
if (id <= 0) {
wrapper.setField(persistent, field.getName(), template.nextVal(field.getSequenceName(row.getTableName())));
}
}
}
}
}
Aggregations