Search in sources :

Example 66 with Model

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

the class AdvancedImportServiceImpl method removeSubRecords.

@SuppressWarnings("unchecked")
@Transactional
public void removeSubRecords(Class<? extends Model> klass, JsonContext jsonContext) throws ClassNotFoundException {
    for (Property prop : Mapper.of(klass).getProperties()) {
        if (prop.getTarget() == null || prop.isCollection()) {
            continue;
        }
        String simpleModelName = StringUtils.substringAfterLast(prop.getTarget().getName(), ".");
        String field = inflector.camelize(simpleModelName, true) + "Set";
        if (!jsonContext.containsKey(field)) {
            continue;
        }
        List<Object> recList = (List<Object>) jsonContext.get(field);
        String ids = recList.stream().map(obj -> {
            Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
            return recordMap.get("id").toString();
        }).collect(Collectors.joining(","));
        JpaRepository<? extends Model> modelRepo = JpaRepository.of((Class<? extends Model>) Class.forName(prop.getTarget().getName()));
        modelRepo.all().filter("self.id IN (" + ids + ")").delete();
    }
}
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) ArrayList(java.util.ArrayList) List(java.util.List) Property(com.axelor.db.mapper.Property) HashMap(java.util.HashMap) Map(java.util.Map) Transactional(com.google.inject.persist.Transactional)

Example 67 with Model

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

the class AdvancedImportServiceImpl method resetSubPropertyValue.

@SuppressWarnings("unchecked")
private void resetSubPropertyValue(Class<? extends Model> klass, JsonContext jsonContext) throws ClassNotFoundException {
    for (Property prop : Mapper.of(klass).getProperties()) {
        if (prop.getTarget() == null || prop.isRequired()) {
            continue;
        }
        String simpleModelName = StringUtils.substringAfterLast(prop.getTarget().getName(), ".");
        String field = inflector.camelize(simpleModelName, true) + "Set";
        if (!jsonContext.containsKey(field)) {
            continue;
        }
        List<Object> recordList = (List<Object>) jsonContext.get(field);
        Class<? extends Model> modelKlass = (Class<? extends Model>) Class.forName(prop.getTarget().getName());
        this.resetPropertyValue(modelKlass, recordList);
    }
}
Also used : Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) ArrayList(java.util.ArrayList) List(java.util.List) Property(com.axelor.db.mapper.Property)

Example 68 with Model

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

the class AdvancedImportServiceImpl method resetRelationalFields.

@SuppressWarnings("unchecked")
public boolean resetRelationalFields(List<FileTab> fileTabList) throws ClassNotFoundException {
    boolean isResetValue = false;
    for (FileTab fileTab : fileTabList) {
        Map<String, Object> jsonContextMap = dataImportService.createJsonContext(fileTab);
        JsonContext jsonContext = (JsonContext) jsonContextMap.get("jsonContext");
        String fieldName = inflector.camelize(fileTab.getMetaModel().getName(), true) + "Set";
        List<Object> recordList = (List<Object>) jsonContext.get(fieldName);
        if (CollectionUtils.isEmpty(recordList)) {
            continue;
        }
        isResetValue = true;
        Class<? extends Model> modelKlass = (Class<? extends Model>) Class.forName(fileTab.getMetaModel().getFullName());
        this.resetPropertyValue(modelKlass, recordList);
        this.resetSubPropertyValue(modelKlass, jsonContext);
    }
    return isResetValue;
}
Also used : 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)

Example 69 with Model

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

the class ValidatorService method validateObjectRequiredFields.

private void validateObjectRequiredFields(FileTab fileTab) throws ClassNotFoundException, IOException, AxelorException {
    if (CollectionUtils.isEmpty(fileTab.getFileFieldList())) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, String.format(I18n.get(IExceptionMessage.ADVANCED_IMPORT_NO_FIELDS), fileTab.getName()));
    }
    List<String> fieldList = new ArrayList<String>();
    for (FileField fileField : fileTab.getFileFieldList()) {
        if (fileField.getImportField() != null) {
            fieldList.add(fileField.getImportField().getName());
        } else {
            logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_2, fileField.getColumnTitle(), null);
        }
    }
    if (fileTab.getImportType() == FileFieldRepository.IMPORT_TYPE_FIND) {
        return;
    }
    if (fileTab.getMetaModel() != null) {
        Mapper mapper = advancedImportService.getMapper(fileTab.getMetaModel().getFullName());
        Model obj = null;
        try {
            obj = (Model) Class.forName(fileTab.getMetaModel().getFullName()).newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            TraceBackService.trace(e);
        }
        for (Property prop : mapper.getProperties()) {
            if (prop.isRequired() && !fieldList.contains(prop.getName())) {
                if (obj != null && mapper.get(obj, prop.getName()) != null) {
                    continue;
                }
                logService.addLog(IExceptionMessage.ADVANCED_IMPORT_LOG_3, prop.getName(), null);
            }
        }
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) Mapper(com.axelor.db.mapper.Mapper) ArrayList(java.util.ArrayList) Model(com.axelor.db.Model) FileField(com.axelor.apps.base.db.FileField) Property(com.axelor.db.mapper.Property)

Example 70 with Model

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

the class DataImportServiceImpl method createJsonContext.

@Override
public Map<String, Object> createJsonContext(FileTab fileTab) {
    Class<? extends Model> klass = (Class<? extends Model>) fileTab.getClass();
    Context context = new Context(klass);
    JsonContext jsonContext = new JsonContext(context, Mapper.of(klass).getProperty("attrs"), fileTab.getAttrs());
    Map<String, Object> _map = new HashMap<String, Object>();
    _map.put("context", context);
    _map.put("jsonContext", jsonContext);
    return _map;
}
Also used : JsonContext(com.axelor.rpc.JsonContext) Context(com.axelor.rpc.Context) JsonContext(com.axelor.rpc.JsonContext) HashMap(java.util.HashMap) MetaModel(com.axelor.meta.db.MetaModel) Model(com.axelor.db.Model)

Aggregations

Model (com.axelor.db.Model)77 MetaModel (com.axelor.meta.db.MetaModel)22 AxelorException (com.axelor.exception.AxelorException)19 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)16 Context (com.axelor.rpc.Context)15 Transactional (com.google.inject.persist.Transactional)15 List (java.util.List)14 Mapper (com.axelor.db.mapper.Mapper)13 IOException (java.io.IOException)13 File (java.io.File)12 Map (java.util.Map)12 Property (com.axelor.db.mapper.Property)11 MetaJsonRecord (com.axelor.meta.db.MetaJsonRecord)9 JsonContext (com.axelor.rpc.JsonContext)8 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)7 Strings (com.google.common.base.Strings)7 HashSet (java.util.HashSet)7 FullContext (com.axelor.apps.tool.context.FullContext)6 Beans (com.axelor.inject.Beans)6