/SampleApplication/ColdBox3/model/abstract/AbstractService.cfc

http://github.com/bobsilverberg/ValidateThisColdBoxPlugin · ColdFusion CFScript · 161 lines · 82 code · 23 blank · 56 comment · 5 complexity · 50e45b767262f36c408f3a6f309bf289 MD5 · raw file

  1. /**
  2. Abstract Service
  3. */
  4. component
  5. {
  6. /*
  7. ---------------------------------------------------------------------------
  8. autowire
  9. ---------------------------------------------------------------------------
  10. */
  11. property name="BeanFactory" type="coldbox:plugin:BeanFactory" scope="instance";
  12. property name="ValidateThis" type="coldbox:myplugin:ValidateThisCBPlugin" scope="instance";
  13. /*
  14. ---------------------------------------------------------------------------
  15. instance variables
  16. ---------------------------------------------------------------------------
  17. */
  18. instance = {};
  19. /*
  20. ---------------------------------------------------------------------------
  21. constructor
  22. ---------------------------------------------------------------------------
  23. */
  24. any function init()
  25. {
  26. return this;
  27. }
  28. /*
  29. ---------------------------------------------------------------------------
  30. public methods
  31. ---------------------------------------------------------------------------
  32. */
  33. /**
  34. * validates, then inserts or updates a persisted entity (if valid)
  35. */
  36. ValidateThis.util.Result function save( required entity )
  37. {
  38. var ValidatorResult = "";
  39. transaction
  40. {
  41. instance.BeanFactory.populateModel( arguments.entity );
  42. ValidatorResult = instance.ValidateThis.validate( theObject=arguments.entity );
  43. if ( ValidatorResult.getIsSuccess() )
  44. {
  45. // Save entity and commit transaction
  46. EntitySave( arguments.entity );
  47. transactionCommit();
  48. }
  49. else
  50. {
  51. // rollback setter calls
  52. transactionRollback();
  53. }
  54. }
  55. return ValidatorResult;
  56. }
  57. /**
  58. * deletes a persisted entity
  59. */
  60. void function delete( required entity )
  61. {
  62. transaction
  63. {
  64. EntityDelete( arguments.entity );
  65. }
  66. }
  67. /**
  68. * returns a new validation result
  69. */
  70. ValidateThis.util.Result function newValidationResult()
  71. {
  72. return instance.ValidateThis.newResult();
  73. }
  74. /*
  75. ---------------------------------------------------------------------------
  76. private methods
  77. ---------------------------------------------------------------------------
  78. */
  79. /**
  80. * helper method to delete an entity by id
  81. */
  82. private boolean function deleteById( required entityname, required id )
  83. {
  84. var deleted = false;
  85. transaction{
  86. var entity = EntityLoadByPK( arguments.entityname, arguments.id );
  87. if ( !IsNull( entity ) )
  88. {
  89. EntityDelete( entity );
  90. deleted = true;
  91. }
  92. }
  93. return deleted;
  94. }
  95. /**
  96. * returns an array of entities matching the filter
  97. */
  98. private array function findByfilter( required entityname, filter=StructNew(), sortorder="", options=StructNew() )
  99. {
  100. return EntityLoad( arguments.entityname, arguments.filter, arguments.sortorder, arguments.options );
  101. }
  102. /**
  103. * retrieves an entity by name and id
  104. */
  105. private any function getById( required entityname, id="" )
  106. {
  107. var result = "";
  108. if ( Len( arguments.id ) != 0 )
  109. {
  110. // id passed so retrieve
  111. result = EntityLoadByPK( arguments.entityname, arguments.id );
  112. if ( IsNull( result ) )
  113. {
  114. // not found
  115. result = new( arguments.entityname );
  116. }
  117. }
  118. else
  119. {
  120. result = new( arguments.entityname );
  121. }
  122. return result;
  123. }
  124. /**
  125. * return a struct of required fields for the named entity
  126. */
  127. private struct function getRequiredFields( required entityname )
  128. {
  129. return instance.ValidateThis.getRequiredFields( objectType=arguments.entityname );
  130. }
  131. /**
  132. * returns a new persisted entity
  133. */
  134. private any function new( required entityname )
  135. {
  136. return EntityNew( arguments.entityname );
  137. }
  138. }