{
public static void main( String[] args )
throws Exception
{
final Application application = new Energy4Java().newApplication( new AppAssembler() );
application.activate();
Runtime.getRuntime().addShutdownHook( new Thread( new Runnable()
{
@Override
@SuppressWarnings( "CallToThreadDumpStack" )
public void run()
{
try {
application.passivate();
} catch ( Exception ex ) {
System.err.println( "Unable to passivate Qi4j application!" );
ex.printStackTrace();
}
}
} ) );
Module domainModule = application.findModule( "app", "domain" );
int exitStatus = 0;
try {
UnitOfWork uow = domainModule.newUnitOfWork();
EntityBuilder<PretextEntity> builder = uow.newEntityBuilder( PretextEntity.class );
PretextEntity pretext = builder.instance();
pretext.reason().set( "Testing purpose" );
builder.newInstance();
uow.complete();
uow = domainModule.newUnitOfWork();
QueryBuilder<PretextEntity> queryBuilder = domainModule.newQueryBuilder( PretextEntity.class );
queryBuilder = queryBuilder.where( eq( templateFor( PretextEntity.class ).reason(), "Testing purpose" ) );
Query<PretextEntity> query = uow.newQuery( queryBuilder );
pretext = query.find();
if ( pretext == null ) {
System.err.println( "ERROR: Unable to find pretext!" );
exitStatus = -1;
} else {
System.out.println( "SUCCESS: Found Pretext with reason: " + pretext.reason().get() );
}
uow.discard();
} finally {
deleteData( application.findModule( "infra", "persistence" ) );
}
System.exit( exitStatus );
}