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;
}
}
}
}
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();
}
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);
}
}
}
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);
}
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);
}
}
Aggregations