Search in sources :

Example 11 with QualifiedName

use of org.qi4j.api.common.QualifiedName in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method extractManyAssociationQNames.

private void extractManyAssociationQNames(EntityDescriptor entityDesc, Map<QualifiedName, QNameInfo> extractedQNames, Set<QualifiedName> newQNames, Boolean setQNameTableNameToNull) {
    for (AssociationDescriptor mAssoDesc : entityDesc.state().manyAssociations()) {
        QualifiedName qName = mAssoDesc.qualifiedName();
        if (SQLSkeletonUtil.isQueryable(mAssoDesc.accessor())) {
            if (!extractedQNames.containsKey(qName)) {
                //
                extractedQNames.put(//
                qName, //
                QNameInfo.fromManyAssociation(//
                qName, setQNameTableNameToNull ? null : (QNAME_TABLE_NAME_PREFIX + extractedQNames.size()), //
                mAssoDesc));
                newQNames.add(qName);
            }
        }
    }
}
Also used : QualifiedName(org.qi4j.api.common.QualifiedName) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor)

Example 12 with QualifiedName

use of org.qi4j.api.common.QualifiedName in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method initConnection.

@Override
public void initConnection() throws SQLException {
    this._configuration.refresh();
    this.initTypes();
    this.modifyPrimitiveTypes(this._primitiveTypes, this._state.javaTypes2SQLTypes().get());
    String schemaName = this._configuration.get().schemaName().get();
    if (schemaName == null) {
        schemaName = DEFAULT_SCHEMA_NAME;
    } else {
        this.checkSchemaName(schemaName);
    }
    LOGGER.debug("Will use '{}' as schema name", schemaName);
    this._state.schemaName().set(schemaName);
    this._state.entityTypePKs().set(new HashMap<String, Integer>());
    this._state.usedClassesPKs().set(new HashMap<CompositeDescriptor, Integer>());
    this._state.entityUsedQNames().set(new HashMap<EntityDescriptor, Set<QualifiedName>>());
    this._state.qNameInfos().set(new HashMap<QualifiedName, QNameInfo>());
    this._state.enumPKs().set(new HashMap<String, Integer>());
    Connection connection = this._dataSource.getConnection();
    try {
        connection.setAutoCommit(true);
        connection.setReadOnly(false);
        this.syncDB(connection);
    } finally {
        SQLUtil.closeQuietly(connection);
    }
    if (LOGGER.isDebugEnabled()) {
        String newline = "\n";
        String tab = "\t";
        String colonspace = ": ";
        StringBuilder report = new StringBuilder();
        report.append("schemaName: ").append(_state.schemaName().get()).append(newline);
        report.append("qNameInfos: ").append(newline);
        for (Map.Entry<QualifiedName, QNameInfo> entry : _state.qNameInfos().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        report.append("entityUsedQNames:").append(newline);
        for (Map.Entry<EntityDescriptor, Set<QualifiedName>> entry : _state.entityUsedQNames().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        report.append("usedClassesPKs:").append(newline);
        for (Map.Entry<CompositeDescriptor, Integer> entry : _state.usedClassesPKs().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        report.append("javaTypes2SQLTypes:").append(newline);
        for (Map.Entry<Class<?>, Integer> entry : _state.javaTypes2SQLTypes().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        report.append("entityTypePKs:").append(newline);
        for (Map.Entry<String, Integer> entry : _state.entityTypePKs().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        report.append("enumPKs:").append(newline);
        for (Map.Entry<String, Integer> entry : _state.enumPKs().get().entrySet()) {
            report.append(tab).append(entry.getKey()).append(colonspace).append(entry.getValue()).append(newline);
        }
        LOGGER.debug("SQLDBState after initConnection:\n{}", report.toString());
    }
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) HashSet(java.util.HashSet) QualifiedName(org.qi4j.api.common.QualifiedName) Connection(java.sql.Connection) BigInteger(java.math.BigInteger) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) CompositeDescriptor(org.qi4j.api.composite.CompositeDescriptor) QNameInfo(org.qi4j.index.sql.support.common.QNameInfo) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with QualifiedName

use of org.qi4j.api.common.QualifiedName in project qi4j-sdk by Qi4j.

the class AbstractSQLIndexing method insertAssoAndManyAssoQNames.

private void insertAssoAndManyAssoQNames(Map<QualifiedName, PreparedStatement> qNameInsertPSs, PreparedStatement insertToAllQNamesPS, EntityState state, Integer qNamePK, Long entityPK) throws SQLException {
    for (AssociationDescriptor aDesc : state.entityDescriptor().state().associations()) {
        if (SQLSkeletonUtil.isQueryable(aDesc.accessor())) {
            QualifiedName qName = aDesc.qualifiedName();
            PreparedStatement ps = qNameInsertPSs.get(qName);
            EntityReference ref = state.associationValueOf(qName);
            if (ref != null) {
                insertToAllQNamesPS.setInt(1, qNamePK);
                insertToAllQNamesPS.setLong(2, entityPK);
                insertToAllQNamesPS.addBatch();
                ps.setInt(1, qNamePK);
                ps.setLong(2, entityPK);
                ps.setString(3, ref.identity());
                ps.addBatch();
                ++qNamePK;
            }
        }
    }
    for (AssociationDescriptor mDesc : state.entityDescriptor().state().manyAssociations()) {
        if (SQLSkeletonUtil.isQueryable(mDesc.accessor())) {
            QualifiedName qName = mDesc.qualifiedName();
            PreparedStatement ps = qNameInsertPSs.get(qName);
            Integer index = 0;
            for (EntityReference ref : state.manyAssociationValueOf(qName)) {
                if (ref != null) {
                    insertToAllQNamesPS.setInt(1, qNamePK);
                    insertToAllQNamesPS.setLong(2, entityPK);
                    insertToAllQNamesPS.addBatch();
                    ps.setInt(1, qNamePK);
                    ps.setLong(2, entityPK);
                    ps.setInt(3, index);
                    ps.setString(4, ref.identity());
                    ps.addBatch();
                    ++qNamePK;
                }
                ++index;
            }
        }
    }
}
Also used : QualifiedName(org.qi4j.api.common.QualifiedName) EntityReference(org.qi4j.api.entity.EntityReference) PreparedStatement(java.sql.PreparedStatement) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor)

Example 14 with QualifiedName

use of org.qi4j.api.common.QualifiedName in project qi4j-sdk by Qi4j.

the class Triples method addTripleManyAssociation.

public Triple addTripleManyAssociation(final ManyAssociationFunction<?> manyAssociationReference, final boolean optional) {
    AssociationFunction<?> traversedAssociation = manyAssociationReference.traversedAssociation();
    String subject = "?entity";
    if (traversedAssociation != null) {
        subject = addTripleAssociation(traversedAssociation, false).value;
    }
    QualifiedName qualifiedName = QualifiedName.fromAccessor(manyAssociationReference.accessor());
    String predicatePrefix = addNamespace(qualifiedName.toNamespace());
    String predicate = predicatePrefix + ":" + qualifiedName.name();
    Triple collectionTriple = addTriple(subject, predicate, optional);
    String liSubject = collectionTriple.value;
    return addTriple(liSubject, "rdf:li", false);
}
Also used : QualifiedName(org.qi4j.api.common.QualifiedName)

Example 15 with QualifiedName

use of org.qi4j.api.common.QualifiedName in project qi4j-sdk by Qi4j.

the class ValueCompositeCxfType method readObject.

@Override
public Object readObject(final MessageReader reader, final Context context) throws DatabindingException {
    QName qname = getSchemaType();
    final String className = (qname.getNamespaceURI() + "." + qname.getLocalPart()).substring(20);
    // Read attributes
    ValueDescriptor descriptor = module.valueDescriptor(className);
    StateDescriptor stateDescriptor = descriptor.state();
    final Map<QualifiedName, Object> values = new HashMap<>();
    while (reader.hasMoreElementReaders()) {
        MessageReader childReader = reader.getNextElementReader();
        QName childName = childReader.getName();
        QualifiedName childQualifiedName = QualifiedName.fromClass((Class) typeClass, childName.getLocalPart());
        PropertyDescriptor propertyDescriptor = stateDescriptor.findPropertyModelByQualifiedName(childQualifiedName);
        Type propertyType = propertyDescriptor.type();
        AegisType type = getTypeMapping().getType(propertyType);
        Object value = type.readObject(childReader, context);
        values.put(childQualifiedName, value);
    }
    ValueBuilder<?> builder = module.newValueBuilderWithState((Class<?>) typeClass, new Function<PropertyDescriptor, Object>() {

        @Override
        public Object map(PropertyDescriptor descriptor1) {
            return values.get(descriptor1.qualifiedName());
        }
    }, new Function<AssociationDescriptor, EntityReference>() {

        @Override
        public EntityReference map(AssociationDescriptor descriptor) {
            Object value = values.get(descriptor.qualifiedName());
            if (value == null) {
                return null;
            }
            return EntityReference.parseEntityReference(value.toString());
        }
    }, new Function<AssociationDescriptor, Iterable<EntityReference>>() {

        @Override
        public Iterable<EntityReference> map(AssociationDescriptor descriptor) {
            Object value = values.get(descriptor.qualifiedName());
            if (value == null) {
                return Iterables.empty();
            }
            String[] ids = value.toString().split(",");
            List<EntityReference> references = new ArrayList<>(ids.length);
            for (String id : ids) {
                references.add(EntityReference.parseEntityReference(id));
            }
            return references;
        }
    }, new Function<AssociationDescriptor, Map<String, EntityReference>>() {

        @Override
        public Map<String, EntityReference> map(AssociationDescriptor descriptor) {
            Object value = values.get(descriptor.qualifiedName());
            if (value == null) {
                return Collections.emptyMap();
            }
            String[] namedRefs = value.toString().split(",");
            Map<String, EntityReference> references = new HashMap<>(namedRefs.length);
            for (String namedRef : namedRefs) {
                String[] splitted = namedRef.split(":");
                references.put(splitted[0], EntityReference.parseEntityReference(splitted[1]));
            }
            return references;
        }
    });
    return builder.newInstance();
}
Also used : HashMap(java.util.HashMap) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) MessageReader(org.apache.cxf.aegis.xml.MessageReader) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) EntityReference(org.qi4j.api.entity.EntityReference) ArrayList(java.util.ArrayList) List(java.util.List) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) QualifiedName(org.qi4j.api.common.QualifiedName) CollectionType(org.apache.cxf.aegis.type.collection.CollectionType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) MapType(org.apache.cxf.aegis.type.collection.MapType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AegisType(org.apache.cxf.aegis.type.AegisType) StateDescriptor(org.qi4j.api.composite.StateDescriptor) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

QualifiedName (org.qi4j.api.common.QualifiedName)27 HashMap (java.util.HashMap)11 Map (java.util.Map)10 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)10 EntityReference (org.qi4j.api.entity.EntityReference)9 ArrayList (java.util.ArrayList)8 List (java.util.List)7 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)7 LinkedHashMap (java.util.LinkedHashMap)6 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)6 QNameInfo (org.qi4j.index.sql.support.common.QNameInfo)6 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)6 Type (java.lang.reflect.Type)5 Member (java.lang.reflect.Member)4 JSONArray (org.json.JSONArray)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 AccessibleObject (java.lang.reflect.AccessibleObject)3 TypeVariable (java.lang.reflect.TypeVariable)3 HashSet (java.util.HashSet)3