Concrete Virtual Services
Let's say you are using the virtual service but you find that they do not complete your requirements, or you need some custom methods or change functionality. Then you will be building concrete services that inherit from the virtual entity service. This is the very purpose of these support classes as most of the time you will have custom requirements and your own style of coding. You will do this in two steps:
    1.
    Inherit from cborm.models.VirtualEntityService
    2.
    Call the super.init() constructor with the entity to root the service and any other options
Below is a sample service layer:
1
component extends="cborm.models.VirtualEntityService" singleton{
2
3
// DI
4
property name="settingService" inject="id:[email protected]";
5
property name="CBHelper" inject="id:[email protected]";
6
property name="log" inject="logbox:logger:{this}";
7
8
/**
9
* Constructor
10
*/
11
CommentService function init(){
12
super.init( entityName="cbComment", useQueryCaching="true");
13
return this;
14
}
15
16
/**
17
* Get the total number of approved comments in the system
18
*/
19
numeric function getApprovedCommentCount(){
20
return countWhere( { "isApproved" = true } );
21
}
22
23
/**
24
* Get the total number of unapproved comments in the system
25
*/
26
numeric function getUnApprovedCommentCount(){
27
return countWhere( { "isApproved" = false } );
28
}
29
30
/**
31
* Comment listing for UI of approved comments, returns struct of results=[comments,count]
32
* @contentID.hint The content ID to filter on
33
* @contentType.hint The content type discriminator to filter on
34
* @max.hint The maximum number of records to return, 0 means all
35
* @offset.hint The offset in the paging, 0 means 0
36
* @sortOrder.hint Sort the comments asc or desc, by default it is desc
37
*/
38
function findApprovedComments(
39
contentID,
40
contentType,
41
max=0,
42
offset=0,
43
sortOrder="desc"
44
){
45
var results = {};
46
var c = newCriteria();
47
48
// only approved comments
49
c.isTrue("isApproved");
50
51
// By Content?
52
if( structKeyExists( arguments,"contentID" ) AND len( arguments.contentID ) ){
53
c.eq( "relatedContent.contentID", idCast( arguments.contentID ) );
54
}
55
// By Content Type Discriminator: class is a special hibernate deal
56
if( structKeyExists( arguments,"contentType" ) AND len( arguments.contentType ) ){
57
c.createCriteria("relatedContent")
58
.isEq( "class", arguments.contentType );
59
}
60
61
// run criteria query and projections count
62
results.count = c.count();
63
results.comments = c.list(
64
offset = arguments.offset,
65
max = arguments.max,
66
sortOrder = "createdDate #arguments.sortOrder#",
67
asQuery = false
68
);
69
70
return results;
71
}
72
}
Copied!
Last modified 2yr ago
Copy link