Search in sources :

Example 11 with JsonContext

use of com.axelor.rpc.JsonContext 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 12 with JsonContext

use of com.axelor.rpc.JsonContext 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 13 with JsonContext

use of com.axelor.rpc.JsonContext 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

JsonContext (com.axelor.rpc.JsonContext)13 Model (com.axelor.db.Model)8 HashMap (java.util.HashMap)7 MetaModel (com.axelor.meta.db.MetaModel)6 Context (com.axelor.rpc.Context)6 List (java.util.List)6 FileTab (com.axelor.apps.base.db.FileTab)5 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 FileField (com.axelor.apps.base.db.FileField)3 FileFieldRepository (com.axelor.apps.base.db.repo.FileFieldRepository)3 Configurator (com.axelor.apps.sale.db.Configurator)3 ConfiguratorService (com.axelor.apps.sale.service.configurator.ConfiguratorService)3 EntityHelper (com.axelor.db.EntityHelper)3 Mapper (com.axelor.db.mapper.Mapper)3 Beans (com.axelor.inject.Beans)3 MetaField (com.axelor.meta.db.MetaField)3 MetaFieldRepository (com.axelor.meta.db.repo.MetaFieldRepository)3 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)3 Strings (com.google.common.base.Strings)3