Search in sources :

Example 1 with Provider

use of org.meveo.model.crm.Provider in project meveo by meveo-org.

the class MeveoValueExpressionWrapper method getAdditionalSources.

@Override
protected Map<String, Object> getAdditionalSources(String expression, Map<Object, Object> userMap) {
    final Map<String, Object> additionalSources = super.getAdditionalSources(expression, userMap);
    if (userMap != null && expression.contains("appProvider")) {
        Provider appProvider = ((ProviderService) EjbUtils.getServiceInterface("ProviderService")).getProvider();
        additionalSources.put("appProvider", appProvider);
    }
    return additionalSources;
}
Also used : ProviderService(org.meveo.service.crm.impl.ProviderService) Provider(org.meveo.model.crm.Provider)

Example 2 with Provider

use of org.meveo.model.crm.Provider in project meveo by meveo-org.

the class CustomFieldInstanceService method setCFValue.

/**
 * Set a Custom field value on an entity.
 *
 * @param entity Entity
 * @param cfCode Custom field value code
 * @param value  Value to set
 * @return custom field value
 * @throws BusinessException business exception.
 */
@SuppressWarnings("unchecked")
public CustomFieldValue setCFValue(ICustomFieldEntity entity, String cfCode, Object value) throws BusinessException {
    log.trace("Setting CF value. Code: {}, entity {} value {}", cfCode, entity, value);
    String repository = "default";
    CustomEntityTemplate cet = null;
    if (entity instanceof CustomEntityInstance) {
        cet = ((CustomEntityInstance) entity).getCet();
        repository = ((CustomEntityInstance) entity).getRepository() != null ? ((CustomEntityInstance) entity).getRepository().getCode() : "default";
    }
    // Can not set the value if field is versionable without a date
    CustomFieldTemplate cft = cfTemplateService.findByCodeAndAppliesTo(cfCode, entity);
    if (cft == null) {
        throw new BusinessException("Custom field template with code " + cfCode + " not found found for entity " + entity);
    }
    // Handle serialized map values
    if ((cft.getStorageType() == CustomFieldStorageTypeEnum.MAP || cft.getStorageType() == CustomFieldStorageTypeEnum.MATRIX) && value instanceof String) {
        value = JacksonUtil.fromString((String) value, Map.class);
    }
    if (cft.isVersionable()) {
        throw new RuntimeException("Can not determine a period for Custom Field " + entity.getClass().getSimpleName() + "/" + cfCode + " value if no date or date range is provided");
    }
    // Handle cases when appProvider was passed instead of a real Provider entity. The class in this case is org.meveo.model.crm.Provider$Proxy$_$$_WeldClientProxy
    if (entity instanceof Provider && entity.getClass().getSimpleName().contains("Proxy")) {
        entity = providerService.findById(appProvider.getId());
    }
    CustomFieldValue cfValue = null;
    if (entity.getCfValues() != null) {
        cfValue = entity.getCfValues().getCfValue(cfCode);
    }
    log.trace("Setting CF value1. Code: {}, cfValue {}", cfCode, cfValue);
    // No existing CF value. Create CF value with new value. Assign(persist) NULL value only if cft.defaultValue is present
    if (cfValue == null) {
        if (cft.getFieldType() == CustomFieldTypeEnum.ENTITY) {
            /* Don't wrap primitive entity references */
            if (cft.getStoragesNullSafe().contains(DBStorageType.NEO4J)) {
                String cetCode = cft.getEntityClazzCetCode();
                CustomEntityTemplate refCet = customEntityTemplateService.findByCode(cetCode);
                if (refCet == null) {
                    throw new org.meveo.exceptions.EntityDoesNotExistsException(CustomEntityTemplate.class, cetCode);
                }
                if (refCet.getNeo4JStorageConfiguration() != null && refCet.getNeo4JStorageConfiguration().isPrimitiveEntity()) {
                    return entity.getCfValuesNullSafe().setValue(cfCode, value);
                }
            }
            if (customFieldTemplateService.isReferenceJpaEntity(cft.getEntityClazzCetCode())) {
                if (cft.getStorageType() == CustomFieldStorageTypeEnum.LIST) {
                    try {
                        var collectionValue = JacksonUtil.convert(value, new TypeReference<List<EntityReferenceWrapper>>() {
                        });
                        cfValue = entity.getCfValuesNullSafe().setValue(cfCode, collectionValue, EntityReferenceWrapper.class);
                    } catch (Exception e) {
                        // FIXME: Handle these cases
                        log.warn("Unhandled data : {}", value.toString());
                    }
                } else {
                    EntityReferenceWrapper entityReferenceWrapper = new EntityReferenceWrapper();
                    entityReferenceWrapper.setClassnameCode(cft.getEntityClazzCetCode());
                    if (value instanceof EntityReferenceWrapper) {
                        entityReferenceWrapper = (EntityReferenceWrapper) value;
                    } else if (value instanceof String) {
                        entityReferenceWrapper.setUuid((String) value);
                        entityReferenceWrapper.setCode((String) value);
                    } else if (value instanceof BusinessEntity) {
                        entityReferenceWrapper.setCode(((BusinessEntity) value).getCode());
                        entityReferenceWrapper.setId(((BusinessEntity) value).getId());
                    } else if (StringUtils.isNumeric(String.valueOf(value))) {
                        entityReferenceWrapper.setId(Long.parseLong(String.valueOf(value)));
                    } else if (value instanceof BaseEntity) {
                        JpaUtils.extractNaturalId(value).ifPresent(entityReferenceWrapper::setCode);
                        entityReferenceWrapper.setId(((BaseEntity) value).getId());
                    }
                    cfValue = entity.getCfValuesNullSafe().setValue(cfCode, entityReferenceWrapper);
                }
            } else {
                EntityReferenceWrapper entityReferenceWrapper = new EntityReferenceWrapper();
                entityReferenceWrapper.setClassnameCode(cft.getEntityClazzCetCode());
                entityReferenceWrapper.setRepository(repository);
                if (value instanceof Map) {
                    Map<String, Object> valueAsMap = (Map<String, Object>) value;
                    entityReferenceWrapper.setCode((String) valueAsMap.get("code"));
                    entityReferenceWrapper.setUuid((String) valueAsMap.get("uuid"));
                    if (entityReferenceWrapper.getUuid() == null) {
                        entityReferenceWrapper.setUuid((String) valueAsMap.get("meveo_uuid"));
                    }
                } else if (value instanceof String) {
                    entityReferenceWrapper.setUuid((String) value);
                    fetchCode(cft, (String) value, entityReferenceWrapper);
                } else if (value instanceof EntityReferenceWrapper) {
                    entityReferenceWrapper = (EntityReferenceWrapper) value;
                }
                if (entityReferenceWrapper.getUuid() != null) {
                    cfValue = entity.getCfValuesNullSafe().setValue(cfCode, entityReferenceWrapper);
                } else if (value instanceof Collection) {
                    List<EntityReferenceWrapper> entityReferences = new ArrayList<>();
                    List<Map<String, Object>> entityValues = new ArrayList<>();
                    for (Object item : (Collection<?>) value) {
                        EntityReferenceWrapper itemWrapper = new EntityReferenceWrapper();
                        itemWrapper.setClassnameCode(cft.getEntityClazzCetCode());
                        if (item instanceof Map) {
                            Map<String, Object> valueAsMap = (Map<String, Object>) item;
                            itemWrapper.setCode((String) valueAsMap.get("code"));
                            itemWrapper.setUuid((String) valueAsMap.get("uuid"));
                            if (itemWrapper.getUuid() == null) {
                                itemWrapper.setUuid((String) valueAsMap.get("meveo_uuid"));
                            }
                            if (itemWrapper.getUuid() == null) {
                                entityValues.add(valueAsMap);
                            }
                        } else if (item instanceof String) {
                            itemWrapper.setUuid((String) item);
                            itemWrapper.setCode((String) item);
                            // Try to fetch code
                            fetchCode(cft, (String) item, itemWrapper);
                        } else if (item instanceof EntityReferenceWrapper) {
                            itemWrapper = (EntityReferenceWrapper) item;
                        }
                        if (itemWrapper.getUuid() != null || itemWrapper.getCode() != null) {
                            entityReferences.add(itemWrapper);
                        }
                    }
                    // If entity references list is empty, the entities referenced are probably being created
                    if (!entityReferences.isEmpty()) {
                        cfValue = entity.getCfValuesNullSafe().setValue(cfCode, entityReferences);
                    } else if (!entityValues.isEmpty()) {
                        cfValue = entity.getCfValuesNullSafe().setValue(cfCode, entityValues);
                    }
                } else {
                    entity.getCfValuesNullSafe().setValue(cfCode, value);
                }
            }
            if (value instanceof CustomEntityInstance) {
                cfValue = entity.getCfValuesNullSafe().setValue(cfCode, value);
            }
        } else {
            cfValue = entity.getCfValuesNullSafe().setValue(cfCode, value);
        }
        log.trace("Setting CF value 2. Code: {}, cfValue {}", cfCode, cfValue);
    // Existing CFI found. Update with new value or NULL value only if cft.defaultValue is present
    } else if (value != null || cft.getDefaultValue() != null) {
        try {
            cfValue.setValue(value);
        } catch (IllegalArgumentException e) {
            log.error("Error setting value for field template with code " + cfCode + " for entity " + entity + " : " + e.getMessage());
            throw e;
        }
    // Existing CF value found, but new value is null, so remove CF value all together
    } else {
        entity.getCfValues().removeValue(cfCode);
        return null;
    }
    return cfValue;
}
Also used : CustomFieldValue(org.meveo.model.crm.custom.CustomFieldValue) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) BusinessException(org.meveo.admin.exception.BusinessException) CurrentUserProvider(org.meveo.security.keycloak.CurrentUserProvider) Provider(org.meveo.model.crm.Provider) BusinessException(org.meveo.admin.exception.BusinessException) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) EntityReferenceWrapper(org.meveo.model.crm.EntityReferenceWrapper) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance)

Example 3 with Provider

use of org.meveo.model.crm.Provider in project meveo by meveo-org.

the class CustomFieldInstanceService method getCFValueByClosestMatch.

/**
 * Match for a given date (versionable values) for a given entity's custom field as close as possible map's key to the key provided and return a map value. Match is performed
 * by matching a full string and then reducing one by one symbol until a match is found.
 *
 * @param entity     Entity to match
 * @param cfCode     Custom field code
 * @param date       Date
 * @param keyToMatch Key to match
 * @return Map value that closely matches map key
 */
public Object getCFValueByClosestMatch(ICustomFieldEntity entity, String cfCode, Date date, String keyToMatch) {
    // Handle cases when appProvider was passed instead of a real Provider entity. The class in this case is org.meveo.model.crm.Provider$Proxy$_$$_WeldClientProxy
    if (entity instanceof Provider && entity.getClass().getSimpleName().contains("Proxy")) {
        entity = providerService.findById(appProvider.getId());
    }
    if (entity.getCfValues() == null) {
        return null;
    }
    Object value = entity.getCfValues().getValue(cfCode, date);
    Object valueMatched = CustomFieldUtils.matchClosestValue(value, keyToMatch);
    log.trace("Found closest match value {} for period {} and keyToMatch={}", valueMatched, date, keyToMatch);
    // Need to check if it is a multi-value type value and convert it to a map
    if (valueMatched != null && valueMatched instanceof String) {
        CustomFieldTemplate cft = cfTemplateService.findByCodeAndAppliesTo(cfCode, entity);
        if (cft.getFieldType() == CustomFieldTypeEnum.MULTI_VALUE) {
            return cft.deserializeMultiValue((String) valueMatched, null);
        }
    }
    return valueMatched;
}
Also used : CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CurrentUserProvider(org.meveo.security.keycloak.CurrentUserProvider) Provider(org.meveo.model.crm.Provider)

Example 4 with Provider

use of org.meveo.model.crm.Provider in project meveo by meveo-org.

the class CustomFieldInstanceService method getCFValueByClosestMatch.

/**
 * Match for a given entity's custom field (non-versionable values) as close as possible map's key to the key provided and return a map value. Match is performed by matching a
 * full string and then reducing one by one symbol until a match is found.
 *
 * @param entity     Entity to match
 * @param cfCode     Custom field code
 * @param keyToMatch Key to match
 * @return Map value that closely matches map key
 */
public Object getCFValueByClosestMatch(ICustomFieldEntity entity, String cfCode, String keyToMatch) {
    // Handle cases when appProvider was passed instead of a real Provider entity. The class in this case is org.meveo.model.crm.Provider$Proxy$_$$_WeldClientProxy
    if (entity instanceof Provider && entity.getClass().getSimpleName().contains("Proxy")) {
        entity = providerService.findById(appProvider.getId());
    }
    if (entity.getCfValues() == null) {
        return null;
    }
    Object value = entity.getCfValues().getValue(cfCode);
    Object valueMatched = CustomFieldUtils.matchClosestValue(value, keyToMatch);
    log.trace("Found closest match value {} for keyToMatch={}", valueMatched, keyToMatch);
    // Need to check if it is a multi-value type value and convert it to a map
    if (valueMatched != null && valueMatched instanceof String) {
        CustomFieldTemplate cft = cfTemplateService.findByCodeAndAppliesTo(cfCode, entity);
        if (cft.getFieldType() == CustomFieldTypeEnum.MULTI_VALUE) {
            return cft.deserializeMultiValue((String) valueMatched, null);
        }
    }
    return valueMatched;
}
Also used : CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CurrentUserProvider(org.meveo.security.keycloak.CurrentUserProvider) Provider(org.meveo.model.crm.Provider)

Example 5 with Provider

use of org.meveo.model.crm.Provider in project meveo by meveo-org.

the class CustomFieldInstanceService method setCFValue.

public CustomFieldValue setCFValue(ICustomFieldEntity entity, String cfCode, Object value, Date valueDateFrom, Date valueDateTo, Integer valuePriority) throws BusinessException {
    log.trace("Setting CF value. Code: {}, entity {} value {} valueDateFrom {} valueDateTo {}", cfCode, entity, value, valueDateFrom, valueDateTo);
    // If field is not versionable - set the value without the date
    CustomFieldTemplate cft = cfTemplateService.findByCodeAndAppliesTo(cfCode, entity);
    if (cft == null) {
        throw new BusinessException("Custom field template with code " + cfCode + " not found found for entity " + entity);
    }
    if (!cft.isVersionable()) {
        return setCFValue(entity, cfCode, value);
    // If calendar is provided - use calendar by the valueDateFrom date
    } else if (cft.getCalendar() != null) {
        log.warn("Calendar is provided in Custom Field template {}/{} while trying to assign value period start and end dates with two values. Only start date will be considered", entity.getClass().getSimpleName(), cfCode);
        return setCFValue(entity, cfCode, value, valueDateFrom);
    }
    // Handle cases when appProvider was passed instead of a real Provider entity. The class in this case is org.meveo.model.crm.Provider$Proxy$_$$_WeldClientProxy
    if (entity instanceof Provider && entity.getClass().getSimpleName().contains("Proxy")) {
        entity = providerService.findById(appProvider.getId());
    }
    // Should not match more then one record, as match is strict
    CustomFieldValue cfValue = null;
    if (entity.getCfValues() != null) {
        cfValue = entity.getCfValues().getCfValue(cfCode, valueDateFrom, valueDateTo);
    }
    // No existing CF value. Create CF value with new value. Persist NULL value only if cft.defaultValue is present
    if (cfValue == null) {
        if (value == null && cft.getDefaultValue() == null) {
            return null;
        }
        entity.getCfValuesNullSafe().setValue(cfCode, new DatePeriod(valueDateFrom.toInstant(), valueDateTo.toInstant()), valuePriority, value);
    // Existing CF value found. Update with new value or NULL value only if cft.defaultValue is present
    } else if (value != null || (value == null && cft.getDefaultValue() != null)) {
        cfValue.setValue(value);
    // Existing CF value found, but new value is null, so remove CF value
    } else {
        entity.getCfValues().removeValue(cfCode, valueDateFrom, valueDateTo);
        return null;
    }
    return cfValue;
}
Also used : CustomFieldValue(org.meveo.model.crm.custom.CustomFieldValue) BusinessException(org.meveo.admin.exception.BusinessException) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CurrentUserProvider(org.meveo.security.keycloak.CurrentUserProvider) Provider(org.meveo.model.crm.Provider)

Aggregations

Provider (org.meveo.model.crm.Provider)34 CurrentUserProvider (org.meveo.security.keycloak.CurrentUserProvider)19 CustomFieldTemplate (org.meveo.model.crm.CustomFieldTemplate)18 BusinessException (org.meveo.admin.exception.BusinessException)8 CustomFieldsCacheContainerProvider (org.meveo.cache.CustomFieldsCacheContainerProvider)5 ApplicationProvider (org.meveo.util.ApplicationProvider)5 JobCacheContainerProvider (org.meveo.cache.JobCacheContainerProvider)4 NotificationCacheContainerProvider (org.meveo.cache.NotificationCacheContainerProvider)4 CustomFieldValue (org.meveo.model.crm.custom.CustomFieldValue)4 IOException (java.io.IOException)3 PaginationConfiguration (org.meveo.admin.util.pagination.PaginationConfiguration)3 CustomEntityInstance (org.meveo.model.customEntities.CustomEntityInstance)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 AsyncResult (javax.ejb.AsyncResult)2 Asynchronous (javax.ejb.Asynchronous)2 TransactionAttribute (javax.ejb.TransactionAttribute)2 NoResultException (javax.persistence.NoResultException)2