Search in sources :

Example 26 with MetaField

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

the class ObjectDataAnonymizeServiceImpl method getDefaultValues.

private Map<String, Object> getDefaultValues(Mapper mapper, Set<MetaField> fields) {
    Map<String, Object> defaultValues = new HashMap<>();
    LocalDate defaultDate = LocalDate.of(1900, 01, 01);
    for (MetaField field : fields) {
        String name = field.getName();
        Property property = mapper.getProperty(name);
        if (!property.isRequired()) {
            defaultValues.put(name, null);
            continue;
        }
        if (property.getTarget() != null) {
            continue;
        }
        switch(field.getTypeName()) {
            case "String":
                {
                    defaultValues.put(name, "anonymous");
                    break;
                }
            case "BigDecimal":
                {
                    defaultValues.put(name, BigDecimal.ZERO);
                    break;
                }
            case "Integer":
                {
                    defaultValues.put(name, Integer.valueOf(0));
                    break;
                }
            case "Boolean":
                {
                    defaultValues.put(name, Boolean.FALSE);
                    break;
                }
            case "Long":
                {
                    defaultValues.put(name, Long.valueOf(0));
                    break;
                }
            case "LocalTime":
                {
                    defaultValues.put(name, LocalTime.MIDNIGHT);
                    break;
                }
            case "LocalDateTime":
                {
                    defaultValues.put(name, defaultDate.atStartOfDay());
                    break;
                }
            case "ZonedDateTime":
                {
                    defaultValues.put(name, defaultDate.atStartOfDay(ZoneId.systemDefault()));
                    break;
                }
            default:
                {
                    break;
                }
        }
    }
    return defaultValues;
}
Also used : HashMap(java.util.HashMap) MetaField(com.axelor.meta.db.MetaField) LocalDate(java.time.LocalDate) Property(com.axelor.db.mapper.Property)

Example 27 with MetaField

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

the class ConfiguratorCreatorServiceImpl method completeSelection.

/**
 * Fill {@link MetaJsonField#selection} searching in java class code.
 *
 * @param metaField a meta field.
 * @param newField a meta json field.
 */
protected void completeSelection(MetaField metaField, MetaJsonField newField) {
    try {
        Field correspondingField = Class.forName(metaField.getMetaModel().getPackageName() + "." + metaField.getMetaModel().getName()).getDeclaredField(metaField.getName());
        Widget widget = correspondingField.getAnnotation(Widget.class);
        if (widget == null) {
            return;
        }
        String selection = widget.selection();
        if (!Strings.isNullOrEmpty(selection)) {
            newField.setSelection(selection);
        }
    } catch (ClassNotFoundException | NoSuchFieldException e) {
        TraceBackService.trace(e);
    }
}
Also used : MetaJsonField(com.axelor.meta.db.MetaJsonField) MetaField(com.axelor.meta.db.MetaField) Field(java.lang.reflect.Field) Widget(com.axelor.db.annotations.Widget)

Example 28 with MetaField

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

the class ConfiguratorCreatorServiceImpl method addIfMissing.

/**
 * Add the {@link ConfiguratorFormula} in {@link ConfiguratorCreator#indicators} if the formula is
 * not represented by an existing indicator.
 *
 * @param formula
 * @param creator
 */
protected void addIfMissing(ConfiguratorFormula formula, ConfiguratorCreator creator) throws AxelorException {
    MetaField formulaMetaField = formula.getMetaField();
    List<MetaJsonField> fields = Optional.ofNullable(creator.getIndicators()).orElse(Collections.emptyList());
    for (MetaJsonField field : fields) {
        if (field.getName().equals(formulaMetaField.getName() + "_" + creator.getId())) {
            return;
        }
    }
    String metaModelName = formulaMetaField.getMetaModel().getName();
    MetaJsonField newField = new MetaJsonField();
    newField.setModel(Configurator.class.getName());
    newField.setModelField("indicators");
    MetaField metaField = Beans.get(MetaFieldRepository.class).all().filter("self.metaModel.name = :metaModelName AND self.name = :name").bind("metaModelName", metaModelName).bind("name", formulaMetaField.getName()).fetchOne();
    String typeName;
    if (!Strings.isNullOrEmpty(metaField.getRelationship())) {
        typeName = metaField.getRelationship();
        completeDefaultGridAndForm(metaField, newField);
    } else {
        typeName = metaField.getTypeName();
    }
    completeSelection(metaField, newField);
    newField.setType(MetaTool.typeToJsonType(typeName));
    newField.setName(formulaMetaField.getName() + "_" + creator.getId());
    newField.setTitle(formulaMetaField.getLabel());
    creator.addIndicator(newField);
}
Also used : MetaField(com.axelor.meta.db.MetaField) Configurator(com.axelor.apps.sale.db.Configurator) MetaJsonField(com.axelor.meta.db.MetaJsonField) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository)

Example 29 with MetaField

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

the class ConfiguratorServiceImpl method computeIndicatorValue.

/**
 * Compute the value of one indicator. Using the corresponding formula and the values in {@link
 * Configurator#attributes}
 *
 * @param configurator
 * @param indicatorName
 * @param jsonAttributes
 * @return
 */
protected Object computeIndicatorValue(Configurator configurator, String indicatorName, JsonContext jsonAttributes) {
    ConfiguratorCreator creator = configurator.getConfiguratorCreator();
    List<? extends ConfiguratorFormula> formulas;
    if (creator.getGenerateProduct()) {
        formulas = creator.getConfiguratorProductFormulaList();
    } else {
        formulas = creator.getConfiguratorSOLineFormulaList();
    }
    String groovyFormula = null;
    for (ConfiguratorFormula formula : formulas) {
        String fieldName = indicatorName;
        fieldName = fieldName.substring(0, fieldName.indexOf('_'));
        MetaField metaField = formula.getMetaField();
        if (metaField.getName().equals(fieldName)) {
            groovyFormula = formula.getFormula();
            break;
        }
    }
    if (groovyFormula == null || jsonAttributes == null) {
        return null;
    }
    return computeFormula(groovyFormula, jsonAttributes);
}
Also used : ConfiguratorCreator(com.axelor.apps.sale.db.ConfiguratorCreator) ConfiguratorFormula(com.axelor.apps.sale.db.ConfiguratorFormula) MetaField(com.axelor.meta.db.MetaField)

Example 30 with MetaField

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

the class ConfiguratorServiceImpl method computeMappedByMethod.

/**
 * Find the method used to fill the mapped by many-to-one of a one-to-many relationship. Example:
 * for a one-to-many "purchaseProductMultipleQtyList" with a mapped by many-to-one called
 * "purchaseProduct", this method will return the method "setPurchaseProduct"
 *
 * @param oneToManyFormula a ConfiguratorFormula used to fill a one-to-many
 * @return the found method
 * @throws AxelorException if the mapped by field in meta field is empty.
 */
protected Method computeMappedByMethod(ConfiguratorFormula oneToManyFormula) throws AxelorException, ClassNotFoundException {
    MetaField metaField = oneToManyFormula.getMetaField();
    String mappedBy = metaField.getMappedBy();
    if (mappedBy == null || "".equals(mappedBy)) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.CONFIGURATOR_ONE_TO_MANY_WITHOUT_MAPPED_BY_UNSUPPORTED));
    }
    return Mapper.of(Class.forName(MetaTool.computeFullClassName(metaField))).getSetter(mappedBy);
}
Also used : AxelorException(com.axelor.exception.AxelorException) MetaField(com.axelor.meta.db.MetaField)

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