Skip to content

Commit

Permalink
BIFs - Retrieve ORM service in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelborn committed Nov 2, 2024
1 parent 11d721e commit ad4b502
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.apache.commons.lang3.NotImplementedException;

import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -10,6 +13,11 @@
@BoxBIF
public class EntityDelete extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* ExampleBIF
*
Expand All @@ -18,6 +26,7 @@ public class EntityDelete extends BIF {
*/
public String _invoke( IBoxContext context, ArgumentsScope arguments ) {
// TODO implement BIF
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
throw new NotImplementedException();
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/ortus/boxlang/modules/orm/bifs/EntityLoad.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.apache.commons.lang3.NotImplementedException;

import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -10,6 +13,11 @@
@BoxBIF
public class EntityLoad extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* ExampleBIF
*
Expand All @@ -18,6 +26,7 @@ public class EntityLoad extends BIF {
*/
public String _invoke( IBoxContext context, ArgumentsScope arguments ) {
// TODO implement BIF
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
throw new NotImplementedException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.apache.commons.lang3.NotImplementedException;

import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -10,6 +13,11 @@
@BoxBIF
public class EntityLoadByExample extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* ExampleBIF
*
Expand All @@ -18,6 +26,7 @@ public class EntityLoadByExample extends BIF {
*/
public String _invoke( IBoxContext context, ArgumentsScope arguments ) {
// TODO implement BIF
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
throw new NotImplementedException();
}

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/ortus/boxlang/modules/orm/bifs/EntityLoadByPK.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -19,12 +20,19 @@
@BoxBIF
public class EntityLoadByPK extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* Constructor
*/
public EntityLoadByPK() {

super();
declaredArguments = new Argument[] {
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
declaredArguments = new Argument[] {
new Argument( true, "String", ORMKeys.entity, Set.of( Validator.REQUIRED, Validator.NON_EMPTY ) ),
new Argument( true, "String", Key.id, Set.of( Validator.REQUIRED, Validator.NON_EMPTY ) ),
new Argument( false, "String", ORMKeys.unique, Set.of( Validator.NOT_IMPLEMENTED ) )
Expand All @@ -51,7 +59,7 @@ public EntityLoadByPK() {
* @param arguments Argument scope for the BIF.
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
Session session = ORMService.getInstance().getORMApp( context ).getSession( context );
Session session = this.ormService.getORMApp( context ).getSession( context );

// @TODO: Move this to a more sensible location.
if ( session.getTransaction() == null ) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/ortus/boxlang/modules/orm/bifs/EntityMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.apache.commons.lang3.NotImplementedException;

import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -10,6 +13,11 @@
@BoxBIF
public class EntityMerge extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* ExampleBIF
*
Expand All @@ -18,6 +26,7 @@ public class EntityMerge extends BIF {
*/
public String _invoke( IBoxContext context, ArgumentsScope arguments ) {
// TODO implement BIF
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
throw new NotImplementedException();
}

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/ortus/boxlang/modules/orm/bifs/EntityNameArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.modules.orm.mapping.EntityRecord;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -22,12 +23,19 @@
@BoxBIF( alias = "EntityNameList" )
public class EntityNameArray extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* Constructor
*/
public EntityNameArray() {

super();
declaredArguments = new Argument[] {
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
declaredArguments = new Argument[] {
new Argument( false, "String", Key.delimiter, Set.of( Validator.NON_EMPTY ) ),
new Argument( false, "String", Key.datasource, Set.of( Validator.NON_EMPTY ) ),
};
Expand All @@ -51,7 +59,7 @@ public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
}
String datasourceName = ( String ) arguments.getAsString( Key.datasource );

ORMApp ormApp = ORMService.getInstance().getORMApp( context );
ORMApp ormApp = this.ormService.getORMApp( context );
List<EntityRecord> entityList = datasourceName != null
? ormApp.getEntityRecords( datasourceName )
: ormApp.getEntityRecords();
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/ortus/boxlang/modules/orm/bifs/EntityNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.modules.orm.hibernate.BoxClassInstantiator;
import ortus.boxlang.modules.orm.mapping.EntityRecord;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.ApplicationBoxContext;
Expand All @@ -23,12 +24,19 @@
@BoxBIF
public class EntityNew extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* Constructor
*/
public EntityNew() {

super();
declaredArguments = new Argument[] {
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
declaredArguments = new Argument[] {
new Argument( true, "String", ORMKeys.entityName, Set.of( Validator.REQUIRED, Validator.NON_EMPTY ) ),
new Argument( false, "Struct", Key.properties )
};
Expand All @@ -41,7 +49,7 @@ public EntityNew() {
* @param arguments Argument scope for the BIF.
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
Session session = ORMService.getInstance().getORMApp( context ).getSession( context );
Session session = this.ormService.getORMApp( context ).getSession( context );
EntityRecord entityRecord = SessionFactoryBuilder.lookupEntity( session, arguments.getAsString( ORMKeys.entityName ) );
IStruct properties = arguments.containsKey( Key.properties ) ? arguments.getAsStruct( Key.properties ) : Struct.EMPTY;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.apache.commons.lang3.NotImplementedException;

import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -10,6 +13,11 @@
@BoxBIF
public class EntityReload extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* ExampleBIF
*
Expand All @@ -18,6 +26,7 @@ public class EntityReload extends BIF {
*/
public String _invoke( IBoxContext context, ArgumentsScope arguments ) {
// TODO implement BIF
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
throw new NotImplementedException();
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/ortus/boxlang/modules/orm/bifs/EntitySave.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -20,14 +21,21 @@
@BoxBIF
public class EntitySave extends BIF {

Logger logger = LoggerFactory.getLogger( EntitySave.class );
/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

Logger logger = LoggerFactory.getLogger( EntitySave.class );

/**
* Constructor
*/
public EntitySave() {

super();
declaredArguments = new Argument[] {
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
declaredArguments = new Argument[] {
new Argument( true, "Any", ORMKeys.entity, Set.of( Validator.REQUIRED, Validator.NON_EMPTY ) ),
new Argument( false, "Boolean", ORMKeys.forceinsert )
};
Expand All @@ -42,7 +50,7 @@ public EntitySave() {
* @return
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
Session session = ORMService.getInstance().getORMApp( context ).getSession( context );
Session session = this.ormService.getORMApp( context ).getSession( context );
// @TODO: Implement forceinsert
IClassRunnable entity = ( IClassRunnable ) arguments.get( ORMKeys.entity );
// @TODO: Should we look up the EntityRecord and use that to grab the class name?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.apache.commons.lang3.NotImplementedException;

import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -10,6 +13,11 @@
@BoxBIF
public class EntityToQuery extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* ExampleBIF
*
Expand All @@ -18,6 +26,7 @@ public class EntityToQuery extends BIF {
*/
public String _invoke( IBoxContext context, ArgumentsScope arguments ) {
// TODO implement BIF
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
throw new NotImplementedException();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ortus.boxlang.modules.orm.bifs;

import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -8,6 +9,11 @@
@BoxBIF
public class GetHibernateVersion extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* Retrieve the installed Hibernate version.
*
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/ortus/boxlang/modules/orm/bifs/ORMClearSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import ortus.boxlang.modules.orm.ORMService;
import ortus.boxlang.modules.orm.config.ORMKeys;
import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIF;
import ortus.boxlang.runtime.bifs.BoxBIF;
import ortus.boxlang.runtime.context.IBoxContext;
Expand All @@ -18,12 +19,19 @@
@BoxBIF
public class ORMClearSession extends BIF {

/**
* ORM Service, responsible for managing ORM applications.
*/
private ORMService ormService;

/**
* Constructor
*/
public ORMClearSession() {

super();
declaredArguments = new Argument[] {
this.ormService = ( ORMService ) BoxRuntime.getInstance().getGlobalService( ORMKeys.ORMService );
declaredArguments = new Argument[] {
new Argument( false, "String", ORMKeys.datasource, Set.of( Validator.NON_EMPTY ) )
};
}
Expand All @@ -42,9 +50,9 @@ public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
Session session;
String datasourceName = StringCaster.attempt( arguments.get( ORMKeys.datasource ) ).getOrDefault( "" );
if ( !datasourceName.isBlank() ) {
session = ORMService.getInstance().getORMApp( context ).getSession( context, Key.of( datasourceName ) );
session = this.ormService.getORMApp( context ).getSession( context, Key.of( datasourceName ) );
} else {
session = ORMService.getInstance().getORMApp( context ).getSession( context );
session = this.ormService.getORMApp( context ).getSession( context );
}
session.clear();

Expand Down
Loading

0 comments on commit ad4b502

Please sign in to comment.