You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by membersound <me...@web.de> on 2012/11/01 12:03:06 UTC

BeanModel - how to add results from a method?

Hi,

when I create a BeanModel, how can I add results eg from a method that is
not contained in the Entity-Object with which the BeanModel has been
created? Like:

Here I create a beanmodel for the class UserProfile. And I want to add a
field that gets its content from a method that is placed NOT in the
userProfile, eg in the same page class where the model is created, or in
some other service that is been injected.

BeanModel.createDisplayModel(UserProfile.class, messages);
model.add(...);
model.add("customProp",
PropertyConduitSource.create(someService.getSomeMethodResult()));

@Inject
SomeService someService;



How can I do this?



--
View this message in context: http://tapestry.1045711.n5.nabble.com/BeanModel-how-to-add-results-from-a-method-tp5717499.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BeanModel - how to add results from a method?

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 01 Nov 2012 11:13:19 -0200, membersound <me...@web.de> wrote:

>> Why not? That would be perfectly good, even recommended, object-oriented
>
> Because it was just an example here, I want to apply some calculations  
> for some rows, which should not clutter my entities. And of course  
> itself rely again on other entities, so the calculation is more a  
> process for a service than being directly placed in the entity object  
> itself.

For me, if some calculation or process can be done just by using the  
fields of a given entity class, the method that does it should be in the  
entity class. That's OOP. That's not clutter.

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BeanModel - how to add results from a method?

Posted by membersound <me...@web.de>.
OK got it, thank you very much!


> Why not? That would be perfectly good, even recommended, object-oriented  

Because it was just an example here, I want to apply some calculations for
some rows, which should not clutter my entities. And of course itself rely
again on other entities, so the calculation is more a process for a service
than being directly placed in the entity object itself.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/BeanModel-how-to-add-results-from-a-method-tp5717499p5717520.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BeanModel - how to add results from a method?

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 01 Nov 2012 10:06:34 -0200, membersound <me...@web.de> wrote:

> Sorry, I think I didn't get it.

Nope, you didn't. :P

>   setupRender() {
>     model.add("result", pcSource.create("Product.class", "price"));

Does this even compile? I guess it should be model.add("result",  
pcSource.create(Product.class), "price"));. Anyway, you're not following  
what I suggested at all. Have you even read it? :P In addition, you can't  
add more than one property with the same name.

You should implement your own PropertyConduit, not using one provided by  
PropertyConduitSource. Something like this (not tested).

class MultiplyPropertyConduit implements PropertyConduit {

	// that's where the logic goes
	public Object get(Object instance) {
		Product product = (Product) instance;
		return product.getPrice() * product.getQuantity();
	}

	public void set(Object instance, Object value) {}

	public Class getPropertyType() {
		return BigDecimal.class;
	}

	public T getAnnotation(Class<T> annotationClass) {
		return null:
	}

}

...

beanModel.add("multiply", new MultiplyPropertyConduitSource());

> I do NOT want to introduce a getMultiply() method within my product  
> entity
> (which would solve the problem as I could just add a "multiply"  
> conduit). I
> want it to be computed either in the productService or directly in the
> product backing page class.

Why not? That would be perfectly good, even recommended, object-oriented  
modelling and programming.

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BeanModel - how to add results from a method?

Posted by membersound <me...@web.de>.
Sorry, I think I didn't get it.
Lets make an example here:


<t:grid source="products" row="product />

class Product {
   int price, quantity;
}

class ProductPage {
   @Inject
   PropertyConduitSource pcSource;

   @Property
   List<Product> products;

   @Property
   @Persist
   Product product;

  setupRender() {
    model.add("result", pcSource.create("Product.class", "price"));
    model.add("result", pcSource.create("Product.class", quantity));
    model.add("result", pcSource.create("Product.class", getMultiply()));
    //or
    model.add("result", pcSource.create("Product.class",
productService.getMultiply(product)));
  }

  int getMultiply(Product product) {
     return product.getPrice() * product.getQuantity();
  }
}


I do NOT want to introduce a getMultiply() method within my product entity
(which would solve the problem as I could just add a "multiply" conduit). I
want it to be computed either in the productService or directly in the
product backing page class.




--
View this message in context: http://tapestry.1045711.n5.nabble.com/BeanModel-how-to-add-results-from-a-method-tp5717499p5717512.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: BeanModel - how to add results from a method?

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Thu, 01 Nov 2012 09:03:06 -0200, membersound <me...@web.de> wrote:

> Hi,

Hi!

> When I create a BeanModel, how can I add results eg from a method that is
> not contained in the Entity-Object with which the BeanModel has been
> created?

You can't pass the result itself, but you can implement a PropertyConduit  
that receives the object being shown and computes the BeanModel property  
value for it using any logic you want. The key here is implementing  
PropertyConduit yourself (it's easy, and if you're just showing the object  
you can provide an empty implementation to its set() method) and passing  
it to the BeanModel.add() method.

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org