Search in sources :

Example 21 with DtObject

use of io.vertigo.dynamo.domain.model.DtObject in project vertigo by KleeGroup.

the class AdvancedTestWebServices method createFilterFunction.

private <C extends DtObject, E extends Entity> Predicate<E> createFilterFunction(final C criteria, final Class<E> resultClass) {
    Predicate<E> filter = (o) -> true;
    final DtDefinition criteriaDefinition = DtObjectUtil.findDtDefinition(criteria);
    final DtDefinition resultDefinition = DtObjectUtil.findDtDefinition(resultClass);
    final Set<String> alreadyAddedField = new HashSet<>();
    for (final DtField field : criteriaDefinition.getFields()) {
        final String fieldName = field.getName();
        if (!alreadyAddedField.contains(fieldName)) {
            // when we consume two fields at once (min;max)
            final Object value = field.getDataAccessor().getValue(criteria);
            if (value != null) {
                if (fieldName.endsWith("_MIN") || fieldName.endsWith("_MAX")) {
                    final String filteredField = fieldName.substring(0, fieldName.length() - "_MIN".length());
                    final DtField resultDtField = resultDefinition.getField(filteredField);
                    final DtField minField = fieldName.endsWith("_MIN") ? field : criteriaDefinition.getField(filteredField + "_MIN");
                    final DtField maxField = fieldName.endsWith("_MAX") ? field : criteriaDefinition.getField(filteredField + "_MAX");
                    final Serializable minValue = (Serializable) minField.getDataAccessor().getValue(criteria);
                    final Serializable maxValue = (Serializable) maxField.getDataAccessor().getValue(criteria);
                    filter = filter.and(Criterions.isBetween(() -> resultDtField.getName(), CriterionLimit.ofIncluded(minValue), CriterionLimit.ofExcluded(maxValue)).toPredicate());
                } else {
                    final Predicate predicate = Criterions.isEqualTo(() -> fieldName, Serializable.class.cast(value)).toPredicate();
                    filter.and(predicate);
                }
            }
        }
    // si null, alors on ne filtre pas
    }
    return filter;
}
Also used : Arrays(java.util.Arrays) ServerSideSave(io.vertigo.vega.webservice.stereotype.ServerSideSave) URL(java.net.URL) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) POST(io.vertigo.vega.webservice.stereotype.POST) Doc(io.vertigo.vega.webservice.stereotype.Doc) CriterionLimit(io.vertigo.dynamo.criteria.CriterionLimit) PathParam(io.vertigo.vega.webservice.stereotype.PathParam) ExtendedObject(io.vertigo.vega.webservice.model.ExtendedObject) Criterions(io.vertigo.dynamo.criteria.Criterions) Validate(io.vertigo.vega.webservice.stereotype.Validate) AccessTokenConsume(io.vertigo.vega.webservice.stereotype.AccessTokenConsume) MandatoryPkValidator(io.vertigo.vega.webservice.data.domain.MandatoryPkValidator) ContactValidator(io.vertigo.vega.webservice.data.domain.ContactValidator) Predicate(java.util.function.Predicate) DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) Collection(java.util.Collection) Set(java.util.Set) UiContext(io.vertigo.vega.engines.webservice.json.UiContext) Instant(java.time.Instant) QueryParam(io.vertigo.vega.webservice.stereotype.QueryParam) VFile(io.vertigo.dynamo.file.model.VFile) WebServices(io.vertigo.vega.webservice.WebServices) ExcludedFields(io.vertigo.vega.webservice.stereotype.ExcludedFields) Serializable(java.io.Serializable) GET(io.vertigo.vega.webservice.stereotype.GET) VUserException(io.vertigo.lang.VUserException) FileManager(io.vertigo.dynamo.file.FileManager) List(java.util.List) HeaderParam(io.vertigo.vega.webservice.stereotype.HeaderParam) DtObject(io.vertigo.dynamo.domain.model.DtObject) StringUtil(io.vertigo.util.StringUtil) Optional(java.util.Optional) ResourceManager(io.vertigo.core.resource.ResourceManager) DtField(io.vertigo.dynamo.domain.metamodel.DtField) DtObjectUtil(io.vertigo.dynamo.domain.util.DtObjectUtil) AnonymousAccessAllowed(io.vertigo.vega.webservice.stereotype.AnonymousAccessAllowed) AccessTokenPublish(io.vertigo.vega.webservice.stereotype.AccessTokenPublish) ContactDao(io.vertigo.vega.webservice.data.domain.ContactDao) HashSet(java.util.HashSet) Inject(javax.inject.Inject) InnerBodyParam(io.vertigo.vega.webservice.stereotype.InnerBodyParam) PathPrefix(io.vertigo.vega.webservice.stereotype.PathPrefix) DateUtil(io.vertigo.util.DateUtil) ServerSideRead(io.vertigo.vega.webservice.stereotype.ServerSideRead) PUT(io.vertigo.vega.webservice.stereotype.PUT) HttpServletResponse(javax.servlet.http.HttpServletResponse) AutoSortAndPagination(io.vertigo.vega.webservice.stereotype.AutoSortAndPagination) DtList(io.vertigo.dynamo.domain.model.DtList) File(java.io.File) DtListState(io.vertigo.dynamo.domain.model.DtListState) ContactCriteria(io.vertigo.vega.webservice.data.domain.ContactCriteria) Entity(io.vertigo.dynamo.domain.model.Entity) EmptyPkValidator(io.vertigo.vega.webservice.data.domain.EmptyPkValidator) VCollectors(io.vertigo.dynamo.domain.util.VCollectors) Contact(io.vertigo.vega.webservice.data.domain.Contact) IncludedFields(io.vertigo.vega.webservice.stereotype.IncludedFields) AccessTokenMandatory(io.vertigo.vega.webservice.stereotype.AccessTokenMandatory) CollectionsManager(io.vertigo.dynamo.collections.CollectionsManager) Serializable(java.io.Serializable) DtDefinition(io.vertigo.dynamo.domain.metamodel.DtDefinition) ExtendedObject(io.vertigo.vega.webservice.model.ExtendedObject) DtObject(io.vertigo.dynamo.domain.model.DtObject) HashSet(java.util.HashSet) DtField(io.vertigo.dynamo.domain.metamodel.DtField) Predicate(java.util.function.Predicate)

Example 22 with DtObject

use of io.vertigo.dynamo.domain.model.DtObject 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 23 with DtObject

use of io.vertigo.dynamo.domain.model.DtObject in project vertigo by KleeGroup.

the class DtListDeltaJsonConverter method populateWebServiceCallContext.

/**
 * {@inheritDoc}
 */
@Override
public void populateWebServiceCallContext(final Object input, final WebServiceParam webServiceParam, final WebServiceCallContext routeContext) {
    final Class<?> paramClass = webServiceParam.getType();
    Assertion.checkArgument(canHandle(paramClass), "This JsonConverter can't read the asked type {0}. Only {1} is supported", paramClass.getSimpleName(), UiListDelta.class.getSimpleName());
    Assertion.checkArgument(getSupportedInputs()[0].isInstance(input) || getSupportedInputs()[1].isInstance(input), "This JsonConverter doesn't support this input type {0}. Only {1} is supported", input.getClass().getSimpleName(), Arrays.toString(getSupportedInputs()));
    // -----
    final Type paramGenericType = webServiceParam.getGenericType();
    final String objectPath;
    final UiListDelta<DtObject> uiListDelta;
    if (input instanceof String) {
        uiListDelta = jsonReaderEngine.uiListDeltaFromJson((String) input, paramGenericType);
        objectPath = "";
    } else if (input instanceof UiContext) {
        uiListDelta = (UiListDelta<DtObject>) ((UiContext) input).get(webServiceParam.getName());
        Assertion.checkNotNull(uiListDelta, "InnerParam not found : {0}", webServiceParam);
        objectPath = webServiceParam.getName();
    } else {
        throw new IllegalArgumentException(String.format("This JsonConverter can't read the asked type %s. Only %s is supported", paramClass.getSimpleName(), UiListDelta.class.getSimpleName()));
    }
    UiObjectUtil.postReadUiListDelta(uiListDelta, objectPath, webServiceParam);
    routeContext.setParamValue(webServiceParam, uiListDelta);
}
Also used : UiListDelta(io.vertigo.vega.engines.webservice.json.UiListDelta) Type(java.lang.reflect.Type) DtObject(io.vertigo.dynamo.domain.model.DtObject) UiContext(io.vertigo.vega.engines.webservice.json.UiContext)

Example 24 with DtObject

use of io.vertigo.dynamo.domain.model.DtObject in project vertigo by KleeGroup.

the class DtListJsonConverter method populateWebServiceCallContext.

/**
 * {@inheritDoc}
 */
@Override
public void populateWebServiceCallContext(final Object input, final WebServiceParam webServiceParam, final WebServiceCallContext routeContext) {
    final Class<?> paramClass = webServiceParam.getType();
    Assertion.checkArgument(DtList.class.isAssignableFrom(paramClass), "This JsonConverter can't read the asked type {0}. Only {1} is supported", paramClass.getSimpleName(), DtList.class.getSimpleName());
    Assertion.checkArgument(getSupportedInputs()[0].isInstance(input) || getSupportedInputs()[1].isInstance(input), "This JsonConverter doesn't support this input type {0}. Only {1} is supported", input.getClass().getSimpleName(), Arrays.toString(getSupportedInputs()));
    // -----
    final Type paramGenericType = webServiceParam.getGenericType();
    final String objectPath;
    final UiListModifiable<DtObject> uiList;
    if (input instanceof String) {
        uiList = jsonReaderEngine.uiListFromJson((String) input, paramGenericType);
        objectPath = "";
    } else if (input instanceof UiContext) {
        uiList = (UiListModifiable<DtObject>) ((UiContext) input).get(webServiceParam.getName());
        Assertion.checkNotNull(uiList, "InnerParam not found : {0}", webServiceParam);
        objectPath = webServiceParam.getName();
    } else {
        throw new IllegalArgumentException(String.format("This JsonConverter can't read the asked type %s. Only %s is supported", paramClass.getSimpleName(), UiListDelta.class.getSimpleName()));
    }
    // -----
    UiObjectUtil.postReadUiList(uiList, objectPath, webServiceParam);
    routeContext.setParamValue(webServiceParam, uiList);
}
Also used : UiListDelta(io.vertigo.vega.engines.webservice.json.UiListDelta) Type(java.lang.reflect.Type) DtObject(io.vertigo.dynamo.domain.model.DtObject) UiListModifiable(io.vertigo.vega.engines.webservice.json.UiListModifiable) DtList(io.vertigo.dynamo.domain.model.DtList) UiContext(io.vertigo.vega.engines.webservice.json.UiContext)

Example 25 with DtObject

use of io.vertigo.dynamo.domain.model.DtObject in project vertigo by KleeGroup.

the class RamLuceneIndex method addAll.

/**
 * Add element to index.
 * @param fullDtc Full Dtc to index
 * @param storeValue if data are store in index
 * @throws IOException Indexation error
 */
public void addAll(final DtList<D> fullDtc, final boolean storeValue) throws IOException {
    Assertion.checkNotNull(fullDtc);
    // -----
    try (final IndexWriter indexWriter = createIndexWriter()) {
        final DtField idField = fullDtc.getDefinition().getIdField().get();
        final Collection<DtField> dtFields = fullDtc.getDefinition().getFields();
        for (final D dto : fullDtc) {
            final Document document = new Document();
            final Object pkValue = idField.getDataAccessor().getValue(dto);
            Assertion.checkNotNull(pkValue, "Indexed DtObject must have a not null primary key. {0}.{1} was null.", fullDtc.getDefinition().getName(), idField.getName());
            final String indexedPkValue = String.valueOf(pkValue);
            addKeyword(document, idField.getName(), indexedPkValue, true);
            for (final DtField dtField : dtFields) {
                final Object value = dtField.getDataAccessor().getValue(dto);
                if (value != null && !dtField.equals(idField)) {
                    if (value instanceof String) {
                        final String valueAsString = getStringValue(dto, dtField);
                        addIndexed(document, dtField.getName(), valueAsString, storeValue);
                    } else if (value instanceof Date) {
                        final String valueAsString = DateTools.dateToString((Date) value, DateTools.Resolution.DAY);
                        addKeyword(document, dtField.getName(), valueAsString, storeValue);
                    } else {
                        addKeyword(document, dtField.getName(), value.toString(), storeValue);
                    }
                }
            }
            indexWriter.addDocument(document);
            mapDocument(indexedPkValue, dto);
        }
    }
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) DtObject(io.vertigo.dynamo.domain.model.DtObject) Document(org.apache.lucene.document.Document) Date(java.util.Date) DtField(io.vertigo.dynamo.domain.metamodel.DtField)

Aggregations

DtObject (io.vertigo.dynamo.domain.model.DtObject)32 DtField (io.vertigo.dynamo.domain.metamodel.DtField)14 Entity (io.vertigo.dynamo.domain.model.Entity)9 URI (io.vertigo.dynamo.domain.model.URI)8 Type (java.lang.reflect.Type)8 DtDefinition (io.vertigo.dynamo.domain.metamodel.DtDefinition)5 DtList (io.vertigo.dynamo.domain.model.DtList)5 ParameterizedType (java.lang.reflect.ParameterizedType)5 VFile (io.vertigo.dynamo.file.model.VFile)4 UiContext (io.vertigo.vega.engines.webservice.json.UiContext)4 UiListDelta (io.vertigo.vega.engines.webservice.json.UiListDelta)4 FileInfoURI (io.vertigo.dynamo.domain.model.FileInfoURI)3 Instant (java.time.Instant)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 JsonObject (com.google.gson.JsonObject)2 CollectionsManager (io.vertigo.dynamo.collections.CollectionsManager)2