use of org.jumpmind.pos.persist.PersistException in project openpos-framework by JumpMind.
the class DmlTemplate method generateSQL.
public SqlStatement generateSQL(String sql, Map<String, Object> params) {
List<String> keys = new ArrayList<>();
StringSubstitutor literalSubstitution = new StringSubstitutor(new StringLookup() {
@Override
public String lookup(String key) {
Object paramValue = params.get(key);
return paramValue != null ? paramValue.toString() : "null";
}
}, "$${", "}", '\\');
StringSubstitutor sub = new StringSubstitutor(new StringLookup() {
@Override
public String lookup(String key) {
keys.add(key);
return ":" + key;
}
});
String preparedSql = literalSubstitution.replace(sql);
preparedSql = sub.replace(preparedSql);
String preppedWhereClause = literalSubstitution.replace(this.getWhere());
preppedWhereClause = sub.replace(preppedWhereClause);
StringBuilder buff = new StringBuilder();
preparedSql = stripWhere(preparedSql);
boolean hasWhereKeyword = false;
buff.append(preparedSql);
if (!StringUtils.isEmpty(preppedWhereClause)) {
hasWhereKeyword = true;
buff.append(" WHERE ");
buff.append(preppedWhereClause);
}
for (String optionalWhereClause : this.getOptionalWhereClauses()) {
Set<String> optionalWhereClauseKeys = new LinkedHashSet<>();
String preppedOptionalWhereClause = literalSubstitution.replace(optionalWhereClause);
StringSubstitutor optionalSubstitution = new StringSubstitutor(new StringLookup() {
@Override
public String lookup(String key) {
optionalWhereClauseKeys.add(key);
return ":" + key;
}
});
preppedOptionalWhereClause = optionalSubstitution.replace(preppedOptionalWhereClause);
boolean shouldInclude = true;
for (String key : optionalWhereClauseKeys) {
if (!params.containsKey(key)) {
shouldInclude = false;
break;
}
}
if (shouldInclude) {
if (!hasWhereKeyword) {
buff.append(" WHERE 1=1 ");
hasWhereKeyword = true;
}
buff.append(" AND (");
buff.append(preppedOptionalWhereClause);
buff.append(")");
keys.addAll(optionalWhereClauseKeys);
}
}
SqlStatement sqlStatement = new SqlStatement();
sqlStatement.setSql(buff.toString());
for (String key : keys) {
Object value = params.get(key);
if (value == null) {
value = params.get("*");
params.put(key, value);
}
if (value == null) {
if (params.containsKey(key)) {
throw new PersistException(String.format("Required query parameter '%s' was present but the value is null. A value must be provided. Cannot build query: %s", key, sqlStatement.getSql()));
} else {
throw new PersistException(String.format("Missing required query parameter '%s'. Cannot build query: %s", key, sqlStatement.getSql()));
}
} else if (value instanceof Boolean) {
boolean bool = (Boolean) value;
value = bool ? 1 : 0;
params.put(key, value);
} else if (value instanceof AbstractTypeCode) {
value = ((AbstractTypeCode) value).value();
params.put(key, value);
}
}
if (params != null) {
params.remove("*");
}
sqlStatement.setParameters(params);
return sqlStatement;
}
use of org.jumpmind.pos.persist.PersistException in project openpos-framework by JumpMind.
the class ModelValidator method checkCrossRefFields.
protected static void checkCrossRefFields(ModelClassMetaData meta) {
Class<?> modelClass = meta.getClazz();
List<Class<?>> compositeDefClasses = getCompositeDefClasses(modelClass);
for (Field field : modelClass.getDeclaredFields()) {
ColumnDef columnAnnotation = field.getAnnotation(ColumnDef.class);
if (columnAnnotation != null) {
if (field.getType().isAssignableFrom(Money.class)) {
if (StringUtils.isEmpty(columnAnnotation.crossReference()) && columnAnnotation.crossReferences().length == 0) {
throw new PersistException("columns of Money type require a ColumnDef with crossReference, " + "such as @ColumnDef(crossReference=\"isoCurrencyCode\"). see " + field.getName() + " on model " + modelClass);
}
}
if (!StringUtils.isEmpty(columnAnnotation.crossReference())) {
FieldMetaData xRefFieldMeta = meta.getEntityFieldMetaDatas().get(columnAnnotation.crossReference());
if (xRefFieldMeta == null) {
xRefFieldMeta = meta.getEntityIdFieldMetaDatas().get(columnAnnotation.crossReference());
}
if (xRefFieldMeta == null) {
throw new PersistException("No matching field found for ColumnDef crossReference=\"" + columnAnnotation.crossReference() + "\" see the \"" + field.getName() + "\" field on model " + modelClass);
}
}
}
}
}
use of org.jumpmind.pos.persist.PersistException in project openpos-framework by JumpMind.
the class ModelValidator method checkAugmentedFields.
protected static void checkAugmentedFields(ModelClassMetaData meta) {
if (CollectionUtils.size(meta.getAugmenterConfigs()) > 1) {
Map<String, Integer> augmenterNameCounts = new HashMap<>();
for (AugmenterConfig config : meta.getAugmenterConfigs()) {
for (String name : config.getAugmenterNames()) {
Integer count = augmenterNameCounts.get(name);
if (count == null) {
count = 1;
} else {
count++;
}
augmenterNameCounts.put(name, count);
}
}
for (Map.Entry<String, Integer> entry : augmenterNameCounts.entrySet()) {
if (entry.getValue() > 1) {
throw new PersistException("Duplicate augmenter name " + entry.getKey() + " found on model " + meta.getClazz());
}
}
}
}
use of org.jumpmind.pos.persist.PersistException in project openpos-framework by JumpMind.
the class ModelValidator method checkOrphanedFields.
protected static void checkOrphanedFields(ModelClassMetaData meta) {
List<Class<?>> compositeDefClasses;
Class<?> modelClass = meta.getClazz();
for (Field field : modelClass.getDeclaredFields()) {
ColumnDef columnAnnotation = field.getAnnotation(ColumnDef.class);
if (columnAnnotation != null) {
// an annotated column MUST have a getter/setter pair to be handled properly
// by the persistence layer.
String fieldNameCapatalized = StringUtils.capitalize(field.getName());
try {
Method setter = modelClass.getDeclaredMethod("set" + fieldNameCapatalized, field.getType());
if (setter == null) {
throw new PersistException("Failed to locate setter set" + fieldNameCapatalized);
}
String prefix = field.getType().isAssignableFrom(boolean.class) ? "is" : "get";
Method getter = modelClass.getDeclaredMethod(prefix + fieldNameCapatalized);
if (!getter.getReturnType().isAssignableFrom(field.getType())) {
throw new PersistException("getter has wrong return type. " + getter);
}
} catch (Exception ex) {
throw new PersistException("Failed to locate required getter/setter pair for " + field.getName() + " on model " + modelClass + ". Make sure your model class as the proper getter/setter for @ColumnDef field " + field.getName() + " (" + ex.toString() + ")");
}
}
}
}
use of org.jumpmind.pos.persist.PersistException in project openpos-framework by JumpMind.
the class QueryTemplate method generateSQL.
public SqlStatement generateSQL(Query<?> query, Map<String, Object> params) {
String select = this.getSelect();
List<String> keys = new ArrayList<>();
StringSubstitutor literalSubstitution = new StringSubstitutor(new StringLookup() {
@Override
public String lookup(String key) {
Object paramValue = params.get(key);
return paramValue != null ? paramValue.toString() : "null";
}
}, "$${", "}", '\\');
StringSubstitutor sub = new StringSubstitutor(new StringLookup() {
@Override
public String lookup(String key) {
keys.add(key);
return ":" + key;
}
});
String preppedSelectClause = literalSubstitution.replace(select);
preppedSelectClause = sub.replace(preppedSelectClause);
String preppedWhereClause = literalSubstitution.replace(this.getWhere());
preppedWhereClause = sub.replace(preppedWhereClause);
StringBuilder buff = new StringBuilder();
preppedSelectClause = stripWhere(preppedSelectClause);
boolean hasWhereKeyword = false;
buff.append(preppedSelectClause);
if (!StringUtils.isEmpty(preppedWhereClause)) {
hasWhereKeyword = true;
buff.append(" WHERE ");
buff.append(preppedWhereClause);
}
boolean firstIncluded = true;
for (String optionalWhereClause : this.getOptionalWhereClauses()) {
Set<String> optionalWhereClauseKeys = new LinkedHashSet<>();
String preppedOptionalWhereClause = literalSubstitution.replace(optionalWhereClause);
StringSubstitutor optionalSubstitution = new StringSubstitutor(new StringLookup() {
@Override
public String lookup(String key) {
optionalWhereClauseKeys.add(key);
return ":" + key;
}
});
preppedOptionalWhereClause = optionalSubstitution.replace(preppedOptionalWhereClause);
boolean shouldInclude = true;
for (String key : optionalWhereClauseKeys) {
if (!params.containsKey(key)) {
shouldInclude = false;
break;
}
}
if (shouldInclude) {
if (!hasWhereKeyword) {
buff.append(" WHERE 1=1 ");
hasWhereKeyword = true;
}
if (query.isUseAnd() || firstIncluded) {
buff.append(" AND (");
} else {
buff.append(" OR (");
}
buff.append(preppedOptionalWhereClause);
buff.append(")");
keys.addAll(optionalWhereClauseKeys);
}
if (shouldInclude) {
firstIncluded = false;
}
}
splitTooManyValuesInClause(query, params, buff);
if (!StringUtils.isEmpty(this.getGroupBy())) {
buff.append(" GROUP BY ");
buff.append(this.getGroupBy());
}
if (!StringUtils.isEmpty(this.getOrderBy())) {
buff.append(" ORDER BY ");
buff.append(this.getOrderBy());
}
SqlStatement sqlStatement = new SqlStatement();
sqlStatement.setSql(buff.toString());
for (String key : keys) {
Object value = params.get(key);
if (value == null) {
value = params.get("*");
params.put(key, value);
}
if (value == null) {
if (params.containsKey(key)) {
throw new PersistException(String.format("Required query parameter '%s' was present but the value is null. A value must be provided. Cannot build query: %s", key, sqlStatement.getSql()));
} else {
throw new PersistException(String.format("Missing required query parameter '%s'. Cannot build query: %s", key, sqlStatement.getSql()));
}
} else if (value instanceof Boolean) {
boolean bool = (Boolean) value;
value = bool ? 1 : 0;
params.put(key, value);
} else if (value instanceof AbstractTypeCode) {
value = ((AbstractTypeCode) value).value();
params.put(key, value);
}
}
if (params != null) {
params.remove("*");
}
sqlStatement.setParameters(params);
return sqlStatement;
}
Aggregations