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);
}
}
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());
}
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;
}
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);
}
}
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());
}
}
Aggregations