use of com.axelor.meta.db.MetaJsonField in project axelor-open-suite by axelor.
the class ConfiguratorCreatorServiceImpl method getTestingValues.
@Override
public ScriptBindings getTestingValues(ConfiguratorCreator creator) {
Map<String, Object> attributesValues = new HashMap<>();
List<MetaJsonField> attributes = creator.getAttributes();
if (attributes != null) {
for (MetaJsonField attribute : attributes) {
Object defaultAttribute = getAttributesDefaultValue(attribute);
if (defaultAttribute != null) {
attributesValues.put(attribute.getName(), getAttributesDefaultValue(attribute));
}
}
}
return new ScriptBindings(attributesValues);
}
use of com.axelor.meta.db.MetaJsonField in project axelor-open-suite by axelor.
the class ConfiguratorServiceImpl method updateIndicators.
@Override
public void updateIndicators(Configurator configurator, JsonContext jsonAttributes, JsonContext jsonIndicators) throws AxelorException {
if (configurator.getConfiguratorCreator() == null) {
return;
}
List<MetaJsonField> indicators = configurator.getConfiguratorCreator().getIndicators();
indicators = indicators.stream().filter(metaJsonField -> !"one-to-many".equals(metaJsonField.getType())).collect(Collectors.toList());
for (MetaJsonField indicator : indicators) {
try {
Object calculatedValue = computeIndicatorValue(configurator, indicator.getName(), jsonAttributes);
checkType(calculatedValue, indicator);
jsonIndicators.put(indicator.getName(), calculatedValue);
} catch (MissingPropertyException e) {
// if a field is missing, the value needs to be set to null
continue;
}
}
}
use of com.axelor.meta.db.MetaJsonField in project axelor-open-suite by axelor.
the class FilterGroovyService method getMetaFieldType.
private String getMetaFieldType(MetaField field, String targetField, boolean isJson) throws AxelorException {
if (targetField == null || !targetField.contains(".")) {
return field.getTypeName();
}
targetField = targetField.substring(targetField.indexOf(".") + 1);
String targetName = targetField.contains(".") ? targetField.substring(0, targetField.indexOf(".")) : targetField;
MetaModel model = metaModelRepo.findByName(field.getTypeName());
if (model == null) {
throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, "No model found: %s ", field.getName());
}
MetaField subMeta = filterSqlService.findMetaField(targetName, model.getFullName());
if (subMeta != null) {
return getMetaFieldType(subMeta, targetField, isJson);
} else if (isJson) {
MetaJsonField subJson = filterSqlService.findJsonField(targetName, model.getName());
if (subJson != null) {
return getJsonFieldType(subJson, targetField);
}
}
throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, "No sub field found field: %s model: %s ", targetName, model.getFullName());
}
use of com.axelor.meta.db.MetaJsonField in project axelor-open-suite by axelor.
the class FilterSqlService method parseJsonField.
public Object parseJsonField(MetaJsonField field, String target, List<String> joins, StringBuilder parent) throws AxelorException {
log.debug("Parse json target: {}", target);
if (target == null || !target.contains(".")) {
if (field.getTargetJsonModel() != null && joins != null) {
target = getDefaultTargetJson(field.getName(), field.getTargetJsonModel())[0];
} else if (field.getTargetModel() != null && joins != null) {
target = getDefaultTarget(field.getName(), field.getTargetModel())[0];
} else {
return field;
}
}
target = target.substring(target.indexOf(".") + 1);
String targetName = target.contains(".") ? target.substring(0, target.indexOf(".")) : target;
if (field.getTargetJsonModel() == null && field.getTargetModel() == null) {
return field;
}
if (field.getTargetJsonModel() != null) {
MetaJsonField subJson = metaJsonFieldRepo.all().filter("self.name = ?1 and self.jsonModel = ?2", targetName, field.getTargetJsonModel()).fetchOne();
if (subJson != null) {
if (joins != null) {
addJoin(field, joins, parent);
}
return parseJsonField(subJson, target, joins, parent);
}
throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, "No sub field found model: %s field %s ", field.getTargetJsonModel().getName(), targetName);
} else {
MetaField subMeta = findMetaField(targetName, field.getTargetModel());
if (subMeta != null) {
if (joins != null) {
addJoin(field, joins, parent);
}
return parseMetaField(subMeta, target, joins, parent, true);
}
throw new AxelorException(TraceBackRepository.CATEGORY_MISSING_FIELD, "No sub field found model: %s field %s ", field.getTargetModel(), targetName);
}
}
use of com.axelor.meta.db.MetaJsonField in project axelor-open-suite by axelor.
the class FilterSqlService method getSqlField.
public String[] getSqlField(Object target, String source, List<String> joins) {
String field = null;
String type = null;
String selection = null;
if (target instanceof MetaField) {
MetaField metaField = (MetaField) target;
field = source + "." + getColumn(metaField);
type = metaField.getTypeName();
try {
selection = Mapper.of(Class.forName(metaField.getMetaModel().getFullName())).getProperty(metaField.getName()).getSelection();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} else {
MetaJsonField metaJsonField = (MetaJsonField) target;
selection = metaJsonField.getSelection();
String jsonColumn = getColumn(metaJsonField.getModel(), metaJsonField.getModelField());
field = "cast(" + source + "." + jsonColumn + "->>'" + metaJsonField.getName() + "' as " + getSqlType(metaJsonField.getType()) + ")";
type = metaJsonField.getType();
}
log.debug("Selection for field :{} : {}", field, selection);
if (joins != null && !Strings.isNullOrEmpty(selection)) {
int join = joins.size();
joins.add("left join meta_select obj" + join + " on (obj" + join + ".name = '" + selection + "')");
join = joins.size();
joins.add("left join meta_select_item obj" + join + " on (obj" + join + ".select_id = obj" + (join - 1) + ".id and obj" + join + ".value = cast(" + field + " as varchar))");
join = joins.size();
joins.add("left join meta_translation obj" + join + " on (obj" + join + ".message_key = obj" + (join - 1) + ".title and obj" + join + ".language = (select language from auth_user where id = :__user__))");
field = "COALESCE(nullif(obj" + join + ".message_value,''), obj" + (join - 1) + ".title)";
}
return new String[] { field, type };
}
Aggregations