use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class AbstractRiakMapEntityStore method applyChanges.
@Override
public void applyChanges(MapChanges changes) throws IOException {
try {
final Bucket bucket = riakClient.fetchBucket(bucketKey).execute();
changes.visitMap(new MapChanger() {
@Override
public Writer newEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
return new StringWriter(1000) {
@Override
public void close() throws IOException {
try {
super.close();
bucket.store(ref.identity(), toString()).execute();
} catch (RiakException ex) {
throw new EntityStoreException("Unable to apply entity change: newEntity", ex);
}
}
};
}
@Override
public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
return new StringWriter(1000) {
@Override
public void close() throws IOException {
try {
super.close();
IRiakObject entity = bucket.fetch(ref.identity()).execute();
if (entity == null) {
throw new EntityNotFoundException(ref);
}
bucket.store(ref.identity(), toString()).execute();
} catch (RiakException ex) {
throw new EntityStoreException("Unable to apply entity change: updateEntity", ex);
}
}
};
}
@Override
public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
try {
IRiakObject entity = bucket.fetch(ref.identity()).execute();
if (entity == null) {
throw new EntityNotFoundException(ref);
}
bucket.delete(ref.identity()).execute();
} catch (RiakException ex) {
throw new EntityStoreException("Unable to apply entity change: removeEntity", ex);
}
}
});
} catch (RiakRetryFailedException ex) {
throw new EntityStoreException("Unable to apply entity changes.", ex);
}
}
use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class SQLEntityFinder method findEntities.
@Override
public Iterable<EntityReference> findEntities(Class<?> resultType, @Optional Specification<Composite> whereClause, @Optional OrderBy[] orderBySegments, @Optional final Integer firstResult, @Optional final Integer maxResults, Map<String, Object> variables) throws EntityFinderException {
// TODO what is Qi4j's policy on negative firstResult and/or maxResults? JDBC has its own way of interpreting
// these values - does it match with Qi4j's way?
Iterable<EntityReference> result;
if (maxResults == null || maxResults > 0) {
final List<Object> values = new ArrayList<>();
final List<Integer> valueSQLTypes = new ArrayList<>();
final String query = this.parser.constructQuery(resultType, whereClause, orderBySegments, firstResult, maxResults, variables, values, valueSQLTypes, false);
result = this.performQuery(new DoQuery<Iterable<EntityReference>>() {
@Override
public Iterable<EntityReference> doIt(Connection connection) throws SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
List<EntityReference> resultList = new ArrayList<>(maxResults == null ? 100 : maxResults);
try {
// TODO possibility to further optimize by setting fetch size (not too small not too little).
Integer rsType = parser.getResultSetType(firstResult, maxResults);
ps = createPS(connection, query, values, valueSQLTypes, rsType, ResultSet.CLOSE_CURSORS_AT_COMMIT);
rs = ps.executeQuery();
if (firstResult != null && !parser.isFirstResultSettingSupported() && rsType != ResultSet.TYPE_FORWARD_ONLY) {
rs.absolute(firstResult);
}
Integer i = 0;
while (rs.next() && (maxResults == null || i < maxResults)) {
resultList.add(new EntityReference(rs.getString(1)));
++i;
}
} finally {
SQLUtil.closeQuietly(rs);
SQLUtil.closeQuietly(ps);
}
return resultList;
}
});
} else {
result = new ArrayList<>(0);
}
return result;
}
use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class ValueToEntityMixin method doUnqualifiedConversion.
private <T> EntityBuilder<?> doUnqualifiedConversion(Class<T> entityType, String identity, final AssociationStateHolder vState, final AssociationStateDescriptor vStateDesc) {
Function<PropertyDescriptor, Object> props = new Function<PropertyDescriptor, Object>() {
@Override
public Object map(PropertyDescriptor ePropDesc) {
String propName = ePropDesc.qualifiedName().name();
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(propName);
return vState.propertyFor(vPropDesc.accessor()).get();
} catch (IllegalArgumentException propNotFoundOnValue) {
// Property not found on Value
return null;
}
}
};
Function<AssociationDescriptor, EntityReference> assocs = new Function<AssociationDescriptor, EntityReference>() {
@Override
public EntityReference map(AssociationDescriptor eAssocDesc) {
String assocName = eAssocDesc.qualifiedName().name();
try {
AssociationDescriptor vAssocDesc = vStateDesc.getAssociationByName(assocName);
Object assocEntity = vState.associationFor(vAssocDesc.accessor()).get();
return assocEntity == null ? null : EntityReference.entityReferenceFor(assocEntity);
} catch (IllegalArgumentException assocNotFoundOnValue) {
// Association not found on Value, find Property<String> and convert to Association
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(assocName);
if (STRING_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
String assocId = (String) vState.propertyFor(vPropDesc.accessor()).get();
return assocId == null ? null : EntityReference.parseEntityReference(assocId);
}
return null;
} catch (IllegalArgumentException propNotFoundOnValue) {
return null;
}
}
}
};
Function<AssociationDescriptor, Iterable<EntityReference>> manyAssocs = new Function<AssociationDescriptor, Iterable<EntityReference>>() {
@Override
public Iterable<EntityReference> map(AssociationDescriptor eAssocDesc) {
String assocName = eAssocDesc.qualifiedName().name();
try {
AssociationDescriptor vAssocDesc = vStateDesc.getManyAssociationByName(assocName);
ManyAssociation<Object> vManyAssoc = vState.manyAssociationFor(vAssocDesc.accessor());
return MANY_ASSOC_TO_ENTITY_REF_ITERABLE.map(vManyAssoc);
} catch (IllegalArgumentException assocNotFoundOnValue) {
// ManyAssociation not found on Value, find List<String> and convert to ManyAssociation
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(assocName);
if (STRING_COLLECTION_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
Collection<String> vAssocState = (Collection) vState.propertyFor(vPropDesc.accessor()).get();
return STRING_COLLEC_TO_ENTITY_REF_ITERABLE.map(vAssocState);
}
return Iterables.empty();
} catch (IllegalArgumentException propNotFoundOnValue) {
return Iterables.empty();
}
}
}
};
Function<AssociationDescriptor, Map<String, EntityReference>> namedAssocs = new Function<AssociationDescriptor, Map<String, EntityReference>>() {
@Override
public Map<String, EntityReference> map(AssociationDescriptor eAssocDesc) {
String assocName = eAssocDesc.qualifiedName().name();
try {
AssociationDescriptor vAssocDesc = vStateDesc.getNamedAssociationByName(assocName);
NamedAssociation<Object> vAssocState = vState.namedAssociationFor(vAssocDesc.accessor());
return NAMED_ASSOC_TO_ENTITY_REF_MAP.map(vAssocState);
} catch (IllegalArgumentException assocNotFoundOnValue) {
// Find Map<String,String> Property and convert to NamedAssociation
try {
PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName(assocName);
if (STRING_MAP_TYPE_SPEC.satisfiedBy(vPropDesc.valueType())) {
Map<String, String> vAssocState = (Map) vState.propertyFor(vPropDesc.accessor()).get();
return STRING_MAP_TO_ENTITY_REF_MAP.map(vAssocState);
}
return Collections.EMPTY_MAP;
} catch (IllegalArgumentException propNotFoundOnValue) {
return Collections.EMPTY_MAP;
}
}
}
};
return module.currentUnitOfWork().newEntityBuilderWithState(entityType, identity, props, assocs, manyAssocs, namedAssocs);
}
use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class EntityStateSerializer method serialize.
public void serialize(final EntityState entityState, final boolean includeNonQueryable, final Graph graph) {
ValueFactory values = graph.getValueFactory();
EntityReference identity = entityState.identity();
URI entityUri = createEntityURI(values, identity);
graph.add(entityUri, Rdfs.TYPE, values.createURI(Classes.toURI(first(entityState.entityDescriptor().types()))));
serializeProperties(entityState, graph, entityUri, entityState.entityDescriptor(), includeNonQueryable);
serializeAssociations(entityState, graph, entityUri, entityState.entityDescriptor().state().associations(), includeNonQueryable);
serializeManyAssociations(entityState, graph, entityUri, entityState.entityDescriptor().state().manyAssociations(), includeNonQueryable);
}
use of org.qi4j.api.entity.EntityReference in project qi4j-sdk by Qi4j.
the class EntityStateSerializer method serializeAssociations.
private void serializeAssociations(final EntityState entityState, final Graph graph, URI entityUri, final Iterable<? extends AssociationDescriptor> associations, final boolean includeNonQueryable) {
ValueFactory values = graph.getValueFactory();
// Associations
for (AssociationDescriptor associationType : associations) {
if (!(includeNonQueryable || associationType.queryable())) {
// Skip non-queryable
continue;
}
EntityReference associatedId = entityState.associationValueOf(associationType.qualifiedName());
if (associatedId != null) {
URI assocURI = values.createURI(associationType.qualifiedName().toURI());
URI assocEntityURI = values.createURI(associatedId.toURI());
graph.add(entityUri, assocURI, assocEntityURI);
}
}
}
Aggregations