Search in sources :

Example 1 with Values

use of org.neo4j.driver.v1.Values in project meveo by meveo-org.

the class Neo4jService method deleteEntity.

public void deleteEntity(String neo4jConfiguration, String cetCode, Map<String, Object> values) throws BusinessException {
    /* Get entity template */
    final CustomEntityTemplate customEntityTemplate = customFieldsCache.getCustomEntityTemplate(cetCode);
    /* Extract unique fields values for node */
    final Map<String, Object> uniqueFields = getNodeKeys(customEntityTemplate.getAppliesTo(), values);
    /* No unique fields has been found */
    if (uniqueFields.isEmpty()) {
        throw new BusinessException("At least one unique field must be provided for cet to delete");
    }
    final String uniqueFieldStatement = neo4jDao.getFieldsString(uniqueFields.keySet());
    /* Map the variables declared in the statement */
    Map<String, Object> valuesMap = new HashMap<>();
    valuesMap.put("cetCode", cetCode);
    valuesMap.put("uniqueFields", uniqueFieldStatement);
    String deleteStatement = getStatement(new StrSubstitutor(valuesMap), Neo4JRequests.deleteCet);
    final Transaction transaction = crossStorageTransaction.getNeo4jTransaction(neo4jConfiguration);
    InternalNode internalNode = null;
    try {
        /* Delete the node and all its associated relationships and fire node deletion event */
        // Execute query
        final StatementResult result = transaction.run(deleteStatement, values);
        for (Record record : result.list()) {
            // Fire deletion event
            // Parse properties
            final Map<String, Value> properties = record.get("properties").asMap(e -> e);
            // Parse labels
            final List<String> labels = record.get("labels").asList(Value::asString);
            // Parse id
            final long id = record.get("id").asLong();
            // Create Node object
            internalNode = new InternalNode(id, labels, properties);
        }
        transaction.success();
    } catch (Exception e) {
        log.error("Cannot delete node with code {} and values {}", cetCode, values, e);
        transaction.failure();
        throw new BusinessException(e);
    }
    if (internalNode != null) {
        // Fire notification
        nodeRemovedEvent.fire(new Neo4jEntity(internalNode, neo4jConfiguration));
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ELException(org.meveo.elresolver.ELException) PatternSyntaxException(java.util.regex.PatternSyntaxException) ElementNotFoundException(org.meveo.admin.exception.ElementNotFoundException) BusinessException(org.meveo.admin.exception.BusinessException) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException) RemoteAuthenticationException(org.meveo.export.RemoteAuthenticationException) InvalidCustomFieldException(org.meveo.exceptions.InvalidCustomFieldException) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) BusinessException(org.meveo.admin.exception.BusinessException) CrossStorageTransaction(org.meveo.persistence.CrossStorageTransaction) Transaction(org.neo4j.driver.v1.Transaction) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) CustomFieldValue(org.meveo.model.crm.custom.CustomFieldValue) Value(org.neo4j.driver.v1.Value) Neo4jEntity(org.meveo.persistence.neo4j.graph.Neo4jEntity) Record(org.neo4j.driver.v1.Record) InternalNode(org.neo4j.driver.internal.InternalNode)

Example 2 with Values

use of org.neo4j.driver.v1.Values in project meveo by meveo-org.

the class Neo4jService method saveCRT2Neo4j.

/**
 * Save CRT to Neo4j
 *
 * @param neo4JConfiguration         Neo4J coordinates
 * @param customRelationshipTemplate Template of the CRT
 * @param startNodeKeysMap           Unique fields values of the start node
 * @param endNodeKeysMap             Unique fields values of the start node
 * @param crtFields                  Fields values of the relationship
 */
public List<String> saveCRT2Neo4j(String neo4JConfiguration, CustomRelationshipTemplate customRelationshipTemplate, Map<String, Object> startNodeKeysMap, Map<String, Object> endNodeKeysMap, Map<String, Object> crtFields, boolean isTemporaryCET) {
    // Alias to use in query
    final String relationshipAlias = "relationship";
    // Build values map
    Map<String, Object> valuesMap = new HashMap<>();
    valuesMap.put("startAlias", Neo4JRequests.START_NODE_ALIAS);
    valuesMap.put("endAlias", Neo4JRequests.END_NODE_ALIAS);
    valuesMap.put("startNode", customRelationshipTemplate.getStartNode().getCode());
    valuesMap.put("endNode", customRelationshipTemplate.getEndNode().getCode());
    valuesMap.put("relationType", customRelationshipTemplate.getName());
    valuesMap.put("starNodeKeys", Values.value(startNodeKeysMap));
    valuesMap.put("endNodeKeys", Values.value(endNodeKeysMap));
    valuesMap.put("updateDate", isTemporaryCET ? -1 : System.currentTimeMillis());
    final String fieldsString = neo4jDao.getFieldsString(crtFields.keySet());
    valuesMap.put(FIELDS, fieldsString);
    valuesMap.putAll(crtFields);
    valuesMap.put(NODE_ID, UUID.randomUUID().toString());
    // Build the statement
    StringBuffer statement = neo4jDao.appendReturnStatement(Neo4JRequests.crtStatement, relationshipAlias, valuesMap);
    StrSubstitutor sub = new StrSubstitutor(valuesMap);
    String resolvedStatement = sub.replace(statement);
    // Begin Neo4J transaction
    final Transaction transaction = crossStorageTransaction.getNeo4jTransaction(neo4JConfiguration);
    List<Record> recordList = new ArrayList<>();
    try {
        /* Execute query and parse result inside a relationship.
            If relationship was created fire creation event, fire update event when updated. */
        // Execute query
        final StatementResult result = transaction.run(resolvedStatement, valuesMap);
        // Fire notification for each relation created or updated
        recordList = result.list();
        // Commit transaction
        transaction.success();
    } catch (Exception e) {
        log.error("Failed to save relationship", e);
        transaction.failure();
    }
    List<String> relationUuids = new ArrayList<>();
    for (Record record : recordList) {
        // Parse relationship
        final Neo4jRelationship relationship = new Neo4jRelationship(record.get(relationshipAlias).asRelationship(), neo4JConfiguration);
        if (relationship.containsKey(MEVEO_UUID)) {
            relationUuids.add(relationship.get(MEVEO_UUID).asString());
        }
        if (relationship.containsKey("update_date") || relationship.containsKey("updateDate")) {
            // Check if relationship contains the "update_date" key
            // Fire update event if contains the key
            edgeUpdatedEvent.fire(relationship);
        } else {
            // Fire creation event if does not contains the key
            edgeCreatedEvent.fire(relationship);
        }
    }
    return relationUuids;
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Neo4jRelationship(org.meveo.persistence.neo4j.graph.Neo4jRelationship) ELException(org.meveo.elresolver.ELException) PatternSyntaxException(java.util.regex.PatternSyntaxException) ElementNotFoundException(org.meveo.admin.exception.ElementNotFoundException) BusinessException(org.meveo.admin.exception.BusinessException) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException) RemoteAuthenticationException(org.meveo.export.RemoteAuthenticationException) InvalidCustomFieldException(org.meveo.exceptions.InvalidCustomFieldException) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) CrossStorageTransaction(org.meveo.persistence.CrossStorageTransaction) Transaction(org.neo4j.driver.v1.Transaction) Record(org.neo4j.driver.v1.Record)

Example 3 with Values

use of org.neo4j.driver.v1.Values in project meveo by meveo-org.

the class Neo4jService method addSourceNodeUniqueCrt.

/**
 * Persist a source node of an unique relationship.
 * If a relationship that targets the target node exists, then we merge the fields of the start in parameter to
 * the fields of the source node of the relationship.
 * If such a relation does not exists, we create the source node with it fields.
 *
 * @param neo4JConfiguration Neo4J coordinates
 * @param crtCode            Code of the unique relation
 * @param startNodeValues    Values to assign to the start node
 * @param endNodeValues      Filters on the target node values
 */
@JpaAmpNewTx
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public PersistenceActionResult addSourceNodeUniqueCrt(String neo4JConfiguration, String crtCode, Map<String, Object> startNodeValues, Map<String, Object> endNodeValues) throws BusinessException, ELException {
    // Get relationship template
    final CustomRelationshipTemplate customRelationshipTemplate = customFieldsCache.getCustomRelationshipTemplate(crtCode);
    final CustomEntityTemplate endNode = customRelationshipTemplate.getEndNode();
    // Extract unique fields values for the start node
    Map<String, CustomFieldTemplate> endNodeCfts = customFieldTemplateService.findByAppliesTo(endNode.getAppliesTo());
    Map<String, CustomFieldTemplate> startNodeCfts = customFieldTemplateService.findByAppliesTo(customRelationshipTemplate.getStartNode().getAppliesTo());
    final Map<String, Object> endNodeUniqueFields = new HashMap<>();
    Map<String, Object> endNodeConvertedValues = validateAndConvertCustomFields(endNodeCfts, endNodeValues, endNodeUniqueFields, true);
    Map<String, Object> startNodeConvertedValues = validateAndConvertCustomFields(startNodeCfts, startNodeValues, null, true);
    // Map the variables declared in the statement
    Map<String, Object> valuesMap = new HashMap<>();
    final String cetCode = customRelationshipTemplate.getStartNode().getCode();
    valuesMap.put("cetCode", cetCode);
    valuesMap.put("crtCode", crtCode);
    valuesMap.put("endCetcode", customRelationshipTemplate.getEndNode().getCode());
    // Prepare the key maps for unique fields and start node fields
    final String uniqueFieldStatements = neo4jDao.getFieldsString(endNodeConvertedValues.keySet());
    final String startNodeValuesStatements = neo4jDao.getFieldsString(startNodeConvertedValues.keySet());
    // No unique fields has been found
    if (endNodeUniqueFields.isEmpty()) {
        // If no unique fields are provided / defined, retrieve the meveo_uuid of the target node using unicity rules
        Set<String> ids = endNode.getNeo4JStorageConfiguration().getUniqueConstraints().stream().filter(uniqueConstraint -> uniqueConstraint.getTrustScore() == 100).filter(uniqueConstraint -> isApplicableConstraint(endNodeValues, uniqueConstraint)).sorted(Neo4jService.CONSTRAINT_COMPARATOR).map(uniqueConstraint -> neo4jDao.executeUniqueConstraint(neo4JConfiguration, uniqueConstraint, endNodeValues, endNode.getCode())).findFirst().orElse(Set.of());
        if (ids.isEmpty()) {
            log.error("At least one unique field must be provided for target entity [code = {}, fields = {}]. " + "Unique fields are : {}", customRelationshipTemplate.getEndNode().getCode(), endNodeValues, endNodeUniqueFields);
            throw new BusinessException("Unique field must be provided");
        }
        if (ids.size() > 1) {
            throw new BusinessException(String.format("Multiple targets for unique relationship %s : %s.", crtCode, ids));
        }
        String id = ids.iterator().next();
        endNodeValues.put("meveo_uuid", id);
        endNodeUniqueFields.put("meveo_uuid", id);
    }
    // Assign the keys names
    valuesMap.put(FIELD_KEYS, uniqueFieldStatements);
    valuesMap.put(FIELDS, startNodeValuesStatements);
    // Create the substitutor
    StrSubstitutor sub = new StrSubstitutor(valuesMap);
    // Values of the keys defined in valuesMap
    Map<String, Object> parametersValues = new HashMap<>();
    parametersValues.putAll(startNodeConvertedValues);
    parametersValues.putAll(endNodeConvertedValues);
    final Transaction transaction = crossStorageTransaction.getNeo4jTransaction(neo4JConfiguration);
    // Try to find the id of the source node
    String findStartNodeStatement = getStatement(sub, Neo4JRequests.findStartNodeId);
    final StatementResult run = transaction.run(findStartNodeStatement, parametersValues);
    Neo4jEntity startNode = null;
    try {
        try {
            /* Update the source node with the found id */
            // Alias to use in queries
            final String startNodeAlias = "startNode";
            // Retrieve ID of the node
            final Record idRecord = run.single();
            final Value id = idRecord.get(0);
            parametersValues.put(NODE_ID, id);
            // Create statement
            CustomEntityTemplate startCet = customRelationshipTemplate.getStartNode();
            List<String> additionalLabels = getAdditionalLabels(startCet);
            final Map<String, Object> updatableValues = valuesMap.entrySet().stream().filter(s -> startNodeCfts.get(s.getKey()).isAllowEdit()).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
            StringBuffer statement = neo4jDao.appendAdditionalLabels(Neo4JRequests.updateNodeWithId, additionalLabels, startNodeAlias, updatableValues);
            statement = neo4jDao.appendReturnStatement(statement, startNodeAlias, valuesMap);
            String updateStatement = getStatement(sub, statement);
            // Execute query
            final StatementResult result = transaction.run(updateStatement, parametersValues);
            // Fire node update event
            startNode = new Neo4jEntity(result.single().get(startNodeAlias).asNode(), neo4JConfiguration);
        } catch (NoSuchRecordException e) {
            /* Create the source node */
            addCetNode(neo4JConfiguration, customRelationshipTemplate.getStartNode(), startNodeValues, null);
        }
        transaction.success();
    } catch (Exception e) {
        transaction.failure();
        log.error("Transaction for persisting entity with code {} and fields {} was rolled back", cetCode, startNodeValues, e);
        throw new BusinessException(e);
    }
    if (startNode != null) {
        nodeUpdatedEvent.fire(startNode);
        return new PersistenceActionResult(startNode.get("meveo_uuid").asString());
    } else {
        return null;
    }
}
Also used : CustomRelationshipTemplateService(org.meveo.service.custom.CustomRelationshipTemplateService) CrossStorageTransaction(org.meveo.persistence.CrossStorageTransaction) CustomPersistenceService(org.meveo.persistence.CustomPersistenceService) Date(java.util.Date) ELException(org.meveo.elresolver.ELException) LoggerFactory(org.slf4j.LoggerFactory) RepositoryService(org.meveo.service.storage.RepositoryService) StringUtils(org.meveo.commons.utils.StringUtils) ScriptInstanceService(org.meveo.service.script.ScriptInstanceService) CustomEntityTemplateUniqueConstraint(org.meveo.model.crm.CustomEntityTemplateUniqueConstraint) CustomRelationshipTemplate(org.meveo.model.customEntities.CustomRelationshipTemplate) Future(java.util.concurrent.Future) Repository(org.meveo.model.storage.Repository) Matcher(java.util.regex.Matcher) TransactionAttributeType(javax.ejb.TransactionAttributeType) MeveoJpa(org.meveo.jpa.MeveoJpa) Asynchronous(javax.ejb.Asynchronous) CustomFieldValue(org.meveo.model.crm.custom.CustomFieldValue) Map(java.util.Map) CETUtils(org.meveo.api.CETUtils) AsyncResult(javax.ejb.AsyncResult) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) Value(org.neo4j.driver.v1.Value) PatternSyntaxException(java.util.regex.PatternSyntaxException) ApplicationProvider(org.meveo.util.ApplicationProvider) ElementNotFoundException(org.meveo.admin.exception.ElementNotFoundException) ImmutableMap(com.google.common.collect.ImmutableMap) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) Neo4jRelationship(org.meveo.persistence.neo4j.graph.Neo4jRelationship) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) UUID(java.util.UUID) Instant(java.time.Instant) Transaction(org.neo4j.driver.v1.Transaction) Collectors(java.util.stream.Collectors) Entity(javax.ws.rs.client.Entity) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) Node(org.neo4j.driver.v1.types.Node) List(java.util.List) Response(javax.ws.rs.core.Response) CustomFieldStorageTypeEnum(org.meveo.model.crm.custom.CustomFieldStorageTypeEnum) HttpURLConnection(org.apache.commons.httpclient.util.HttpURLConnection) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) Entry(java.util.Map.Entry) CustomFieldsCacheContainerProvider(org.meveo.cache.CustomFieldsCacheContainerProvider) Optional(java.util.Optional) CustomEntityTemplateUtils(org.meveo.service.custom.CustomEntityTemplateUtils) Pattern(java.util.regex.Pattern) StatementResult(org.neo4j.driver.v1.StatementResult) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException) BusinessEntity(org.meveo.model.BusinessEntity) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) RemoteAuthenticationException(org.meveo.export.RemoteAuthenticationException) JpaAmpNewTx(org.meveo.jpa.JpaAmpNewTx) NODE_ID(org.meveo.persistence.neo4j.base.Neo4jDao.NODE_ID) EntityReferenceWrapper(org.meveo.model.crm.EntityReferenceWrapper) BasicAuthentication(org.jboss.resteasy.client.jaxrs.BasicAuthentication) PersistenceActionResult(org.meveo.persistence.PersistenceActionResult) HashMap(java.util.HashMap) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) Updated(org.meveo.event.qualifier.Updated) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Record(org.neo4j.driver.v1.Record) HashSet(java.util.HashSet) Inject(javax.inject.Inject) JacksonUtil(org.meveo.model.persistence.JacksonUtil) Removed(org.meveo.event.qualifier.Removed) Provider(org.meveo.model.crm.Provider) CustomFieldTemplateService(org.meveo.service.crm.impl.CustomFieldTemplateService) MeveoValueExpressionWrapper(org.meveo.service.base.MeveoValueExpressionWrapper) TransactionAttribute(javax.ejb.TransactionAttribute) Neo4jDao(org.meveo.persistence.neo4j.base.Neo4jDao) Event(javax.enterprise.event.Event) InvalidCustomFieldException(org.meveo.exceptions.InvalidCustomFieldException) Logger(org.slf4j.Logger) DBStorageType(org.meveo.model.persistence.DBStorageType) Created(org.meveo.event.qualifier.Created) EntityManagerWrapper(org.meveo.jpa.EntityManagerWrapper) CustomFieldTypeEnum(org.meveo.model.crm.custom.CustomFieldTypeEnum) Neo4jEntity(org.meveo.persistence.neo4j.graph.Neo4jEntity) CustomFieldIndexTypeEnum(org.meveo.model.crm.custom.CustomFieldIndexTypeEnum) InternalNode(org.neo4j.driver.internal.InternalNode) Relationship(org.neo4j.driver.v1.types.Relationship) Comparator(java.util.Comparator) Collections(java.util.Collections) EntityRef(org.meveo.persistence.scheduler.EntityRef) Values(org.neo4j.driver.v1.Values) StatementResult(org.neo4j.driver.v1.StatementResult) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ELException(org.meveo.elresolver.ELException) PatternSyntaxException(java.util.regex.PatternSyntaxException) ElementNotFoundException(org.meveo.admin.exception.ElementNotFoundException) BusinessException(org.meveo.admin.exception.BusinessException) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException) RemoteAuthenticationException(org.meveo.export.RemoteAuthenticationException) InvalidCustomFieldException(org.meveo.exceptions.InvalidCustomFieldException) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) BusinessException(org.meveo.admin.exception.BusinessException) CrossStorageTransaction(org.meveo.persistence.CrossStorageTransaction) Transaction(org.neo4j.driver.v1.Transaction) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CustomFieldValue(org.meveo.model.crm.custom.CustomFieldValue) Value(org.neo4j.driver.v1.Value) Neo4jEntity(org.meveo.persistence.neo4j.graph.Neo4jEntity) Record(org.neo4j.driver.v1.Record) PersistenceActionResult(org.meveo.persistence.PersistenceActionResult) CustomRelationshipTemplate(org.meveo.model.customEntities.CustomRelationshipTemplate) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException) JpaAmpNewTx(org.meveo.jpa.JpaAmpNewTx) TransactionAttribute(javax.ejb.TransactionAttribute)

Example 4 with Values

use of org.neo4j.driver.v1.Values in project meveo by meveo-org.

the class CrossStorageService method remove.

/**
 * Remove an entity from database
 *
 * @param repository Repository
 * @param cet        Template of the entity
 * @param uuid       UUID of the entity
 * @throws BusinessException if error happens
 */
public void remove(Repository repository, CustomEntityTemplate cet, String uuid) throws BusinessException {
    if (uuid == null) {
        throw new IllegalArgumentException("Cannot remove entity by UUID without uuid");
    }
    CustomEntityInstance cei = new CustomEntityInstance();
    cei.setCet(cet);
    cei.setCetCode(cet.getCode());
    cei.setUuid(uuid);
    cei.setRepository(repository);
    var listener = customEntityTemplateService.loadCrudEventListener(cei.getCet());
    CustomEntity cetClassInstance = null;
    transaction.beginTransaction(repository);
    try {
        if (listener != null) {
            var cetClass = listener.getEntityClass();
            try {
                var values = find(repository, cet, uuid, false);
                cei = CEIUtils.fromMap(values, cet);
                cetClassInstance = CEIUtils.ceiToPojo(cei, cetClass);
                listener.preRemove(cetClassInstance);
            } catch (EntityDoesNotExistsException e) {
                e.printStackTrace();
            }
        }
        if (cet.getAvailableStorages().contains(DBStorageType.SQL)) {
            if (cet.getSqlStorageConfiguration().isStoreAsTable()) {
                final String dbTablename = SQLStorageConfiguration.getDbTablename(cet);
                customTableService.remove(repository.getSqlConfigurationCode(), cet, uuid);
            } else {
                final CustomEntityInstance customEntityInstance = customEntityInstanceService.findByUuid(cet.getCode(), uuid);
                customEntityInstanceService.remove(customEntityInstance);
            }
        }
        if (cet.getAvailableStorages().contains(DBStorageType.NEO4J)) {
            neo4jDao.removeNodeByUUID(repository.getNeo4jConfiguration().getCode(), cet.getCode(), uuid);
        }
        fileSystemService.delete(repository, cet, uuid);
        if (!(cet.getAvailableStorages().contains(DBStorageType.SQL) && !cet.getSqlStorageConfiguration().isStoreAsTable())) {
            customEntityInstanceDelete.fire(cei);
        }
        if (cetClassInstance != null) {
            listener.postRemove(cetClassInstance);
        }
        // Delete binaries
        fileSystemService.delete(repository, cet, uuid);
        transaction.commitTransaction(repository);
    } catch (Exception e) {
        transaction.rollbackTransaction(e);
        throw e;
    }
}
Also used : EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) CustomEntity(org.meveo.model.CustomEntity) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) IllegalTransitionException(org.meveo.admin.exception.IllegalTransitionException) ELException(org.meveo.elresolver.ELException) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) NonUniqueResultException(javax.persistence.NonUniqueResultException) BusinessApiException(org.meveo.api.exception.BusinessApiException) BusinessException(org.meveo.admin.exception.BusinessException) EJBException(javax.ejb.EJBException) PersistenceException(javax.persistence.PersistenceException) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException) InvalidCustomFieldException(org.meveo.exceptions.InvalidCustomFieldException) IOException(java.io.IOException)

Example 5 with Values

use of org.neo4j.driver.v1.Values in project meveo by meveo-org.

the class CrossStorageService method find.

/**
 * Retrieves entity instances
 *
 * @param repository              Repository code
 * @param cet                     Template of the entities to retrieve
 * @param paginationConfiguration Pagination and filters
 * @return list of matching entities
 * @throws EntityDoesNotExistsException if data with given id can't be found
 */
@SuppressWarnings("unchecked")
public List<Map<String, Object>> find(Repository repository, CustomEntityTemplate cet, PaginationConfiguration paginationConfiguration) throws EntityDoesNotExistsException {
    final Map<String, CustomFieldTemplate> fields = customFieldTemplateService.getCftsWithInheritedFields(cet);
    final Set<String> actualFetchFields;
    // If no pagination nor fetch fields are defined, we consider that we must fetch everything
    boolean fetchAllFields = paginationConfiguration == null || paginationConfiguration.getFetchFields() == null || paginationConfiguration.getFetchFields().isEmpty();
    if (fetchAllFields) {
        actualFetchFields = new HashSet<>(fields.keySet());
    } else {
        actualFetchFields = new HashSet<>(paginationConfiguration.getFetchFields());
    }
    final Map<String, Set<String>> subFields = extractSubFields(actualFetchFields);
    final Map<String, Object> filters = paginationConfiguration == null ? null : paginationConfiguration.getFilters();
    final List<Map<String, Object>> valuesList = new ArrayList<>();
    // In case where only graphql query is passed, we will only use it so we won't
    // fetch sql
    boolean dontFetchSql = paginationConfiguration != null && paginationConfiguration.getFilters() == null && paginationConfiguration.getGraphQlQuery() != null;
    boolean hasSqlFetchField = paginationConfiguration != null && actualFetchFields != null && actualFetchFields.stream().anyMatch(s -> customTableService.sqlCftFilter(cet, s));
    boolean hasSqlFilter = paginationConfiguration != null && paginationConfiguration.getFilters() != null && filters.keySet().stream().anyMatch(s -> customTableService.sqlCftFilter(cet, s));
    // Make sure the filters matches the fields
    if (filters != null) {
        filters.keySet().forEach(key -> {
            String[] fieldInfo = key.split(" ");
            String fieldName = fieldInfo.length == 1 ? fieldInfo[0] : fieldInfo[1];
            if (fields.get(fieldName) == null && !"uuid".equals(fieldName) && !("code".equals(fieldName) && cet.getAvailableStorages().contains(DBStorageType.SQL) && !cet.getSqlStorageConfiguration().isStoreAsTable())) {
                throw new IllegalArgumentException("Filter " + key + " does not match fields of " + cet.getCode());
            }
        });
    }
    // Collect initial data
    if (cet.getAvailableStorages() != null && cet.getAvailableStorages().contains(DBStorageType.SQL) && !dontFetchSql && (fetchAllFields || hasSqlFetchField || hasSqlFilter)) {
        PaginationConfiguration sqlPaginationConfiguration = new PaginationConfiguration(paginationConfiguration);
        sqlPaginationConfiguration.setFetchFields(List.copyOf(actualFetchFields));
        if (cet.getSqlStorageConfiguration().isStoreAsTable()) {
            final List<Map<String, Object>> values = customTableService.list(repository.getSqlConfigurationCode(), cet, sqlPaginationConfiguration);
            values.forEach(v -> replaceKeys(cet, actualFetchFields, v));
            valuesList.addAll(values);
        } else {
            final List<CustomEntityInstance> ceis = customEntityInstanceService.list(cet.getCode(), cet.isStoreAsTable(), filters, sqlPaginationConfiguration);
            final List<Map<String, Object>> values = new ArrayList<>();
            for (CustomEntityInstance cei : ceis) {
                Map<String, Object> cfValuesAsValues = cei.getCfValuesAsValues();
                final Map<String, Object> map = cfValuesAsValues == null ? new HashMap<>() : new HashMap<>(cfValuesAsValues);
                map.put("uuid", cei.getUuid());
                map.put("code", cei.getCode());
                map.put("description", cei.getDescription());
                if (!fetchAllFields) {
                    for (String k : cei.getCfValuesAsValues().keySet()) {
                        if (!actualFetchFields.contains(k)) {
                            map.remove(k);
                        }
                    }
                }
                values.add(map);
            }
            valuesList.addAll(values);
        }
    }
    if (cet.getAvailableStorages() != null && cet.getAvailableStorages().contains(DBStorageType.NEO4J)) {
        String graphQlQuery;
        Map<String, Object> graphQlVariables = new HashMap<>();
        // Find by graphql if query provided
        if (paginationConfiguration != null && paginationConfiguration.getGraphQlQuery() != null) {
            graphQlQuery = paginationConfiguration.getGraphQlQuery();
            graphQlQuery = graphQlQuery.replaceAll("([\\w)]\\s*\\{)(\\s*\\w*)", "$1meveo_uuid,$2");
        } else {
            graphQlQuery = generateGraphQlFromPagination(cet.getCode(), paginationConfiguration, actualFetchFields, filters, subFields).toString();
        }
        // Check if filters contains a field not stored in Neo4J
        var dontFilterOnNeo4J = filters != null && filters.keySet().stream().anyMatch(f -> fields.get(f) != null && !fields.get(f).getStorages().contains(DBStorageType.NEO4J));
        Map<String, Object> result = null;
        if (!dontFilterOnNeo4J && repository.getNeo4jConfiguration() != null) {
            result = neo4jDao.executeGraphQLQuery(repository.getNeo4jConfiguration().getCode(), graphQlQuery, null, null);
        }
        if (result != null) {
            List<Map<String, Object>> values = (List<Map<String, Object>>) result.get(cet.getCode());
            values = values != null ? values : new ArrayList<>();
            values.forEach(map -> {
                final HashMap<String, Object> resultMap = new HashMap<>(map);
                map.forEach((key, mapValue) -> {
                    if (!key.equals("uuid") && !key.equals("meveo_uuid") && actualFetchFields != null && !actualFetchFields.contains(key)) {
                        resultMap.remove(key);
                    }
                    // and optionally "meveo_uuid" attribute)
                    if (mapValue instanceof Map && ((Map<?, ?>) mapValue).size() == 1 && ((Map<?, ?>) mapValue).containsKey("value")) {
                        Object value = ((Map<?, ?>) mapValue).get("value");
                        resultMap.put(key, value);
                    } else if (mapValue instanceof Map && ((Map<?, ?>) mapValue).size() == 2 && ((Map<?, ?>) mapValue).containsKey("value") && ((Map<?, ?>) mapValue).containsKey("meveo_uuid")) {
                        Object value = ((Map<?, ?>) mapValue).get("value");
                        resultMap.put(key, value);
                    }
                });
                // Rewrite "meveo_uuid" to "uuid"
                if (resultMap.get("meveo_uuid") != null) {
                    resultMap.put("uuid", resultMap.remove("meveo_uuid"));
                }
                // merge the values from sql and neo4j
                // valuesList.add(resultMap);
                mergeData(valuesList, resultMap);
            });
        }
    }
    // Complete missing data
    for (Map<String, Object> data : valuesList) {
        String uuid = (String) (data.get("uuid") != null ? data.get("uuid") : data.get("meveo_uuid"));
        final Map<String, Object> missingData = getMissingData(data, repository, cet, uuid, actualFetchFields, subFields);
        if (missingData != null) {
            data.putAll(missingData);
        }
    }
    return valuesList.stream().map(values -> deserializeData(values, fields.values())).collect(Collectors.toList());
}
Also used : IllegalTransitionException(org.meveo.admin.exception.IllegalTransitionException) ELException(org.meveo.elresolver.ELException) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) CustomRelationshipTemplate(org.meveo.model.customEntities.CustomRelationshipTemplate) CustomEntity(org.meveo.model.CustomEntity) NonUniqueResultException(javax.persistence.NonUniqueResultException) Repository(org.meveo.model.storage.Repository) SQLStorageConfiguration(org.meveo.model.persistence.sql.SQLStorageConfiguration) Map(java.util.Map) FileSystemService(org.meveo.service.storage.FileSystemService) BusinessApiException(org.meveo.api.exception.BusinessApiException) PasswordUtils(org.meveo.security.PasswordUtils) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) Objects(java.util.Objects) EJBException(javax.ejb.EJBException) CustomEntityInstanceService(org.meveo.service.custom.CustomEntityInstanceService) List(java.util.List) CustomFieldStorageTypeEnum(org.meveo.model.crm.custom.CustomFieldStorageTypeEnum) PersistenceException(javax.persistence.PersistenceException) CustomFieldsCacheContainerProvider(org.meveo.cache.CustomFieldsCacheContainerProvider) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) CustomTableService(org.meveo.service.custom.CustomTableService) EntityReferenceWrapper(org.meveo.model.crm.EntityReferenceWrapper) CustomEntityTemplateService(org.meveo.service.custom.CustomEntityTemplateService) HashMap(java.util.HashMap) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) Updated(org.meveo.event.qualifier.Updated) CustomFieldValues(org.meveo.model.crm.custom.CustomFieldValues) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) JacksonUtil(org.meveo.model.persistence.JacksonUtil) Removed(org.meveo.event.qualifier.Removed) CustomFieldTemplateService(org.meveo.service.crm.impl.CustomFieldTemplateService) CustomTableRelationService(org.meveo.service.custom.CustomTableRelationService) CollectionUtils(org.apache.commons.collections.CollectionUtils) CEIUtils(org.meveo.model.persistence.CEIUtils) Neo4jDao(org.meveo.persistence.neo4j.base.Neo4jDao) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) Neo4jService(org.meveo.persistence.neo4j.service.Neo4jService) GraphQLQueryBuilder(org.meveo.persistence.graphql.GraphQLQueryBuilder) Event(javax.enterprise.event.Event) InvalidCustomFieldException(org.meveo.exceptions.InvalidCustomFieldException) Logger(org.slf4j.Logger) DBStorageType(org.meveo.model.persistence.DBStorageType) Created(org.meveo.event.qualifier.Created) IOException(java.io.IOException) CustomFieldInstanceService(org.meveo.service.crm.impl.CustomFieldInstanceService) File(java.io.File) CustomFieldTypeEnum(org.meveo.model.crm.custom.CustomFieldTypeEnum) Collections(java.util.Collections) EntityRef(org.meveo.persistence.scheduler.EntityRef) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance)

Aggregations

HashMap (java.util.HashMap)22 NoSuchRecordException (org.neo4j.driver.v1.exceptions.NoSuchRecordException)22 BusinessException (org.meveo.admin.exception.BusinessException)14 ELException (org.meveo.elresolver.ELException)14 InvalidCustomFieldException (org.meveo.exceptions.InvalidCustomFieldException)14 StatementResult (org.neo4j.driver.v1.StatementResult)14 HashSet (java.util.HashSet)13 Map (java.util.Map)13 StrSubstitutor (org.apache.commons.lang3.text.StrSubstitutor)13 Set (java.util.Set)12 Collection (java.util.Collection)11 Collectors (java.util.stream.Collectors)11 Collections (java.util.Collections)10 List (java.util.List)10 Event (javax.enterprise.event.Event)10 Inject (javax.inject.Inject)10 Created (org.meveo.event.qualifier.Created)10 Removed (org.meveo.event.qualifier.Removed)10 Updated (org.meveo.event.qualifier.Updated)10 CustomFieldTemplate (org.meveo.model.crm.CustomFieldTemplate)10