Search in sources :

Example 6 with Property

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

the class AdvancedImportServiceImpl method resetPropertyValue.

@Transactional
private void resetPropertyValue(Class<? extends Model> klass, List<Object> recordList) throws ClassNotFoundException {
    JpaRepository<? extends Model> modelRepo = JpaRepository.of(klass);
    for (Property prop : Mapper.of(klass).getProperties()) {
        if (prop.getTarget() == null || prop.isRequired()) {
            continue;
        }
        for (Object obj : recordList) {
            Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
            Long id = Long.valueOf(recordMap.get("id").toString());
            Object bean = modelRepo.find(id);
            if (bean != null) {
                prop.set(bean, null);
            }
        }
    }
}
Also used : Property(com.axelor.db.mapper.Property) Transactional(com.google.inject.persist.Transactional)

Example 7 with Property

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

the class AdvancedImportServiceImpl method removeRecord.

@SuppressWarnings("unchecked")
@Transactional
public void removeRecord(FileTab fileTab, Class<? extends Model> modelKlass, List<Object> recordList, List<FileTab> fileTabList) throws ClassNotFoundException {
    JpaRepository<? extends Model> modelRepo = JpaRepository.of(modelKlass);
    for (FileTab tab : fileTabList) {
        Map<String, Object> jsonContextMap = dataImportService.createJsonContext(tab);
        JsonContext jsonContext = (JsonContext) jsonContextMap.get("jsonContext");
        String fieldName = inflector.camelize(tab.getMetaModel().getName(), true) + "Set";
        List<Object> recList = (List<Object>) jsonContext.get(fieldName);
        if (CollectionUtils.isEmpty(recList)) {
            continue;
        }
        Class<? extends Model> klass = (Class<? extends Model>) Class.forName(tab.getMetaModel().getFullName());
        Property[] props = Mapper.of(klass).getProperties();
        for (Property prop : props) {
            if (prop.getTarget() != null && prop.getTarget() == modelKlass && prop.isRequired()) {
                removeRecord(tab, klass, recList, fileTabList);
            }
        }
    }
    String ids = recordList.stream().map(obj -> {
        Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
        return recordMap.get("id").toString();
    }).collect(Collectors.joining(","));
    modelRepo.all().filter("self.id IN (" + ids + ")").delete();
    fileTab.setAttrs(null);
    LOG.debug("Reset imported data : {}", modelKlass.getSimpleName());
}
Also used : MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) Arrays(java.util.Arrays) EntityHelper(com.axelor.db.EntityHelper) JpaRepository(com.axelor.db.JpaRepository) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Property(com.axelor.db.mapper.Property) HashMap(java.util.HashMap) Mapper(com.axelor.db.mapper.Mapper) StringUtils(org.apache.commons.lang3.StringUtils) JsonContext(com.axelor.rpc.JsonContext) Transactional(com.google.inject.persist.Transactional) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FileField(com.axelor.apps.base.db.FileField) Strings(com.google.common.base.Strings) AxelorException(com.axelor.exception.AxelorException) AdvancedImportRepository(com.axelor.apps.base.db.repo.AdvancedImportRepository) Files(com.google.common.io.Files) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) I18n(com.axelor.i18n.I18n) Inflector(com.axelor.common.Inflector) DataReaderFactory(com.axelor.apps.tool.reader.DataReaderFactory) Logger(org.slf4j.Logger) Model(com.axelor.db.Model) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) MetaField(com.axelor.meta.db.MetaField) MethodHandles(java.lang.invoke.MethodHandles) Set(java.util.Set) Collectors(java.util.stream.Collectors) DataReaderService(com.axelor.apps.tool.reader.DataReaderService) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) Beans(com.axelor.inject.Beans) FileFieldRepository(com.axelor.apps.base.db.repo.FileFieldRepository) MetaFieldRepository(com.axelor.meta.db.repo.MetaFieldRepository) IExceptionMessage(com.axelor.apps.base.exceptions.IExceptionMessage) FileTab(com.axelor.apps.base.db.FileTab) AdvancedImport(com.axelor.apps.base.db.AdvancedImport) JsonContext(com.axelor.rpc.JsonContext) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) ArrayList(java.util.ArrayList) List(java.util.List) FileTab(com.axelor.apps.base.db.FileTab) Property(com.axelor.db.mapper.Property) HashMap(java.util.HashMap) Map(java.util.Map) Transactional(com.google.inject.persist.Transactional)

Example 8 with Property

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

the class ValidatorService method validateImportRequiredField.

private void validateImportRequiredField(int line, Class<?> model, String fieldName, FileField fileField, List<String> relationalFieldList) throws IOException, ClassNotFoundException {
    Mapper mapper = advancedImportService.getMapper(model.getName());
    String field = getField(fileField);
    Integer rowNum = fileField.getIsMatchWithFile() ? line : null;
    int importType = fileField.getImportType();
    for (Property prop : mapper.getProperties()) {
        if (prop.isRequired()) {
            if (prop.getName().equals(fieldName) && importType == FileFieldRepository.IMPORT_TYPE_IGNORE_EMPTY) {
                logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_5, field, rowNum);
            } else if ((importType == FileFieldRepository.IMPORT_TYPE_FIND_NEW || importType == FileFieldRepository.IMPORT_TYPE_NEW) && field.contains(".") && !fileField.getTargetType().equals("MetaFile")) {
                String newField = StringUtils.substringBeforeLast(field, ".");
                newField = newField + "." + prop.getName();
                if (!relationalFieldList.contains(newField)) {
                    logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_3, newField, null);
                }
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) Mapper(com.axelor.db.mapper.Mapper) Property(com.axelor.db.mapper.Property)

Example 9 with Property

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

the class ValidatorService method validateData.

private void validateData(String[] dataRow, int line, boolean isConfig, FileTab fileTab) throws IOException, ClassNotFoundException {
    Map<String, Object> map = isConfig ? fieldMap : titleMap;
    for (int fieldIndex = 0; fieldIndex < fileTab.getFileFieldList().size(); fieldIndex++) {
        FileField fileField = fileTab.getFileFieldList().get(fieldIndex);
        if (!fileField.getIsMatchWithFile() || !Strings.isNullOrEmpty(fileField.getExpression())) {
            continue;
        }
        Mapper mapper = null;
        Property property = null;
        String key = null;
        if (isConfig) {
            key = this.getField(fileField);
        } else {
            key = fileField.getColumnTitle();
        }
        int cellIndex = 0;
        if (map.containsKey(key)) {
            cellIndex = (int) map.get(key);
        }
        cellIndex = (!isConfig && !fileTab.getAdvancedImport().getIsHeader()) ? fieldIndex : cellIndex;
        if (fileField.getImportField() != null) {
            mapper = advancedImportService.getMapper(fileField.getImportField().getMetaModel().getFullName());
            property = mapper.getProperty(fileField.getImportField().getName());
        }
        if (property != null && Strings.isNullOrEmpty(fileField.getSubImportField())) {
            if (this.validateDataRequiredField(dataRow, cellIndex, line, Class.forName(fileTab.getMetaModel().getFullName()), property.getName(), fileField)) {
                continue;
            }
            if (!Strings.isNullOrEmpty(property.getSelection()) && fileField.getForSelectUse() != FileFieldRepository.SELECT_USE_VALUES) {
                continue;
            }
            this.validateDataType(dataRow, cellIndex, line, property.getJavaType().getSimpleName(), fileField);
        } else if (!Strings.isNullOrEmpty(fileField.getSubImportField())) {
            Property subProperty = this.getAndValidateSubField(line, property, fileField, true);
            if (subProperty != null) {
                if (this.validateDataRequiredField(dataRow, cellIndex, line, subProperty.getEntity(), subProperty.getName(), fileField)) {
                    continue;
                }
                if (!Strings.isNullOrEmpty(subProperty.getSelection()) && fileField.getForSelectUse() != FileFieldRepository.SELECT_USE_VALUES) {
                    continue;
                }
                this.validateDataType(dataRow, cellIndex, line, subProperty.getJavaType().getSimpleName(), fileField);
            }
        }
    }
}
Also used : Mapper(com.axelor.db.mapper.Mapper) FileField(com.axelor.apps.base.db.FileField) Property(com.axelor.db.mapper.Property)

Example 10 with Property

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

the class DuplicateObjectsService method concatFields.

/*
   * get all records for duplicate records
   */
private String concatFields(Class<?> modelClass, Set<String> fieldSet) throws AxelorException {
    StringBuilder fields = new StringBuilder("LOWER(concat(");
    Mapper mapper = Mapper.of(modelClass);
    int count = 0;
    for (String field : fieldSet) {
        Property property = mapper.getProperty(field);
        if (property == null) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.GENERAL_8), field, modelClass.getSimpleName());
        }
        if (property.isCollection()) {
            throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.GENERAL_9), field);
        }
        if (count != 0) {
            fields.append(",");
        }
        count++;
        fields.append("cast(self" + "." + field);
        if (property.getTarget() != null) {
            fields.append(".id");
        }
        fields.append(" as string)");
    }
    fields.append("))");
    return fields.toString();
}
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