Search in sources :

Example 1 with Property

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

the class DataImportServiceImpl method checkAndWriteData.

private void checkAndWriteData(String dataCell, MetaModel model, FileField fileField, Mapper mapper, List<String> dataList) throws ClassNotFoundException {
    Property parentProp = mapper.getProperty(fileField.getImportField().getName());
    if (Strings.isNullOrEmpty(fileField.getSubImportField())) {
        if (!Strings.isNullOrEmpty(parentProp.getSelection())) {
            this.writeSelectionData(parentProp.getSelection(), dataCell, fileField.getForSelectUse(), dataList);
        } else {
            dataList.add(dataCell);
        }
    } else {
        String[] subFields = fileField.getSubImportField().split("\\.");
        this.checkSubFieldAndWriteData(subFields, 0, parentProp, dataCell, fileField.getForSelectUse(), dataList);
    }
}
Also used : Property(com.axelor.db.mapper.Property)

Example 2 with Property

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

the class DataImportServiceImpl method createCSVBinding.

private List<CSVBind> createCSVBinding(String column, FileField fileField, Mapper mapper, List<CSVBind> allBindings) throws ClassNotFoundException {
    Property prop = mapper.getProperty(fileField.getImportField().getName());
    if (prop == null) {
        return allBindings;
    }
    CSVBind dummyBind = null;
    if (!fileField.getIsMatchWithFile()) {
        dummyBind = this.createCSVBind(null, column, null, null, null, null);
        allBindings.add(0, dummyBind);
    }
    if (Strings.isNullOrEmpty(fileField.getSubImportField())) {
        String expression = this.setExpression(column, fileField, prop);
        String adapter = null;
        String dateFormat = fileField.getDateFormat();
        if (Strings.isNullOrEmpty(expression) && !Strings.isNullOrEmpty(dateFormat)) {
            adapter = this.getAdapter(prop.getJavaType().getSimpleName(), dateFormat.trim());
        }
        CSVBind bind = null;
        if (!fileField.getIsMatchWithFile()) {
            dummyBind.setExpression(expression);
            dummyBind.setAdapter(adapter);
            bind = this.createCSVBind(column, prop.getName(), null, null, null, null);
            this.setImportIf(prop, bind, column);
        } else {
            bind = this.createCSVBind(column, prop.getName(), null, expression, adapter, null);
            this.setImportIf(prop, bind, column);
        }
        allBindings.add(bind);
    } else {
        CSVBind parentBind = null;
        boolean isSameParentExist = false;
        if (parentBindMap.containsKey(prop.getName())) {
            parentBind = parentBindMap.get(prop.getName());
            isSameParentExist = true;
        } else {
            parentBind = this.createCSVBind(null, prop.getName(), null, null, null, true);
            parentBind.setBindings(new ArrayList<>());
            allBindings.add(parentBind);
            parentBindMap.put(prop.getName(), parentBind);
        }
        fullFieldName = prop.getName();
        String[] subFields = fileField.getSubImportField().split("\\.");
        this.createCSVSubBinding(subFields, 0, column, prop, fileField, parentBind, dummyBind, isSameParentExist);
    }
    if (!Strings.isNullOrEmpty(fileField.getNoImportIf())) {
        String importIf = this.convertExpression(fileField.getNoImportIf().trim(), fileField.getTargetType(), column);
        ifList.add(importIf);
    }
    return allBindings;
}
Also used : CSVBind(com.axelor.data.csv.CSVBind) Property(com.axelor.db.mapper.Property)

Example 3 with Property

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

the class PrintServiceImpl method saveMetaFileInModel.

protected void saveMetaFileInModel(Class<? extends Model> modelClass, Model objectModel, MetaFile metaFile, String metaFileFieldName) {
    Mapper mapper = Mapper.of(modelClass);
    Property p = mapper.getProperty(metaFileFieldName);
    if (ObjectUtils.notEmpty(p)) {
        p.set(objectModel, metaFile);
        JPA.save(objectModel);
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) Property(com.axelor.db.mapper.Property)

Example 4 with Property

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

the class ActionServiceImpl method updateBean.

@SuppressWarnings("unchecked")
protected <T extends Model> Object updateBean(Object bean) {
    final Class<T> klass = (Class<T>) JPA.model(this.modelName);
    T updatedBean = Mapper.toBean(klass, this.context);
    for (Property property : JPA.fields(klass)) {
        Object oldValue = property.get(bean);
        Object newValue = property.get(updatedBean);
        if (!ObjectUtils.isEmpty(newValue) && property.valueChanged(updatedBean, oldValue)) {
            property.set(bean, this.validate(property, newValue));
        }
    }
    return bean;
}
Also used : Property(com.axelor.db.mapper.Property)

Example 5 with Property

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

the class AdvancedImportServiceImpl method checkSubFields.

private boolean checkSubFields(String[] subFields, int index, Property parentProp, String model) throws AxelorException, ClassNotFoundException {
    boolean isValid = true;
    if (index < subFields.length) {
        if (parentProp.getTarget() != null) {
            Mapper mapper = getMapper(parentProp.getTarget().getName());
            Property childProp = mapper.getProperty(subFields[index]);
            if (childProp == null) {
                throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, String.format(I18n.get(IExceptionMessage.ADVANCED_IMPORT_2), subFields[index], parentProp.getName(), model));
            }
            if (childProp.getTarget() != null) {
                if (childProp.getType().name().equals("ONE_TO_MANY")) {
                    isValid = false;
                    return isValid;
                }
                if (index != subFields.length - 1) {
                    isValid = this.checkSubFields(subFields, index + 1, childProp, model);
                } else {
                    throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, String.format(I18n.get(IExceptionMessage.ADVANCED_IMPORT_5), subFields[index], model));
                }
            }
        }
    }
    return isValid;
}
Also used : Mapper(com.axelor.db.mapper.Mapper) AxelorException(com.axelor.exception.AxelorException) 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