Search in sources :

Example 1 with JpaRepository

use of com.axelor.db.JpaRepository 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 2 with JpaRepository

use of com.axelor.db.JpaRepository 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)

Aggregations

AdvancedImport (com.axelor.apps.base.db.AdvancedImport)2 FileField (com.axelor.apps.base.db.FileField)2 FileTab (com.axelor.apps.base.db.FileTab)2 AdvancedImportRepository (com.axelor.apps.base.db.repo.AdvancedImportRepository)2 FileFieldRepository (com.axelor.apps.base.db.repo.FileFieldRepository)2 IExceptionMessage (com.axelor.apps.base.exceptions.IExceptionMessage)2 DataReaderFactory (com.axelor.apps.tool.reader.DataReaderFactory)2 DataReaderService (com.axelor.apps.tool.reader.DataReaderService)2 Inflector (com.axelor.common.Inflector)2 EntityHelper (com.axelor.db.EntityHelper)2 JpaRepository (com.axelor.db.JpaRepository)2 Model (com.axelor.db.Model)2 Mapper (com.axelor.db.mapper.Mapper)2 Property (com.axelor.db.mapper.Property)2 AxelorException (com.axelor.exception.AxelorException)2 TraceBackRepository (com.axelor.exception.db.repo.TraceBackRepository)2 I18n (com.axelor.i18n.I18n)2 Beans (com.axelor.inject.Beans)2 MetaField (com.axelor.meta.db.MetaField)2 MetaModel (com.axelor.meta.db.MetaModel)2