public void updateEntity( UnitOfWork current ) { try { entity = current.get( entity ); } catch( NoSuchEntityException e ) { // Entity was deleted entity = null; } }
@Override public Schedule scheduleCron( Task task, String cronExpression, long initialDelay, boolean durable ) { DateTime start = new DateTime( System.currentTimeMillis() + initialDelay ); Schedule schedule = scheduleFactory.newCronSchedule( task, cronExpression, start, durable ); if( durable ) { Schedules schedules = module.currentUnitOfWork().get( Schedules.class, getSchedulesIdentity( me ) ); schedules.schedules().add( schedule ); } dispatchForExecution( schedule ); return schedule; }
@Override public Schedule scheduleOnce( Task task, int initialSecondsDelay, boolean durable ) { long now = System.currentTimeMillis(); Schedule schedule = scheduleFactory.newOnceSchedule( task, new DateTime( now + initialSecondsDelay * 1000 ), durable ); if( durable ) { Schedules schedules = module.currentUnitOfWork().get( Schedules.class, getSchedulesIdentity( me ) ); schedules.schedules().add( schedule ); } dispatchForExecution( schedule ); return schedule; }
@Override public Iterable<TimelineRecord> getRecords( DateTime from, DateTime to ) { SortedSet<TimelineRecord> result = new TreeSet<>(); UnitOfWork uow = module.currentUnitOfWork(); String schedulesName = SchedulerMixin.getSchedulesIdentity( scheduler ); Schedules schedules = uow.get( Schedules.class, schedulesName ); for( Schedule schedule : schedules.schedules() ) { Timeline timeline = (Timeline) schedule; Iterable<TimelineRecord> lastRecords = timeline.getRecords( from, to ); Iterables.addAll( result, lastRecords ); } return result; }
@Override public Iterable<TimelineRecord> getRecords( long from, long to ) { SortedSet<TimelineRecord> result = new TreeSet<>(); UnitOfWork uow = module.currentUnitOfWork(); String schedulesName = SchedulerMixin.getSchedulesIdentity( scheduler ); Schedules schedules = uow.get( Schedules.class, schedulesName ); for( Schedule schedule : schedules.schedules() ) { Timeline timeline = (Timeline) schedule; Iterable<TimelineRecord> lastRecords = timeline.getRecords( from, to ); Iterables.addAll( result, lastRecords ); } return result; } }
protected <T> T select( Class<T> entityClass, String id ) throws ResourceException { try { T composite = module.currentUnitOfWork().get( entityClass, id ); current().select( composite ); return composite; } catch( EntityTypeNotFoundException | NoSuchEntityException e ) { throw new ResourceException( Status.CLIENT_ERROR_NOT_FOUND ); } }
@Override public void setAttribute( Attribute attribute ) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { UnitOfWork uow = module.newUnitOfWork(); try { EntityComposite configuration = uow.get( EntityComposite.class, identity ); AssociationStateHolder state = spi.stateOf( configuration ); AccessibleObject accessor = propertyNames.get( attribute.getName() ); Property<Object> property = state.propertyFor( accessor ); property.set( attribute.getValue() ); try { uow.complete(); } catch ( UnitOfWorkCompletionException e ) { throw new ReflectionException( e ); } } finally { uow.discard(); } }
protected <T> T selectFromNamedAssociation( NamedAssociation<T> namedAssociation, String id ) throws ResourceException { T entity = (T) module.currentUnitOfWork().get( Object.class, id ); String name = namedAssociation.nameOf( entity ); if(name == null) { throw new ResourceException( Status.CLIENT_ERROR_NOT_FOUND ); } current().select( entity ); return entity; }
@Override public Schedule scheduleCron( Task task, String cronExpression, boolean durable ) { DateTime now = new DateTime(); Schedule schedule = scheduleFactory.newCronSchedule( task, cronExpression, now, durable ); if( durable ) { Schedules schedules = module.currentUnitOfWork().get( Schedules.class, getSchedulesIdentity( me ) ); schedules.schedules().add( schedule ); } dispatchForExecution( schedule ); return schedule; }
@Override public Iterable<TimelineRecord> getNextRecords( int maxResults ) { SortedSet<TimelineRecord> result = new TreeSet<>(); UnitOfWork uow = module.currentUnitOfWork(); String schedulesName = SchedulerMixin.getSchedulesIdentity( scheduler ); Schedules schedules = uow.get( Schedules.class, schedulesName ); for( Schedule schedule : schedules.schedules() ) { Timeline timeline = (Timeline) schedule; Iterable<TimelineRecord> lastRecords = timeline.getNextRecords( maxResults ); Iterables.addAll( result, lastRecords ); } return Iterables.limit( maxResults, result ); }
protected <T> T selectFromManyAssociation( ManyAssociation<T> manyAssociation, String id ) throws ResourceException { T entity = (T) module.currentUnitOfWork().get( Object.class, id ); if( !manyAssociation.contains( entity ) ) { throw new ResourceException( Status.CLIENT_ERROR_NOT_FOUND ); } current().select( entity ); return entity; }
@Override public Object getAttribute( String name ) throws AttributeNotFoundException, MBeanException, ReflectionException { UnitOfWork uow = module.newUnitOfWork(); try { EntityComposite configuration = uow.get( EntityComposite.class, identity ); AssociationStateHolder state = spi.stateOf( configuration ); AccessibleObject accessor = propertyNames.get( name ); Property<Object> property = state.propertyFor( accessor ); return property.get(); } catch ( Exception ex ) { throw new ReflectionException( ex, "Could not get attribute " + name ); } finally { uow.discard(); } }
@Test public void script36() { QueryBuilder<Person> qb = this.module.newQueryBuilder( Person.class ); Person person = templateFor( Person.class ); Account anns = unitOfWork.get( Account.class, "accountOfAnnDoe" ); Query<Person> query = unitOfWork.newQuery( qb.where( contains( person.accounts(), anns ) ) ); System.out.println( "*** script36: " + query ); verifyUnorderedResults( query, "Jack Doe", "Ann Doe" ); }
@Test public void script33() { QueryBuilder<Person> qb = this.module.newQueryBuilder( Person.class ); Person person = templateFor( Person.class ); Domain gaming = unitOfWork.get( Domain.class, "Gaming" ); Query<Person> query = unitOfWork.newQuery( qb.where( contains( person.interests(), gaming ) ) ); System.out.println( "*** script33: " + query ); verifyUnorderedResults( query, "Joe Doe" ); }
@Test public void script34() { QueryBuilder<Person> qb = this.module.newQueryBuilder( Person.class ); Person person = templateFor( Person.class ); Female annDoe = unitOfWork.get( Female.class, "anndoe" ); Query<Person> query = unitOfWork.newQuery( qb.where( eq( person.mother(), annDoe ) ) ); System.out.println( "*** script34: " + query ); verifyUnorderedResults( query, "Joe Doe" ); }
@Override public Iterable<TimelineRecord> getLastRecords( int maxResults ) { SortedSet<TimelineRecord> result = new TreeSet<>(); UnitOfWork uow = module.currentUnitOfWork(); String schedulesName = SchedulerMixin.getSchedulesIdentity( scheduler ); Schedules schedules = uow.get( Schedules.class, schedulesName ); for( Schedule schedule : schedules.schedules() ) { Timeline timeline = (Timeline) schedule; Iterable<TimelineRecord> lastRecords = timeline.getLastRecords( maxResults ); Iterables.addAll( result, lastRecords ); } return Iterables.limit( maxResults, Iterables.reverse( result ) ); }
@Override public Schedule scheduleOnce( Task task, DateTime runAt, boolean durable ) { Schedule schedule = scheduleFactory.newOnceSchedule( task, runAt, durable ); dispatchForExecution( schedule ); if( durable ) { Schedules schedules = module.currentUnitOfWork().get( Schedules.class, getSchedulesIdentity( me ) ); schedules.schedules().add( schedule ); } return schedule; }
@Override public Schedule scheduleCron( Task task, @CronExpression String cronExpression, DateTime start, boolean durable ) { Schedule schedule = scheduleFactory.newCronSchedule( task, cronExpression, start, durable ); if( durable ) { Schedules schedules = module.currentUnitOfWork().get( Schedules.class, getSchedulesIdentity( me ) ); schedules.schedules().add( schedule ); } dispatchForExecution( schedule ); return schedule; }
@SuppressWarnings( "unchecked" ) public <V> V findConfigurationInstanceFor( ServiceComposite serviceComposite, UnitOfWork uow ) throws InstantiationException { ServiceDescriptor serviceModel = api.serviceDescriptorFor( serviceComposite ); String identity = serviceComposite.identity().get(); V configuration; try { configuration = uow.get( serviceModel.<V>configurationType(), identity ); uow.pause(); } catch( NoSuchEntityException | EntityTypeNotFoundException e ) { return (V) initializeConfigurationInstance( serviceComposite, uow, serviceModel, identity ); } return configuration; }
@Test public void givenEntityStoredLoadedChangedWhenUnitOfWorkDiscardsThenDontStoreState() throws UnitOfWorkCompletionException { UnitOfWork unitOfWork = module.newUnitOfWork(); try { String identity = createEntity( unitOfWork ).identity().get(); unitOfWork.complete(); unitOfWork = module.newUnitOfWork(); TestEntity entity = unitOfWork.get( TestEntity.class, identity ); assertThat( entity.intValue().get(), is( 42 ) ); entity.intValue().set( 23 ); unitOfWork.discard(); unitOfWork = module.newUnitOfWork(); entity = unitOfWork.get( TestEntity.class, identity ); assertThat( entity.intValue().get(), is( 42 ) ); } finally { unitOfWork.discard(); } }