Firestore Security Rule List Check Duplicate - google-cloud-firestore

Let's say that request.resource.data[field] is a list. I am trying to check if any of the items in the list is a duplicate of the other items. In other words, I am checking if any of the items are repeated more than once inside the list. I read the documentation and reference related to list, and did not find a way to implement this. Is this possible to do?

You can check to see if there are any duplicates in a List type field by:
Converting the List into a Set using toSet() (which cannot contain duplicates
Comparing the size of the List to the size of the Set
If the size of the set is less then the size of the list, then you know that there was at least one duplicate that got removed. So, something like this will return true if there was a duplicate in the list:
listField.toSet().size() < listField.size()

Related

Filter out specific objects of Iterable list by specific property

I have an Iterable list that I am sorting by for different values. I want to add a new filter for users to filter out the list to just show a specific property the list contains. I am trying to use .where, but it does not seem to be working and I am not sure what to do. I have searched the web and see many examples on how to filter out just a plain list for a specific value in the objects with comparable but not able to get this to where for an Iterable list.
Here is what I am trying and not getting any results I would expect.
case 'specificValue':
return sorted.where((t) => t.state == 'speficicValue').toList();
Not sure if .where is the right way to filter out an Iterable list by a specific value within the objects of the list.
This was an issue with my new experience to Flutter and not knowing how too print. The list being filtered with .where was working but the list was so large it was hard to make sure it was showing up how I wanted. So printing it to the terminal I was able to look at the objects easier to validate the list was being filtered as I expected.

Take an Element of a List which is in a List

I know this sounds confusing. I have a List which consists of Lists in these Lists i have always 2 Elements. I want to display the first Element of all Lists. How does it work?
When you trying to display the list of elements as a group using foreach() loop. By default the list will be displayed in Ascending order only, try that approach once. If you face any issue, please provide your source code, as you trying out to display group of sublists in a Listview. Happy coding

NSFetchedResultsController predicate to eliminate duplicates of several properties

I am using an NSFetchedResultsController in my UITableViewController.
Is it possible to specify a predicate that will not retrieve items which have duplicate fields in x number of fields that I specify.
For example, I want to search all results for items but if the itemName AND itemDescription AND itemQuantity are the same, I want only one of these items.
Option 1
When the page loads do a single run through the data and keep a list of objectID that are duplicate. For duplicate object set the row height of the cell to be 0. So they are technically still there, but you can't see it. This make dealing with the NSFetchedResultsControllerDelegate calls easy because no indexPaths have changed
Option 2
If the dataset is always selected in the same way and an object that is a duplicate is always a duplicate you can set an 'isDuplicate' in the object and filter it out in the predicate. Or you can not store at all in the first place. If objects are displayed in different sets and in different way and sometime should be displayed and sometime not be displayed this is not a good solution
Option 3
If you are sorting by the same criteria that make an object duplicate (that is duplicates always appear right next to a non-duplicate) and you are NOT using sections, then you can use sectionKeyPath. SectionKeyPath groups items together into sections. Group the duplicate and non duplicate together and then display every section as a single row (use the first item in each section). The indexPaths of the fetchedResultsController will not match the indexPaths of the tableview so you have to careful to convert them.
Option 4
Instead of accessing the objects from a fetchedResultsController do a fetch and and filter the array. Then use the array to display the objects. The downside is that you don't get updates on when objects change. This can be especially problematic is objects are deleted, as accessing a managedObject that's entity was delete can lead to a crash.
I recommend option 1

Modify / Update element in ArrayList in working memory in rule Drools

We are working on a rule that is structured as follows:
We have a list that contains validated and non-validated elements. The difference between validated and non-validated is done by giving a different status to the elements.
We obtain the non-validated elements from the list.
Each non-validated element is compared to the validated elements in the same list.
After comparison a non-validated element would become a validated element that needs to be considered when validating the non-validated elements.
The problem arises when we want to update the original ArrayList of elements. We need to be able to update the non-validated element that has just become validated in its original ArrayList in the working memory so the loop can take the "refreshed" ArrayList into account.
Three questions:
First of all, what is the exact difference between using modify and update in Drools. According to Drools documentation:
"On the right hand side of a rule the modifystatement is recommended,
as it makes the changes and notifies the engine in a single
statement."
So the difference between one and other would be just simple semantics like:
modify( $sprinkler ) { setOn( true ) };
and
$sprinkler.setOn(true);
update($sprinkler);
Is this assumption correct?
Secondly, is it possible to update or modify a specific element within the ArrayList in the working memory in Drools without using a plain Java iterator (a "for"). In our case each element is identified with a unique ID we obtain from the list so we will have a valid reference for updating its status (validated or non-validated).
Finally, we are aware that updating the working memory would cause the rule to fire again. Let's say we have an ArrayList with two non-validated items. If we were to obtain all non-validated items to validate them, I would create a rule "first" that fires twice, for each non-validated item once:
rule "first"
when
$listOfElements : java.util.ArrayList ( )
$itemsToValidate : Element ( status == "not validated" ) from $listOfElements
then
//do something or not
end
If in a second rule I would validate certain items and wish to change the status of the non-validated element to validated in the working memory like this:
rule "second" extends "first"
when
//we validate attributes of the non-validated element against attributes of the validated elements
then
//my second question is if something like this is actually possible (solution without Java iterator)
$itemsToValidate.setStatus("validated")
modify ($listOfElements) { $itemsToValidate };
end
How would this affect the whole process? The actualisation of the working memory will provoke the first rule to be reevaluated. This rule would already be launched twice because we had encountered two non-validated elements. Will the remaining element be evaluated only once or more than once?
The answer to your first question is that there is no difference between the two snippets. (Statements inserted between the two of the second one would influence my answer.)
The scenario for the second question doesn't use an update in the same form. Inserting a List as a fact but operating on list elements is usually considered as bad. Insert the list elements. The container may or may not be useful as a container holding the list elements to one transaction or whatever.

Get selected items from a table with growing threshold SAPUI5

I'm wondering if there is a way to get all the total number of selecteditems of a table with properties growing and growingThreshold set. It seems like the call
oTable.getSelectedItems().length only works for items in the table that are on display/visible.
JSBIN Sample
getGrowingInfo().total will return all items no matter visible or not.
When growing property is set only visible items are being selected on clicking selectall.So it will return only visible items length.You may have to try for getting all items
ocheckSerialBatchJsonModel.getProperty("/results").length
If you want to get selected items you may have to add a seperate column for checkboxes and bind a property to enabled property of checkbox to json model first.Then you have to loop the entire list.
I had the same problem with sap.m.List. What I did was using the returned Listbase from the bindItems function and used the paths from getSelectedContextPaths(). Compared it with the oData and got all selected items. A little bit of a hack. Feel free to suggest something else. Didn't find anything else yet.