You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Guillaume Lucazeau <gl...@gmail.com> on 2015/12/09 15:42:25 UTC

Get field from model in JSON view

Hello,

I have a class model where I generate a value for a property from two other
properties, something like that:

@Model(adaptables = Resource.class)
public class MyModel extends AbstractResource implements Resource {

  @Inject
  private int valueOne;

  @Inject
  private int valueTwo;

  private int sum;

  public getSum() {
    return sum;
  }

  @PostConstruct
  public void calculateScore() {
    sum = valueOne + valueTwo;
  }
}

I'd like to see this "sum" property when I call the JSON view:
{
  "sling:resourceType": "model/myModel",
  "jcr:createdBy": "admin",
  "jcr:created": "Mon Dec 07 2015 17:11:31 GMT+0100",
  "jcr:primaryType": "sling:Folder",
  "valueOne": 10,
  "valueTwo": 5,
  "sum": 15
  }

I tried to save it in a ModifiableValueMap properties that I get with
adaptTo() and display it with new JSONObject(properties) but it requires an
administrative authentication, or now a service with a special user.

I don't mind trying to do that but it seems a bit heavy as a solution, I
was wondering if there would be something easier to do such thing?

Thank you
Guillaume

RE: Get field from model in JSON view

Posted by Stefan Seifert <ss...@pro-vision.de>.
hello guillaume.

the default json view of sling is 1:1 the content of the repository.
normally it makes no sense to store "computed values" in the repository, because it is redundant.

the best solution would be to create your own json view for the resource type, e.g. a servlet or a sightly/jsp template. this can use your sling model and output the data (both repository and computed value) as required.

stefan


>-----Original Message-----
>From: Guillaume Lucazeau [mailto:glucazeau@gmail.com]
>Sent: Wednesday, December 09, 2015 3:42 PM
>To: users@sling.apache.org
>Subject: Get field from model in JSON view
>
>Hello,
>
>I have a class model where I generate a value for a property from two other
>properties, something like that:
>
>@Model(adaptables = Resource.class)
>public class MyModel extends AbstractResource implements Resource {
>
>  @Inject
>  private int valueOne;
>
>  @Inject
>  private int valueTwo;
>
>  private int sum;
>
>  public getSum() {
>    return sum;
>  }
>
>  @PostConstruct
>  public void calculateScore() {
>    sum = valueOne + valueTwo;
>  }
>}
>
>I'd like to see this "sum" property when I call the JSON view:
>{
>  "sling:resourceType": "model/myModel",
>  "jcr:createdBy": "admin",
>  "jcr:created": "Mon Dec 07 2015 17:11:31 GMT+0100",
>  "jcr:primaryType": "sling:Folder",
>  "valueOne": 10,
>  "valueTwo": 5,
>  "sum": 15
>  }
>
>I tried to save it in a ModifiableValueMap properties that I get with
>adaptTo() and display it with new JSONObject(properties) but it requires an
>administrative authentication, or now a service with a special user.
>
>I don't mind trying to do that but it seems a bit heavy as a solution, I
>was wondering if there would be something easier to do such thing?
>
>Thank you
>Guillaume