How to map java collection to its size - mapstruct

I would like to know how I can map for example List to its size? I have some SourseClass.class containing List of something and I would like to map this List only to its size so DtoClass.class would contain "List.size()"

You can use expression for that, here is example:
#Mapping(target = "list", expression = "java(sourceClass.getList()!= null ? sourceClass.getList().size():0)")
DtoClass toDto(SourceClass sourceClass);

Related

Can I get a field from a list of maps using a variable name instead of a specific field name

I have a list of maps (bText) and the map of field names include (kjvtext, nasbtext, esvtext) string fields.
I have another preferences variable: String bVersion = "esvtext";
I want to access the map field bText[index].esvtext when the bVersion field contains "esvtext" and field bText[index].nasbtext when the bVersion field contains "nasbtext".
I've tried several combinations of code such as:
Text(bText[index].bVersion)
Text(bText[index].[bVersion])
Text(bText[index]."$bVersion")...
I've finally resorted to:
xText = (bVersion == "kjvtext")
? verselist[index].kjvtext
: (bVersion == "nasbtext")
? verselist[index].nasbtext
: (bVersion == "esvtext")
? verselist[index].esvtext
: "bVersion version error\n",
Seems like there would be a simpler way to code this?
Any thoughts? Thanks in advance!

How to override Ag Grid QuickFilter to receive exact match results?

By default Ag Grid Quick Filter function return rows that contains search string. For example if I type "30 June" in the searchbox, quick filter will also return rows that contains "30 cars were sold by 2 June" text. How can I override default behavior to receive only rows that exactly match my search string?
What I did was the following:
In the search itself, I removed the spaces from the search criteria:
this.gridApi.setQuickFilter(event.toLowerCase().replace(" ", ""));
In each column that I wanted an exact match, I added this code in the column definition:
getQuickFilterText: (params) => { return params.value && params.value.toLowerCase().replace(" ", "");}
(That is the override method for search. See here for more details: https://www.ag-grid.com/angular-data-grid/filter-quick/)
It seems to be working for me.
To achieve the exact match results column wise, You have to do these two things :
Remove cacheQuickFilter property from your default column definition object in gridOptions as caching convert all the columns data into a string separated by backward slash. That's the reason it will not be able to search column by column.
Add getQuickFilterText function in each column definition and add a condition for a exact match else return an empty string.
getQuickFilterText: params => {
return (params.value === <quick filter value>) ? params.value : ''
}
Now the tricky part here is how to access quick filter value inside getQuickFilterText function. You can achieve this in two ways :
Assign an id to quick filter search element and then access it's value using document.getElementById('quick-filter').value
Store the quick filter search value on change and put into a store state or service and then access that inside getQuickFilterText function.

I would like to store the values in array list after iterating the select options , is it possible in Watir

I am trying to store the values of select list in an array variable
a = b.options.each {|option| puts option.attribute_value "value" }
Output :
IN PROGRESS
UPCOMING
FINAL
POSTPONED
CANCELLED
a.to_i
Is it possible to store all values which getting from attribute and store in An array
The element collections in Watir include the Enumerable module, which gives a lot of useful methods for iterating. In particular, it includes a map method that will perform a block on each element and collect the result in an array.
To store the value of all options, you can simply do:
total_list_values = #browser.options.map(&:value)
#=> ["IN PROGRESS", "UPCOMING", "FINAL", "POSTPONED", "CANCELLED"]
I coded it like this and its worked, posted if anyone wanted this
total_list_values = Array.new
body = #browser.options
body.options.each do |option|
total_list_values << option.value
end

Filter array using NSPredicate and obtains new object composed by some elements in the query

I've got an array like that
Word array (
{
translation = (
{
name = Roma;
lang = it;
},
{
name = Rome;
lang = en;
}
);
type = provenance;
value = RMU;
},
{
translation = (
{
name = "Milano";
lang = it;
},
{
name = "Milan";
lang = en;
}
);
type = destination;
value = MIL;
},)
The idea is to filter it using an NSPredicate and receive and an array of dictionaries based on the lang key, I'd like to get something like this made by filtering for lang == it,
Word array (
{
name = Roma;
lang = it;
type = provenance;
value = RMU;
},
{
name = "Milano";
lang = it;
type = destination;
value = MIL;
})
I can't simplify the data because it comes from a "JSON" service.
I've tried different predicates using SUBQUERY but none of them works, documentation about SUBQUERY is pretty poor, I'm missing something, probably the problem is that I'd like to receive an object that is really different from the source.
Of course I'm able to obtain that structure enumerating, I'm wondering if there is a shorter solution
This answer from Dave DeLong link to SUBQUERY explanation gave a me a lot of hints about SUBQUERY, but I'm not able to find a solution to my problem.
Can someone give me a hints about?
You can't do this with a predicate. (Well, you could, but it would be stupidly complex, difficult to understand and maintain, and in the end it would be easier to write the code yourself)
NSPredicate is for extracting a subset of data from an existing set. It only* does filtering, because a predicate is simply a statement that evaluates to true or false. If you have a collection and filter it with a predicate, then what happens is the collection starts iterating over its elements and asks the predicate: "does this pass your test?" "does this pass your test?" "does this pass your test?"... Every time that the predicate answers "yes this passes my test", the collection adds that object to a new collection. It is that new collection that is returned from the filter method.
THUS:
NSPredicate does not (easily) allow for merging two sets of data (which is what you're asking for). It is possible (because you can do pretty much anything with a FUNCTION() expression), but it makes for inherently unreadable predicates.
SO:
Don't use NSPredicate to merge your dataset. Do it yourself.

zend route exclude one string

I defined
routes.kategoriler.route = ":lang/categories/:cat/:name"
routes.kategoriler.defaults.controller = "category"
routes.kategoriler.defaults.action = "index"
routes.categories.route = ":lang/:type/:cat/:name"
routes.categories.defaults.controller = "types"
routes.categories.defaults.action = "index"
type catches "categories" word. I put definitions before and after but no way. Can you offer a solution?
Use a requirement for type to do not match 'categories'. Like this:
routes.categories.reqs.type = "[^categories]"
Sadly my regex skills aren't on the top, so you might want to rewrite it (It's a little ugly, and it disapproves anything that contains 'categories', but it gives you the idea.)
using hint from
Regular expression to match a line that doesn't contain a word?
routes.categories.reqs.type_name = "^((?!categories).)*$"
Solved my case
To be able to accept anything except the exact match of "categories" use the below regexp
^((?!^categories$).)*$
this will allow you to use the type as "my-categories" or "categories-of-products" or anything else that is not an exact match of "categories"