Skip to content

Commit

Permalink
Fix sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Jun 23, 2024
1 parent 4ea9248 commit f3524f9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,31 @@ private void setSpecifiedValues(List<GeneratedAttribute> gattrs, String[] attrNa
log.trace("setSpecifiedValues: attrName={} attrValue={}", attrNames[i], attrValues[i]);
GeneratedAttribute gattr = findGeneratedAttribute(gattrs, attrNames[i]);
log.trace("setSpecifiedValues: before: {}", gattr);
gattr.specValue = attrValues[i];
if (symbols.isSymbol(attrValues[i])) {
// The specified value is symbolic, it is a uid that will store its value in a symbol
// or a rid that will use a previously stored value.
// Note that if is both uid and rid, it must use a previously stored value
// #tdrules-oa-rp#29)
if (gattr.attr.isUid() && !gattr.attr.isRid())
gattr.genType = GenType.GEN_PKSRV; // backend generated uid
else
gattr.genType = GenType.SPEC_FKSYM; // rid with symbolic value of a uid
} else {
gattr.genType = GenType.SPEC_USER;
gattr.genValue = attrValues[i];
}
// If composite, as a value has been specified, remove descendants
if (!gattr.isPrimitive())
gattr.children.clear();
setSpecifiedValuesForAttribute(gattr, attrNames[i], attrValues[i]);
log.trace("setSpecifiedValues: after: {}", gattr);
}
}
}

private void setSpecifiedValuesForAttribute(GeneratedAttribute gattr, String attrName, String attrValue) {
gattr.specValue = attrValue;
if (symbols.isSymbol(attrValue)) {
// The specified value is symbolic, it is a uid that will store its value in a symbol
// or a rid that will use a previously stored value.
// Note that if is both uid and rid, it must use a previously stored value
// #tdrules-oa-rp#29)
if (gattr.attr.isUid() && !gattr.attr.isRid())
gattr.genType = GenType.GEN_PKSRV; // backend generated uid
else
gattr.genType = GenType.SPEC_FKSYM; // rid with symbolic value of a uid
} else {
gattr.genType = GenType.SPEC_USER;
gattr.genValue = attrValue;
}
// If composite, as a value has been specified, remove descendants
if (!gattr.isPrimitive())
gattr.children.clear();
}

private void setArrayKeyAttributes(List<GeneratedAttribute> gattrs) {
for (GeneratedAttribute gattr : gattrs) {
if (OaExtensions.ARRAY_PK.equals(gattr.attr.getName()))
Expand All @@ -184,28 +187,10 @@ private void mainSetGeneratedValue(int depth, GeneratedAttribute ga, String enti
// Depending on the generation type, sets the generated falue or changes the generation type
if (ga.genType == GenType.GEN_DEFAULT) {
// Not specified, if it is not nullable and there is no default value, must generate a value
if (!ga.attr.isNullable() && !ga.attr.hasDefaultvalue()
|| config.genNullable && ga.attr.isNullable() && !ga.attr.isRid()
|| config.genDefault && ga.attr.hasDefaultvalue() && !ga.attr.isRid()) {
ga.genType = GenType.GEN_HERE;
if (ga.isPrimitive()) {
// Do not generate data when it is a composite that references an object with an uid
// (this is because in this case, the values are given by the reference object)
ga.genValue = isInCompositeTypeWithFk(ga) ? "" : generateAttributeValue(ga.constraints, entity, ga.attr);
} else {
log.debug(" generate composite: entity={} children={}", ga.getName(), ga.children);
setGeneratedValues(depth + 1, ga.children, ga.getName());
log.trace(" end generate composite: entity={}", ga.getName());
}
}
mainSetGeneratedValueDefault(depth, ga, entity);
} else if (ga.genType == GenType.GEN_PKSRV) {
// A new uid must be generated now (not backend generated), only if the key gen indicates this
String generated = config.uidGen.getNew(entity, ga.getName());
if (generated != null) {
ga.genValue = generated;
// as a value has been generated, changes the generation type
ga.genType = GenType.GEN_HERE;
}
mainSetGeneratedValuePkSrv(ga, entity);
} else if (ga.genType == GenType.SPEC_FKSYM) {
// A rid with a symbol, uses the symbol value that has been previously generated
ga.genValue = symbols.getValue(ga.attr.getRid(), ga.specValue);
Expand All @@ -220,7 +205,31 @@ private void mainSetGeneratedValue(int depth, GeneratedAttribute ga, String enti
config.attrGen.incrementAttrCount(); // for deterministic generation
log.trace("setGeneratedValue: {}", ga);
}

private void mainSetGeneratedValueDefault(int depth, GeneratedAttribute ga, String entity) {
if (!ga.attr.isNullable() && !ga.attr.hasDefaultvalue()
|| config.genNullable && ga.attr.isNullable() && !ga.attr.isRid()
|| config.genDefault && ga.attr.hasDefaultvalue() && !ga.attr.isRid()) {
ga.genType = GenType.GEN_HERE;
if (ga.isPrimitive()) {
// Do not generate data when it is a composite that references an object with an uid
// (this is because in this case, the values are given by the reference object)
ga.genValue = isInCompositeTypeWithFk(ga) ? "" : generateAttributeValue(ga.constraints, entity, ga.attr);
} else {
log.debug(" generate composite: entity={} children={}", ga.getName(), ga.children);
setGeneratedValues(depth + 1, ga.children, ga.getName());
log.trace(" end generate composite: entity={}", ga.getName());
}
}
}
private void mainSetGeneratedValuePkSrv(GeneratedAttribute ga, String entity) {
String generated = config.uidGen.getNew(entity, ga.getName());
if (generated != null) {
ga.genValue = generated;
// as a value has been generated, changes the generation type
ga.genType = GenType.GEN_HERE;
}
}

private boolean isInCompositeTypeWithFk(GeneratedAttribute ga) {
if (ga.parent == null)
return false;
Expand All @@ -246,35 +255,37 @@ private String mainWriteGeneratedValues(IDataAdapter adapter, List<GeneratedAttr
adapter.beginWrite(entity);
for (GeneratedAttribute gc : gattrs) // only if specified or generated value
if (gc.genType == GenType.SPEC_USER || gc.genType == GenType.GEN_HERE || gc.genType == GenType.SPEC_FKSYM) {
if (gc.isPrimitive() && !gc.isArray) {
String val = gc.genValue == null || "null".equalsIgnoreCase(gc.genValue) ? null : gc.genValue;
if (!gc.attr.isReadonly())
adapter.writeValue(gc.attr.getDatatype(), gc.getName(), val);
} else if (!gc.isArray) {
// if it is a type, generates the children using another data adapter instance.
// Note that the adapter must be local because no data must be sent now trough the api,
// data is sent as part of the entity that contains the composite
IDataAdapter clon = adapter.getNewLocalAdapter();
setGeneratedValues(0, gattrs, entity);
mainWriteGeneratedValues(clon, gc.children, gc.getName());
writeCompositeType(adapter, gc, clon.getLast());
} else {
// Array, the values were generated previously and stored in a symbol
GeneratedAttribute gkey = gattrs.get(0);
List<String> arrayValues = symbols.getArray(gc.attr.getDatatype(), OaExtensions.ARRAY_FK, gkey.specValue);
arrayValues = arrayValues == null ? new ArrayList<>() : arrayValues; // avoid null if no values
log.debug("writeGeneratedValues: Array values, attribute: {}={}", gc.getName(), arrayValues);
// Different handling of object and primitive arrays, adds a prefix to the data type
String dataType = ("object".equals(gc.attr.getSubtype()) ? "object" : "primitive") + EntityTypes.DT_ARRAY;
if (!gc.attr.isReadonly())
adapter.writeValue(dataType, gc.getName(), arrayValues.toString());
}
mainWriteGeneratedValueForAttribute(adapter, gattrs, entity, gc);
}

adapter.endWrite();
config.uidGen.setLastResponse(entity, adapter.getLastResponse());
return adapter.getLast();
}
private void mainWriteGeneratedValueForAttribute(IDataAdapter adapter, List<GeneratedAttribute> gattrs, String entity, GeneratedAttribute gc) {
if (gc.isPrimitive() && !gc.isArray) {
String val = gc.genValue == null || "null".equalsIgnoreCase(gc.genValue) ? null : gc.genValue;
if (!gc.attr.isReadonly())
adapter.writeValue(gc.attr.getDatatype(), gc.getName(), val);
} else if (!gc.isArray) {
// if it is a type, generates the children using another data adapter instance.
// Note that the adapter must be local because no data must be sent now trough the api,
// data is sent as part of the entity that contains the composite
IDataAdapter clon = adapter.getNewLocalAdapter();
setGeneratedValues(0, gattrs, entity);
mainWriteGeneratedValues(clon, gc.children, gc.getName());
writeCompositeType(adapter, gc, clon.getLast());
} else {
// Array, the values were generated previously and stored in a symbol
GeneratedAttribute gkey = gattrs.get(0);
List<String> arrayValues = symbols.getArray(gc.attr.getDatatype(), OaExtensions.ARRAY_FK, gkey.specValue);
arrayValues = arrayValues == null ? new ArrayList<>() : arrayValues; // avoid null if no values
log.debug("writeGeneratedValues: Array values, attribute: {}={}", gc.getName(), arrayValues);
// Different handling of object and primitive arrays, adds a prefix to the data type
String dataType = ("object".equals(gc.attr.getSubtype()) ? "object" : "primitive") + EntityTypes.DT_ARRAY;
if (!gc.attr.isReadonly())
adapter.writeValue(dataType, gc.getName(), arrayValues.toString());
}
}

private void writeCompositeType(IDataAdapter adapter, GeneratedAttribute gc, String value) {
// If the generated value must be null, the values for children that have been generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void setUp() throws SQLException {
*/
protected Connection getConnection(String database) throws SQLException {
log.debug("Create connection to '{}' database", dbmsname);
String propPrefix = "tdrules." + PLATFORM + "." + TEST_DBNAME + "." + dbmsname;
String propPrefix = "tdrules." + PLATFORM + "." + database + "." + dbmsname;
Connection conn = DriverManager.getConnection(
new JdbcProperties().getProp(DATABASE_PROPERTIES, propPrefix + ".url"),
new JdbcProperties().getProp(DATABASE_PROPERTIES, propPrefix + ".user"),
Expand Down

0 comments on commit f3524f9

Please sign in to comment.