Search in sources :

Example 1 with CustomEntityTemplate

use of org.meveo.model.customEntities.CustomEntityTemplate in project meveo by meveo-org.

the class CustomTableService method saveBatch.

/**
 * Inserts a list of {@linkplain CustomEntityInstance} into the database in batch.
 *
 * @param cfts map of {@link CustomFieldTemplate}
 * @param fields list of {@link CustomFieldTemplate}
 * @param ceis list of transient {@link CustomEntityInstance}
 * @param entityReferencesCache
 * @throws BusinessException batch saving failed
 */
private void saveBatch(String sqlConnectionCode, Map<String, CustomFieldTemplate> cfts, List<CustomFieldTemplate> fields, List<CustomEntityInstance> ceis, Map<String, Map<String, String>> entityReferencesCache) throws BusinessException {
    if (ceis == null || ceis.isEmpty()) {
        return;
    }
    List<Map<String, Object>> values = ceis.stream().map(CustomEntityInstance::getCfValuesAsValues).collect(Collectors.toList());
    values = convertValues(values, cfts, false);
    values = replaceEntityReferences(sqlConnectionCode, fields, values, entityReferencesCache);
    final CustomEntityTemplate cet = customEntityTemplateService.findByCodeOrDbTablename(ceis.get(0).getTableName());
    customTableService.get().createInNewTx(sqlConnectionCode, cet, false, values);
}
Also used : CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with CustomEntityTemplate

use of org.meveo.model.customEntities.CustomEntityTemplate in project meveo by meveo-org.

the class CustomEntityTemplateService method update.

@Override
public CustomEntityTemplate update(CustomEntityTemplate cet) throws BusinessException {
    CustomEntityTemplate oldValue = customFieldsCache.getCustomEntityTemplate(cet.getCode());
    if (!EntityCustomizationUtils.validateOntologyCode(cet.getCode())) {
        throw new IllegalArgumentException("The code of ontology elements must not contain numbers");
    }
    checkCrudEventListenerScript(cet);
    ParamBean paramBean = paramBeanFactory.getInstance();
    if (cet.getCustomEntityCategory() != null && !cet.getCustomEntityCategory().isTransient()) {
        CustomEntityCategory cec = customEntityCategoryService.reattach(cet.getCustomEntityCategory());
        cet.setCustomEntityCategory(cec);
    }
    CustomEntityTemplate cetUpdated = super.update(cet);
    customFieldsCache.addUpdateCustomEntityTemplate(cet);
    try {
        permissionService.createIfAbsent(cet.getModifyPermission(), paramBean.getProperty("role.modifyAllCE", "ModifyAllCE"));
        permissionService.createIfAbsent(cet.getReadPermission(), paramBean.getProperty("role.readAllCE", "ReadAllCE"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    flush();
    // Synchronize custom fields storages with CET available storages
    for (CustomFieldTemplate cft : customFieldTemplateService.findByAppliesToNoCache(cet.getAppliesTo()).values()) {
        cft.setHasReferenceJpaEntity(cet.hasReferenceJpaEntity());
        if (cft.getStoragesNullSafe() != null) {
            for (DBStorageType storage : new ArrayList<>(cft.getStoragesNullSafe())) {
                if (!cet.getAvailableStorages().contains(storage)) {
                    log.info("Remove storage '{}' from CFT '{}' of CET '{}'", storage, cft.getCode(), cet.getCode());
                    cft.getStoragesNullSafe().remove(storage);
                    customFieldTemplateService.update(cft);
                }
            }
        }
    }
    // Synchronize neoj4 indexes
    if (cet.getAvailableStorages() != null && cet.getAvailableStorages().contains(DBStorageType.NEO4J)) {
        neo4jService.addUUIDIndexes(cet);
    } else {
        neo4jService.removeUUIDIndexes(cet);
    }
    var sqlConfs = sqlConfigurationService.listActiveAndInitialized();
    // Handle SQL inheritance
    if (cet.storedIn(DBStorageType.SQL)) {
        if (oldValue.getSuperTemplate() != null && cet.getSuperTemplate() == null) {
            // Inheritance removed
            sqlConfs.forEach(sc -> customTableCreatorService.removeInheritance(sc.getCode(), cet));
        } else if (oldValue.getSuperTemplate() == null && cet.getSuperTemplate() != null) {
            // Inheritance added
            sqlConfs.forEach(sc -> customTableCreatorService.addInheritance(sc.getCode(), cet));
        }
    }
    return cetUpdated;
}
Also used : JSONSchemaGenerator(org.meveo.service.crm.impl.JSONSchemaGenerator) NoResultException(javax.persistence.NoResultException) CustomEntityTemplateDto(org.meveo.api.dto.CustomEntityTemplateDto) MeveoRepository(org.meveo.service.git.MeveoRepository) ICustomFieldEntity(org.meveo.model.ICustomFieldEntity) ScriptInstanceService(org.meveo.service.script.ScriptInstanceService) CustomEntity(org.meveo.model.CustomEntity) Repository(org.meveo.model.storage.Repository) TransactionAttributeType(javax.ejb.TransactionAttributeType) SQLStorageConfiguration(org.meveo.model.persistence.sql.SQLStorageConfiguration) Asynchronous(javax.ejb.Asynchronous) Map(java.util.Map) Observes(javax.enterprise.event.Observes) MeveoUser(org.meveo.security.MeveoUser) CustomEntityCategory(org.meveo.model.customEntities.CustomEntityCategory) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Stateless(javax.ejb.Stateless) TxType(javax.transaction.Transactional.TxType) Transactional(javax.transaction.Transactional) MeveoModule(org.meveo.model.module.MeveoModule) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) ElasticClient(org.meveo.service.index.ElasticClient) MeveoModuleHelper(org.meveo.service.admin.impl.MeveoModuleHelper) List(java.util.List) EntityCustomizationUtils(org.meveo.util.EntityCustomizationUtils) ModuleUninstall(org.meveo.service.admin.impl.ModuleUninstall) Entry(java.util.Map.Entry) PostConstruct(javax.annotation.PostConstruct) CustomFieldsCacheContainerProvider(org.meveo.cache.CustomFieldsCacheContainerProvider) Optional(java.util.Optional) GitRepository(org.meveo.model.git.GitRepository) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) EntityCustomAction(org.meveo.model.crm.custom.EntityCustomAction) MeveoModuleItem(org.meveo.model.module.MeveoModuleItem) HashMap(java.util.HashMap) ModulePostUninstall(org.meveo.model.ModulePostUninstall) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) PermissionService(org.meveo.service.admin.impl.PermissionService) TypedQuery(javax.persistence.TypedQuery) MeveoFileUtils(org.meveo.commons.utils.MeveoFileUtils) BusinessService(org.meveo.service.base.BusinessService) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) PrimitiveTypeEnum(org.meveo.model.crm.custom.PrimitiveTypeEnum) JacksonUtil(org.meveo.model.persistence.JacksonUtil) Lists(com.google.common.collect.Lists) CustomFieldTemplateService(org.meveo.service.crm.impl.CustomFieldTemplateService) ImmutableList(com.google.common.collect.ImmutableList) TransactionAttribute(javax.ejb.TransactionAttribute) ParamBean(org.meveo.commons.utils.ParamBean) LinkedList(java.util.LinkedList) Neo4jService(org.meveo.persistence.neo4j.service.Neo4jService) MapUtils(org.apache.commons.collections4.MapUtils) QueryBuilder(org.meveo.commons.utils.QueryBuilder) CrudEventListenerScript(org.meveo.model.customEntities.CrudEventListenerScript) BaseEntityDto(org.meveo.api.dto.BaseEntityDto) Files(java.nio.file.Files) DBStorageType(org.meveo.model.persistence.DBStorageType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) GitHelper(org.meveo.service.git.GitHelper) ParamBeanFactory(org.meveo.commons.utils.ParamBeanFactory) File(java.io.File) CustomFieldTypeEnum(org.meveo.model.crm.custom.CustomFieldTypeEnum) GitClient(org.meveo.service.git.GitClient) JSONSchemaIntoJavaClassParser(org.meveo.service.crm.impl.JSONSchemaIntoJavaClassParser) Collections(java.util.Collections) SqlConfigurationService(org.meveo.persistence.sql.SqlConfigurationService) CustomEntityCategory(org.meveo.model.customEntities.CustomEntityCategory) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) DBStorageType(org.meveo.model.persistence.DBStorageType) ArrayList(java.util.ArrayList) ParamBean(org.meveo.commons.utils.ParamBean) NoResultException(javax.persistence.NoResultException) BusinessException(org.meveo.admin.exception.BusinessException) IOException(java.io.IOException)

Example 3 with CustomEntityTemplate

use of org.meveo.model.customEntities.CustomEntityTemplate in project meveo by meveo-org.

the class CustomEntityTemplateService method removeCETsByCategoryId.

/**
 * retrieve Custom Entity Templates given by categoryId then remove it so that we can remove it in the cache
 *
 * @param categoryId if of the category
 * @throws BusinessException the the CETs can't be removed
 */
public void removeCETsByCategoryId(Long categoryId) throws BusinessException {
    TypedQuery<CustomEntityTemplate> query = getEntityManager().createNamedQuery("CustomEntityTemplate.getCETsByCategoryId", CustomEntityTemplate.class);
    List<CustomEntityTemplate> results = query.setParameter("id", categoryId).getResultList();
    if (CollectionUtils.isNotEmpty(results)) {
        for (CustomEntityTemplate entityTemplate : results) {
            remove(entityTemplate);
        }
    }
}
Also used : CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate)

Example 4 with CustomEntityTemplate

use of org.meveo.model.customEntities.CustomEntityTemplate in project meveo by meveo-org.

the class CustomRelationshipTemplateService method create.

@Override
public void create(CustomRelationshipTemplate crt) throws BusinessException {
    if (!EntityCustomizationUtils.validateOntologyCode(crt.getCode())) {
        throw new IllegalArgumentException("The code of ontology elements must not contain numbers");
    }
    if (crt.getStartNode() == null) {
        throw new IllegalArgumentException("Can't create relation " + crt.getCode() + ": start node can't be null");
    }
    if (crt.getEndNode() == null) {
        throw new IllegalArgumentException("Can't create relation " + crt.getCode() + ": end node can't be null");
    }
    super.create(crt);
    try {
        permissionService.createIfAbsent(crt.getModifyPermission(), paramBean.getProperty("role.modifyAllCR", "ModifyAllCR"));
        permissionService.createIfAbsent(crt.getReadPermission(), paramBean.getProperty("role.readAllCR", "ReadAllCR"));
        if (crt.getAvailableStorages().contains(DBStorageType.SQL)) {
            customTableCreatorService.createCrtTable(crt);
        }
        customFieldsCache.addUpdateCustomRelationshipTemplate(crt);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    // Synchronize start and end CETs
    CustomEntityTemplate startCet = crt.getStartNode();
    MeveoModule cetModule = cetService.findModuleOf(startCet);
    cetService.addFilesToModule(startCet, cetModule);
    CustomEntityTemplate endCrt = crt.getEndNode();
    MeveoModule cet2Module = cetService.findModuleOf(endCrt);
    cetService.addFilesToModule(endCrt, cet2Module);
}
Also used : CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) MeveoModule(org.meveo.model.module.MeveoModule) NoResultException(javax.persistence.NoResultException) IOException(java.io.IOException) BusinessException(org.meveo.admin.exception.BusinessException)

Example 5 with CustomEntityTemplate

use of org.meveo.model.customEntities.CustomEntityTemplate in project meveo by meveo-org.

the class CustomRelationshipTemplateService method remove.

@Override
public void remove(CustomRelationshipTemplate crt) throws BusinessException {
    Map<String, CustomFieldTemplate> fields = customFieldTemplateService.findByAppliesToNoCache(crt.getAppliesTo());
    for (CustomFieldTemplate cft : fields.values()) {
        customFieldTemplateService.remove(cft.getId());
    }
    if (crt.getAvailableStorages().contains(DBStorageType.SQL)) {
        customTableCreatorService.removeTable(repositoryService.findDefaultRepository().getCode(), SQLStorageConfiguration.getDbTablename(crt));
    }
    customFieldsCache.removeCustomRelationshipTemplate(crt);
    permissionService.removeIfPresent(crt.getModifyPermission());
    permissionService.removeIfPresent(crt.getReadPermission());
    super.remove(crt);
    CustomEntityTemplate startCet = crt.getStartNode();
    MeveoModule cetModule = cetService.findModuleOf(startCet);
    cetService.addFilesToModule(startCet, cetModule);
    CustomEntityTemplate endCrt = crt.getEndNode();
    MeveoModule cet2Module = cetService.findModuleOf(endCrt);
    cetService.addFilesToModule(endCrt, cet2Module);
}
Also used : CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) MeveoModule(org.meveo.model.module.MeveoModule)

Aggregations

CustomEntityTemplate (org.meveo.model.customEntities.CustomEntityTemplate)119 CustomFieldTemplate (org.meveo.model.crm.CustomFieldTemplate)54 BusinessException (org.meveo.admin.exception.BusinessException)42 ArrayList (java.util.ArrayList)38 HashMap (java.util.HashMap)35 Map (java.util.Map)35 EntityDoesNotExistsException (org.meveo.api.exception.EntityDoesNotExistsException)35 CustomEntityInstance (org.meveo.model.customEntities.CustomEntityInstance)31 IOException (java.io.IOException)28 List (java.util.List)24 PaginationConfiguration (org.meveo.admin.util.pagination.PaginationConfiguration)23 CustomRelationshipTemplate (org.meveo.model.customEntities.CustomRelationshipTemplate)22 Collectors (java.util.stream.Collectors)21 Inject (javax.inject.Inject)21 Repository (org.meveo.model.storage.Repository)21 CustomFieldTypeEnum (org.meveo.model.crm.custom.CustomFieldTypeEnum)20 CustomModelObject (org.meveo.model.customEntities.CustomModelObject)20 Collection (java.util.Collection)19 HashSet (java.util.HashSet)19 Set (java.util.Set)19