Search in sources :

Example 6 with MetaJsonField

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);
}
Also used : HashMap(java.util.HashMap) MetaJsonField(com.axelor.meta.db.MetaJsonField) ScriptBindings(com.axelor.script.ScriptBindings)

Example 7 with MetaJsonField

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;
        }
    }
}
Also used : MissingPropertyException(groovy.lang.MissingPropertyException) MetaJsonField(com.axelor.meta.db.MetaJsonField)

Example 8 with MetaJsonField

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());
}
Also used : AxelorException(com.axelor.exception.AxelorException) MetaModel(com.axelor.meta.db.MetaModel) MetaField(com.axelor.meta.db.MetaField) MetaJsonField(com.axelor.meta.db.MetaJsonField)

Example 9 with MetaJsonField

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);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) MetaField(com.axelor.meta.db.MetaField) MetaJsonField(com.axelor.meta.db.MetaJsonField)

Example 10 with MetaJsonField

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 };
}
Also used : MetaField(com.axelor.meta.db.MetaField) MetaJsonField(com.axelor.meta.db.MetaJsonField)

Aggregations

MetaJsonField (com.axelor.meta.db.MetaJsonField)30 MetaField (com.axelor.meta.db.MetaField)14 AxelorException (com.axelor.exception.AxelorException)7 HashMap (java.util.HashMap)5 MetaJsonFieldRepository (com.axelor.meta.db.repo.MetaJsonFieldRepository)4 Transactional (com.google.inject.persist.Transactional)4 Map (java.util.Map)4 Mapper (com.axelor.db.mapper.Mapper)3 WkfCommonService (com.axelor.apps.bpm.service.WkfCommonService)2 ConfiguratorFormula (com.axelor.apps.sale.db.ConfiguratorFormula)2 Property (com.axelor.db.mapper.Property)2 MetaModel (com.axelor.meta.db.MetaModel)2 Filter (com.axelor.studio.db.Filter)2 File (java.io.File)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 FileTab (com.axelor.apps.base.db.FileTab)1 FileTabService (com.axelor.apps.base.service.advanced.imports.FileTabService)1 Configurator (com.axelor.apps.sale.db.Configurator)1