Skip to content

Commit

Permalink
CBORM-37
Browse files Browse the repository at this point in the history
#resolved
  • Loading branch information
lmajano committed Nov 19, 2024
1 parent 11e45ab commit 6ff96ec
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 40 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- [CBORM-37](https://ortussolutions.atlassian.net/browse/CBORM-37) - Fixed issue on `PostLoad` event handler for multi-datasource entities

### Added

- Added several flow helpers to `ActiveEntity`:
Expand Down
20 changes: 3 additions & 17 deletions models/BaseORMService.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1201,25 +1201,11 @@ component accessors="true" {
* Returns the entity name from a given entity object via session lookup or if new object via metadata lookup
*
* @entity The entity to get it's name from
*
* @return The entity name
*/
function getEntityGivenName( required entity ){
// Short-cut discovery via ActiveEntity
if ( structKeyExists( arguments.entity, "getEntityName" ) ) {
return arguments.entity.getEntityName();
}

// Hibernate Discovery
try {
var entityName = getOrm()
.getSession( getOrm().getEntityDatasource( arguments.entity ) )
.getEntityName( arguments.entity );
} catch ( org.hibernate.TransientObjectException e ) {
// ignore it, it is not in session, go for long-discovery
}

// Long Discovery
var md = getMetadata( arguments.entity );
return ( md.keyExists( "entityName" ) ? md.entityName : listLast( md.name, "." ) );
return getOrm().getEntityGivenName( arguments.entity );
}

/*****************************************************************************************/
Expand Down
48 changes: 25 additions & 23 deletions models/EventHandler.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -17,103 +17,93 @@ component extends="coldbox.system.remote.ColdboxProxy" implements="CFIDE.orm.IEv
* preLoad called by hibernate which in turn announces a coldbox interception: ORMPreLoad
*/
public void function preLoad( any entity ){
announce( "ORMPreLoad", { entity : arguments.entity } );
announce( "ORMPreLoad", { "entity" : arguments.entity } );
}

/**
* postLoad called by hibernate which in turn announces a coldbox interception: ORMPostLoad
*/
public void function postLoad( any entity ){
var args = { entity : arguments.entity, entityName : "" };

// Short-cut discovery via ActiveEntity
if ( structKeyExists( arguments.entity, "getEntityName" ) ) {
args.entityName = arguments.entity.getEntityName();
} else {
// it must be in session.
args.entityName = ormGetSession().getEntityName( arguments.entity );
}

var args = { "entity" : arguments.entity, "entityName" : getOrm().getEntityGivenName( arguments.entity ) };
processEntityInjection( args.entityName, args.entity );

announce( "ORMPostLoad", args );
}

/**
* postDelete called by hibernate which in turn announces a coldbox interception: ORMPostDelete
*/
public void function postDelete( any entity ){
announce( "ORMPostDelete", { entity : arguments.entity } );
announce( "ORMPostDelete", { "entity" : arguments.entity } );
}

/**
* preDelete called by hibernate which in turn announces a coldbox interception: ORMPreDelete
*/
public void function preDelete( any entity ){
announce( "ORMPreDelete", { entity : arguments.entity } );
announce( "ORMPreDelete", { "entity" : arguments.entity } );
}

/**
* preUpdate called by hibernate which in turn announces a coldbox interception: ORMPreUpdate
*/
public void function preUpdate( any entity, Struct oldData = {} ){
announce( "ORMPreUpdate", { entity : arguments.entity, oldData : arguments.oldData } );
announce( "ORMPreUpdate", { "entity" : arguments.entity, "oldData" : arguments.oldData } );
}

/**
* postUpdate called by hibernate which in turn announces a coldbox interception: ORMPostUpdate
*/
public void function postUpdate( any entity ){
announce( "ORMPostUpdate", { entity : arguments.entity } );
announce( "ORMPostUpdate", { "entity" : arguments.entity } );
}

/**
* preInsert called by hibernate which in turn announces a coldbox interception: ORMPreInsert
*/
public void function preInsert( any entity ){
announce( "ORMPreInsert", { entity : arguments.entity } );
announce( "ORMPreInsert", { "entity" : arguments.entity } );
}

/**
* postInsert called by hibernate which in turn announces a coldbox interception: ORMPostInsert
*/
public void function postInsert( any entity ){
announce( "ORMPostInsert", { entity : arguments.entity } );
announce( "ORMPostInsert", { "entity" : arguments.entity } );
}

/**
* preSave called by ColdBox Base service before save() calls
*/
public void function preSave( any entity ){
announce( "ORMPreSave", { entity : arguments.entity } );
announce( "ORMPreSave", { "entity" : arguments.entity } );
}

/**
* postSave called by ColdBox Base service after transaction commit or rollback via the save() method
*/
public void function postSave( any entity ){
announce( "ORMPostSave", { entity : arguments.entity } );
announce( "ORMPostSave", { "entity" : arguments.entity } );
}

/**
* Called before the session is flushed.
*/
public void function preFlush( any entities ){
announce( "ORMPreFlush", { entities : arguments.entities } );
announce( "ORMPreFlush", { "entities" : arguments.entities } );
}

/**
* Called after the session is flushed.
*/
public void function postFlush( any entities ){
announce( "ORMPostFlush", { entities : arguments.entities } );
announce( "ORMPostFlush", { "entities" : arguments.entities } );
}

/**
* postNew called by ColdBox which in turn announces a coldbox interception: ORMPostNew
*/
public void function postNew( any entity, any entityName ){
var args = { entity : arguments.entity, entityName : "" };
var args = { "entity" : arguments.entity, "entityName" : "" };

// Do we have an incoming name
if ( !isNull( arguments.entityName ) && len( arguments.entityName ) ) {
Expand Down Expand Up @@ -176,4 +166,16 @@ component extends="coldbox.system.remote.ColdboxProxy" implements="CFIDE.orm.IEv
return arguments.entity;
}

/**
* Lazy loading of the ORM utility according to the CFML engine you are on
*
* @return cborm.models.util.IORMUtil
*/
private function getOrm(){
if ( isNull( variables.orm ) ) {
variables.orm = new cborm.models.util.ORMUtilFactory().getORMUtil();
}
return variables.orm;
}

}
25 changes: 25 additions & 0 deletions models/util/support/ORMUtilSupport.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,29 @@ component singleton {
.getEntityMode();
}

/**
* Returns the entity name from a given entity object via session lookup or if new object via metadata lookup
*
* @entity The entity to get it's name from
*
* @return The entity name
*/
function getEntityGivenName( required entity ){
// Short-cut discovery via ActiveEntity
if ( structKeyExists( arguments.entity, "getEntityName" ) ) {
return arguments.entity.getEntityName();
}

// Hibernate Discovery
try {
var entityName = getSession( getEntityDatasource( arguments.entity ) ).getEntityName( arguments.entity );
} catch ( org.hibernate.TransientObjectException e ) {
// ignore it, it is not in session, go for long-discovery
}

// Long Discovery
var md = getMetadata( arguments.entity );
return ( md.keyExists( "entityName" ) ? md.entityName : listLast( md.name, "." ) );
}

}

0 comments on commit 6ff96ec

Please sign in to comment.