How to set nested list to parent item after distinct - rx-java2

I have begun to deal with RxJava and its operators. It became interesting to me how to set nested list to parent item after distinct.
Lazy Simple example:
Observable.just(parentItem)
.flatMapIterable { parentItem -> parentItem.array }
.distinct { arrayItem -> arrayItem.name }
.toList()
/// other stuff which have to return the observable, not item list
Please, tell me as to make it truly and correctly

Related

How can I use the entitymanager metamodel to find fields with certain properties

I wonder if it is possible to use the entitymanager metamodel to scan over loaded entities to identify fields with certain properties during a test. For example I want to see if any fields are defined as strings with a "max length of 1" to report them as possible "Char" type columns.
val model = em.metamodel
model.entities.forEach { entity ->
entity.attributes.filter { !it.isCollection }.filter { it.javaType.isAssignableFrom(String::class.java) }
.forEach { attribute ->
attribute // <- TODO here be dragons
}
}"

Getting Cassandra query results asynchronously using Scala + Monix

I'm building a REST API using AKKA Http, Monix and Datastax Java Driver for Apache Cassandra and I'm having some troubles while trying to fetch some Items from cassandra, wait for the query to be fulfilled and returning the results.
I'm able to print all the results easily, but unable to wait for the
query to be done and return all the items. My rest point simply
returns an empty array of items since it does not wait for the query
to be done.
I have an executeQuery method that takes:
queryString: String representing a cassandra query
page: Int useful for pagination
parameters: Any* representing parameters, if necessary for the query
And returns an Observable[Row].
Then, in order to perform such query, retrieve its result, parse them and send them back, I use Monix Observable and Subscription.
Let's suppose I want to retrieve some items by a common field known as pid:
import monix.execution.Ack
import monix.execution.Scheduler.Implicits.global
import com.datastax.driver.core.Row
import monix.reactive.Observable
import cassandra.src.CassandraHelper
import item.src.entity.{Item, Items}
. . .
val keyspace = "my_keyspace"
val table = "items"
. . .
def getItems() : Items = {
var itemList: Items = List()
val observable: Observable[Row] = CassandraHelper.executeQuery(
"SELECT * FROM " + keyspace + "." + table,
1
)
observable.subscribe { row =>
itemList ::= ItemMapper.rowToItem()(row)
Ack.Continue
}
Items(itemList)
}
Where rowToItem simply parses a row into an Item and Items: List[Item].
I was taking a look at Task but I'm not quite sure its what I'm looking for.
EDIT
With #Alexandru Nedelcu solution I'm able to print all the items in itemList as soon as they get inserted into it, but still getting an empty response for that call: { "items" : [] }.
Here's the edited code:
def getItems() : Items = {
var itemList: List[Item] = List()
val observable: Observable[Row] = CassandraHelper.executeQuery(
"SELECT * FROM " + keyspace + "." + table,
1
)
observable.subscribe { row =>
println(itemList)
itemList ::= ItemMapper.rowToItem()(row)
Ack.Continue
}
Items(itemList)
}
How can I wait for the results to be all parsed and inserted into items and then send them back?
From what I understand you have an Observable[Row] and you want to build an Items out of it, which aggregates every Row element from the source stream, is that correct?
If so, the foldLeftL is what you want, which will aggregate every element into a state and return the final result once the source stream completes:
// We need to suspend the Task, because your Items is probably a
// mutable object and it's best to suspend side effects ;-)
val items: Task[Items] = Task.suspend {
val initial: Items = _
val observable: Observable[Row] = ???
// This returns a Task[Items] when the source completes
observable.foldLeftL(initial) { (items, elem) =>
items ::= ItemMapper.rowToItem()(row)
// I don't understand if your `Items` is mutable or not
// but returning the same reference is fine
items
}
}
A Task is a lazy Future. And you can convert it into a Future with runAsync. More details here: https://monix.io/docs/2x/eval/task.html

How to keep a groupedby list sorted in Play Framework 2 templates

I've got a list of complex objects that I want to display, grouped by one of its attribute in a Play 2 template.
I managed to do it :
#measures.groupBy(_.question.category).map {
case (category, items) => {
// Category stuff
#for(item <- items) {
// List of items
}
}
}
The problem is that the list was sorted in my Java controller, but the keyset of the map that I create is not sorted anymore (I would like to sort the key set using something like _.question.category.order).
Is there a way to have a sorted map on this attribute?
Thanks !
What type is measures? Did you try to use a LinkedHashSet? This should keep the elements order in contrast to e.g. a HashSet.

Querying a list item through UniqueId

I'm trying to get a list item through its Unique Id through REST API end point for list items.
URL
https://{site-collection}/{personal-site}/_api/Web/Lists(guid'id')/items?$filter=UniqueId eq 'uniqueid'
But it isn't returning the item, instead it returns an empty list;
Result
{
"d": {
"results": []
}
}
When I make a similar query using ItemId or GUID, it works fine but for UniqueId I'm getting an empty list. Is this even possible? If not then why have a UniqueId, can't GUID/ItemId suffice? One possible explanation could be that it is the UniqueId that is referenced in other lists not GUID or ItemID.
So why can't I filter an item with UniqueId?
If anyone else comes stumbling upon this thread, then the answer is
_api/Web/Lists('GUID')/GetItemByUniqueId('GUID')
:)

How to access the items inside the result of my query?

here is a snippet of my code:
using (var uow = new UnitOfWork())
{
//ItemType itself
ItemType itemType1 = uow.ItemTypeRepository.Get(i => i.Name == "ServerTypeList").FirstOrDefault();
Assert.IsTrue(itemType1.ID != null);
var itemType2 = uow.ItemTypeRepository.Get(i => i.Name == "ServerTypeList", orderBy: o => o.OrderBy(d => d.Name), includeProperties: "Items");
//itemType2[0].
...
I am trying to list all the items inside itemType2 ("Get" method returns an IEnumerable):
What's wrong with itemType2.First().Items[0]?
This works because itemType2 is a sequence. Based on what you're seeing in the debugger, it has only one element. If it always has only one element, then you should use itemType2.Single().Items[0] instead.
Every element of this sequence has an Items property, which appears to be a list or array or something else that can be indexed, so once you get there, you can index into it, as above, or you can iterate over it:
foreach (var item in itemType2.Single().Items)
{
// Do something with each item in the sequence
}