Search in sources :

Example 1 with DtListDelta

use of io.vertigo.vega.webservice.model.DtListDelta in project vertigo by KleeGroup.

the class ValidatorWebServiceHandlerPlugin method validateParam.

private static void validateParam(final Object value, final UiMessageStack uiMessageStack, final WebServiceParam webServiceParam, final WebServiceCallContext routeContext) {
    final Map<String, DtObject> contextKeyMap = new HashMap<>();
    if (value instanceof UiObject) {
        final UiObject<DtObject> uiObject = (UiObject<DtObject>) value;
        final List<DtObjectValidator<DtObject>> dtObjectValidators = obtainDtObjectValidators(webServiceParam);
        // Only authorized fields have already been checked (JsonConverterHandler)
        final DtObject updatedDto = uiObject.mergeAndCheckInput(dtObjectValidators, uiMessageStack);
        contextKeyMap.put(uiObject.getInputKey(), updatedDto);
        routeContext.registerUpdatedDtObjects(webServiceParam, updatedDto, contextKeyMap);
    } else if (value instanceof UiListDelta) {
        final UiListDelta<DtObject> uiListDelta = (UiListDelta<DtObject>) value;
        final List<DtObjectValidator<DtObject>> dtObjectValidators = obtainDtObjectValidators(webServiceParam);
        // Only authorized fields have already been checked (JsonConverterHandler)
        final DtList<DtObject> dtListCreates = mergeAndCheckInput(uiListDelta.getObjectType(), uiListDelta.getCreatesMap(), dtObjectValidators, uiMessageStack, contextKeyMap);
        final DtList<DtObject> dtListUpdates = mergeAndCheckInput(uiListDelta.getObjectType(), uiListDelta.getUpdatesMap(), dtObjectValidators, uiMessageStack, contextKeyMap);
        final DtList<DtObject> dtListDeletes = mergeAndCheckInput(uiListDelta.getObjectType(), uiListDelta.getDeletesMap(), dtObjectValidators, uiMessageStack, contextKeyMap);
        final DtListDelta<DtObject> dtListDelta = new DtListDelta<>(dtListCreates, dtListUpdates, dtListDeletes);
        routeContext.registerUpdatedDtObjects(webServiceParam, dtListDelta, contextKeyMap);
    } else if (value instanceof UiListModifiable) {
        final UiListModifiable<DtObject> uiList = (UiListModifiable<DtObject>) value;
        final List<DtObjectValidator<DtObject>> dtObjectValidators = obtainDtObjectValidators(webServiceParam);
        // Only authorized fields have already been checked (JsonConverterHandler)
        final DtList<DtObject> dtList = mergeAndCheckInput(uiList.getObjectType(), uiList, dtObjectValidators, uiMessageStack, contextKeyMap);
        routeContext.registerUpdatedDtObjects(webServiceParam, dtList, contextKeyMap);
    } else if (value instanceof ExtendedObject) {
        final ExtendedObject<?> extendedObject = (ExtendedObject) value;
        validateParam(extendedObject.getInnerObject(), uiMessageStack, webServiceParam, routeContext);
        final Object updatedValue = routeContext.getParamValue(webServiceParam);
        final ExtendedObject<?> updatedExtendedObject = new ExtendedObject(updatedValue);
        updatedExtendedObject.putAll(extendedObject);
        routeContext.setParamValue(webServiceParam, updatedExtendedObject);
    } else if (value instanceof Optional && ((Optional) value).isPresent()) {
        validateParam(((Optional) value).get(), uiMessageStack, webServiceParam, routeContext);
    }
}
Also used : UiListDelta(io.vertigo.vega.engines.webservice.json.UiListDelta) UiObject(io.vertigo.vega.webservice.model.UiObject) Optional(java.util.Optional) HashMap(java.util.HashMap) DtObject(io.vertigo.dynamo.domain.model.DtObject) DtObjectValidator(io.vertigo.vega.webservice.validation.DtObjectValidator) UiListModifiable(io.vertigo.vega.engines.webservice.json.UiListModifiable) DtListDelta(io.vertigo.vega.webservice.model.DtListDelta) ExtendedObject(io.vertigo.vega.webservice.model.ExtendedObject) DtList(io.vertigo.dynamo.domain.model.DtList) ArrayList(java.util.ArrayList) List(java.util.List) ExtendedObject(io.vertigo.vega.webservice.model.ExtendedObject) DtObject(io.vertigo.dynamo.domain.model.DtObject) UiObject(io.vertigo.vega.webservice.model.UiObject) DtList(io.vertigo.dynamo.domain.model.DtList)

Example 2 with DtListDelta

use of io.vertigo.vega.webservice.model.DtListDelta in project vertigo by KleeGroup.

the class GoogleJsonEngine method uiContextFromJson.

/**
 * {@inheritDoc}
 */
@Override
public UiContext uiContextFromJson(final String json, final Map<String, Type> paramTypes) {
    final UiContext result = new UiContext();
    try {
        final JsonElement jsonElement = new JsonParser().parse(json);
        final JsonObject jsonObject = jsonElement.getAsJsonObject();
        for (final Entry<String, Type> entry : paramTypes.entrySet()) {
            final String key = entry.getKey();
            final Type paramType = entry.getValue();
            final JsonElement jsonSubElement = jsonObject.get(key);
            final Serializable value;
            if (WebServiceTypeUtil.isAssignableFrom(DtObject.class, paramType)) {
                final Type typeOfDest = new KnownParameterizedType(UiObject.class, paramType);
                value = gson.fromJson(jsonSubElement, typeOfDest);
            } else if (WebServiceTypeUtil.isAssignableFrom(DtListDelta.class, paramType)) {
                // we known that DtListDelta has one parameterized type
                final Class<DtObject> dtoClass = (Class<DtObject>) ((ParameterizedType) paramType).getActualTypeArguments()[0];
                final Type typeOfDest = new KnownParameterizedType(UiListDelta.class, dtoClass);
                value = gson.fromJson(jsonSubElement, typeOfDest);
            } else if (WebServiceTypeUtil.isAssignableFrom(DtList.class, paramType)) {
                // we known that DtList has one parameterized type
                final Class<DtObject> dtoClass = (Class<DtObject>) ((ParameterizedType) paramType).getActualTypeArguments()[0];
                final Type typeOfDest = new KnownParameterizedType(UiListModifiable.class, dtoClass);
                value = gson.fromJson(jsonSubElement, typeOfDest);
            } else {
                value = gson.fromJson(jsonSubElement, paramType);
            }
            result.put(key, value);
        }
        return result;
    } catch (final IllegalStateException e) {
        throw new JsonSyntaxException("JsonObject expected", e);
    }
}
Also used : Serializable(java.io.Serializable) DtObject(io.vertigo.dynamo.domain.model.DtObject) JsonObject(com.google.gson.JsonObject) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) FieldType(io.vertigo.dynamo.domain.metamodel.DtField.FieldType) JsonSyntaxException(com.google.gson.JsonSyntaxException) DtListDelta(io.vertigo.vega.webservice.model.DtListDelta) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Aggregations

DtObject (io.vertigo.dynamo.domain.model.DtObject)2 DtListDelta (io.vertigo.vega.webservice.model.DtListDelta)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 FieldType (io.vertigo.dynamo.domain.metamodel.DtField.FieldType)1 DtList (io.vertigo.dynamo.domain.model.DtList)1 UiListDelta (io.vertigo.vega.engines.webservice.json.UiListDelta)1 UiListModifiable (io.vertigo.vega.engines.webservice.json.UiListModifiable)1 ExtendedObject (io.vertigo.vega.webservice.model.ExtendedObject)1 UiObject (io.vertigo.vega.webservice.model.UiObject)1 DtObjectValidator (io.vertigo.vega.webservice.validation.DtObjectValidator)1 Serializable (java.io.Serializable)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1