Search in sources :

Example 86 with DtDefinition

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

the class DtObjectUtil method createURI.

/**
 * Récupération d'une URI de DTO.
 * On récupère l'URI d'un DTO référencé par une association.
 * Il est nécessaire que l'association soit simple.
 * Si l'association est multiple on ne récupère pas une URI mais une DtListURI, c'est à dire le pointeur vers une liste.
 *
 *  On recherche une URI correspondant à une association.
 *  Exemple : Une Commande possède un bénéficiaire.
 *  Dans cetexemple on recherche l'URI du bénéficiaire à partir de l'objet commande.
 * @param <E>
 *
 * @param associationDefinitionName Nom de la définition d'une association
 * @param dto  Object
 * @param dtoTargetClass Class of entity of this association
 * @return dto du DTO relié via l'association au dto passé en paramètre (Nullable)
 */
public static <E extends Entity> URI<E> createURI(final DtObject dto, final String associationDefinitionName, final Class<E> dtoTargetClass) {
    Assertion.checkNotNull(associationDefinitionName);
    Assertion.checkNotNull(dto);
    Assertion.checkNotNull(dtoTargetClass);
    // -----
    final AssociationSimpleDefinition associationSimpleDefinition = Home.getApp().getDefinitionSpace().resolve(associationDefinitionName, AssociationSimpleDefinition.class);
    // 1. On recherche le nom du champ portant l'objet référencé (Exemple : personne)
    final DtDefinition dtDefinition = associationSimpleDefinition.getPrimaryAssociationNode().getDtDefinition();
    // 2. On calcule le nom de la fk.
    final DtField fkField = associationSimpleDefinition.getFKField();
    // 3. On calcule l'URI de la clé étrangère
    final Object id = fkField.getDataAccessor().getValue(dto);
    if (id == null) {
        return null;
    }
    return new URI(dtDefinition, id);
}
Also used : DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) DtObject(io.vertigo.dynamo.domain.model.DtObject) AssociationSimpleDefinition(io.vertigo.dynamo.domain.metamodel.association.AssociationSimpleDefinition) URI(io.vertigo.dynamo.domain.model.URI) DtField(io.vertigo.dynamo.domain.metamodel.DtField)

Example 87 with DtDefinition

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

the class DtObjectUtil method createURI.

/**
 * Creates an URI from an existing object.
 * @param entity Object
 * @return this object URI
 */
public static <E extends Entity> URI<E> createURI(final E entity) {
    Assertion.checkNotNull(entity);
    // -----
    final DtDefinition dtDefinition = findDtDefinition(entity);
    return new URI<>(dtDefinition, DtObjectUtil.getId(entity));
}
Also used : DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) URI(io.vertigo.dynamo.domain.model.URI)

Example 88 with DtDefinition

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

the class DtObjectUtil method getId.

/**
 * Returns the 'id' of a 'DtObject'.
 * @param entity the entity
 * @return the id of the specified 'DtObject'
 */
public static Object getId(final Entity entity) {
    Assertion.checkNotNull(entity);
    // -----
    final DtDefinition dtDefinition = findDtDefinition(entity);
    final DtField idField = dtDefinition.getIdField().get();
    return idField.getDataAccessor().getValue(entity);
}
Also used : DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) DtField(io.vertigo.dynamo.domain.metamodel.DtField)

Example 89 with DtDefinition

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

the class URI method fromURN.

// =========================================================================
// =============================STATIC======================================
// =========================================================================
/**
 * Parse URI from URN.
 * @param urn URN to parse
 * @return URI to result
 */
public static URI<?> fromURN(final String urn) {
    Assertion.checkNotNull(urn);
    // -----
    final int i = urn.indexOf(D2A_SEPARATOR);
    final String dname = urn.substring(0, i);
    final Object id = stringToId(urn.substring(i + 1));
    // On ne type pas, la seule chose que l'on sait est qu'il s'agit d'une définition.
    final DtDefinition definition = Home.getApp().getDefinitionSpace().resolve(dname, DtDefinition.class);
    return new URI(definition, id);
}
Also used : DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition)

Example 90 with DtDefinition

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

the class UiObjectDeserializer method deserialize.

/**
 * {@inheritDoc}
 */
@Override
public UiObject<D> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) {
    final Type[] typeParameters = ((ParameterizedType) typeOfT).getActualTypeArguments();
    // Id has only one parameterized type T
    final Class<D> dtoClass = (Class<D>) typeParameters[0];
    final JsonObject jsonObject = json.getAsJsonObject();
    final D inputDto = context.deserialize(jsonObject, dtoClass);
    final DtDefinition dtDefinition = DtObjectUtil.findDtDefinition(dtoClass);
    final Set<String> dtFields = getFieldNames(dtDefinition);
    final Set<String> modifiedFields = new HashSet<>();
    for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
        final String fieldName = entry.getKey();
        if (dtFields.contains(fieldName)) {
            // we only keep fields of this dtObject
            modifiedFields.add(fieldName);
        }
    }
    // Send a alert if no fields match the DtObject ones : details may be a security issue ?
    if (modifiedFields.isEmpty()) {
        final Set<String> jsonEntry = new HashSet<>();
        for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            jsonEntry.add(entry.getKey());
        }
        throw new JsonSyntaxException("Received Json's fields doesn't match " + dtoClass.getSimpleName() + " ones : " + jsonEntry);
    }
    final UiObject<D> uiObject = new VegaUiObject<>(inputDto, modifiedFields);
    if (jsonObject.has(JsonEngine.SERVER_SIDE_TOKEN_FIELDNAME)) {
        uiObject.setServerSideToken(jsonObject.get(JsonEngine.SERVER_SIDE_TOKEN_FIELDNAME).getAsString());
    }
    return uiObject;
}
Also used : DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) JsonObject(com.google.gson.JsonObject) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) HashSet(java.util.HashSet)

Aggregations

DtDefinition (io.vertigo.dynamo.domain.metamodel.DtDefinition)91 DtField (io.vertigo.dynamo.domain.metamodel.DtField)29 Test (org.junit.Test)21 URI (io.vertigo.dynamo.domain.model.URI)18 List (java.util.List)17 ArrayList (java.util.ArrayList)16 Assertion (io.vertigo.lang.Assertion)12 Collectors (java.util.stream.Collectors)11 Home (io.vertigo.app.Home)10 Optional (java.util.Optional)10 Domain (io.vertigo.dynamo.domain.metamodel.Domain)9 Collections (java.util.Collections)8 DefinitionUtil (io.vertigo.core.definition.DefinitionUtil)7 DtObjectUtil (io.vertigo.dynamo.domain.util.DtObjectUtil)7 TaskDefinition (io.vertigo.dynamo.task.metamodel.TaskDefinition)7 Inject (javax.inject.Inject)7 Authorization (io.vertigo.account.authorization.metamodel.Authorization)6 SecuredEntity (io.vertigo.account.authorization.metamodel.SecuredEntity)6 Criterions (io.vertigo.dynamo.criteria.Criterions)6 DefinitionSpace (io.vertigo.core.definition.DefinitionSpace)5