EntityEncoder customization

27 views
Skip to first unread message

Сергей Нелюбин

unread,
Oct 17, 2019, 8:52:32 AM10/17/19
to Agrest Framework User Group
Hello, Andrus!

Can i customize EntityEncoder for include calculated from persistence fields?

For example:
EntityA {
    int a = 2
    int b = 3
}

I want to get http response:
{
    a: 2,
    b: 3,
    c: 6 // calculated as c=a*b
}     

P.S. logic of calculation may be complex, and i can't make it on data layer, but i need it on application(agrest) layer

Andrus Adamchik

unread,
Oct 17, 2019, 9:40:31 AM10/17/19
to Agrest Framework User Group
Hi Sergey,

The best way to handle it is via AgEntityOverlay. It allows to register a property that does not belong to the entity, with an arbitrary calculation function. We are improving overlays significantly in 3.4 (e.g. we'll allow to set it per-request, there's a more powerful builder API for overlays, etc.). Still in the current 3.3 (and earlier) you can register an overlay in the runtime scope like this:

AgRuntime agRuntime = AgBuilder
.builder(cayenneRuntime)
.entityOverlay(new AgEntityOverlay(EntityA.class).addAttribute("c", Integer.class, ea -> ea.a * ea.b))
.build();


Hope this helps.

Andrus
> --
> You received this message because you are subscribed to the Google Groups "Agrest Framework User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to agrest-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/agrest-user/2946a507-b741-41db-8b69-1eddd333ec47%40googlegroups.com.

Сергей Нелюбин

unread,
Oct 18, 2019, 2:31:05 AM10/18/19
to Agrest Framework User Group
Thanks!

And new question: can i use only simple type (Integer.class for example) for this or collection or custom Pojo?


четверг, 17 октября 2019 г., 16:40:31 UTC+3 пользователь Andrus Adamchik написал:
Hi Sergey,

The best way to handle it is via AgEntityOverlay. It allows to register a property that does not belong to the entity, with an arbitrary calculation function. We are improving overlays significantly in 3.4 (e.g. we'll allow to set it per-request, there's a more powerful builder API for overlays, etc.). Still in the current 3.3 (and earlier) you can register an overlay in the runtime scope like this:

AgRuntime agRuntime = AgBuilder
   .builder(cayenneRuntime)
   .entityOverlay(new AgEntityOverlay(EntityA.class).addAttribute("c", Integer.class, ea -> ea.a * ea.b))
   .build();


Hope this helps.

Andrus


> On Oct 17, 2019, at 8:52 AM, Сергей Нелюбин <sergey....@gmail.com> wrote:
>
> Hello, Andrus!
>
> Can i customize EntityEncoder for include calculated from persistence fields?
>
> For example:
> EntityA {
>     int a = 2
>     int b = 3
> }
>
> I want to get http response:
> {
>     a: 2,
>     b: 3,
>     c: 6 // calculated as c=a*b
> }    
>
> P.S. logic of calculation may be complex, and i can't make it on data layer, but i need it on application(agrest) layer
>
> --
> You received this message because you are subscribed to the Google Groups "Agrest Framework User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to agrest-user+unsubscribe@googlegroups.com.

Andrus Adamchik

unread,
Oct 19, 2019, 4:10:48 AM10/19/19
to Agrest Framework User Group
In 3.4 you should be able to use a POJO or collection of POJOs, as we started to support relationships in overlays. In 3.3 and earlier you can only use simple attributes.

Andrus
> > To unsubscribe from this group and stop receiving emails from it, send an email to agrest-user...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups "Agrest Framework User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to agrest-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/agrest-user/de422e56-fbe3-471a-a076-9c99ca355d66%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages