use of com.ramussoft.jdbc.RowMapper 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.jdbc.RowMapper in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method getSystemQualifier.
@Override
public Qualifier getSystemQualifier(String qualifierName) {
long branch = getActiveBranchId();
Qualifier qualifier = (Qualifier) template.queryForObjects("SELECT * FROM " + prefix + "qualifiers WHERE QUALIFIER_NAME=? AND qualifier_system=TRUE AND removed_branch_id>? AND created_branch_id <=? ", new QualifierRowMapper(branch), new Object[] { qualifierName, branch, branch }, true);
if (qualifier != null) {
Qualifier q = getQualifier(qualifier.getId());
if (!q.getName().equals(qualifierName))
qualifier = null;
}
if (branch > 0l) {
Long l = (Long) template.queryForObjects("SELECT QUALIFIER_ID FROM " + prefix + "qualifiers_history qh WHERE QUALIFIER_NAME=? AND created_branch_id IN (" + "SELECT MAX(created_branch_id) FROM " + prefix + "qualifiers_history WHERE QUALIFIER_NAME=qh.QUALIFIER_NAME AND created_branch_id<=?)", new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong(1);
}
}, new Object[] { qualifierName, branch }, false);
if (l != null)
return getQualifier(l);
}
if (qualifier == null)
return null;
throwExaptionIfNotCan(getAccessor().canReadQualifier(qualifier.getId()), "Can not get qualifier.");
return qualifier;
}
use of com.ramussoft.jdbc.RowMapper in project ramus by Vitaliy-Yakovchuk.
the class IEngineImpl method createElement.
@Override
public Element createElement(long qualifierId, long elementId) {
throwExaptionIfNotCan(getAccessor().canCreateElement(qualifierId), "Can not create element.");
if (elementId == -1) {
elementId = nextValue("elements_sequence");
long id = template.queryForLong("SELECT MAX(ELEMENT_ID) FROM " + prefix + "elements;");
while (id >= elementId) {
elementId = template.nextVal(prefix + "elements_sequence");
}
}
Element element = new Element(elementId, qualifierId, "");
element.setId(elementId);
long branchId = getActiveBranchId();
Long l = (Long) template.queryForObjects("SELECT COUNT(*) FROM " + prefix + "elements WHERE element_id=? AND removed_branch_id=?", new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong(1);
}
}, new Object[] { elementId, branchId }, false);
if (l.longValue() == 0)
template.update("INSERT INTO " + prefix + "elements (ELEMENT_ID, ELEMENT_NAME, QUALIFIER_ID, created_branch_id) VALUES (?, ?, ?, ?)", new Object[] { elementId, element.getName(), qualifierId, branchId }, true);
else {
template.update("UPDATE " + prefix + "elements SET removed_branch_id=? WHERE ELEMENT_ID=? AND removed_branch_id=?", new Object[] { Integer.MAX_VALUE, elementId, branchId }, false);
}
return element;
}
use of com.ramussoft.jdbc.RowMapper in project ramus by Vitaliy-Yakovchuk.
the class EvalPlugin method init.
@Override
public void init(final Engine engine, AccessRules rules) {
super.init(engine, rules);
Qualifier qualifier = StandardAttributesPlugin.getQualifiersQualifier(engine);
engine.setPluginProperty(getName(), "Plugin", this);
function = getFunction(qualifier);
if (function == null) {
function = engine.createSystemAttribute(new AttributeType(EVAL, "Function", false));
function.setName(FUNCTION_ATTRIBUTE);
engine.updateAttribute(function);
qualifier.getSystemAttributes().add(function);
engine.updateQualifier(qualifier);
}
functionDependences = engine.getSystemQualifier(QUALIFIER_EVAL_FUNCTION_DEPENDENCES);
if (functionDependences == null) {
createEvalObjects(engine);
} else {
functionDependence = engine.getSystemAttribute(ATTRIBUTE_EVAL_FUNCTION_DEPENDENCE);
functionDependenceQualifier = engine.getSystemAttribute(ATTRIBUTE_EVAL_DEPENDENCE_QUALIFIER);
functionDependenceAttribute = engine.getSystemAttribute(ATTRIBUTE_EVAL_DEPENDENCE_ATTRIBUTE);
functionDependenceSourceAttribute = engine.getSystemAttribute(ATTRIBUTE_EVAL_FUNCTION_DEPENDENCE_SOURCE_ATTRIBUTE);
}
if (!StandardAttributesPlugin.isDisableAutoupdate(engine)) {
final Util util = Util.getUtils(engine);
util.getScriptHolders().addFunctionsChangeListener(new FunctionsChangeListener() {
@SuppressWarnings("unchecked")
@Override
public void functionsChanged(FunctionsChangeEvent event) {
IEngine iEngine = engine.getDeligate();
if ((iEngine != null) && (iEngine instanceof IEngineImpl)) {
JDBCTemplate template = ((IEngineImpl) iEngine).getTemplate();
String prefix = ((IEngineImpl) iEngine).getPrefix();
for (String function : event.getFunctionNames()) {
List<FunctionPersistent> list = template.query("SELECT function, qualifier_attribute_id, qualifier_table_attribute_id, autochange, attribute_id, element_id FROM " + prefix + "attribute_functions WHERE function LIKE ? AND autochange=1", new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
FunctionPersistent persistent = new FunctionPersistent(rs.getString(1), rs.getLong(2), rs.getLong(3), rs.getInt(4));
persistent.setAttributeId(rs.getLong(5));
persistent.setElementId(rs.getLong(6));
return persistent;
}
}, new Object[] { "%" + function + "%" }, true);
for (FunctionPersistent fp : list) {
recalculateQualifierAttribute(engine, StandardAttributesPlugin.getQualifier(engine, engine.getElement(fp.getElementId())), util, fp);
}
}
}
for (String function : event.getFunctionNames()) {
for (CalculateInfo info : engine.findCalculateInfos("%" + function + "%", true)) {
recalculate(engine, info);
}
}
}
});
engine.addElementAttributeListener(null, new ElementAttributeListener() {
@SuppressWarnings("unchecked")
@Override
public void attributeChanged(AttributeEvent event) {
if (event.isJournaled())
return;
if (DISABLE_RECALC)
return;
if (event.getElement() == null) {
IEngine d = engine.getDeligate();
List<Element> allElements = null;
if ((d != null) && (d instanceof IEngineImpl)) {
JDBCTemplate template = ((IEngineImpl) d).getTemplate();
String prefix = ((IEngineImpl) d).getPrefix();
allElements = template.query("SELECT * FROM " + prefix + "elements WHERE qualifier_id in\n" + "(SELECT qualifier_id FROM " + prefix + "qualifiers_attributes WHERE attribute_id=?)", new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Element(rs.getLong("element_id"), rs.getLong("qualifier_id"), rs.getString("element_name"));
}
}, new Object[] { event.getAttribute().getId() }, true);
} else {
Attribute attribute = event.getAttribute();
allElements = new ArrayList<Element>();
for (Qualifier qualifier : engine.getQualifiers()) {
if (qualifier.getAttributes().indexOf(attribute) >= 0) {
allElements.addAll(engine.getElements(qualifier.getId()));
}
}
}
Attribute attribute = event.getAttribute();
for (Element element : allElements) {
recalculateElement(engine, new AttributeEvent(engine, element, attribute, null, null));
}
} else
recalculateElement(engine, event);
}
private void recalculateElement(final Engine engine, AttributeEvent event) {
MetaValue metaValue = new MetaValue(event.getElement().getId(), event.getAttribute().getId());
List<MetaValue> metaValueList = hashtable.get(Thread.currentThread());
if (metaValueList == null) {
metaValueList = new ArrayList<MetaValue>();
hashtable.put(Thread.currentThread(), metaValueList);
} else {
if (metaValueList.indexOf(metaValue) >= 0)
return;
}
metaValueList.add(metaValue);
try {
for (CalculateInfo info : engine.getDependences(event.getElement().getId(), event.getAttribute().getId(), true)) {
recalculate(engine, info);
}
List<Element> elements = engine.findElements(functionDependences.getId(), functionDependence, event.getElement().getId());
for (Element e : elements) {
Qualifier qualifier = engine.getQualifier((Long) engine.getAttribute(e, functionDependenceQualifier));
Attribute attribute = engine.getAttribute((Long) engine.getAttribute(e, functionDependenceAttribute));
if ((qualifier != null) && (attribute != null)) {
Element el = StandardAttributesPlugin.getElement(event.getEngine(), qualifier.getId());
if (el != null)
for (Element child : engine.getElements(qualifier.getId())) {
recalculateInQualifier(engine, Util.elementIdToValue(event.getElement().getId(), event.getAttribute().getId()), null, el, child);
}
}
}
Element element = StandardAttributesPlugin.getElement(event.getEngine(), event.getElement().getQualifierId());
if (element == null) {
Qualifier qualifier = engine.getQualifier(event.getElement().getQualifierId());
if (StandardAttributesPlugin.isTableQualifier(qualifier)) {
Element parent = StandardAttributesPlugin.getElementForTableElement(engine, event.getElement());
if (parent == null)
return;
Qualifier main = engine.getQualifier(parent.getQualifierId());
Attribute tableAttribute = null;
for (Attribute attr : main.getAttributes()) {
if (StandardAttributesPlugin.getTableQualifeirName(attr).equals(qualifier.getName())) {
tableAttribute = attr;
break;
}
}
if (tableAttribute == null)
return;
element = StandardAttributesPlugin.getElement(event.getEngine(), parent.getQualifierId());
if (element != null) {
if (event.getAttribute().equals(StandardAttributesPlugin.getTableElementIdAttribute(engine)))
recalculateInQualifier(engine, null, event.getElement(), element, parent);
else
recalculateInQualifier(engine, Util.tableAttributeToValue(tableAttribute.getId(), event.getAttribute().getId()), event.getElement(), element, parent);
}
}
return;
}
recalculateInQualifier(engine, Util.attributeIdToValue(event.getAttribute().getId()), null, element, event.getElement());
} finally {
metaValueList.remove(metaValueList.size() - 1);
}
}
});
engine.addElementListener(null, new ElementAdapter() {
@SuppressWarnings("unchecked")
@Override
public void elementCreated(ElementEvent event) {
if (event.isJournaled())
return;
Element element = StandardAttributesPlugin.getElement(event.getEngine(), event.getNewElement().getQualifierId());
if (element == null) {
return;
}
List<FunctionPersistent> list = getFunctionAttribute(engine, element);
if (list == null)
return;
Util utils = Util.getUtils(engine);
for (FunctionPersistent fp : list) if (fp.getAutochange() != 0) {
Eval eval = new Eval(fp.getFunction());
try {
utils.fillAttributes(eval, engine.getQualifier(event.getNewElement().getQualifierId()), event.getNewElement(), null, null, null, true);
utils.fillResult(fp.getQualifierAttributeId(), eval, event.getNewElement());
} catch (Exception e) {
utils.fillResult(fp.getQualifierAttributeId(), e, event.getNewElement());
if (e instanceof RuntimeException)
throw (RuntimeException) e;
throw new RuntimeException(e);
} finally {
}
}
}
@SuppressWarnings("unchecked")
@Override
public void beforeElementDeleted(final ElementEvent event) {
if ((event.isJournaled()) || (event.getNewElement() != null))
return;
Element oldElement = event.getOldElement();
Qualifier qualifier = engine.getQualifier(oldElement.getQualifierId());
for (Attribute attribute : qualifier.getAttributes()) {
removeAttributeFromCalculateInfo(engine, oldElement, attribute);
CalculateInfo info = engine.getCalculateInfo(oldElement.getId(), attribute.getId());
if (info != null) {
info.setFormula(null);
engine.setCalculateInfo(info);
}
}
List<Element> elements = engine.findElements(functionDependences.getId(), functionDependence, oldElement.getId());
Vector<Long> qualifiers = new Vector<Long>(elements.size());
final String start = Util.ELEMENT_PREFIX + oldElement.getId();
for (Element element : elements) {
Long q = (Long) engine.getAttribute(element, functionDependenceQualifier);
if (qualifiers.indexOf(q) < 0) {
qualifiers.add(q);
Element qElement = StandardAttributesPlugin.getElement(engine, q.longValue());
List<FunctionPersistent> fpl = (List<FunctionPersistent>) engine.getAttribute(qElement, function);
for (FunctionPersistent fp : fpl) {
try {
Eval eval = new Eval(fp.getFunction());
eval.replaceValueNames(new Replacementable() {
@Override
public String getNewName(String oldName) {
if (oldName.startsWith(start))
return "NULL";
return oldName;
}
});
fp.setFunction(eval.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
engine.setAttribute(qElement, function, fpl);
}
// engine.deleteElement(element.getId());
}
}
});
engine.addQualifierListener(new QualifierAdapter() {
@SuppressWarnings("unchecked")
@Override
public void beforeQualifierUpdated(QualifierEvent event) {
List<Attribute> rem = new ArrayList<Attribute>();
for (Attribute a : event.getOldQualifier().getAttributes()) {
if (event.getNewQualifier().getAttributes().indexOf(a) < 0)
rem.add(a);
}
if (rem.size() > 0) {
List<Element> list = engine.getElements(event.getOldQualifier().getId());
for (Attribute attribute : rem) {
for (Element element : list) {
removeAttributeFromCalculateInfo(engine, element, attribute);
CalculateInfo info = engine.getCalculateInfo(element.getId(), attribute.getId());
if (info != null) {
info.setFormula(null);
engine.setCalculateInfo(info);
}
}
List<Element> elements = engine.findElements(functionDependences.getId(), functionDependenceSourceAttribute, attribute.getId());
final String end = Util.ATTRIBUTE_PREFIX + attribute.getId();
for (Element element : elements) {
Long id = (Long) engine.getAttribute(element, functionDependence);
if (engine.getQualifierIdForElement(id) == event.getNewQualifier().getId()) {
Long qId = (Long) engine.getAttribute(element, functionDependenceQualifier);
Element element2 = StandardAttributesPlugin.getElement(engine, qId);
List<FunctionPersistent> fpl = (List<FunctionPersistent>) engine.getAttribute(element2, function);
for (FunctionPersistent fp : fpl) {
Eval eval = new Eval(fp.getFunction());
eval.replaceValueNames(new Replacementable() {
@Override
public String getNewName(String oldName) {
if (oldName.endsWith(end)) {
return "NULL";
}
return oldName;
}
});
fp.setFunction(eval.toString());
}
engine.setAttribute(element2, function, fpl);
}
}
}
}
}
@Override
public void qualifierDeleted(QualifierEvent event) {
Qualifier qualifier = event.getOldQualifier();
for (Element element : engine.findElements(functionDependences.getId(), functionDependenceQualifier, qualifier.getId())) {
engine.deleteElement(element.getId());
}
}
});
Qualifier qq = StandardAttributesPlugin.getQualifiersQualifier(engine);
engine.addElementAttributeListener(qq, new ElementAttributeListener() {
@Override
public void attributeChanged(AttributeEvent event) {
if (event.isJournaled())
return;
if (function.equals(event.getAttribute())) {
recalculateFunctionOfQualifier(engine, event);
}
}
});
engine.addFormulaListener(new FormulaListener() {
@Override
public void formulaChanged(FormulaEvent event) {
if (event.isJournaled())
return;
CalculateInfo info = event.getNewFormula();
recalculate(engine, info);
}
});
}
}
use of com.ramussoft.jdbc.RowMapper in project ramus by Vitaliy-Yakovchuk.
the class FileIEngineImpl method getRealPath.
private String getRealPath(String path) {
String path2;
if (!path.startsWith("/elements/"))
path2 = path;
else {
long branchId = getActiveBranchId();
List<Long> list = template.query("SELECT * FROM " + prefix + "streams WHERE STREAM_ID=? AND created_branch_id IN " + "(SELECT MAX(created_branch_id) FROM " + prefix + "streams WHERE STREAM_ID=? AND created_branch_id<=?) AND removed_branch_id >?", new RowMapper() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getLong("created_branch_id");
}
}, new Object[] { path, path, branchId, branchId }, false);
if (list.size() == 0) {
return null;
} else if (list.size() > 1) {
System.err.println("Error with streams branch " + path + " " + list);
}
long sb = list.get(0);
if (sb == 0l)
path2 = path;
else
path2 = "/branches/" + sb + path;
}
return path2;
}
Aggregations