get random entries from entity field

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

get random entries from entity field

Robert Gan
Hey guys,

I want to get a list of reviews which are rated > 30. The list shall contain a specific amount of reviews, which shall be randomly selected from all reviews.

All that I wanna do in the groovy file. How can I do this?

Reply | Threaded
Open this post in threaded view
|

Re: get random entries from entity field

Rishi Solanki
Use delegator.find method, this will return you an object of
EntityListIterator. You can use its getPartialList() method to get N
records you want.
For random records you may need to add logic in groovy itself.

--
Rishi Solanki
Manager, Enterprise Software Development
HotWax Media Pvt. Ltd.
Direct: +91-9893287847
http://www.hotwaxmedia.com


On Fri, Jun 29, 2012 at 9:23 AM, Robert G. <[hidden email]> wrote:

> Hey guys,
>
> I want to get a list of reviews which are rated > 30. The list shall
> contain
> a specific amount of reviews, which shall be randomly selected from all
> reviews.
>
> All that I wanna do in the groovy file. How can I do this?
>
>
>
> --
> View this message in context:
> http://ofbiz.135035.n4.nabble.com/get-random-entries-from-entity-field-tp4634190.html
> Sent from the OFBiz - User mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: get random entries from entity field

Robert Gan
ok thanks, I can get the whole list..but Im scared, when you have in future sth. like 150.000 Reviews, that this takes to much server resources...cause I just neeed like 10 reviews to show, so calling 150.000 reviews instead is kind of wasting data...what do you think?
Reply | Threaded
Open this post in threaded view
|

Re: get random entries from entity field

Robert Gan
I should read better :)

Ok when I say get "N" records, does it mean getting the latest N records? Or is it randomly from all?
Reply | Threaded
Open this post in threaded view
|

Re: get random entries from entity field

Rishi Solanki
By default it gives you latest records, for getting it in your way pass the
orderBy list. Also see the delegator.findListItereatorByCondition() method.

--
Rishi Solanki
Manager, Enterprise Software Development
HotWax Media Pvt. Ltd.
Direct: +91-9893287847
http://www.hotwaxmedia.com


On Mon, Jul 2, 2012 at 12:18 AM, Robert G. <[hidden email]> wrote:

> I should read better :)
>
> Ok when I say get "N" records, does it mean getting the latest N records?
> Or
> is it randomly from all?
>
> --
> View this message in context:
> http://ofbiz.135035.n4.nabble.com/get-random-entries-from-entity-field-tp4634190p4634309.html
> Sent from the OFBiz - User mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: get random entries from entity field

Robert Gan
In reply to this post by Robert Gan
Hey, I tried to use this method:

public List<GenericValue> findList(String entityName, EntityCondition entityCondition, Set<String> fieldsToSelect, List<String> orderBy, EntityFindOptions findOptions, boolean useCache) throws GenericEntityException;

Now I understood, that I can choose the columns of the database table which I wanna get with:
Set<String> fieldsToSelect

is that right? but how do the Set does have to look? I checked the java API ref, but couldnt find an example. If I just give a string (name of the column) to the method, I get an error.

please help me on that.

Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: get random entries from entity field

Deepak Dixit-2
You have to pass fieldToSelect as an Set, Use UtilMisc.toSet("field");
e.g
Set<String> fieldToSelect = UtilMisc.toSet("field");


Thanks & Regards
--
Deepak Dixit
HotWax Media Pvt. Ltd.
www.hotwaxmedia.com
Contact :- +91-98267-54548
Skype  :- deepakdixit

On Jul 2, 2012, at 3:42 PM, Robert G. wrote:

> Hey, I tried to use this method:
>
> /public List<GenericValue> findList(String entityName, EntityCondition
> entityCondition, Set<String> fieldsToSelect, List<String> orderBy,
> EntityFindOptions findOptions, boolean useCache) throws
> GenericEntityException;/
>
> Now I understood, that I can choose the columns of the database table which
> I wanna get with:
> Set<String> fieldsToSelect
>
> is that right? but how do the Set does have to look? I checked the java API
> ref, but couldnt find an example. If I just give a string (name of the
> column) to the method, I get an error.
>
> please help me on that.
>
> Thank you.
>
> --
> View this message in context: http://ofbiz.135035.n4.nabble.com/get-random-entries-from-entity-field-tp4634190p4634321.html
> Sent from the OFBiz - User mailing list archive at Nabble.com.


smime.p7s (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: get random entries from entity field

Robert Gan
great thanks!
Reply | Threaded
Open this post in threaded view
|

Re: get random entries from entity field

Robert Gan
ok, I think this is my last question concerning this subject :)

I get a list with "delegator.findList(...)"
f.ex. for the Entity ProductReview I get:

{[GenericEntity:ProductReview][productId,10000(java.lang.String)][productReview,this is an example review no. 1(java.lang.String)][userLoginId,admin(java.lang.String)], [GenericEntity:ProductReview][productId,10000(java.lang.String)][productReview,this is an example review no. 2(java.lang.String)][userLoginId,admin(java.lang.String)]}


now I want to add a key value pair to both entries of this list. I could only succeed when I change a value of an existing key,  but adding a new K-V combination I could not succeed. I also tried it with "UtilMisc.addToSetInList", but couldnt get it work.

Do someone know the solution?

I want a result like (bold marked is new):

{[GenericEntity:ProductReview][productId,10000(java.lang.String)][productReview,this is an example review no. 1(java.lang.String)][userLoginId,admin(java.lang.String)][newValue,thisIsTheNewValue(java.lang.String)], [GenericEntity:ProductReview][productId,10000(java.lang.String)][productReview,this is an example review no. 2(java.lang.String)][userLoginId,admin(java.lang.String)][newValue,thisIsTheNewValue(java.lang.String)]}