Springboot using reflection with stream - spring-boot

i would filter the result of my pagination result in springboot by iterating on all fields , i have the idea to iterate on each result and call each getter method of this object to compare it to my keyword (im doing smart search) , but i dont call each method hard coded cause the structure may change in fututr and there is a lot of feilds , i would to know how to pass each getters to stream
i already found how to get method with :
for(PropertyDescription Introspector.getBeanInfo(Myclass).getPropertyDescriptors()){
propertyDeescriptor.getReadMethod()}
thank you a lot

Related

is there any way to convert vaadin CallbackDataProvider<Object, Void> to ListDataProvider<Object>

I am using CallbackDataProvider<Object, Void> dataprovider and then setting dataprovider in grid using grid.setDataProvider.
Then in TextField addValueChangeListener method I am trying to call grid.getDataProvider() which is returning me CallbackDataProvider instance.
After that, I am trying to cast in ListDataProvider for filtering in which I am getting ClassCastException But when I am using PaginatedGrid getDataProvider is returning ListDataProvider instance.
Here I don't want to use PaginatedGrid, is there any way to convert CallBackDataProvider to ListDataProvider using Grid?
To answer your literal question, no, you can not convert a CallbackDataProvider to a ListDataProvider. Your underlying problem is stated between the lines here:
I am trying to cast in ListDataProvider for filtering
So your problem is that you want to filter the data, but the CallbackDataProvider doesn't provide a similar method for it as a ListDataProvider. There is a reason for this - ListDataProvider stores all of its items in memory inside a Java Collection, so it's straightforward to filter the backing Collection. A CallbackDataProvider is lazy-loading and doesn't know where the full set of items is, so you need to implement the filtering yourself against the backing data source.
To filter a lazy loading DataProvider, you'll need a ConfigurableFilterDataProvider. First, instead of creating your CallbackDataProvider with DataProvider.fromCallbacks, you need to use DataProvider.fromFilteringCallbacks. This enables you to use the filter of the query object and you can implement the filtering of your backend data as you see fit. Second, you'll need to wrap your DataProvider into a ConfigurableFilterDataProvider with dataProvider.withConfigurableFilter so that you can pass a filter object of your choosing.
You can find a full example here: https://vaadin.com/docs/v14/flow/binding-data/tutorial-flow-data-provider/#filtering-based-on-another-component

Spring Data Rest: pass Collection<Entity> as query String parameter

First off, this is related to Spring Data Rest: How to search by another object's key? which appears to be resolved in https://jira.spring.io/browse/DATAREST-502
My issue is (I believe) and extension of this. I'm seeing the following behavior:
I have two repository queries defined e.g.
Person findByAccount(#Param("account") Account account));
Collection<Person> findByAccountIn(#Param("accounts") Collection<Account> accounts));
Both search methods are exposed via spring-data-rest. I can access the first using a url such as http://localhost:8080/people/search/findByAccount?account=http://localhost:8080/accounts/1
I can access the second method using a url such as http://localhost:8080/people/search/findByAccountIn?accounts=http://localhost:8080/accounts/1, but if I try to pass in MULTIPLE accounts, such as
http://localhost:8080/people/search/findByAccountIn?accounts=http://localhost:8080/accounts/1,http://localhost:8080/accounts/2,
It will run the query except will IGNORE the first account (http://localhost:8080/accounts/1) and only search based on the second (http://localhost:8080/accounts/2)
What is the proper technique for passing in a Collection of entities to a repository argument over the REST API? I find it works great for a single entity, but not for a Collection. Note that both of these repository methods are working as expected when directly accessing the JpaRepository.
Also note that these queries seem to work if the collection is of some primitive type, for example findByAccountIdIn(#Param("accountIds") Collection<Long> accountIds) is accessible with intended functionality via http://localhost:8080/people/search/findByAccountIdIn?accountIds=1,2. This leads me to believe it's possibly an error in the way a list of URIs is passed into a query method which expects a Collection of corresponding entities.
Thanks in advance for any help!
Try repeat the query parameter as most servers will interpret this as a list. It might not be the prettiest solution but should work.
http://localhost:8080/people/search/findByAccountIn?accounts=http://localhost:8080/accounts/1&accounts=http://localhost:8080/accounts/2
I know that this is forever old, but I found the answer.
I will put this here for anyone else who should wander this way:
How to use List in endpoint exported by Spring Data REST?
List<Person> findByAccountIn(#Param("accounts") Account... accounts);
the request would look like this:
http://localhost:8080/people/search/findByAccountIn?accounts=http://localhost:8080/accounts/1&accounts=http://localhost:8080/accounts/2&accounts=http://localhost/accounts/anotheraccountid

Nested Hash from JSON structure - Critical look

I'm new on ruby, I come from C/C++.
I'm currently working on data integration between a partner and me.
I get the API response with httparty and then parse it with JSON.parse.
The hash result is like multi level nested ( around 5-6 levels )
Initially, since I'm new on ruby, I wanted to develop naturally, without thinking of number of methods, number of line in methods, the only goal was to clearly separate each extraction from another in distinct methods.
The extraction from this nested hash is conditional extract, what I mean is there is multiples object of the same structure inside the hash.
And my extraction is like something like this:
if get_flight(json_response) == blabla_id
stored_blabla_id = blabla_id
end
then later
get_departure_place_from_flight(json_response, stored_blabla_id)
I read many articles about obectifying hash like this good one, or building some engines-extractor that getting value based on key passed in arguments.
Since I'm getting a really huge json response, and since I'm not extracting all the values but specifics one, I'm wondering if its not bad for usage/performance.
My point : the class is working properly BUT: I have 25 methods in one class, and the content of theses methods are like a direct access from the nested hash. I find it very ugly.
I was wondering since I have 2 request methods to the API, 1 method dedicated to construct URL, and the others one dedicated to extraction from the JSON response , is it appropriate to split the class into modules?
Or, is this kind of ugly class common in JSON parse/extracting value from whatever API's?

How to write a custom ConverterHelper class in Restlet 2.1

I can't seem to find a concrete example of how to write a custom converter for Restlet. I'm having an issue with an object containing an ArrayList of a base type. (ie List )
I've been able to painstakingly do the conversion by reading in the object as JSON, converting it to a JSONObject and processing each field individually to create the representation I need on the deserialization. This seems very odd that I'd have to do this.
any help would be appreciated.

Workarounds for using custom methods/extension methods in LINQ to Entities

I have defined a GenericRepository class which does the db interaction.
protected GenericRepository rep = new GenericRepository();
And in my BLL classes, I can query the db like:
public List<Album> GetVisibleAlbums(int accessLevel)
{
return rep.Find<Album>(a => a.AccessLevel.BinaryAnd(accessLevel)).ToList();
}
BinaryAnd is an extension method which checks two int values bit by bit. e.g. AccessLevel=5 => AccessLevel.BinaryAnd(5) and AccessLevel.binaryAnd(1) both return true.
However I cannot use this extension method in my LINQ queries. I get a runtime error as follows:
LINQ to Entities does not recognize the method 'Boolean BinaryAnd(System.Object, System.Object)' method, and this method cannot be translated into a store expression.
Also tried changing it to a custom method but no luck. What are the workarounds?
Should I get all the albums and then iterate them through a foreach loop and pick those which match the AccessLevels?
I realize this already has an accepted answer, I just thought I'd post this in case someone wanted to try writing a LINQ expression interceptor.
So... here is what I did to make translatable custom extension methods: Code Sample
I don't believe this to be a finished solution, but it should hopefully provide a good starting point for anyone brave enough to see it through to completion.
You can only use the core extension methods and CLR methods defined for your EF provider when using Entity Framework and queries on IQueryable<T>. This is because the query is translated directly to SQL code and run on the server.
You can stream the entire collection (using .ToEnumerable()) then query this locally, or convert this to a method that is translatable directly to SQL by your provider.
That being said, basic bitwise operations are supported:
The bitwise AND, OR, NOT, and XOR operators are also mapped to canonical functions when the operand is a numeric type.
So, if you rewrite this to not use a method, and just do the bitwise operation on the value directly, it should work as needed. Try something like the following:
public List<Album> GetVisibleAlbums(int accessLevel)
{
return rep.Find<Album>(a => (a.AccessLevel & accessLevel > 0)).ToList();
}
(I'm not sure exactly how your current extension method works - the above would check to see if any of the flags come back true, which seems to match your statement...)
There are ways to change the linq query just before EF translates it to SQL, at that moment you'd have to translate your ''foreign'' method into a construct translatable by EF.
See an previous question of mine How to wrap Entity Framework to intercept the LINQ expression just before execution? and mine EFWrappableFields extension which does just this for wrapped fields.

Resources