Can't I use Mybatis' resultMap's association and collection together? - collections

The team table and the schdule table are in a 1:n relationship.
I made a select query showing the reservation details of team as below.
<select id="getTeamSchdules" resultMap="teamResultMap">
select a.team_id, a.team_name, b.sche_date
from team a, schedule b
where 1 = 1
and a.STADIUM_ID = b.stadium_id
and a.team_id = #{teamId}
</select>
The contents of the select query result were mapped to mybatis resultMap as follows:
<resultMap id="teamResultMap" type="com.example.api.domain.team.domain.TeamSchedulerInfo">
<association property="team" javaType="com.example.api.domain.team.domain.Team">
<result property="teamId" column="team_id"/>
<result property="teamName" column="team_name" />
</association>
<collection property="dates" column="stadium_id" javaType="list" ofType="string">
<result column="sche_date" />
</collection>
</resultMap>
And the Java pojo class and controller is as follows.
#Data
public class Team {
private String teamId;
private String teamName;
}
#Data
public class TeamSchedulerInfo {
private Team team;
private List<String> dates;
}
The question here is that when I print out the result of the query as an API response, it comes out as follows.
[
{
"team": {
"teamId": "K04",
"teamName": "UNITED"
},
"dates": [
"20120324"
]
},
{
"team": {
"teamId": "K04",
"teamName": "UNITED"
},
"dates": [
"20120414"
]
},
{
"team": {
"teamId": "K04",
"teamName": "UNITED"
},
"dates": [
"20121117"
]
}
]
This is not the result I expected.
Here, I want to show one Team object result and date in the form of a List.
The JSON API response results I want are as follows.
[
{
"team": {
"teamId": "K04",
"teamName": "UNITED"
},
"dates": [
"20120324",
"20120414",
"20121117"
]
}
]
In other words, I want one team object and the date reserved in that team to appear in list form.
How do I deal with it? Can't I use the association and collection of resultMap together? Or did I use it wrong?

Related

Scan a String Set(SS) that contains a substring

Suppose I have the following schema:
id: S
my_list: SS
With the following example record:
{
"id": "auniquestring",
"list": [
"item1",
"item11",
"item111",
"item2",
"item22",
"item3"
]
I am scanning the table with the following params:
params = {
ExpressionAttributeValues: {
":v1": {
S: item_substring
}
},
FilterExpression: "contains(list, contains(list_item, :v1))",
TableName: "MyItemsTable"
};
The basic idea is to return the record that contains a string in a list of strings, containing the queried substring.
Answer in JavaScript would be preferred.
I think your FilterExpression is not correct. Why are you nesting contains?
Have you tried FilterExpression: "contains(list,:v1)"?

Observable contains array of ID to call another observable

I have a data structure in firebase
{
"name": "Sample",
"category": ["123456", "789012"]
}
The array of category contains ID which refers to documents in another collection. I can get the above document as Observable. What I really what as the end result is the below data structure
{
"name": "Sample"
"category": [
{
"name": "Category 1"
},
{
"name": "Category 2"
}
]
}
How can I bring this data? I don't think switchMap works for this. If so, can someone give an example of that?
You can try using flatMap and forkJoin. FlatMap allows you to chain multiple async requests together and forkJoin allows you to wait for all observables to return a value before continuing.
And you could wright something like this:
var finalData;
firstRequest('sample').flatMap((data) => {
// assuming data = { name: "Sample", catagory: [ "23123", "31321", ... ] }
finalData = data;
var observables = [];
data.catagory.forEach((c) => {
observable.push(secondRequest(c));
});
return forkJoin(observables);
}).flatMap((results) => {
// assuming results is an array like [ { name: "Catagory 1", ... } ]
finalData.category = results;
return finalData;
});

API Gateway and DynamoDB PutItem for String Set

I can't seem to find how to correctly call PutItem for a StringSet in DynamoDB through API Gateway. If I call it like I would for a List of Maps, then I get objects returned. Example data is below.
{
"eventId": "Lorem",
"eventName": "Lorem",
"companies": [
{
"companyId": "Lorem",
"companyName": "Lorem"
}
],
"eventTags": [
"Lorem",
"Lorem"
]
}
And my example template call for companies:
"companies" : {
"L": [
#foreach($elem in $inputRoot.companies) {
"M": {
"companyId": {
"S": "$elem.companyId"
},
"companyName": {
"S": "$elem.companyName"
}
}
} #if($foreach.hasNext),#end
#end
]
}
I've tried to call it with String Set listed, but it errors out still and tells me that "Start of structure or map found where not expected" or that serialization failed.
"eventTags" : {
"SS": [
#foreach($elem in $inputRoot.eventTags) {
"S":"$elem"
} #if($foreach.hasNext),#end
#end
]
}
What is the proper way to call PutItem for converting an array of strings to a String Set?
If you are using JavaScript AWS SDK, you can use document client API (docClient.createSet) to store the SET data type.
docClient.createSet - converts the array into SET data type
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName:table,
Item:{
"yearkey": year,
"title": title
"product" : docClient.createSet(['milk','veg'])
}
};

Normalize data model in RxJava without for loops and temporary arrays

Given the following response from a api request I would like to normalize the datamodel into a simpler one using rxjava without using any loops or temporary arrays to store information.
Given: List<MyItem>
===================
MyItem
String category_name
List<Switch> switches;
Switch
String id
boolean active
Response:
[{
"category_name": "Sport",
"switches": [{
"id": "sport_01",
"active": true
}, {
"id": "sport_02",
"active": false
}, {
"id": "sport_03",
"active": true
}]
}, {
"category_name": "Economy",
"switches": [{
"id": "economy_01",
"active": true
}, {
"id": "economy_02",
"active": true
}]
}]
Expected Normalised: List<MyViewModel>
===========================
MyViewModel
String categoryName
String switchId
boolean switchActiveState
Example
[{
"category_name": "Sport",
"id": "sport_01",
"active": true
}, {
"category_name": "Sport",
"id": "sport_02",
"active": false
}, ...
]
My first approach was the following
Observable<MyItem> mMyItemObservable = mApi.getReponse()
.map(mToMyItemList)
.flatMapIterable(mToMyItem)
.share();
Observable<Switch> switchObservable = mMyItemObservable.flatMap(new Func1<MyItem, Observable<List<Switch>>>() {
#Override
public Observable<List<Switch>> call(final MyItem item) {
return Observable.defer(new Func0<Observable<List<Switch>>>() {
#Override public Observable<List<Switch>> call() {
return Observable.just(item.switches);
}
});
}
}).flatMapIterable(new Func1<List<Switch>, Iterable<Switch>>() {
#Override
public Iterable<Switch> call(List<Switch> switches) {
return switches;
}
});
I have tried to use the following
Observale.zip(mMyItemObservable, switchObservable, Func...)
Observale.combineLatest(mMyItemObservable, switchObservable, Func...)
in order to produce the endResult List but with no success because most probably the length of the 2 observables were different.
ie. mMyItemObservable with length 2 (items) and the switchObservable with length 5 items.
Any ideas of alternative ways to group these 2 observables together to achieve the end result?
You need the 2-element version of flatMap:
mMyItemObservable =
mApi.getReponse()
.map(mToMyItemList)
.flatMapIterable(mToMyItem)
.flatMap(item -> Observable.from(item.switches),
(item, switch) -> new MyViewModel(item, switch)
);
Also please tell whoever gave you this homework to a) get into the 21st century and drop all the m- prefixes b) start using Java 8 features on Android (makes life with RxJava much easier.

All instances of a node by xxxx name

Is there a one liner or how can I get all instances of a named list in any node?
say I get jason where multiple nodes could have a sub collection called "comments". How can I get all nodes that contain a collection of "comments"?
Thanks,
If you can provide an example of the JSON, I can give you a definitive answer.
However, I can post some of the JSON I'm parsing and you can see how it works and possibly shape it to fit your needs.
"abridged_cast": [
{
"name": "Clark Gable",
"characters": ["Rhett Butler"]
},
{
"name": "Vivien Leigh",
"characters": ["Scarlett O'Hara"]
},
{
"name": "Leslie Howard",
"characters": ["Ashley Wilkes"]
},
{
"name": "Olivia de Havilland",
"characters": ["Melanie Hamilton"]
},
{
"name": "Hattie McDaniel",
"characters": ["Mammy"]
}
],
Notice how abridged_cast is an array of values, and one value in that array (characters) is an array itself.
Here's how I fetch the data:
var castMembers = (JArray) x["abridged_cast"];
foreach (var castMember in castMembers)
{
CastMember member = new CastMember();
member.Actor = (string) castMember["name"];
var characters = (JArray) castMember["characters"];
foreach (var character in characters)
{
member.Characters.Add((string)character);
movie.Cast.Add(member);
}
}

Resources