Search in sources :

Example 1 with AssociationNode

use of io.vertigo.dynamo.domain.metamodel.association.AssociationNode in project vertigo by KleeGroup.

the class AnnotationWriter method writeSimpleAssociationAnnotation.

/**
 * Ectiture des annotations sur un DT_FIELD gérant une association.
 *
 * @param associationSimple Definition de l'association
 * @return Liste des lignes de code java à ajouter.
 */
List<String> writeSimpleAssociationAnnotation(final AssociationSimpleDefinition associationSimple) {
    final AssociationNode primaryNode = associationSimple.getPrimaryAssociationNode();
    final AssociationNode foreignNode = associationSimple.getForeignAssociationNode();
    final String primaryMultiplicity = AssociationUtil.getMultiplicity(primaryNode.isNotNull(), primaryNode.isMultiple());
    final String foreignMultiplipicity = AssociationUtil.getMultiplicity(foreignNode.isNotNull(), foreignNode.isMultiple());
    return new ListBuilder<String>().add("@" + Association.class.getCanonicalName() + "(").add(INDENT + "name = \"" + associationSimple.getName() + "\",").add(INDENT + "fkFieldName = \"" + associationSimple.getFKField().getName() + "\",").add(INDENT + "primaryDtDefinitionName = \"" + primaryNode.getDtDefinition().getName() + "\",").add(INDENT + "primaryIsNavigable = " + primaryNode.isNavigable() + ',').add(INDENT + "primaryRole = \"" + primaryNode.getRole() + "\",").add(INDENT + "primaryLabel = \"" + primaryNode.getLabel() + "\",").add(INDENT + "primaryMultiplicity = \"" + primaryMultiplicity + "\",").add(INDENT + "foreignDtDefinitionName = \"" + foreignNode.getDtDefinition().getName() + "\",").add(INDENT + "foreignIsNavigable = " + foreignNode.isNavigable() + ',').add(INDENT + "foreignRole = \"" + foreignNode.getRole() + "\",").add(INDENT + "foreignLabel = \"" + foreignNode.getLabel() + "\",").add(INDENT + "foreignMultiplicity = \"" + foreignMultiplipicity + "\")").build();
}
Also used : ListBuilder(io.vertigo.util.ListBuilder) AssociationNode(io.vertigo.dynamo.domain.metamodel.association.AssociationNode)

Example 2 with AssociationNode

use of io.vertigo.dynamo.domain.metamodel.association.AssociationNode in project vertigo by KleeGroup.

the class AnnotationWriter method writeNNAssociationAnnotation.

/**
 * Ectiture des annotations sur un DT_FIELD gérant une association.
 *
 * @param associationNN Definition de l'association
 * @return Liste des lignes de code java à ajouter.
 */
List<String> writeNNAssociationAnnotation(final AssociationNNDefinition associationNN) {
    final AssociationNode nodeA = associationNN.getAssociationNodeA();
    final AssociationNode nodeB = associationNN.getAssociationNodeB();
    return new ListBuilder<String>().add("@" + AssociationNN.class.getCanonicalName() + "(").add(INDENT + "name = \"" + associationNN.getName() + "\",").add(INDENT + "tableName = \"" + associationNN.getTableName() + "\",").add(INDENT + "dtDefinitionA = \"" + nodeA.getDtDefinition().getName() + "\",").add(INDENT + "dtDefinitionB = \"" + nodeB.getDtDefinition().getName() + "\",").add(INDENT + "navigabilityA = " + nodeA.isNavigable() + ',').add(INDENT + "navigabilityB = " + nodeB.isNavigable() + ',').add(INDENT + "roleA = \"" + nodeA.getRole() + "\",").add(INDENT + "roleB = \"" + nodeB.getRole() + "\",").add(INDENT + "labelA = \"" + nodeA.getLabel() + "\",").add(INDENT + "labelB = \"" + nodeB.getLabel() + "\")").build();
}
Also used : ListBuilder(io.vertigo.util.ListBuilder) AssociationNode(io.vertigo.dynamo.domain.metamodel.association.AssociationNode)

Example 3 with AssociationNode

use of io.vertigo.dynamo.domain.metamodel.association.AssociationNode in project vertigo by KleeGroup.

the class JpaDataStorePlugin method doFindAll.

private <E extends Entity> DtList<E> doFindAll(final ProcessAnalyticsTracer tracer, final DtDefinition dtDefinition, final DtListURIForNNAssociation dtcUri) {
    final Class<E> resultClass = (Class<E>) ClassUtil.classForName(dtDefinition.getClassCanonicalName());
    // PK de la DtList recherchée
    final String idFieldName = dtDefinition.getIdField().get().getName();
    // FK dans la table nn correspondant à la collection recherchée. (clé de jointure ).
    final AssociationNNDefinition associationNNDefinition = dtcUri.getAssociationDefinition();
    final String joinTableName = associationNNDefinition.getTableName();
    final DtDefinition joinDtDefinition = AssociationUtil.getAssociationNode(associationNNDefinition, dtcUri.getRoleName()).getDtDefinition();
    final DtField joinDtField = joinDtDefinition.getIdField().get();
    // La condition s'applique sur l'autre noeud de la relation (par rapport à la collection attendue)
    final AssociationNode associationNode = AssociationUtil.getAssociationNodeTarget(associationNNDefinition, dtcUri.getRoleName());
    final DtField fkField = associationNode.getDtDefinition().getIdField().get();
    final String fkFieldName = fkField.getName();
    final String request = new StringBuilder(" select t.* from ").append(dtDefinition.getLocalName()).append(" t").append(" join ").append(joinTableName).append(" j on j.").append(joinDtField.getName()).append(" = t.").append(idFieldName).append(" where j.").append(fkFieldName).append(" = :").append(fkFieldName).toString();
    final URI uri = dtcUri.getSource();
    final Query q = getEntityManager().createNativeQuery(request, resultClass);
    q.setParameter(fkFieldName, uri.getId());
    final List<E> results = q.getResultList();
    final DtList<E> dtc = new DtList<>(dtDefinition);
    dtc.addAll(results);
    tracer.setMeasure("nbSelectedRow", dtc.size());
    return dtc;
}
Also used : TypedQuery(javax.persistence.TypedQuery) Query(javax.persistence.Query) DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) URI(io.vertigo.dynamo.domain.model.URI) AssociationNNDefinition(io.vertigo.dynamo.domain.metamodel.association.AssociationNNDefinition) DtField(io.vertigo.dynamo.domain.metamodel.DtField) AssociationNode(io.vertigo.dynamo.domain.metamodel.association.AssociationNode) DtList(io.vertigo.dynamo.domain.model.DtList)

Example 4 with AssociationNode

use of io.vertigo.dynamo.domain.metamodel.association.AssociationNode in project vertigo by KleeGroup.

the class DomainDynamicRegistry method createAssociationSimpleDefinition.

private AssociationSimpleDefinition createAssociationSimpleDefinition(final DefinitionSpace definitionSpace, final DslDefinition xassociation) {
    final String multiplicityA = (String) xassociation.getPropertyValue(KspProperty.MULTIPLICITY_A);
    final String multiplicityB = (String) xassociation.getPropertyValue(KspProperty.MULTIPLICITY_B);
    // Vérification que l'on est bien dans le cas d'une association simple de type 1-n
    if (AssociationUtil.isMultiple(multiplicityB) && AssociationUtil.isMultiple(multiplicityA)) {
        // Relation n-n
        throw new IllegalArgumentException("Utiliser la déclaration AssociationNN");
    }
    if (!AssociationUtil.isMultiple(multiplicityB) && !AssociationUtil.isMultiple(multiplicityA)) {
        // Relation 1-1
        throw new IllegalArgumentException("Les associations 1-1 sont interdites");
    }
    final String fkFieldName = (String) xassociation.getPropertyValue(KspProperty.FK_FIELD_NAME);
    final DtDefinition dtDefinitionA = definitionSpace.resolve(xassociation.getDefinitionLinkName("dtDefinitionA"), DtDefinition.class);
    final boolean navigabilityA = (Boolean) xassociation.getPropertyValue(KspProperty.NAVIGABILITY_A);
    final String roleA = (String) xassociation.getPropertyValue(KspProperty.ROLE_A);
    final String labelA = (String) xassociation.getPropertyValue(KspProperty.LABEL_A);
    final DtDefinition dtDefinitionB = definitionSpace.resolve(xassociation.getDefinitionLinkName("dtDefinitionB"), DtDefinition.class);
    final boolean navigabilityB = (Boolean) xassociation.getPropertyValue(KspProperty.NAVIGABILITY_B);
    final String roleB = (String) xassociation.getPropertyValue(KspProperty.ROLE_B);
    final String labelB = (String) xassociation.getPropertyValue(KspProperty.LABEL_B);
    // Relation 1-n ou 1-1
    final String urn = fixAssociationName(ASSOCIATION_SIMPLE_DEFINITION_PREFIX, xassociation.getName());
    final AssociationNode associationNodeA = new AssociationNode(dtDefinitionA, navigabilityA, roleA, labelA, AssociationUtil.isMultiple(multiplicityA), AssociationUtil.isNotNull(multiplicityA));
    final AssociationNode associationNodeB = new AssociationNode(dtDefinitionB, navigabilityB, roleB, labelB, AssociationUtil.isMultiple(multiplicityB), AssociationUtil.isNotNull(multiplicityB));
    final AssociationSimpleDefinition associationSimpleDefinition = new AssociationSimpleDefinition(urn, fkFieldName, associationNodeA, associationNodeB);
    final AssociationNode primaryAssociationNode = associationSimpleDefinition.getPrimaryAssociationNode();
    final AssociationNode foreignAssociationNode = associationSimpleDefinition.getForeignAssociationNode();
    final DtDefinition fkDefinition = primaryAssociationNode.getDtDefinition();
    LOGGER.trace("" + xassociation.getName() + " : ajout d'une FK [" + fkFieldName + "] sur la table '" + foreignAssociationNode.getDtDefinition().getName() + "'");
    final String label = primaryAssociationNode.getLabel();
    dtDefinitionBuilders.get(foreignAssociationNode.getDtDefinition().getName()).addForeignKey(fkFieldName, label, fkDefinition.getIdField().get().getDomain(), primaryAssociationNode.isNotNull(), fkDefinition.getName());
    return associationSimpleDefinition;
}
Also used : DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) AssociationSimpleDefinition(io.vertigo.dynamo.domain.metamodel.association.AssociationSimpleDefinition) AssociationNode(io.vertigo.dynamo.domain.metamodel.association.AssociationNode)

Example 5 with AssociationNode

use of io.vertigo.dynamo.domain.metamodel.association.AssociationNode in project vertigo by KleeGroup.

the class SqlDataStorePlugin method findAll.

/**
 * {@inheritDoc}
 */
@Override
public <E extends Entity> DtList<E> findAll(final DtDefinition dtDefinition, final DtListURIForNNAssociation dtcUri) {
    Assertion.checkNotNull(dtDefinition);
    Assertion.checkNotNull(dtcUri);
    // -----
    final String tableName = getTableName(dtDefinition);
    final String taskName = TASK.TK_SELECT + "_N_N_LIST_" + tableName + "_BY_URI";
    // PK de la DtList recherchée
    final String idFieldName = getIdField(dtDefinition).getName();
    // FK dans la table nn correspondant à la collection recherchée. (clé de jointure ).
    final AssociationNNDefinition associationNNDefinition = dtcUri.getAssociationDefinition();
    final String joinTableName = associationNNDefinition.getTableName();
    final DtDefinition joinDtDefinition = AssociationUtil.getAssociationNode(associationNNDefinition, dtcUri.getRoleName()).getDtDefinition();
    final DtField joinDtField = getIdField(joinDtDefinition);
    // La condition s'applique sur l'autre noeud de la relation (par rapport à la collection attendue)
    final AssociationNode associationNode = AssociationUtil.getAssociationNodeTarget(associationNNDefinition, dtcUri.getRoleName());
    final DtField fkField = getIdField(associationNode.getDtDefinition());
    final String fkFieldName = fkField.getName();
    final String request = new StringBuilder(" select t.* from ").append(tableName).append(" t").append(" join ").append(joinTableName).append(" j on j.").append(joinDtField.getName()).append(" = t.").append(idFieldName).append(" where j.").append(fkFieldName).append(" = #").append(fkFieldName).append('#').toString();
    final TaskDefinition taskDefinition = TaskDefinition.builder(taskName).withEngine(TaskEngineSelect.class).withDataSpace(dataSpace).withRequest(request).addInRequired(fkFieldName, fkField.getDomain()).withOutRequired("dtc", Home.getApp().getDefinitionSpace().resolve(DOMAIN_PREFIX + SEPARATOR + dtDefinition.getName() + "_DTC", Domain.class)).build();
    final URI uri = dtcUri.getSource();
    final Task task = Task.builder(taskDefinition).addValue(fkFieldName, uri.getId()).build();
    return taskManager.execute(task).getResult();
}
Also used : TaskDefinition(io.vertigo.dynamo.task.metamodel.TaskDefinition) Task(io.vertigo.dynamo.task.model.Task) TaskEngineSelect(io.vertigo.dynamox.task.TaskEngineSelect) DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) URI(io.vertigo.dynamo.domain.model.URI) AssociationNNDefinition(io.vertigo.dynamo.domain.metamodel.association.AssociationNNDefinition) DtField(io.vertigo.dynamo.domain.metamodel.DtField) AssociationNode(io.vertigo.dynamo.domain.metamodel.association.AssociationNode)

Aggregations

AssociationNode (io.vertigo.dynamo.domain.metamodel.association.AssociationNode)6 DtDefinition (io.vertigo.dynamo.domain.metamodel.DtDefinition)4 AssociationNNDefinition (io.vertigo.dynamo.domain.metamodel.association.AssociationNNDefinition)3 DtField (io.vertigo.dynamo.domain.metamodel.DtField)2 URI (io.vertigo.dynamo.domain.model.URI)2 ListBuilder (io.vertigo.util.ListBuilder)2 AssociationSimpleDefinition (io.vertigo.dynamo.domain.metamodel.association.AssociationSimpleDefinition)1 DtList (io.vertigo.dynamo.domain.model.DtList)1 TaskDefinition (io.vertigo.dynamo.task.metamodel.TaskDefinition)1 Task (io.vertigo.dynamo.task.model.Task)1 TaskEngineSelect (io.vertigo.dynamox.task.TaskEngineSelect)1 Query (javax.persistence.Query)1 TypedQuery (javax.persistence.TypedQuery)1