Search in sources :

Example 16 with MetaField

use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.

the class ConfiguratorCreatorServiceImpl method updateIndicatorAttrs.

/**
 * Update one indicator attrs in the view, using the corresponding formula. Do nothing if
 * indicator and formula do not represent the same field.
 *
 * @param indicator
 * @param formula
 */
protected void updateIndicatorAttrs(ConfiguratorCreator creator, MetaJsonField indicator, ConfiguratorFormula formula) {
    int scale = Beans.get(AppBaseService.class).getNbDecimalDigitForUnitPrice();
    String fieldName = indicator.getName();
    fieldName = fieldName.substring(0, fieldName.indexOf('_'));
    MetaField metaField = formula.getMetaField();
    if (!metaField.getName().equals(fieldName)) {
        return;
    }
    if (formula.getShowOnConfigurator()) {
        indicator.setHidden(false);
        setContextToJsonField(creator, indicator);
    } else {
        indicator.setHidden(true);
    }
    if (metaField.getTypeName().equals("BigDecimal")) {
        indicator.setPrecision(20);
        indicator.setScale(scale);
    } else if (!Strings.isNullOrEmpty(metaField.getRelationship())) {
        indicator.setTargetModel(Beans.get(MetaModelRepository.class).findByName(metaField.getTypeName()).getFullName());
    }
}
Also used : AppBaseService(com.axelor.apps.base.service.app.AppBaseService) MetaField(com.axelor.meta.db.MetaField)

Example 17 with MetaField

use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.

the class ConfiguratorServiceImpl method fetchManyToManyFields.

/**
 * Fix relational fields of a product or a sale order line generated from a configurator. This
 * method may become useless on a future ADK update.
 *
 * @param model
 */
protected void fetchManyToManyFields(Model model) throws AxelorException {
    // get all many to many fields
    List<MetaField> manyToManyFields = metaFieldRepository.all().filter("self.metaModel.name = :name " + "AND self.relationship = 'ManyToMany'").bind("name", model.getClass().getSimpleName()).fetch();
    Mapper mapper = Mapper.of(model.getClass());
    for (MetaField manyToManyField : manyToManyFields) {
        Set<? extends Model> manyToManyValue = (Set<? extends Model>) mapper.get(model, manyToManyField.getName());
        fetchManyToManyField(model, manyToManyValue, manyToManyField);
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) HashSet(java.util.HashSet) Set(java.util.Set) MetaField(com.axelor.meta.db.MetaField) Model(com.axelor.db.Model)

Example 18 with MetaField

use of com.axelor.meta.db.MetaField in project axelor-open-suite by axelor.

the class ConfiguratorServiceImpl method fixRelationalFields.

protected void fixRelationalFields(Model model) throws AxelorException {
    // get all many to one fields
    List<MetaField> manyToOneFields = metaFieldRepository.all().filter("self.metaModel.name = :name " + "AND self.relationship = 'ManyToOne'").bind("name", model.getClass().getSimpleName()).fetch();
    Mapper mapper = Mapper.of(model.getClass());
    for (MetaField manyToOneField : manyToOneFields) {
        Model manyToOneValue = (Model) mapper.get(model, manyToOneField.getName());
        fixRelationalField(model, manyToOneValue, manyToOneField);
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) MetaField(com.axelor.meta.db.MetaField) Model(com.axelor.db.Model)

Example 19 with MetaField

use of com.axelor.meta.db.MetaField 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 20 with MetaField

use of com.axelor.meta.db.MetaField 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)

Aggregations

MetaField (com.axelor.meta.db.MetaField)45 MetaJsonField (com.axelor.meta.db.MetaJsonField)15 MetaModel (com.axelor.meta.db.MetaModel)10 Mapper (com.axelor.db.mapper.Mapper)9 AxelorException (com.axelor.exception.AxelorException)8 ArrayList (java.util.ArrayList)6 Property (com.axelor.db.mapper.Property)5 Filter (com.axelor.studio.db.Filter)4 Model (com.axelor.db.Model)3 MetaFieldRepository (com.axelor.meta.db.repo.MetaFieldRepository)3 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Company (com.axelor.apps.base.db.Company)2 FileField (com.axelor.apps.base.db.FileField)2 ProductCompany (com.axelor.apps.base.db.ProductCompany)2 Context (com.axelor.rpc.Context)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Transactional (com.google.inject.persist.Transactional)2 BigInteger (java.math.BigInteger)2