public static String getName( EntityReference reference ) { return world.get( reference.identity() ); }
@Override public Map<String, EntityReference> map( Map<String, String> stringMap ) { if( stringMap == null ) { return Collections.emptyMap(); } Map<String, EntityReference> refMap = new LinkedHashMap<>( stringMap.size() ); for( Map.Entry<String, String> entry : stringMap.entrySet() ) { refMap.put( entry.getKey(), EntityReference.parseEntityReference( entry.getValue() ) ); } return refMap; } };
/** * Parse an Entity identity to an EntityReference. * @param identity the EntityReference identity * @return the EntityReference represented by the given identity */ public static EntityReference parseEntityReference( String identity ) { return new EntityReference( identity ); }
@Override public EntityReference map( AssociationDescriptor eAssocDesc ) { try { return EntityReference.entityReferenceFor( vState.associationFor( eAssocDesc.accessor() ) ); } catch( IllegalArgumentException assocNotFoundOnValue ) { // Find String Property and convert to Association String propName = eAssocDesc.qualifiedName().name(); try { PropertyDescriptor vPropDesc = vStateDesc.findPropertyModelByName( propName ); if( STRING_TYPE_SPEC.satisfiedBy( vPropDesc.valueType() ) ) { String assocState = (String) vState.propertyFor( vPropDesc.accessor() ).get(); return EntityReference.parseEntityReference( assocState ); } return null; } catch( IllegalArgumentException propNotFoundOnValue ) { return null; } } } };
@Override public Map<String, EntityReference> map( NamedAssociation<?> namedAssoc ) { if( namedAssoc == null ) { return Collections.emptyMap(); } Map<String, EntityReference> refs = new LinkedHashMap<>( namedAssoc.count() ); for( String name : namedAssoc ) { refs.put( name, EntityReference.entityReferenceFor( namedAssoc.get( name ) ) ); } return refs; } };
public URI createEntityURI( ValueFactory valueFactory, EntityReference identity ) { return valueFactory.createURI( identity.toURI() ); }
@Override public void removeEntity( EntityReference ref, EntityDescriptor descriptor ) throws EntityNotFoundException { try { Long stateIndex = getStateIndex( ref.toString() ); recordManager.delete( stateIndex ); index.remove( ref.toString().getBytes( "UTF-8" ) ); } catch( IOException e ) { throw new EntityStoreException( e ); } } } );
@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; } } } };
@Override public Iterable<EntityReference> map( ManyAssociation<?> manyAssoc ) { if( manyAssoc == null ) { return Iterables.empty(); } List<EntityReference> refs = new ArrayList<>( manyAssoc.count() ); for( Object entity : manyAssoc ) { refs.add( EntityReference.entityReferenceFor( entity ) ); } return refs; } };
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() ) ) { continue; // Skip non-queryable } 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 ); } } }
@Override public void receive( EntityState item ) throws RuntimeException { count++; item.setPropertyValue( identityQN, item.identity().identity() ); states.add( item ); if( states.size() >= loadValue ) { reindexState(); } }
@Override public Iterable<EntityReference> map( Collection<String> stringCollec ) { if( stringCollec == null ) { return Iterables.empty(); } List<EntityReference> refList = new ArrayList<>(); for( String assId : stringCollec ) { refList.add( EntityReference.parseEntityReference( assId ) ); } return refList; } };
/** * @param object an EntityComposite * @return the EntityReference for the given EntityComposite */ public static EntityReference entityReferenceFor( Object object ) { return new EntityReference( (EntityComposite) object ); }
@Override public Iterable<EntityReference> map( AssociationDescriptor associationDescriptor ) { ManyAssociation<?> state = associationState.manyAssociationFor( associationDescriptor.accessor() ); List<EntityReference> refs = new ArrayList<>( state.count() ); for( Object entity : state ) { refs.add( EntityReference.entityReferenceFor( entity ) ); } return refs; } },
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() ) ) { continue; // Skip non-queryable } 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 ); } } } }
@ReadLock @Override public Reader get( EntityReference entityReference ) throws EntityStoreException { try { Long stateIndex = getStateIndex( entityReference.identity() ); if( stateIndex == null ) { throw new EntityNotFoundException( entityReference ); } byte[] serializedState = (byte[]) recordManager.fetch( stateIndex, serializer ); if( serializedState == null ) { throw new EntityNotFoundException( entityReference ); } return new StringReader( new String( serializedState, "UTF-8" ) ); } catch( IOException e ) { throw new EntityStoreException( e ); } }
@Test public void givenEntityReferenceValueWhenSerializingAndDeserializingExpectEquals() { String serialized = valueSerialization.serialize( EntityReference.parseEntityReference( "ABCD-1234" ) ); assertThat( serialized, equalTo( "ABCD-1234" ) ); EntityReference deserialized = valueSerialization.deserialize( EntityReference.class, serialized ); assertThat( deserialized, equalTo( EntityReference.parseEntityReference( "ABCD-1234" ) ) ); } }
/** * Parse an URI to an EntityReference. * @param uri the URI to parse * @return the EntityReference represented by the given URI */ public static EntityReference parseURI( String uri ) { String identity = uri.substring( "urn:qi4j:entity:".length() ); return new EntityReference( identity ); }
@Override public Map<String, EntityReference> map( AssociationDescriptor associationDescriptor ) { NamedAssociation<?> assoc = associationState.namedAssociationFor( associationDescriptor.accessor() ); Map<String, EntityReference> refs = new LinkedHashMap<>( assoc.count() ); for( String name : assoc ) { refs.put( name, EntityReference.entityReferenceFor( assoc.get( name ) ) ); } return refs; } } );
public void updateResponse( Response response ) { if( entity != null ) { EntityState state = spi.entityStateOf( entity ); Date lastModified = new Date( state.lastModified() ); Tag tag = new Tag( state.identity().identity() + "/" + state.version() ); response.getEntity().setModificationDate( lastModified ); response.getEntity().setTag( tag ); } }