Search in sources :

Example 26 with EntityReference

use of org.qi4j.api.entity.EntityReference 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 27 with EntityReference

use of org.qi4j.api.entity.EntityReference 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)

Example 28 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class EntityStateSerializer method serializeManyAssociations.

private void serializeManyAssociations(final EntityState entityState, final Graph graph, final URI entityUri, final Iterable<? extends AssociationDescriptor> associations, final boolean includeNonQueryable) {
    ValueFactory values = graph.getValueFactory();
    // Many-Associations
    for (AssociationDescriptor associationType : associations) {
        if (!(includeNonQueryable || associationType.queryable())) {
            // Skip non-queryable
            continue;
        }
        BNode collection = values.createBNode();
        graph.add(entityUri, values.createURI(associationType.qualifiedName().toURI()), collection);
        graph.add(collection, Rdfs.TYPE, Rdfs.SEQ);
        ManyAssociationState associatedIds = entityState.manyAssociationValueOf(associationType.qualifiedName());
        for (EntityReference associatedId : associatedIds) {
            URI assocEntityURI = values.createURI(associatedId.toURI());
            graph.add(collection, Rdfs.LIST_ITEM, assocEntityURI);
        }
    }
}
Also used : BNode(org.openrdf.model.BNode) EntityReference(org.qi4j.api.entity.EntityReference) ValueFactory(org.openrdf.model.ValueFactory) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) ManyAssociationState(org.qi4j.spi.entity.ManyAssociationState) URI(org.openrdf.model.URI)

Example 29 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class EntitySerializerTest method testEntitySerializer.

@Test
public void testEntitySerializer() throws RDFHandlerException {
    EntityReference entityReference = new EntityReference("test2");
    EntityState entityState = entityStore.newUnitOfWork(UsecaseBuilder.newUsecase("Test"), module, System.currentTimeMillis()).entityStateOf(entityReference);
    Iterable<Statement> graph = serializer.serialize(entityState);
    String[] prefixes = new String[] { "rdf", "dc", " vc" };
    String[] namespaces = new String[] { Rdfs.RDF, DcRdf.NAMESPACE, "http://www.w3.org/2001/vcard-rdf/3.0#" };
    new RdfXmlSerializer().serialize(graph, new PrintWriter(System.out), prefixes, namespaces);
}
Also used : Statement(org.openrdf.model.Statement) RdfXmlSerializer(org.qi4j.library.rdf.serializer.RdfXmlSerializer) EntityReference(org.qi4j.api.entity.EntityReference) EntityState(org.qi4j.spi.entity.EntityState) PrintWriter(java.io.PrintWriter) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 30 with EntityReference

use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.

the class EntitiesResource method representAtom.

private Representation representAtom() throws ResourceException {
    try {
        Feed feed = new Feed();
        feed.setTitle(new Text(MediaType.TEXT_PLAIN, "All entities"));
        List<Entry> entries = feed.getEntries();
        final Iterable<EntityReference> query = entityFinder.findEntities(EntityComposite.class, null, null, null, null, Collections.<String, Object>emptyMap());
        for (EntityReference entityReference : query) {
            Entry entry = new Entry();
            entry.setTitle(new Text(MediaType.TEXT_PLAIN, entityReference.toString()));
            Link link = new Link();
            link.setHref(getRequest().getResourceRef().clone().addSegment(entityReference.identity()));
            entry.getLinks().add(link);
            entries.add(entry);
        }
        return feed;
    } catch (Exception e) {
        throw new ResourceException(e);
    }
}
Also used : Entry(org.restlet.ext.atom.Entry) EntityReference(org.qi4j.api.entity.EntityReference) Text(org.restlet.ext.atom.Text) ResourceException(org.restlet.resource.ResourceException) Link(org.restlet.ext.atom.Link) ResourceException(org.restlet.resource.ResourceException) IOException(java.io.IOException) EntityFinderException(org.qi4j.spi.query.EntityFinderException) Feed(org.restlet.ext.atom.Feed)

Aggregations

EntityReference (org.qi4j.api.entity.EntityReference)60 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)13 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)12 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)11 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)11 Person (org.qi4j.test.indexing.model.Person)11 HashMap (java.util.HashMap)9 Map (java.util.Map)9 QualifiedName (org.qi4j.api.common.QualifiedName)9 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)9 EntityStoreException (org.qi4j.spi.entitystore.EntityStoreException)9 Writer (java.io.Writer)8 IOException (java.io.IOException)7 LinkedHashMap (java.util.LinkedHashMap)7 List (java.util.List)7 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)6 StringWriter (java.io.StringWriter)5 JSONArray (org.json.JSONArray)5