use of org.meveo.model.customEntities.CustomEntityInstance in project meveo by meveo-org.
the class CustomTableService method replaceEntityreferences.
private void replaceEntityreferences(String sqlConnectionCode, List<CustomFieldTemplate> fields, Map<String, Map<String, String>> entityReferencesCache, Map<String, Object> entityRefValueMap) throws BusinessException {
final HashMap<String, Object> iterationMap = new HashMap<>(entityRefValueMap);
for (Entry<String, Object> entry : iterationMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
final Optional<CustomFieldTemplate> templateOptional = fields.stream().filter(f -> f.getDbFieldname().equals(key)).findFirst();
if (templateOptional.isPresent() && templateOptional.get().getFieldType() == CustomFieldTypeEnum.ENTITY) {
CustomEntityTemplate entityRef = customEntityTemplateService.findByCode(templateOptional.get().getEntityClazzCetCode());
// Try to retrieve record first
String uuid = entityReferencesCache.computeIfAbsent(key, k -> new HashMap<>()).computeIfAbsent((String) value, serializedValues -> {
Map<String, Object> entityRefValues = JacksonUtil.fromString(serializedValues, GenericTypeReferences.MAP_STRING_OBJECT);
return findIdByUniqueValues(sqlConnectionCode, entityRef, entityRefValues, fields);
});
// If record is not found, create it
if (uuid == null) {
Map<String, Object> entityRefValues = JacksonUtil.fromString((String) value, GenericTypeReferences.MAP_STRING_OBJECT);
log.info("Creating missing entity reference {}", entityRefValues);
CustomEntityTemplate cet = customFieldsCacheContainerProvider.getCustomEntityTemplate(templateOptional.get().getEntityClazzCetCode());
CustomEntityInstance cei = new CustomEntityInstance();
cei.setCetCode(cet.getCode());
customFieldInstanceService.setCfValues(cei, cei.getCetCode(), entityRefValues);
uuid = create(sqlConnectionCode, cet, cei);
}
entityRefValueMap.put(key, uuid);
}
}
}
use of org.meveo.model.customEntities.CustomEntityInstance in project meveo by meveo-org.
the class CustomTableService method saveBatch.
private void saveBatch(String sqlConnectionCode, Map<String, CustomFieldTemplate> cfts, List<CustomFieldTemplate> fields, String cetCode, List<Map<String, Object>> values, Map<String, Map<String, String>> entityReferencesCache) throws BusinessException {
List<CustomEntityInstance> ceis = new ArrayList<>();
for (Map<String, Object> value : values) {
CustomEntityInstance cei = new CustomEntityInstance();
cei.setCetCode(cetCode);
customFieldInstanceService.setCfValues(cei, cei.getCetCode(), value);
ceis.add(cei);
}
saveBatch(sqlConnectionCode, cfts, fields, ceis, entityReferencesCache);
}
use of org.meveo.model.customEntities.CustomEntityInstance in project meveo by meveo-org.
the class CustomEntityInstanceService method list.
public List<CustomEntityInstance> list(String cetCode, boolean isStoreAsTable, Map<String, Object> values, PaginationConfiguration paginationConfiguration) {
QueryBuilder qb = new QueryBuilder(getEntityClass(), "cei", null);
qb.addCriterion("cei.cetCode", "=", cetCode, true);
qb.addPaginationConfiguration(paginationConfiguration);
final List<CustomEntityInstance> resultList = qb.getTypedQuery(getEntityManager(), CustomEntityInstance.class).getResultList();
if (values != null && !values.isEmpty()) {
return resultList.stream().filter(customEntityInstance -> filterOnValues(values, customEntityInstance, isStoreAsTable)).collect(Collectors.toList());
}
return resultList;
}
use of org.meveo.model.customEntities.CustomEntityInstance in project meveo by meveo-org.
the class CustomEntityInstanceService method list.
public List<CustomEntityInstance> list(String cetCode, Map<String, Object> values) {
QueryBuilder qb = new QueryBuilder(getEntityClass(), "cei", null);
qb.addCriterion("cei.cetCode", "=", cetCode, true);
final List<CustomEntityInstance> resultList = qb.getTypedQuery(getEntityManager(), CustomEntityInstance.class).getResultList();
var ownValues = new HashMap<>(values);
for (var entry : values.entrySet()) {
if (entry.getValue() instanceof EntityReferenceWrapper) {
ownValues.remove(entry.getKey());
}
}
if (ownValues != null && !ownValues.isEmpty()) {
return resultList.stream().filter(customEntityInstance -> filterOnValues(ownValues, customEntityInstance)).collect(Collectors.toList());
}
return resultList;
}
use of org.meveo.model.customEntities.CustomEntityInstance in project meveo by meveo-org.
the class GenericEntityPickerBean method getCeiListFromCet.
private List<CustomEntityInstance> getCeiListFromCet(CustomEntityTemplate customEntityTemplate) throws BusinessException {
List<CustomEntityInstance> ceiList = new ArrayList<>();
List<Map<String, Object>> values;
try {
values = crossStorageService.find(// XXX: Maybe we will need to parameterize this or search in all repositories ?
repositoryService.findDefaultRepository(), customEntityTemplate, new PaginationConfiguration());
} catch (EntityDoesNotExistsException e) {
throw new RuntimeException(e);
}
if (CollectionUtils.isNotEmpty(values)) {
for (Map<String, Object> customEntity : values) {
CustomEntityInstance customEntityInstance = new CustomEntityInstance();
customEntityInstance.setUuid((String) customEntity.get("uuid"));
customEntityInstance.setCode((String) customEntity.get("uuid"));
String fieldName = customFieldTemplateService.getFieldName(customEntityTemplate);
if (fieldName != null) {
customEntityInstance.setDescription(fieldName + ": " + customEntity.get(fieldName));
}
customEntityInstance.setCet(customEntityTemplate);
customEntityInstance.setCetCode(customEntityTemplate.getCode());
customFieldInstanceService.setCfValues(customEntityInstance, customEntityTemplate.getCode(), customEntity);
ceiList.add(customEntityInstance);
}
}
return ceiList;
}
Aggregations