Search in sources :

Example 1 with ValueComposite

use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.

the class ModuleInstance method newValueBuilderWithPrototype.

@Override
@SuppressWarnings("unchecked")
public <T> ValueBuilder<T> newValueBuilderWithPrototype(T prototype) {
    NullArgumentException.validateNotNull("prototype", prototype);
    ValueInstance valueInstance = ValueInstance.valueInstanceOf((ValueComposite) prototype);
    Class<Composite> valueType = (Class<Composite>) first(valueInstance.types());
    ModelModule<ValueModel> modelModule = typeLookup.lookupValueModel(valueType);
    if (modelModule == null) {
        throw new NoSuchValueException(valueType.getName(), name());
    }
    return new ValueBuilderWithPrototype<>(modelModule, this, prototype);
}
Also used : NoSuchValueException(org.qi4j.api.value.NoSuchValueException) Composite(org.qi4j.api.composite.Composite) ValueComposite(org.qi4j.api.value.ValueComposite) EntityComposite(org.qi4j.api.entity.EntityComposite) ValueModel(org.qi4j.runtime.value.ValueModel) ValueBuilderWithPrototype(org.qi4j.runtime.value.ValueBuilderWithPrototype) ValueInstance(org.qi4j.runtime.value.ValueInstance)

Example 2 with ValueComposite

use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.

the class ValueTypeFactory method newValueType.

@SuppressWarnings({ "raw", "unchecked" })
public ValueType newValueType(Type type, Class declaringClass, Class compositeType, LayerModel layer, ModuleModel module) {
    ValueType valueType = null;
    if (CollectionType.isCollection(type)) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            Type collectionType = pt.getActualTypeArguments()[0];
            if (collectionType instanceof TypeVariable) {
                TypeVariable collectionTypeVariable = (TypeVariable) collectionType;
                collectionType = Classes.resolveTypeVariable(collectionTypeVariable, declaringClass, compositeType);
            }
            ValueType collectedType = newValueType(collectionType, declaringClass, compositeType, layer, module);
            valueType = new CollectionType(Classes.RAW_CLASS.map(type), collectedType);
        } else {
            valueType = new CollectionType(Classes.RAW_CLASS.map(type), newValueType(Object.class, declaringClass, compositeType, layer, module));
        }
    } else if (MapType.isMap(type)) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            Type keyType = pt.getActualTypeArguments()[0];
            if (keyType instanceof TypeVariable) {
                TypeVariable keyTypeVariable = (TypeVariable) keyType;
                keyType = Classes.resolveTypeVariable(keyTypeVariable, declaringClass, compositeType);
            }
            ValueType keyedType = newValueType(keyType, declaringClass, compositeType, layer, module);
            Type valType = pt.getActualTypeArguments()[1];
            if (valType instanceof TypeVariable) {
                TypeVariable valueTypeVariable = (TypeVariable) valType;
                valType = Classes.resolveTypeVariable(valueTypeVariable, declaringClass, compositeType);
            }
            ValueType valuedType = newValueType(valType, declaringClass, compositeType, layer, module);
            valueType = new MapType(Classes.RAW_CLASS.map(type), keyedType, valuedType);
        } else {
            valueType = new MapType(Classes.RAW_CLASS.map(type), newValueType(Object.class, declaringClass, compositeType, layer, module), newValueType(Object.class, declaringClass, compositeType, layer, module));
        }
    } else if (ValueCompositeType.isValueComposite(type)) {
        // Find ValueModel in module/layer/used layers
        ValueModel model = new ValueFinder(layer, module, Classes.RAW_CLASS.map(type)).getFoundModel();
        if (model == null) {
            if (type.equals(ValueComposite.class)) {
                // Create default model
                MixinsModel mixinsModel = new MixinsModel();
                Iterable valueComposite = (Iterable) Iterables.iterable(ValueComposite.class);
                ValueStateModel valueStateModel = new ValueStateModel(new PropertiesModel(), new AssociationsModel(), new ManyAssociationsModel(), new NamedAssociationsModel());
                model = new ValueModel(valueComposite, Visibility.application, new MetaInfo(), mixinsModel, valueStateModel, new CompositeMethodsModel(mixinsModel));
            } else {
                throw new InvalidApplicationException("[" + module.name() + "] Could not find ValueComposite of type " + type);
            }
        }
        return model.valueType();
    } else if (EnumType.isEnum(type)) {
        valueType = new EnumType(Classes.RAW_CLASS.map(type));
    } else {
        valueType = new ValueType(Classes.RAW_CLASS.map(type));
    }
    return valueType;
}
Also used : MixinsModel(org.qi4j.runtime.composite.MixinsModel) ValueModel(org.qi4j.runtime.value.ValueModel) ValueType(org.qi4j.api.type.ValueType) NamedAssociationsModel(org.qi4j.runtime.association.NamedAssociationsModel) MetaInfo(org.qi4j.api.common.MetaInfo) ValueComposite(org.qi4j.api.value.ValueComposite) MapType(org.qi4j.api.type.MapType) ParameterizedType(java.lang.reflect.ParameterizedType) ManyAssociationsModel(org.qi4j.runtime.association.ManyAssociationsModel) ValueCompositeType(org.qi4j.api.type.ValueCompositeType) EnumType(org.qi4j.api.type.EnumType) MapType(org.qi4j.api.type.MapType) CollectionType(org.qi4j.api.type.CollectionType) ValueType(org.qi4j.api.type.ValueType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CompositeMethodsModel(org.qi4j.runtime.composite.CompositeMethodsModel) TypeVariable(java.lang.reflect.TypeVariable) EnumType(org.qi4j.api.type.EnumType) CollectionType(org.qi4j.api.type.CollectionType) InvalidApplicationException(org.qi4j.api.common.InvalidApplicationException) ValueStateModel(org.qi4j.runtime.value.ValueStateModel) PropertiesModel(org.qi4j.runtime.property.PropertiesModel) ManyAssociationsModel(org.qi4j.runtime.association.ManyAssociationsModel) AssociationsModel(org.qi4j.runtime.association.AssociationsModel) NamedAssociationsModel(org.qi4j.runtime.association.NamedAssociationsModel)

Example 3 with ValueComposite

use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.

the class AbstractSQLQuerying method modifyFromClauseAndWhereClauseToGetValue.

// TODO refactor this monster of a method to something more understandable
protected Integer modifyFromClauseAndWhereClauseToGetValue(final QualifiedName qName, Object value, final Specification<Composite> predicate, final Boolean negationActive, final Integer currentTableIndex, final ModifiableInt maxTableIndex, final String columnName, final String collectionPath, final SQLVendor vendor, final BooleanBuilder whereClause, final BooleanBuilder afterWhere, final TableReferenceBuilder fromClause, final GroupByBuilder groupBy, final BooleanBuilder having, final List<QNameJoin> qNameJoins, Map<String, Object> variables, final List<Object> values, final List<Integer> valueSQLTypes) {
    if (value instanceof Variable) {
        value = variables.get(((Variable) value).variableName());
    }
    final String schemaName = this._state.schemaName().get();
    Integer result = 1;
    final BooleanFactory b = vendor.getBooleanFactory();
    final LiteralFactory l = vendor.getLiteralFactory();
    final ColumnsFactory c = vendor.getColumnsFactory();
    final QueryFactory q = vendor.getQueryFactory();
    final TableReferenceFactory t = vendor.getTableReferenceFactory();
    if (value instanceof Collection<?>) {
        // Collection
        Integer collectionIndex = 0;
        Boolean collectionIsSet = value instanceof Set<?>;
        Boolean topLevel = collectionPath.equals(DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME);
        String collTable = TABLE_NAME_PREFIX + currentTableIndex;
        String collCol = DBNames.QNAME_TABLE_COLLECTION_PATH_COLUMN_NAME;
        ColumnReferenceByName collColExp = c.colName(collTable, collCol);
        BooleanBuilder collectionCondition = b.booleanBuilder();
        if (topLevel && negationActive) {
            afterWhere.and(b.booleanBuilder(b.neq(collColExp, l.s(DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME))).or(b.isNull(collColExp)).createExpression());
        }
        Integer totalItemsProcessed = 0;
        for (Object item : (Collection<?>) value) {
            String path = collectionPath + DBNames.QNAME_TABLE_COLLECTION_PATH_SEPARATOR + (collectionIsSet ? "*{1,}" : collectionIndex);
            Boolean isCollection = (item instanceof Collection<?>);
            BooleanBuilder newWhere = b.booleanBuilder();
            if (!isCollection) {
                newWhere.reset(b.regexp(collColExp, l.s(path)));
            }
            totalItemsProcessed = totalItemsProcessed + modifyFromClauseAndWhereClauseToGetValue(qName, item, predicate, negationActive, currentTableIndex, maxTableIndex, columnName, path, vendor, newWhere, afterWhere, fromClause, groupBy, having, qNameJoins, variables, values, valueSQLTypes);
            ++collectionIndex;
            collectionCondition.or(newWhere.createExpression());
        }
        result = totalItemsProcessed;
        if (topLevel) {
            if (totalItemsProcessed == 0) {
                collectionCondition.and(b.isNotNull(collColExp)).and(b.eq(collColExp, l.l(DBNames.QNAME_TABLE_COLLECTION_PATH_TOP_LEVEL_NAME)));
            } else if (!negationActive) {
                groupBy.addGroupingElements(q.groupingElement(c.colName(TABLE_NAME_PREFIX + currentTableIndex, DBNames.ENTITY_TABLE_PK_COLUMN_NAME)));
                having.and(b.eq(l.func(SQLFunctions.COUNT, c.colName(TABLE_NAME_PREFIX + currentTableIndex, DBNames.QNAME_TABLE_VALUE_COLUMN_NAME)), l.n(totalItemsProcessed)));
            }
        }
        whereClause.and(collectionCondition.createExpression());
    } else if (value instanceof ValueComposite) {
        // @formatter:off
        for (Property<?> property : Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((ValueComposite) value).state().properties()) {
            Boolean qNameJoinDone = false;
            Integer sourceIndex = maxTableIndex.getInt();
            Integer targetIndex = sourceIndex + 1;
            for (QNameJoin join : qNameJoins) {
                if (join.getSourceQName().equals(qName)) {
                    sourceIndex = join.getSourceTableIndex();
                    if (join.getTargetQName().equals(spi.propertyDescriptorFor(property).qualifiedName())) {
                        // This join has already been done once
                        qNameJoinDone = true;
                        targetIndex = join.getTargetTableIndex();
                        break;
                    }
                }
            }
            if (!qNameJoinDone) {
                // @formatter:off
                QNameInfo info = _state.qNameInfos().get().get(spi.propertyDescriptorFor(property).qualifiedName());
                String prevTableName = TABLE_NAME_PREFIX + sourceIndex;
                String nextTableName = TABLE_NAME_PREFIX + targetIndex;
                fromClause.addQualifiedJoin(JoinType.LEFT_OUTER, t.table(t.tableName(schemaName, info.getTableName()), t.tableAlias(TABLE_NAME_PREFIX + targetIndex)), t.jc(b.booleanBuilder(b.eq(c.colName(prevTableName, DBNames.ALL_QNAMES_TABLE_PK_COLUMN_NAME), c.colName(nextTableName, DBNames.QNAME_TABLE_PARENT_QNAME_COLUMN_NAME))).and(b.eq(c.colName(prevTableName, DBNames.ENTITY_TABLE_PK_COLUMN_NAME), c.colName(nextTableName, DBNames.ENTITY_TABLE_PK_COLUMN_NAME))).createExpression()));
                // @formatter:on
                qNameJoins.add(new QNameJoin(qName, spi.propertyDescriptorFor(property).qualifiedName(), sourceIndex, targetIndex));
                maxTableIndex.setInt(maxTableIndex.getInt() + 1);
            }
            modifyFromClauseAndWhereClauseToGetValue(spi.propertyDescriptorFor(property).qualifiedName(), property.get(), predicate, negationActive, targetIndex, maxTableIndex, columnName, collectionPath, vendor, whereClause, afterWhere, fromClause, groupBy, having, qNameJoins, variables, values, valueSQLTypes);
        }
    // @formatter:on
    } else {
        // Primitive
        ColumnReferenceByName valueCol = c.colName(TABLE_NAME_PREFIX + currentTableIndex, columnName);
        if (value == null) {
            whereClause.and(b.isNull(valueCol));
        } else {
            Object dbValue = value;
            if (Enum.class.isAssignableFrom(value.getClass())) {
                dbValue = this._state.enumPKs().get().get(value.getClass().getName());
            }
            whereClause.and(b.and(b.isNotNull(valueCol), this.getOperator(predicate).getExpression(b, valueCol, l.param())));
            values.add(dbValue);
            valueSQLTypes.add(_typeHelper.getSQLType(value));
            LOGGER.info(TABLE_NAME_PREFIX + currentTableIndex + "." + columnName + " is " + dbValue);
        }
    }
    return result;
}
Also used : QueryFactory(org.sql.generation.api.grammar.factories.QueryFactory) Variable(org.qi4j.api.query.grammar.Variable) ResultSet(java.sql.ResultSet) Set(java.util.Set) ColumnsFactory(org.sql.generation.api.grammar.factories.ColumnsFactory) ValueComposite(org.qi4j.api.value.ValueComposite) BooleanFactory(org.sql.generation.api.grammar.factories.BooleanFactory) LiteralFactory(org.sql.generation.api.grammar.factories.LiteralFactory) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) ColumnReferenceByName(org.sql.generation.api.grammar.query.ColumnReferenceByName) BooleanBuilder(org.sql.generation.api.grammar.builders.booleans.BooleanBuilder) Collection(java.util.Collection) AccessibleObject(java.lang.reflect.AccessibleObject) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) Property(org.qi4j.api.property.Property)

Example 4 with ValueComposite

use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.

the class AbstractSQLIndexing method storePropertiesOfVC.

private Integer storePropertiesOfVC(Map<QualifiedName, PreparedStatement> qNameInsertPSs, PreparedStatement insertAllQNamesPS, Integer propertyPK, Long entityPK, Object property) throws SQLException {
    ValueDescriptor vDesc = this._qi4SPI.valueDescriptorFor((ValueComposite) property);
    StateHolder state = Qi4j.FUNCTION_COMPOSITE_INSTANCE_OF.map((ValueComposite) property).state();
    Integer originalPropertyPK = propertyPK;
    ++propertyPK;
    for (PropertyDescriptor pDesc : vDesc.state().properties()) {
        propertyPK = this.insertProperty(qNameInsertPSs, insertAllQNamesPS, propertyPK, entityPK, pDesc.qualifiedName(), state.propertyFor(pDesc.accessor()).get(), originalPropertyPK);
    }
    return propertyPK;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) StateHolder(org.qi4j.api.property.StateHolder) ValueComposite(org.qi4j.api.value.ValueComposite)

Example 5 with ValueComposite

use of org.qi4j.api.value.ValueComposite in project qi4j-sdk by Qi4j.

the class ValueToEntityMixin method update.

@Override
public void update(Object entity, Object value) throws NoSuchEntityException {
    EntityComposite eComposite = (EntityComposite) entity;
    ValueComposite vComposite = (ValueComposite) value;
    EntityDescriptor eDesc = spi.entityDescriptorFor(eComposite);
    AssociationStateHolder eState = spi.stateOf(eComposite);
    AssociationStateDescriptor eStateDesc = eDesc.state();
    ValueDescriptor vDesc = spi.valueDescriptorFor(vComposite);
    AssociationStateHolder vState = spi.stateOf(vComposite);
    AssociationStateDescriptor vStateDesc = vDesc.state();
    Unqualified unqualified = vDesc.metaInfo(Unqualified.class);
    if (unqualified == null || !unqualified.value()) {
        doQualifiedUpdate(eState, eStateDesc, vState, vStateDesc);
    } else {
        doUnQualifiedUpdate(eState, eStateDesc, vState, vStateDesc);
    }
}
Also used : EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) EntityComposite(org.qi4j.api.entity.EntityComposite) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) AssociationStateHolder(org.qi4j.api.association.AssociationStateHolder) ValueComposite(org.qi4j.api.value.ValueComposite) AssociationStateDescriptor(org.qi4j.api.association.AssociationStateDescriptor)

Aggregations

ValueComposite (org.qi4j.api.value.ValueComposite)14 Collection (java.util.Collection)4 ValueCompositeType (org.qi4j.api.type.ValueCompositeType)4 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)4 Map (java.util.Map)3 Set (java.util.Set)3 AssociationStateHolder (org.qi4j.api.association.AssociationStateHolder)3 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)3 CollectionType (org.qi4j.api.type.CollectionType)3 MapType (org.qi4j.api.type.MapType)3 ResourceException (org.restlet.resource.ResourceException)3 IOException (java.io.IOException)2 Writer (java.io.Writer)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 AssociationStateDescriptor (org.qi4j.api.association.AssociationStateDescriptor)2 Composite (org.qi4j.api.composite.Composite)2 EntityComposite (org.qi4j.api.entity.EntityComposite)2