@Override public Object get() { return next.get(); }
@Override public boolean satisfiedBy( Composite item ) { try { Association<T> assoc = association.map( item ); if( assoc == null ) { return true; } return assoc.get() == null; } catch( IllegalArgumentException e ) { return true; } }
@Override public boolean satisfiedBy( Composite item ) { try { Association<T> assoc = association.map( item ); if( assoc == null ) { return false; } return assoc.get() != null; } catch( IllegalArgumentException e ) { return false; } }
@Override public NamedAssociation<T> map( Composite entity ) { try { Object target = entity; if( traversedAssociation != null ) { target = traversedAssociation.map( entity ).get(); } if( traversedManyAssociation != null ) { throw new IllegalArgumentException( "Cannot traverse ManyAssociations" ); } if( traversedNamedAssociation != null ) { throw new IllegalArgumentException( "Cannot traverse NamedAssociations" ); } CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler( target ); return ( (AssociationStateHolder) handler.state() ).namedAssociationFor( accessor ); } catch( IllegalArgumentException e ) { throw e; } catch( Throwable e ) { throw new IllegalArgumentException( e ); } }
@Override public ManyAssociation<T> map( Composite entity ) { try { Object target = entity; if( traversedAssociation != null ) { target = traversedAssociation.map( entity ).get(); } if( traversedManyAssociation != null ) { throw new IllegalArgumentException( "Cannot traverse ManyAssociations" ); } if( traversedNamedAssociation != null ) { throw new IllegalArgumentException( "Cannot traverse NamedAssociations" ); } CompositeInstance handler = (CompositeInstance) Proxy.getInvocationHandler( target ); return ( (AssociationStateHolder) handler.state() ).manyAssociationFor( accessor ); } catch( IllegalArgumentException e ) { throw e; } catch( Throwable e ) { throw new IllegalArgumentException( e ); } }
@Override public EntityReference map( AssociationDescriptor associationDescriptor ) { return EntityReference.entityReferenceFor( associationState.associationFor( associationDescriptor.accessor() ).get() ); } },
@Test public void script05() throws EntityFinderException { Person person = templateFor( Person.class ); // should return Joe Doe Iterable<EntityReference> entities = entityFinder.findEntities( Person.class, eq( person.mother().get().placeOfBirth().get().name(), "Kuala Lumpur" ), NO_SORTING, NO_FIRST_RESULT, NO_MAX_RESULTS, NO_VARIABLES ); assertNames( entities, JOE ); }
@Test public void script05() throws EntityFinderException { QueryBuilder<Person> qb = this.module.newQueryBuilder( Person.class ); Person person = templateFor( Person.class ); Query<Person> query = unitOfWork.newQuery( qb.where( eq( person.mother() .get() .placeOfBirth() .get() .name(), "Kuala Lumpur" ) ) ); System.out.println( "*** script05: " + query ); verifyUnorderedResults( query, "Joe Doe" ); }
@Test public void script21() throws EntityFinderException { Person person = templateFor( Person.class ); final Query<Person> query = unitOfWork.newQuery( this.module .newQueryBuilder( Person.class ) .where( queries.get( "script21" ) ) ); query.orderBy( orderBy( person.placeOfBirth().get().name() ), orderBy( person.yearOfBirth() ) ); System.out.println( "*** script21: " + query ); verifyOrderedResults( query, "Ann Doe", "Joe Doe", "Jack Doe" ); }
@Test public void script04_ne() throws EntityFinderException { QueryBuilder<Person> qb = this.module.newQueryBuilder( Person.class ); Person personTemplate = templateFor( Person.class ); City placeOfBirth = personTemplate.placeOfBirth().get(); Query<Person> query = unitOfWork.newQuery( qb.where( ne( placeOfBirth.name(), "Kuala Lumpur" ) ) ); System.out.println( "*** script04_ne: " + query ); verifyUnorderedResults( query, "Jack Doe" ); }
@Test public void script04() throws EntityFinderException { QueryBuilder<Person> qb = this.module.newQueryBuilder( Person.class ); Person personTemplate = templateFor( Person.class ); City placeOfBirth = personTemplate.placeOfBirth().get(); Query<Person> query = unitOfWork.newQuery( qb.where( eq( placeOfBirth.name(), "Kuala Lumpur" ) ) ); System.out.println( "*** script04: " + query ); verifyUnorderedResults( query, "Joe Doe", "Ann Doe" ); }
@Test public void script21() throws EntityFinderException { QueryBuilder<Person> qb = this.module.newQueryBuilder( Person.class ); // should return all Persons sorted by name of the city they were born, and then by year they were born Person person = templateFor( Person.class ); Query<Person> query = unitOfWork.newQuery( qb ); query.orderBy( orderBy( person.placeOfBirth().get().name() ), orderBy( person.yearOfBirth() ) ); System.out.println( "*** script21: " + query ); verifyOrderedResults( query, "Ann Doe", "Joe Doe", "Jack Doe" ); }
@Test public void script04() throws EntityFinderException { Person person = templateFor( Person.class ); // should return Joe and Ann Doe Iterable<EntityReference> entities = entityFinder.findEntities( Person.class, eq( person.placeOfBirth().get().name(), "Kuala Lumpur" ), NO_SORTING, NO_FIRST_RESULT, NO_MAX_RESULTS, NO_VARIABLES ); assertNames( entities, JOE, ANN ); }
@Test @SuppressWarnings( "unchecked" ) public void script07() throws EntityFinderException { QueryBuilder<Nameable> qb = this.module.newQueryBuilder( Nameable.class ); Person person = templateFor( Person.class ); Query<Nameable> query = unitOfWork.newQuery( qb.where( and( ge( person.yearOfBirth(), 1900 ), eq( person.placeOfBirth().get().name(), "Penang" ) ) ) ); System.out.println( "*** script07: " + query ); verifyUnorderedResults( query, "Jack Doe" ); }
@Override public EntityReference map( AssociationDescriptor descriptor ) { AssociationDescriptor associationDescriptor; try { associationDescriptor = entityDescriptor.state() .getAssociationByName( descriptor.qualifiedName().name() ); } catch( IllegalArgumentException e ) { return null; } AccessibleObject associationMethod = associationDescriptor.accessor(); Association<Object> association = associationState.associationFor( associationMethod ); return EntityReference.entityReferenceFor( association.get() ); } },
@Test public void script21() throws EntityFinderException { // should return all Persons sorted name of the city they were born Person person = templateFor( Person.class ); Iterable<EntityReference> entities = entityFinder.findEntities( Person.class, ALL, new OrderBy[] { orderBy( person.placeOfBirth().get().name() ), orderBy( person.name() ) }, NO_FIRST_RESULT, NO_MAX_RESULTS, NO_VARIABLES ); assertNames( false, entities, ANN, JOE, JACK ); }
private void addRecord( TimelineRecordStep step, String details ) { ValueBuilder<TimelineRecord> builder = module.newValueBuilder( TimelineRecord.class ); TimelineRecord prototype = builder.prototype(); prototype.step().set( step ); prototype.taskName().set( task().get().name().get() ); List<String> tags = task().get().tags().get(); prototype.taskTags().set( tags ); prototype.timestamp().set( System.currentTimeMillis() ); prototype.scheduleIdentity().set( this.identity().get() ); prototype.details().set( details ); TimelineRecord record = builder.newInstance(); List<TimelineRecord> timelineRecords = state.history().get(); timelineRecords.add( record ); state.history().set( timelineRecords ); }
@Test @SuppressWarnings( "unchecked" ) public void script07() throws EntityFinderException { Person person = templateFor( Person.class ); // should return Jack Doe Iterable<EntityReference> entities = entityFinder.findEntities( Nameable.class, and( ge( person.yearOfBirth(), 1900 ), eq( person.placeOfBirth().get().name(), "Penang" ) ), NO_SORTING, NO_FIRST_RESULT, NO_MAX_RESULTS, NO_VARIABLES ); assertNames( entities, JACK ); }
private TimelineRecord createFutureRecord( long when ) { ValueBuilder<TimelineRecord> builder = module.newValueBuilder( TimelineRecord.class ); TimelineRecord prototype = builder.prototype(); prototype.step().set( TimelineRecordStep.FUTURE ); prototype.taskName().set( me.task().get().name().get() ); List<String> tags = me.task().get().tags().get(); prototype.taskTags().set( tags ); prototype.timestamp().set( when ); prototype.scheduleIdentity().set( me.identity().get() ); prototype.details().set( "" ); return builder.newInstance(); } }
@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; } } } };