Search in sources :

Example 1 with CustomEntityInstance

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);
        }
    }
}
Also used : Date(java.util.Date) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) SQLQuery(org.hibernate.SQLQuery) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) GenericTypeReferences(org.meveo.model.typereferences.GenericTypeReferences) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) Future(java.util.concurrent.Future) TransactionAttributeType(javax.ejb.TransactionAttributeType) SQLStorageConfiguration(org.meveo.model.persistence.sql.SQLStorageConfiguration) Asynchronous(javax.ejb.Asynchronous) Map(java.util.Map) SearchResponse(org.elasticsearch.action.search.SearchResponse) BigInteger(java.math.BigInteger) AsyncResult(javax.ejb.AsyncResult) Instance(javax.enterprise.inject.Instance) TxType(javax.transaction.Transactional.TxType) ValidationException(org.meveo.admin.exception.ValidationException) Transactional(javax.transaction.Transactional) DocumentField(org.elasticsearch.common.document.DocumentField) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) Collection(java.util.Collection) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) Set(java.util.Set) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) Objects(java.util.Objects) ElasticClient(org.meveo.service.index.ElasticClient) List(java.util.List) CustomFieldStorageTypeEnum(org.meveo.model.crm.custom.CustomFieldStorageTypeEnum) Entry(java.util.Map.Entry) SortOrder(org.elasticsearch.search.sort.SortOrder) CustomFieldsCacheContainerProvider(org.meveo.cache.CustomFieldsCacheContainerProvider) Optional(java.util.Optional) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) SequenceWriter(com.fasterxml.jackson.databind.SequenceWriter) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) JacksonUtil(org.meveo.model.persistence.JacksonUtil) CustomFieldTemplateService(org.meveo.service.crm.impl.CustomFieldTemplateService) TransactionAttribute(javax.ejb.TransactionAttribute) ParamBean(org.meveo.commons.utils.ParamBean) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) RuntimeJsonMappingException(com.fasterxml.jackson.databind.RuntimeJsonMappingException) MapUtils(org.apache.commons.collections4.MapUtils) QueryBuilder(org.meveo.commons.utils.QueryBuilder) NativePersistenceService(org.meveo.service.base.NativePersistenceService) CustomTableRecord(org.meveo.model.customEntities.CustomTableRecord) DBStorageType(org.meveo.model.persistence.DBStorageType) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) FileWriter(java.io.FileWriter) ColumnType(com.fasterxml.jackson.dataformat.csv.CsvSchema.ColumnType) IOException(java.io.IOException) CustomFieldInstanceService(org.meveo.service.crm.impl.CustomFieldInstanceService) FileInputStream(java.io.FileInputStream) ParamBeanFactory(org.meveo.commons.utils.ParamBeanFactory) InputStreamReader(java.io.InputStreamReader) File(java.io.File) CustomFieldTypeEnum(org.meveo.model.crm.custom.CustomFieldTypeEnum) Feature(com.fasterxml.jackson.core.JsonGenerator.Feature) DateUtils(org.meveo.model.shared.DateUtils) ElasticSearchClassInfo(org.meveo.service.index.ElasticSearchClassInfo) Collections(java.util.Collections) InputStream(java.io.InputStream) HashMap(java.util.HashMap) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance)

Example 2 with CustomEntityInstance

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);
}
Also used : ArrayList(java.util.ArrayList) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance)

Example 3 with CustomEntityInstance

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;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) ELException(org.meveo.elresolver.ELException) NoResultException(javax.persistence.NoResultException) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) Matcher(java.util.regex.Matcher) Map(java.util.Map) Observes(javax.enterprise.event.Observes) BigDecimal(com.ibm.icu.math.BigDecimal) Stateless(javax.ejb.Stateless) ModuleItem(org.meveo.model.ModuleItem) SqlConfiguration(org.meveo.model.sql.SqlConfiguration) WFTransitionService(org.meveo.service.wf.WFTransitionService) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) MeveoModule(org.meveo.model.module.MeveoModule) Collection(java.util.Collection) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) Workflow(org.meveo.model.wf.Workflow) WFAction(org.meveo.model.wf.WFAction) MeveoModuleHelper(org.meveo.service.admin.impl.MeveoModuleHelper) List(java.util.List) ModuleUninstall(org.meveo.service.admin.impl.ModuleUninstall) CustomFieldsCacheContainerProvider(org.meveo.cache.CustomFieldsCacheContainerProvider) Pattern(java.util.regex.Pattern) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) EntityReferenceWrapper(org.meveo.model.crm.EntityReferenceWrapper) HashMap(java.util.HashMap) ModulePostUninstall(org.meveo.model.ModulePostUninstall) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CustomFieldValues(org.meveo.model.crm.custom.CustomFieldValues) MeveoFileUtils(org.meveo.commons.utils.MeveoFileUtils) BusinessService(org.meveo.service.base.BusinessService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) JacksonUtil(org.meveo.model.persistence.JacksonUtil) CustomFieldTemplateService(org.meveo.service.crm.impl.CustomFieldTemplateService) CollectionUtils(org.apache.commons.collections.CollectionUtils) MeveoValueExpressionWrapper(org.meveo.service.base.MeveoValueExpressionWrapper) WFTransition(org.meveo.model.wf.WFTransition) CEIUtils(org.meveo.model.persistence.CEIUtils) WFActionService(org.meveo.service.wf.WFActionService) WorkflowService(org.meveo.service.wf.WorkflowService) QueryBuilder(org.meveo.commons.utils.QueryBuilder) Files(java.nio.file.Files) DBStorageType(org.meveo.model.persistence.DBStorageType) IOException(java.io.IOException) CustomFieldInstanceService(org.meveo.service.crm.impl.CustomFieldInstanceService) GitHelper(org.meveo.service.git.GitHelper) File(java.io.File) Collections(java.util.Collections) QueryBuilder(org.meveo.commons.utils.QueryBuilder) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance)

Example 4 with CustomEntityInstance

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;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) ELException(org.meveo.elresolver.ELException) NoResultException(javax.persistence.NoResultException) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) Matcher(java.util.regex.Matcher) Map(java.util.Map) Observes(javax.enterprise.event.Observes) BigDecimal(com.ibm.icu.math.BigDecimal) Stateless(javax.ejb.Stateless) ModuleItem(org.meveo.model.ModuleItem) SqlConfiguration(org.meveo.model.sql.SqlConfiguration) WFTransitionService(org.meveo.service.wf.WFTransitionService) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) MeveoModule(org.meveo.model.module.MeveoModule) Collection(java.util.Collection) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) Workflow(org.meveo.model.wf.Workflow) WFAction(org.meveo.model.wf.WFAction) MeveoModuleHelper(org.meveo.service.admin.impl.MeveoModuleHelper) List(java.util.List) ModuleUninstall(org.meveo.service.admin.impl.ModuleUninstall) CustomFieldsCacheContainerProvider(org.meveo.cache.CustomFieldsCacheContainerProvider) Pattern(java.util.regex.Pattern) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) EntityReferenceWrapper(org.meveo.model.crm.EntityReferenceWrapper) HashMap(java.util.HashMap) ModulePostUninstall(org.meveo.model.ModulePostUninstall) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CustomFieldValues(org.meveo.model.crm.custom.CustomFieldValues) MeveoFileUtils(org.meveo.commons.utils.MeveoFileUtils) BusinessService(org.meveo.service.base.BusinessService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) JacksonUtil(org.meveo.model.persistence.JacksonUtil) CustomFieldTemplateService(org.meveo.service.crm.impl.CustomFieldTemplateService) CollectionUtils(org.apache.commons.collections.CollectionUtils) MeveoValueExpressionWrapper(org.meveo.service.base.MeveoValueExpressionWrapper) WFTransition(org.meveo.model.wf.WFTransition) CEIUtils(org.meveo.model.persistence.CEIUtils) WFActionService(org.meveo.service.wf.WFActionService) WorkflowService(org.meveo.service.wf.WorkflowService) QueryBuilder(org.meveo.commons.utils.QueryBuilder) Files(java.nio.file.Files) DBStorageType(org.meveo.model.persistence.DBStorageType) IOException(java.io.IOException) CustomFieldInstanceService(org.meveo.service.crm.impl.CustomFieldInstanceService) GitHelper(org.meveo.service.git.GitHelper) File(java.io.File) Collections(java.util.Collections) HashMap(java.util.HashMap) EntityReferenceWrapper(org.meveo.model.crm.EntityReferenceWrapper) QueryBuilder(org.meveo.commons.utils.QueryBuilder) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance)

Example 5 with CustomEntityInstance

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;
}
Also used : EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration)

Aggregations

CustomEntityInstance (org.meveo.model.customEntities.CustomEntityInstance)78 ArrayList (java.util.ArrayList)30 EntityDoesNotExistsException (org.meveo.api.exception.EntityDoesNotExistsException)30 CustomFieldTemplate (org.meveo.model.crm.CustomFieldTemplate)30 BusinessException (org.meveo.admin.exception.BusinessException)28 CustomEntityTemplate (org.meveo.model.customEntities.CustomEntityTemplate)28 Map (java.util.Map)26 HashMap (java.util.HashMap)24 IOException (java.io.IOException)21 List (java.util.List)17 CustomModelObject (org.meveo.model.customEntities.CustomModelObject)17 ELException (org.meveo.elresolver.ELException)16 EntityReferenceWrapper (org.meveo.model.crm.EntityReferenceWrapper)16 PaginationConfiguration (org.meveo.admin.util.pagination.PaginationConfiguration)15 Collection (java.util.Collection)14 Set (java.util.Set)13 Collectors (java.util.stream.Collectors)13 File (java.io.File)12 Inject (javax.inject.Inject)12 NonUniqueResultException (javax.persistence.NonUniqueResultException)12