Search in sources :

Example 1 with JsonContext

use of com.axelor.rpc.JsonContext in project axelor-open-suite by axelor.

the class AdvancedImportServiceImpl method removeRecords.

@SuppressWarnings("unchecked")
public void removeRecords(List<FileTab> fileTabList) throws ClassNotFoundException {
    for (FileTab fileTab : fileTabList) {
        String targetModelName = fileTab.getMetaModel().getFullName();
        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;
        }
        Class<? extends Model> modelKlass = (Class<? extends Model>) Class.forName(targetModelName);
        removeRecord(fileTab, modelKlass, recordList, fileTabList);
        removeSubRecords(modelKlass, jsonContext);
    }
}
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 2 with JsonContext

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

use of com.axelor.rpc.JsonContext in project axelor-open-suite by axelor.

the class FileTabServiceImpl method getShowRecordIds.

@SuppressWarnings("unchecked")
@Override
public String getShowRecordIds(FileTab fileTab, String field) throws ClassNotFoundException {
    Context context = new Context(fileTab.getClass());
    Class<? extends Model> klass = (Class<? extends Model>) Class.forName(fileTab.getClass().getName());
    JsonContext jsonContext = new JsonContext(context, Mapper.of(klass).getProperty("attrs"), fileTab.getAttrs());
    List<Object> recordList = (List<Object>) jsonContext.get(field);
    if (CollectionUtils.isEmpty(recordList)) {
        return null;
    }
    String ids = recordList.stream().map(obj -> {
        Map<String, Object> recordMap = Mapper.toMap(EntityHelper.getEntity(obj));
        return recordMap.get("id").toString();
    }).collect(Collectors.joining(","));
    return ids;
}
Also used : JsonContext(com.axelor.rpc.JsonContext) Context(com.axelor.rpc.Context) MetaModelRepository(com.axelor.meta.db.repo.MetaModelRepository) EntityHelper(com.axelor.db.EntityHelper) Model(com.axelor.db.Model) MetaField(com.axelor.meta.db.MetaField) Inject(com.google.inject.Inject) Mapper(com.axelor.db.mapper.Mapper) Collectors(java.util.stream.Collectors) JsonContext(com.axelor.rpc.JsonContext) FileField(com.axelor.apps.base.db.FileField) Strings(com.google.common.base.Strings) 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) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) FileTab(com.axelor.apps.base.db.FileTab) Context(com.axelor.rpc.Context) JsonContext(com.axelor.rpc.JsonContext) Model(com.axelor.db.Model) MetaModel(com.axelor.meta.db.MetaModel) List(java.util.List) Map(java.util.Map)

Example 4 with JsonContext

use of com.axelor.rpc.JsonContext in project axelor-open-suite by axelor.

the class ConfiguratorController method updateIndicators.

/**
 * Called from configurator form view, set values for the indicators JSON field. call {@link
 * ConfiguratorService#updateIndicators(Configurator, JsonContext, JsonContext)}
 *
 * @param request
 * @param response
 */
public void updateIndicators(ActionRequest request, ActionResponse response) {
    Configurator configurator = request.getContext().asType(Configurator.class);
    JsonContext jsonAttributes = (JsonContext) request.getContext().get("$attributes");
    JsonContext jsonIndicators = (JsonContext) request.getContext().get("$indicators");
    configurator = Beans.get(ConfiguratorRepository.class).find(configurator.getId());
    try {
        Beans.get(ConfiguratorService.class).updateIndicators(configurator, jsonAttributes, jsonIndicators);
        response.setValue("indicators", request.getContext().get("indicators"));
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : JsonContext(com.axelor.rpc.JsonContext) Configurator(com.axelor.apps.sale.db.Configurator) ConfiguratorService(com.axelor.apps.sale.service.configurator.ConfiguratorService)

Example 5 with JsonContext

use of com.axelor.rpc.JsonContext in project axelor-open-suite by axelor.

the class ConfiguratorController method generateProduct.

/**
 * Called from configurator form view, call {@link
 * ConfiguratorService#generateProduct(Configurator, JsonContext, JsonContext)}
 *
 * @param request
 * @param response
 */
public void generateProduct(ActionRequest request, ActionResponse response) {
    Configurator configurator = request.getContext().asType(Configurator.class);
    JsonContext jsonAttributes = (JsonContext) request.getContext().get("$attributes");
    JsonContext jsonIndicators = (JsonContext) request.getContext().get("$indicators");
    configurator = Beans.get(ConfiguratorRepository.class).find(configurator.getId());
    try {
        Beans.get(ConfiguratorService.class).generate(configurator, jsonAttributes, jsonIndicators);
        response.setReload(true);
        if (configurator.getProduct() != null) {
            response.setView(ActionView.define(I18n.get("Product generated")).model(Product.class.getName()).add("form", "product-form").add("grid", "product-grid").param("search-filters", "products-filters").context("_showRecord", configurator.getProduct().getId()).map());
        }
    } catch (Exception e) {
        TraceBackService.trace(e);
        response.setError(e.getMessage());
    }
}
Also used : JsonContext(com.axelor.rpc.JsonContext) Configurator(com.axelor.apps.sale.db.Configurator) Product(com.axelor.apps.base.db.Product) ConfiguratorService(com.axelor.apps.sale.service.configurator.ConfiguratorService)

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