Skip to content

Commit

Permalink
[KNOWAGE-8132] Partially revert the original commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Libanori committed May 28, 2024
1 parent 4d159fb commit 57b02ab
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package it.eng.qbe.datasource.jpa;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Clob;
Expand Down Expand Up @@ -130,7 +130,8 @@ private synchronized Integer getPKValue(EntityType targetEntity, String keyColum

logger.debug("SELECT max(p." + keyColumn + ") as c FROM " + targetEntity.getName() + " p");
// logger.debug("SELECT max(p."+keyColumn+") as c FROM "+targetEntity.getName()+" p");
Query maxQuery = entityManager.createQuery("SELECT max(p." + keyColumn + ") as c FROM " + targetEntity.getName() + " p");
Query maxQuery = entityManager
.createQuery("SELECT max(p." + keyColumn + ") as c FROM " + targetEntity.getName() + " p");

Object result = maxQuery.getSingleResult();

Expand All @@ -144,7 +145,8 @@ private synchronized Integer getPKValue(EntityType targetEntity, String keyColum
return toReturn;
}

private synchronized Integer getPKValueFromTemplateTable(String tableName, String keyColumn, EntityManager entityManager) {
private synchronized Integer getPKValueFromTemplateTable(String tableName, String keyColumn,
EntityManager entityManager) {
logger.debug("IN");
Integer toReturn = 0;

Expand All @@ -163,7 +165,8 @@ private synchronized Integer getPKValueFromTemplateTable(String tableName, Strin
}

@Override
public Integer insertRecord(JSONObject aRecord, RegistryConfiguration registryConf, boolean autoLoadPK, String tableForPkMax, String columnForPkMax) {
public Integer insertRecord(JSONObject aRecord, RegistryConfiguration registryConf, boolean autoLoadPK,
String tableForPkMax, String columnForPkMax) {

EntityTransaction entityTransaction = null;
Integer toReturn = null;
Expand Down Expand Up @@ -213,7 +216,8 @@ public Integer insertRecord(JSONObject aRecord, RegistryConfiguration registryCo
logger.debug("Column [" + attributeName + "] is a foreign key");
if (aRecord.get(attributeName) != null && !aRecord.get(attributeName).equals("")) {
logger.debug("search foreign reference for value " + aRecord.get(attributeName));
Map<String, Object> subEntityProperties = getAllSubEntityProperties(aRecord, registryConf, column.getSubEntity());
Map<String, Object> subEntityProperties = getAllSubEntityProperties(aRecord, registryConf,
column.getSubEntity());
setSubEntity(targetEntity, column, newObj, subEntityProperties, entityManager);
} else {
// no value in column, insert null
Expand All @@ -238,7 +242,8 @@ public Integer insertRecord(JSONObject aRecord, RegistryConfiguration registryCo
pkValue = getPKValueFromTemplateTable(tableForPkMax, columnForPkMax, entityManager);
setKeyProperty(targetEntity, newObj, keyColumn, pkValue);
} else {
logger.debug("calculate max value +1 for key column " + keyColumn + " in table " + targetEntity.getName());
logger.debug("calculate max value +1 for key column " + keyColumn + " in table "
+ targetEntity.getName());
pkValue = getPKValue(targetEntity, keyColumn, entityManager);
setKeyProperty(targetEntity, newObj, keyColumn, pkValue);
}
Expand Down Expand Up @@ -268,8 +273,9 @@ public Integer insertRecord(JSONObject aRecord, RegistryConfiguration registryCo
entityManager.flush();
entityTransaction.commit();

JPAPersistenceManagerAuditLogger.log(Operation.INSERTION, this.getDataSource().getConfiguration().getModelName(), registryConf.getEntity(), null,
aRecord, null);
JPAPersistenceManagerAuditLogger.log(Operation.INSERTION,
this.getDataSource().getConfiguration().getModelName(), registryConf.getEntity(), null, aRecord,
null);

} catch (Throwable t) {
if (entityTransaction != null && entityTransaction.isActive()) {
Expand Down Expand Up @@ -335,7 +341,8 @@ public void updateRecord(JSONObject aRecord, RegistryConfiguration registryConf)
Column column = registryConf.getColumnConfiguration(attributeName);

if (!column.isEditable() || column.isInfoColumn() || column.isAudit()) {
logger.debug("Skip column [" + attributeName + "] because it is not editable or it is an info or audit column");
logger.debug("Skip column [" + attributeName
+ "] because it is not editable or it is an info or audit column");
continue;
}

Expand All @@ -347,9 +354,11 @@ public void updateRecord(JSONObject aRecord, RegistryConfiguration registryConf)
if (column.getSubEntity() != null) {
logger.debug("Column [" + attributeName + "] is a foreign key");

Map<String, Object> subEntityProperties = getAllSubEntityProperties(aRecord, registryConf, column.getSubEntity());
Map<String, Object> subEntityProperties = getAllSubEntityProperties(aRecord, registryConf,
column.getSubEntity());

int changed = updateSubEntity(subEntityProperties, entityManager, targetEntity, obj, oldRecord, column);
int changed = updateSubEntity(subEntityProperties, entityManager, targetEntity, obj, oldRecord,
column);
if (changed > 0) {
changesCounter += changed;
}
Expand Down Expand Up @@ -383,7 +392,8 @@ public void updateRecord(JSONObject aRecord, RegistryConfiguration registryConf)
entityManager.flush();
entityTransaction.commit();

JPAPersistenceManagerAuditLogger.log(Operation.UPDATE, this.getDataSource().getConfiguration().getModelName(), registryConf.getEntity(), oldRecord,
JPAPersistenceManagerAuditLogger.log(Operation.UPDATE,
this.getDataSource().getConfiguration().getModelName(), registryConf.getEntity(), oldRecord,
aRecord, changesCounter);

} catch (Throwable t) {
Expand All @@ -398,7 +408,8 @@ public void updateRecord(JSONObject aRecord, RegistryConfiguration registryConf)

}

private Map<String, Object> getAllSubEntityProperties(JSONObject aRecord, RegistryConfiguration registryConf, String subEntity) throws JSONException {
private Map<String, Object> getAllSubEntityProperties(JSONObject aRecord, RegistryConfiguration registryConf,
String subEntity) throws JSONException {
List<Column> columns = registryConf.getColumns();
// @formatter:off
Map<String, Object> allSubEntityProperties =
Expand All @@ -414,8 +425,8 @@ private Map<String, Object> getAllSubEntityProperties(JSONObject aRecord, Regist
* @return true if sub-entity was changed, false otherwise
* @throws JSONException
*/
protected int updateSubEntity(Map<String, Object> newSubEntityAttributes, EntityManager entityManager, EntityType targetEntity, Object obj,
JSONObject oldRecord, Column column) throws JSONException {
protected int updateSubEntity(Map<String, Object> newSubEntityAttributes, EntityManager entityManager,
EntityType targetEntity, Object obj, JSONObject oldRecord, Column column) throws JSONException {
EntityType subEntityType = getSubEntityType(targetEntity, column.getSubEntity(), entityManager);
Object subEntity = getOldSubEntity(targetEntity, column, obj);
int counter = 0;
Expand All @@ -437,7 +448,8 @@ protected int updateSubEntity(Map<String, Object> newSubEntityAttributes, Entity
* @return true if property was changed, false otherwise
* @throws JSONException
*/
protected boolean updateProperty(JSONObject aRecord, EntityType targetEntity, Object obj, JSONObject oldRecord, String attributeName) throws JSONException {
protected boolean updateProperty(JSONObject aRecord, EntityType targetEntity, Object obj, JSONObject oldRecord,
String attributeName) throws JSONException {
Object oldValue = getOldProperty(targetEntity, obj, attributeName);
oldRecord.put(attributeName, oldValue);
Object newValue = aRecord.get(attributeName);
Expand Down Expand Up @@ -473,7 +485,8 @@ private Object getOldSubEntity(EntityType targetEntity, Column column, Object ob
Object subEntity = new PropertyDescriptor(subKey, clazz).getReadMethod().invoke(obj);
return subEntity;
} catch (Exception e) {
throw new SpagoBIRuntimeException("Error while getting sub entity " + column.getSubEntity() + " from entity " + targetEntity, e);
throw new SpagoBIRuntimeException(
"Error while getting sub entity " + column.getSubEntity() + " from entity " + targetEntity, e);
}
} else {
throw new SpagoBIRuntimeException("Property " + column.getSubEntity() + " is not a many-to-one relation");
Expand Down Expand Up @@ -531,7 +544,8 @@ public void deleteRecord(JSONObject aRecord, RegistryConfiguration registryConf)

// String q =
// "DELETE from "+targetEntity.getName()+" o WHERE o."+keyAttributeName+"="+keyColumnValue.toString();
String q = "DELETE from " + targetEntity.getName() + " WHERE " + keyAttributeName + "=" + keyColumnValue.toString();
String q = "DELETE from " + targetEntity.getName() + " WHERE " + keyAttributeName + "="
+ keyColumnValue.toString();
logger.debug("create Query " + q);
Query deleteQuery = entityManager.createQuery(q);

Expand All @@ -541,8 +555,9 @@ public void deleteRecord(JSONObject aRecord, RegistryConfiguration registryConf)
// entityManager.flush();
entityTransaction.commit();

JPAPersistenceManagerAuditLogger.log(Operation.DELETION, this.getDataSource().getConfiguration().getModelName(), registryConf.getEntity(), aRecord,
null, null);
JPAPersistenceManagerAuditLogger.log(Operation.DELETION,
this.getDataSource().getConfiguration().getModelName(), registryConf.getEntity(), aRecord, null,
null);

} catch (Throwable t) {
if (entityTransaction != null && entityTransaction.isActive()) {
Expand Down Expand Up @@ -614,14 +629,16 @@ public Object getOldProperty(EntityType targetEntity, Object obj, String aKey) {
Object property = new PropertyDescriptor(aKey, clazz).getReadMethod().invoke(obj);
return property;
} catch (Exception e) {
throw new SpagoBIRuntimeException("Error while getting property " + aKey + " from entity " + targetEntity, e);
throw new SpagoBIRuntimeException("Error while getting property " + aKey + " from entity " + targetEntity,
e);
} finally {
logger.debug("OUT");
}
}

// case of foreign key
private void setSubEntity(EntityType targetEntity, Column c, Object obj, Map<String, Object> mewSubEntityAttributes, EntityManager entityManager) {
private void setSubEntity(EntityType targetEntity, Column c, Object obj, Map<String, Object> mewSubEntityAttributes,
EntityManager entityManager) {

Attribute a = targetEntity.getAttribute(c.getSubEntity());

Expand All @@ -634,12 +651,10 @@ private void setSubEntity(EntityType targetEntity, Column c, Object obj, Map<Str
Object referenced = getReferencedObjectJPA(entityManager, entityJavaType, mewSubEntityAttributes);

Class clas = targetEntity.getJavaType();
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(subKey, clas);
Method writeMethod = propertyDescriptor.getWriteMethod();
if (writeMethod != null)
writeMethod.invoke(obj, referenced);
else
throw new SpagoBIRuntimeException("Cannot find setter for property " + subKey + " for the value " + obj);
Field f = clas.getDeclaredField(subKey);
f.setAccessible(true);
// entityManager.refresh(referenced);
f.set(obj, referenced);
} catch (Exception e) {
throw new SpagoBIRuntimeException("Error setting sub-entity", e);
}
Expand All @@ -653,13 +668,10 @@ public void setProperty(EntityType targetEntity, Object obj, String aKey, Object
try {
Attribute a = targetEntity.getAttribute(aKey);
Class clas = targetEntity.getJavaType();
Field f = clas.getDeclaredField(aKey);
f.setAccessible(true);
Object valueConverted = this.convertValue(newValue, a);
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(aKey, clas);
Method writeMethod = propertyDescriptor.getWriteMethod();
if (writeMethod != null)
writeMethod.invoke(obj, valueConverted);
else
throw new SpagoBIRuntimeException("Cannot find setter for property " + aKey + " for the value " + valueConverted);
f.set(obj, valueConverted);
} catch (Exception e) {
throw new SpagoBIRuntimeException("Error setting Field " + aKey + "", e);
} finally {
Expand All @@ -674,13 +686,10 @@ private void setKeyProperty(EntityType targetEntity, Object obj, String aKey, In
try {
Attribute a = targetEntity.getAttribute(aKey);
Class clas = targetEntity.getJavaType();
Field f = clas.getDeclaredField(aKey);
f.setAccessible(true);
Object valueConverted = this.convertValue(value, a);
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(aKey, clas);
Method writeMethod = propertyDescriptor.getWriteMethod();
if (writeMethod != null)
writeMethod.invoke(obj, valueConverted);
else
throw new SpagoBIRuntimeException("Cannot find setter for property " + aKey + " for the value " + valueConverted);
f.set(obj, valueConverted);
} catch (Exception e) {
throw new SpagoBIRuntimeException("Error setting Field " + aKey + "", e);
} finally {
Expand Down Expand Up @@ -789,7 +798,8 @@ private Object convertValue(Object valueObj, Attribute attribute) {
return toReturn;
}

private Object getReferencedObjectJPA(EntityManager em, String entityJavaType, Map<String, Object> subEntityAttributes) {
private Object getReferencedObjectJPA(EntityManager em, String entityJavaType,
Map<String, Object> subEntityAttributes) {

String query = buildReferencedObjectQuery(entityJavaType, subEntityAttributes);
logQuery(query);
Expand All @@ -799,10 +809,12 @@ private Object getReferencedObjectJPA(EntityManager em, String entityJavaType, M
final List result = tmpQuery.getResultList();

if (result == null || result.isEmpty()) {
throw new SpagoBIRuntimeException("Record with attributes [" + subEntityAttributes + "] not found for entity " + entityJavaType);
throw new SpagoBIRuntimeException(
"Record with attributes [" + subEntityAttributes + "] not found for entity " + entityJavaType);
}
if (result.size() > 1) {
throw new SpagoBIRuntimeException("More than 1 record with attributes [" + subEntityAttributes + "] were found in entity " + entityJavaType);
throw new SpagoBIRuntimeException("More than 1 record with attributes [" + subEntityAttributes
+ "] were found in entity " + entityJavaType);
}

// load the object key
Expand Down Expand Up @@ -844,7 +856,8 @@ protected JPQLDataSet buildJPQLDatasetToReferenceObject(it.eng.qbe.query.Query q
try {
attributeValue = profile.getUserAttribute(attributeName);
} catch (EMFInternalError e) {
throw new SpagoBIRuntimeException("Error while getting user profile attribute [" + attributeName + "]", e);
throw new SpagoBIRuntimeException("Error while getting user profile attribute [" + attributeName + "]",
e);
}
userAttributes.put(attributeName, attributeValue);
}
Expand All @@ -853,7 +866,8 @@ protected JPQLDataSet buildJPQLDatasetToReferenceObject(it.eng.qbe.query.Query q
return dataset;
}

protected it.eng.qbe.query.Query buildRegularQueryToReferenceObject(String entityJavaType, Map<String, Object> subEntityAttributes) {
protected it.eng.qbe.query.Query buildRegularQueryToReferenceObject(String entityJavaType,
Map<String, Object> subEntityAttributes) {
it.eng.qbe.query.Query query = new it.eng.qbe.query.Query();
query.setId(StringUtilities.getRandomString(5));
int lastPkgDotSub = entityJavaType.lastIndexOf(".");
Expand All @@ -871,8 +885,8 @@ protected it.eng.qbe.query.Query buildRegularQueryToReferenceObject(String entit
if (value != null) {
WhereField.Operand left = new WhereField.Operand(new String[] { entityJavaType + ":" + key }, "name",
AbstractStatement.OPERAND_TYPE_SIMPLE_FIELD, null, null);
WhereField.Operand right = new WhereField.Operand(new String[] { value.toString() }, "value", AbstractStatement.OPERAND_TYPE_STATIC, null,
null);
WhereField.Operand right = new WhereField.Operand(new String[] { value.toString() }, "value",
AbstractStatement.OPERAND_TYPE_STATIC, null, null);
query.addWhereField(key, key, false, left, CriteriaConstants.EQUALS_TO, right, "AND");
expressionNodes.add(new ExpressionNode("NODE_CONST", "$F{" + key + "}"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package it.eng.qbe.statement.jpa;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -194,15 +193,18 @@ private void getFiltersValues(SessionImpl session, List<?> collectedParameterSpe
} catch (Exception e) {
throw new SpagoBIRuntimeException(e);
}
filterNameField.setAccessible(true);
parameterNameField.setAccessible(true);

/*
* Get values of parameters managing multivalues value
*/
for (Object currParameter : collectedParameterSpecifications) {
try {
String filterName = (String) new PropertyDescriptor(filterNameField.getName(), clas).getReadMethod().invoke(currParameter);
String parameterName = (String) new PropertyDescriptor(parameterNameField.getName(), clas).getReadMethod().invoke(currParameter);
Object value = session.getLoadQueryInfluencers().getFilterParameterValue(filterName + '.' + parameterName);
String filterName = (String) filterNameField.get(currParameter);
String parameterName = (String) parameterNameField.get(currParameter);
Object value = session.getLoadQueryInfluencers()
.getFilterParameterValue(filterName + '.' + parameterName);

if (value instanceof Collection) {
Collection<?> coll = (Collection) value;
Expand Down

0 comments on commit 57b02ab

Please sign in to comment.