Search in sources :

Example 21 with Property

use of com.axelor.db.mapper.Property in project axelor-open-suite by axelor.

the class ConfiguratorCreatorServiceImpl method setContextToJsonField.

/**
 * Set the context field to a json field. Allows to limit the json field to the configurator
 * having the right configurator creator.
 *
 * @param creator
 * @param field
 */
protected void setContextToJsonField(ConfiguratorCreator creator, MetaJsonField field) {
    final String fieldName = "configuratorCreator";
    final Class<?> modelClass;
    final String modelName = field.getModel();
    try {
        modelClass = Class.forName(modelName);
    } catch (ClassNotFoundException e) {
        // this should not happen
        TraceBackService.trace(e);
        return;
    }
    final Mapper mapper = Mapper.of(modelClass);
    final Property property = mapper.getProperty(fieldName);
    final String target = property == null ? null : property.getTarget().getName();
    final String targetName = property == null ? null : property.getTargetName();
    field.setContextField(fieldName);
    field.setContextFieldTarget(target);
    field.setContextFieldTargetName(targetName);
    field.setContextFieldValue(creator.getId().toString());
    field.setContextFieldTitle(creator.getName());
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Property(com.axelor.db.mapper.Property)

Example 22 with Property

use of com.axelor.db.mapper.Property 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 23 with Property

use of com.axelor.db.mapper.Property in project axelor-open-suite by axelor.

the class ImportAdvancedImport method importGeneral.

@SuppressWarnings("unchecked")
public Object importGeneral(Object bean, Map<String, Object> values) throws ClassNotFoundException {
    if (bean == null) {
        return bean;
    }
    FileTab fileTab = fileTabRepo.find(Long.valueOf(values.get("fileTabId").toString()));
    ScriptHelper scriptHelper = new GroovyScriptHelper(new ScriptBindings(values));
    List<String> exprs = (List<String>) values.get("ifConditions" + fileTab.getId());
    if (!CollectionUtils.isEmpty(exprs)) {
        if ((boolean) scriptHelper.eval(String.join(" || ", exprs))) {
            return null;
        }
    }
    if (((Model) bean).getId() == null) {
        List<Property> propList = this.getProperties(bean);
        JPA.save((Model) bean);
        this.addJsonObjectRecord(bean, fileTab, fileTab.getMetaModel().getName(), values);
        int fieldSeq = 2;
        int btnSeq = 3;
        for (Property prop : propList) {
            validatorService.createCustomObjectSet(fileTab.getClass().getName(), prop.getTarget().getName(), fieldSeq);
            validatorService.createCustomButton(fileTab.getClass().getName(), prop.getTarget().getName(), btnSeq);
            this.addJsonObjectRecord(prop.get(bean), fileTab, StringUtils.substringAfterLast(prop.getTarget().getName(), "."), values);
            fieldSeq++;
            btnSeq++;
        }
    }
    final String ACTIONS_TO_APPLY = "actionsToApply" + fileTab.getId();
    if (!ObjectUtils.isEmpty(values.get(ACTIONS_TO_APPLY))) {
        bean = actionService.apply(values.get(ACTIONS_TO_APPLY).toString(), bean);
    }
    return bean;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) FileTab(com.axelor.apps.base.db.FileTab) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) ScriptHelper(com.axelor.script.ScriptHelper) GroovyScriptHelper(com.axelor.script.GroovyScriptHelper) ScriptBindings(com.axelor.script.ScriptBindings) Property(com.axelor.db.mapper.Property)

Example 24 with Property

use of com.axelor.db.mapper.Property in project axelor-open-suite by axelor.

the class WkfCommonServiceImpl method addProperties.

@Override
public Model addProperties(Map<String, String> propertyMap, Model model, ModelElementInstance element) {
    Mapper mapper = Mapper.of(EntityHelper.getEntityClass(model));
    for (String property : propertyMap.keySet()) {
        Object value = element.getAttributeValueNs(BpmnParser.CAMUNDA_BPMN_EXTENSIONS_NS, propertyMap.get(property));
        if (value != null && value.equals("undefined")) {
            value = null;
        }
        Property field = mapper.getProperty(property);
        if (field.isReference()) {
            try {
                value = JpaRepository.of((Class<? extends Model>) field.getTarget()).all().filter("self.name = ?1", value).fetchOne();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        mapper.set(model, property, value);
    }
    return model;
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Property(com.axelor.db.mapper.Property) AxelorException(com.axelor.exception.AxelorException)

Example 25 with Property

use of com.axelor.db.mapper.Property in project axelor-open-suite by axelor.

the class DmnServiceImpl method processMetaField.

private Object processMetaField(String fieldName, String subField, Object value, Class<Model> entityClass) throws AxelorException {
    Property property = Mapper.of(entityClass).getProperty(fieldName);
    if (property == null) {
        return processMetaModelJson(fieldName, subField, value, entityClass);
    }
    String targetModel = property.getTarget().getName();
    boolean isCollection = property.isCollection();
    boolean isSet = property.getJavaType().isAssignableFrom(Set.class);
    return findResult(value, subField, targetModel, isCollection, isSet);
}
Also used : Property(com.axelor.db.mapper.Property)

Aggregations

Property (com.axelor.db.mapper.Property)40 Mapper (com.axelor.db.mapper.Mapper)21 Model (com.axelor.db.Model)12 ArrayList (java.util.ArrayList)11 AxelorException (com.axelor.exception.AxelorException)10 MetaModel (com.axelor.meta.db.MetaModel)8 List (java.util.List)8 Transactional (com.google.inject.persist.Transactional)7 MetaField (com.axelor.meta.db.MetaField)6 FileField (com.axelor.apps.base.db.FileField)5 FileTab (com.axelor.apps.base.db.FileTab)4 Inflector (com.axelor.common.Inflector)4 TraceBackRepository (com.axelor.exception.db.repo.TraceBackRepository)4 I18n (com.axelor.i18n.I18n)4 Inject (com.google.inject.Inject)4 Arrays (java.util.Arrays)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 AdvancedImport (com.axelor.apps.base.db.AdvancedImport)3 FileFieldRepository (com.axelor.apps.base.db.repo.FileFieldRepository)3