Search in sources :

Example 21 with MetaDataObject

use of io.crnk.meta.model.MetaDataObject in project crnk-framework by crnk-project.

the class AnyUtils method setValue.

/**
 * Sets the value of the given anytype.
 *
 * @param partition to use to retrieve information
 * @param dataObject the anytype for which the value is set.
 * @param value      the new value
 */
public static void setValue(MetaPartition partition, AnyTypeObject dataObject, Object value) {
    MetaDataObject meta = (MetaDataObject) partition.getMeta(dataObject.getClass());
    if (value == null) {
        for (MetaAttribute attr : meta.getAttributes()) {
            attr.setValue(dataObject, null);
        }
    } else {
        boolean found = false;
        for (MetaAttribute attr : meta.getAttributes()) {
            if (attr.getName().equals(TYPE_ATTRIBUTE)) {
                continue;
            }
            if (attr.getType().getImplementationClass().isAssignableFrom(value.getClass())) {
                attr.setValue(dataObject, value);
                found = true;
            } else {
                attr.setValue(dataObject, null);
            }
        }
        if (!found) {
            throw new IllegalStateException("cannot assign " + value + " to " + dataObject);
        }
    }
}
Also used : MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute)

Example 22 with MetaDataObject

use of io.crnk.meta.model.MetaDataObject in project crnk-framework by crnk-project.

the class QueryBuilder method applySelectionSpec.

public Map<String, Integer> applySelectionSpec() {
    MetaDataObject meta = query.getMeta();
    Map<String, Integer> selectionBindings = new HashMap<>();
    int index = 1;
    List<IncludeFieldSpec> includedFields = query.getIncludedFields();
    for (IncludeFieldSpec includedField : includedFields) {
        MetaAttributePath path = meta.resolvePath(includedField.getAttributePath(), attributeFinder);
        E attr = backend.getAttribute(path);
        backend.addSelection(attr, path.toString());
        selectionBindings.put(path.toString(), index++);
    }
    return selectionBindings;
}
Also used : HashMap(java.util.HashMap) MetaDataObject(io.crnk.meta.model.MetaDataObject) IncludeFieldSpec(io.crnk.core.queryspec.IncludeFieldSpec) MetaAttributePath(io.crnk.meta.model.MetaAttributePath)

Example 23 with MetaDataObject

use of io.crnk.meta.model.MetaDataObject in project crnk-framework by crnk-project.

the class QueryBuilder method applyFilterSpec.

protected void applyFilterSpec() {
    QueryFilterBuilder<P, F> predicateBuilder = new QueryFilterBuilder<>(backend, attributeFinder);
    MetaDataObject meta = query.getMeta();
    List<FilterSpec> filters = query.getFilterSpecs();
    List<P> predicates = predicateBuilder.filterSpecListToPredicateArray(meta, backend.getRoot(), filters);
    if (predicates != null && !predicates.isEmpty()) {
        backend.addPredicate(backend.and(predicates));
    }
    MetaAttribute parentAttr = query.getParentAttr();
    if (parentAttr != null) {
        MetaDataObject parentMeta = query.getParentMeta();
        MetaKey primaryKey = parentMeta.getPrimaryKey();
        if (primaryKey == null) {
            throw new IllegalStateException("primary key not found for " + parentAttr.getId());
        }
        MetaAttribute primaryKeyAttr = primaryKey.getUniqueElement();
        backend.addParentPredicate(primaryKeyAttr);
    }
}
Also used : MetaKey(io.crnk.meta.model.MetaKey) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaAttribute(io.crnk.meta.model.MetaAttribute) FilterSpec(io.crnk.core.queryspec.FilterSpec)

Aggregations

MetaDataObject (io.crnk.meta.model.MetaDataObject)23 MetaAttribute (io.crnk.meta.model.MetaAttribute)7 MetaElement (io.crnk.meta.model.MetaElement)6 ResourceField (io.crnk.core.engine.information.resource.ResourceField)5 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)5 TSInterfaceType (io.crnk.gen.typescript.model.TSInterfaceType)5 Test (org.junit.Test)5 MetaAttributePath (io.crnk.meta.model.MetaAttributePath)3 MetaResource (io.crnk.meta.model.resource.MetaResource)3 Type (java.lang.reflect.Type)3 MetaPrimaryKey (io.crnk.meta.model.MetaPrimaryKey)2 MetaType (io.crnk.meta.model.MetaType)2 ResourceFieldType (io.crnk.core.engine.information.resource.ResourceFieldType)1 DefaultResourceInstanceBuilder (io.crnk.core.engine.internal.information.resource.DefaultResourceInstanceBuilder)1 TypeParser (io.crnk.core.engine.parser.TypeParser)1 FilterSpec (io.crnk.core.queryspec.FilterSpec)1 IncludeFieldSpec (io.crnk.core.queryspec.IncludeFieldSpec)1 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)1 TSContainerElement (io.crnk.gen.typescript.model.TSContainerElement)1 TSElement (io.crnk.gen.typescript.model.TSElement)1