Last active
March 4, 2016 09:12
-
-
Save evagoras/c717a28fce4550f5235c to your computer and use it in GitHub Desktop.
ColdBox base object for Beans using Virtual Entity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
component { | |
// Framework Services | |
property name="cbEntityService" inject="entityService"; | |
property name="cbValidator" inject="ValidationManager@cbvalidation"; | |
// Custom Services | |
property name="utils" inject="utils" persistent=false getter=false setter=false; | |
public any function init() { | |
return this; | |
} | |
public void function populate | |
( required struct memento ) | |
{ | |
var obj = { | |
"data" = {}, | |
"nulls" = [] | |
}; | |
var keyValue = ""; | |
for ( var key in arguments.memento ) { | |
if ( structKeyExists(arguments.memento, key) ) { | |
keyValue = arguments.memento[ key ]; | |
if ( len(keyValue) && isValid("eurodate", keyValue) ) { | |
obj.data[ key ] = parseDateTime( keyValue ); | |
} | |
else { | |
obj.data[ key ] = arguments.memento[ key ]; | |
} | |
} | |
else { | |
obj.nulls.append( key ); | |
} | |
} | |
variables.cbEntityService.populate( | |
target = this, | |
memento = obj.data, | |
exclude = obj.nulls.toString() | |
); | |
if ( obj.nulls.len() ) this.makeNulls( obj.nulls ); | |
} | |
public any function validate() { | |
return cbValidator.validate( this ); | |
} | |
public any function getValidationErrors() { | |
var validation = this.validate(); | |
var errors = {}; | |
for ( var error in validation.getErrors() ) { | |
var fieldName = error.getField(); | |
if ( ! errors.keyExists( fieldName ) ) errors[ fieldName ] = []; | |
errors[ fieldName ].append( | |
{ | |
"type" = error.getValidationType().lcase(), | |
"data" = error.getValidatioNData(), | |
"message" = error.getMessage() | |
} | |
); | |
} | |
return errors; | |
} | |
public struct function save | |
( | |
boolean forceInsert = false, | |
boolean flush = false, | |
boolean transactional = true | |
) | |
{ | |
cbEntityService.save( | |
entity = this, | |
forceInsert = arguments.forceInsert, | |
flush = arguments.flush, | |
transactional = arguments.transactional | |
); | |
return this.dto(); | |
} | |
public void function delete( boolean transactional=true ) { | |
variables.cbEntityService.delete( | |
entity = this, | |
transactional = arguments.transactional | |
); | |
} | |
/** | |
* Returns the current entity (dirty or otherwise) as a structure | |
* | |
* @param boolean renamekey Whether or not to rename the "id" column of the structure to reflect the entity name (important for auto-population of new models) | |
* @param boolean lower Whether or not to lower case all property names | |
**/ | |
public function dto | |
( | |
boolean renameKey = false, | |
boolean lower = false | |
) | |
{ | |
var s = {}; | |
var properties = ormProperties(); | |
for ( fieldName in properties ) { | |
var accessor = "get" & fieldName; | |
var getField = this[ accessor ]; | |
var field = fieldName; | |
if ( arguments.lower ) { | |
field = fieldName.lcase(); | |
} | |
var fieldValue = getField(); | |
var nullField = isNull( fieldValue ); | |
var jsonContent = ""; | |
if ( ! nullField && isSimpleValue( fieldValue ) ) { | |
s[field] = getField(); | |
if ( properties[fieldName].keyExists("ormtype") && listFindNoCase("timestamp,date", properties[fieldName].ormtype ) ) { | |
s[field] = utils.getIsoTimeString( s[field] ); | |
} | |
//auto-unpack any json fields | |
if ( isJSON( s[field]) ) { | |
//writeoutput( "value=" & s[field] & "<br>" & fieldname & "=" & getMetadata( s[field] ).getName() & "<hr>" ); | |
jsonContent = deSerializeJSON( s[field] ); | |
if ( isArray(jsonContent) || isStruct(jsonContent) ) | |
s[field] = deSerializeJSON( s[field] ); | |
} | |
} | |
else if ( ! nullField && isObject( fieldValue ) ) { | |
s[field] = fieldValue.getId(); | |
} | |
else { | |
s[field] = javaNull(); | |
} | |
//writeoutput( "value=" & s[field] & "<br>" & fieldname & "=" & getMetadata( s[field] ).getName() & "<hr>" ); | |
} | |
if ( arguments.renameKey && structKeyExists( s,"id" ) ) { | |
s[ lcase( getMetadata(this).name ) & "_id" ] = s.id; | |
structDelete( s,"id" ); | |
} | |
//writeoutput( "value=" & s["detail"] & "<br>" & fieldname & "=" & getMetadata( s["detail"] ).getName() & "<hr>" ); | |
//abort; | |
return s; | |
} | |
public void function makeNulls | |
( required array properties ) | |
{ | |
for ( property in properties ) { | |
this.makeNull( property ); | |
} | |
} | |
public void function makeNull | |
( required string property ) | |
{ | |
variables[ arguments.property ] = javaCast( "null", 0 ); | |
} | |
/* | |
-------------------------- PRIVATE METHODS -------------------------- | |
*/ | |
/** | |
* Returns an array of all ORM properties in the entity | |
**/ | |
private function ormPropertyNames() { | |
var aProps = getMetaData(this).properties; | |
var propNames = []; | |
for ( var prop in aProps ) { | |
if ( | |
! structKeyExists(prop, "inject") | |
&& | |
! ( structKeyExists(prop, "fieldtype") && listFindNoCase("one-to-many,many-to-many,many-to-one", prop.fieldtype) ) | |
) | |
{ | |
propNames.append( prop.name ); | |
} | |
} | |
return propNames; | |
} | |
private struct function ormProperties() { | |
var aProps = getMetaData(this).properties; | |
var props = {}; | |
for ( var prop in aProps ) { | |
if ( | |
! structKeyExists(prop, "inject") | |
&& | |
! ( structKeyExists(prop, "fieldtype") && listFindNoCase("one-to-many,many-to-many,many-to-one", prop.fieldtype) ) | |
) | |
{ | |
props[ prop.name ] = prop; | |
} | |
} | |
return props; | |
} | |
private any function javaNull() { | |
return javaCast( "null", "" ); | |
} | |
boolean function isPersisted(required string id) | |
hint="I return true if the Entity is persisted" | |
output=false | |
{ | |
var entityName = getMetaData(this).entityName; | |
if (isNull(entityLoad(entityName, arguments.id, true))) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment