freemarker test for list and iterate it - templates

I have a data element that can either be a Map or a List of Maps.
Here are two simplified data examples (expressed as JSON for readability)
1) Maps
{
"lom:general": {
"lom:title": {"lom:string": {
"language": "fr",
"content": "Premiers Pas avec un Progiciel de Gestion Intégré"
}},
"lom:language": "fr"
},
"lom:technical": {"lom:location": "http://hub11.ecolearning.eu/course/progiciel-metiers-pgi/"}
}
2) List of Maps
{
"lom:general": {
"lom:title": {"lom:string": [
{
"language": "fr",
"content": "DIY Education aux médias et à l'information - 3ed"
},
{
"language": "en",
"content": "DIY Media and Information Literacy - 3ed"
}
]},
"lom:language": [
"fr",
"en"
]
},
"lom:technical": {"lom:location": "http://hub5.ecolearning.eu/course/diy-do-it-yourself/"}
}
I want to check the type of element lom:general.lom:title.lom:string and iterate it if it is a list.
I have tried the following:
<title>
<#assign titles = lom\:general.lom\:title.lom\:string>
<#if titles?is_enumerable>
<#list titles as title>
<string language="${title.language}">${title.content}</string>
</#list>
<#else>
<string language="${titles.language}">${titles.content}</string>
</#if>
</title>
The template works as expected with the first data example. However, the second data example raises a StackOverflowError
java.lang.StackOverflowError
at sun.reflect.misc.ReflectUtil.checkPackageAccess(ReflectUtil.java:188)
at sun.reflect.misc.ReflectUtil.checkPackageAccess(ReflectUtil.java:164)
at sun.reflect.generics.reflectiveObjects.TypeVariableImpl.getGenericDeclaration(TypeVariableImpl.java:164)
at sun.reflect.generics.reflectiveObjects.TypeVariableImpl.equals(TypeVariableImpl.java:189)
at com.sun.beans.TypeResolver.resolve(TypeResolver.java:190)
at com.sun.beans.TypeResolver.resolve(TypeResolver.java:201)
Where the last line is repeated dozens of times

Related

Return the names of heroes that begin with I or J, and all their mentors NOSQL

In this exercise that my professor refuses to correct in class, I have to do this query:
Return the names of heroes that begin with I or J, and all their mentors.
Here is the structure of the objects I'm playing with :
{
"_id": {
"$oid": "63495a11d935f6aa5139ee33"
},
"name": "Medea",
"faits": "Quete de la toison d'or avec les argonautes",
"ascendants": [
"Eson",
"Eole"
],
"gender": "female",
"mentor": "Chipute"
}
And here is my attempt at making it work:
db.collection.aggregate(
[
{$project : {{$name :{{ $substr : {$name, 0,1}}: {$or:[ {$eq: 'I'},{$eq: 'J'}]}} },$mentor: 1}}
]
)
And here is te Error returned to me:
Error: clone(t={}){const r=t.loc||{};return e({loc:new Position("line"in r?r.line:this.loc.line,"column"in r?r.column:...<omitted>...)} could not be cloned.
at Object.serialize (node:v8:332:7)
at u (C:\Users\achop\AppData\Local\MongoDBCompass\app-1.33.1\resources\app.asar.unpacked\node_modules\#mongosh\node-runtime-worker-thread\dist\worker-runtime.js:1917:594983)
at postMessage (C:\Users\achop\AppData\Local\MongoDBCompass\app-1.33.1\resources\app.asar.unpacked\node_modules\#mongosh\node-runtime-worker-thread\dist\worker-runtime.js:1917:595591)
at i (C:\Users\achop\AppData\Local\MongoDBCompass\app-1.33.1\resources\app.asar.unpacked\node_modules\#mongosh\node-runtime-worker-thread\dist\worker-runtime.js:1917:600488)
Thank you for your time, there may be numerous mistakes.
If I've understood correctly you can use this query:
It's a simple find query where you can use $regex to find the documents where name starts with (^) I or (|) J.
Also option i is to do the query case insensitive, you can delete that line if you only want values that starts with capital letters.
And the second object in the query is the projection. Which is used to output the values you want. In this case only name and mentor.
db.collection.find({
name: {
$regex: "^(I|J)",
$options: "i"
}
},
{
name: 1,
mentor: 1
})
Example here

Writing an if statement for the value in a dictionary

I'm taking an intro game design class and am writing a text adventure. I've set up a dictionary for the game map. For example:
"HALLWAY": {
"name": "HALLWAY",
"desc": "This is a hallway in a maze."
"exits": [{
"exit": "NORTH",
"target": "DEADEND"
}],
"items": []
Early on in the game you have the option to "TAKE" an item. IF the player has taken the item I want the target to be a different location (Not a dead end) ELSE the target is a dead end. How can I write the if/else statement into the value of the target? Or is there a more simple way of doing this?
<!DOCTYPE html>
<script>
'use strict';
let input_data = {
"HALLWAY":
{
"name": "HALLWAY"
, "desc": "This is a hallway in a maze."
, "exits": [{ "exit": "NORTH", "target": "DEADEND" }]
, "items": []
}};
//input_data["HALLWAY"]["exits"][0]["exit"] = "SOUTH";
// ABOVE STATEMENT CAN BE USED TO UPDATE THE VALUE
alert("Value of exit: " + input_data["HALLWAY"]["exits"][0]["exit"]);
alert("Value of target: " + input_data["HALLWAY"]["exits"][0]["target"]);
# Now assume that somehow the value of items has changed
# I am doing it manually
input_data["HALLWAY"]["exits"][0]["items"] = { "item1": "value1" };
alert("Value of items: " + input_data["HALLWAY"]["exits"][0]["items"]["item1"]);
# --One way is to ----- this is the condition that you check if the item has changed
if(input_data["HALLWAY"]["exits"][0]["items"] != null)
input_data["HALLWAY"]["exits"][0]["target"] = "target changed";
# ---- the other way --- would be to check the count of items list whether it has changed.
alert("Value of target: " + input_data["HALLWAY"]["exits"][0]["target"]);

Instagram Basic API: Is it possible to get the media_url from "CAROUSEL_ALBUM" in one query?

If you are reading this, you will read that you can add children to the fields query parameter. That means that if you have a media with the type "CAROUSEL_ALBUM" you will get the ids of the images as well.
Example CURL:
https://graph.instagram.com/me?access_token=<mytoken>&fields=id,media_type,media_url,children
The result:
...
"id": "some-id",
"media_type": "CAROUSEL_ALBUM",
"media_url": "some-url",
"children": {
"data": [
{
"id": "another-id"
},
{
"id": "other-id"
}
]
}
...
Is it possible to add media_url to the children's data? I don't really want to fetch everything in a loop...
#Mecha I did this for PHP, but maybe you can read this
/**
* Fetches media from the user
*
*/
public function fetchUserMedia(array $options, int $limit = 24): object
{
$query = http_build_query(array_merge([
'limit' => $limit,
'fields' => 'username,caption,id,media_type,media_url,thumbnail_url,children{media_url,thumbnail_url}',
'access_token' => $this->token
], $options));
return json_decode((string) ($this->client->get("https://graph.instagram.com/me/media?{$query}"))->getBody());
}
One output example is like this:
{
"caption": "I\u2018m addicted to chia pudding, how about you? \ud83d\ude0d Layers of zingy lemon vanilla chia pudding, mango thyme infused coconut yoghurt and juicy fresh mango roses. \ud83e\udd24\nMade by \u0040addictedtodates \ud83c\udf4b\u2063\u2063\n\u2063\u2063\u2800\nAND now to our tagged picture of today \ud83d\ude0b\ud83d\ude0b Swipe left to see \u0040smoothie_yumm\u2019s coconut vanilla smoothie bowl topped with turmeric chia pudding \ud83e\udd29\n\u2800\nYou can find the recipe on her page \ud83d\udc9b\n\u2800\n\ud83c\udf31 Tag #avidofood or\u00a0\u0040avidofood\u00a0to be featured in the future!\n\u2800\n\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\n#vegan #veganfood #veganfoodie #veganfoodshare #vegatarisch #veganpasta #vegandinner #veganeats #healthyfood #plantbased #plantbasedfood #veganpower #plantbaseddiet #veganiseasy #whatveganseat #forksoverknives #vegetarianrecipes #veganinspo #vegetarian #veganism #veganmeals #veganlife #veganlifestyle #veganlove #veganmealprep #veganrecipes #veganhealth #veganlunch",
"id": "18039820117203997",
"media_type": "CAROUSEL_ALBUM",
"media_url": "https://scontent.xx.fbcdn.net/v/t51.2885-15/71511404_203552310658678_3865693276191599368_n.jpg?_nc_cat=100&_nc_oc=AQlet8stFGS32TTdBhXT4NNfpzd8eNq7oI0xilix4qyiVvt50avuk6RVotMgM-BUptmCrsVwLCGkPCc-sL7b-eAy&_nc_ht=scontent.xx&oh=f1a700b4d021d2d577d54cd74f4838fa&oe=5E534677",
"permalink": "https://www.instagram.com/p/B3-XRMOIWPW/",
"username": "avidofood",
"children": {
"data": [
{
"media_url": "https://scontent.xx.fbcdn.net/v/t51.2885-15/71511404_203552310658678_3865693276191599368_n.jpg?_nc_cat=100&_nc_oc=AQlet8stFGS32TTdBhXT4NNfpzd8eNq7oI0xilix4qyiVvt50avuk6RVotMgM-BUptmCrsVwLCGkPCc-sL7b-eAy&_nc_ht=scontent.xx&oh=f1a700b4d021d2d577d54cd74f4838fa&oe=5E534677",
"id": "18041124712207922"
},
{
"media_url": "https://scontent.xx.fbcdn.net/v/t51.2885-15/72483636_1251439178368296_6758852942587086206_n.jpg?_nc_cat=109&_nc_oc=AQmVrktP2g7Z72WifVdu4z17OzwM7ZNFLln1e1ZQxjdUi-j79Ttf-i840mjYkOb-TW3Dwm39Gyoe3EefxwB7UydW&_nc_ht=scontent.xx&oh=a07d3f51aa5eb5eb30697c4ad25d4e35&oe=5E60AEC3",
"id": "17851162897631131"
}
]
}
You just need to nest the fields in the children query params
/media?fields=id,media_url,children{media_url}&access_token=

realtime facebook shares count for URL

I want to get the realtime facebook sharecounts for each url in my site so I can display it.
I found two ways:
using the facebook restserver.php like this:
http://api.facebook.com/restserver.php?method=links.getStats&urls=https://www.daltravel.ro/circuit/Circuit-Anglia-Scotia-Irlanda
And it returns this:
<links_getStats_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
<link_stat>
<url>
https://www.daltravel.ro/circuit/Circuit-Anglia-Scotia-Irlanda
</url>
<normalized_url>
https://www.daltravel.ro/circuit/Circuit-Anglia-Scotia-Irlanda
</normalized_url>
<share_count>0</share_count>
<like_count>0</like_count>
<comment_count>0</comment_count>
<total_count>0</total_count>
<click_count>0</click_count>
<comments_fbid>1084234134929324</comments_fbid>
<commentsbox_count>0</commentsbox_count>
</link_stat>
</links_getStats_response>
Using the garph api, like this:
https://graph.facebook.com/v2.5/https://www.daltravel.ro/circuit/Circuit-Anglia-Scotia-Irlanda?access_token=ACCESS_TOKEN
And it returns like this:
{
"og_object": {
"id": "1084234134929324",
"description": "Marea Britanie si Irlanda. Marea Britanie spune povestea a trei tari: Anglia, Scotia si Tara Galilor, in care castelele, catedralele pub-uri sunt cu adevarat fermecatoare. In Irlanda va bucurati de peisaje naturale uluitoare, dar si de legende stravechi.",
"title": "Dal Travel - agentia devoratorilor de turism",
"type": "website",
"updated_time": "2016-01-10T12:20:56+0000",
"url": "https://www.daltravel.ro/circuit/Circuit-Anglia-Scotia-Irlanda"
},
"share": {
"comment_count": 0,
"share_count": 0
},
"id": "https://www.daltravel.ro/circuit/Circuit-Anglia-Scotia-Irlanda"
}
Both methods show 0 counts, although I shared it several times. Using the second method I also get the "updated_time": "2016-01-10T12:20:56+0000" value.
How can I get the latest information ?
Thank you !

Getting event attending_count on venue-id/events on Facebook Graph API

I'm trying to get the attending_count on facebook graph API. I know that going throug /event-id?fields=attending_count I will have this info available but when trying this inside the venue /venue-id/events?fields=attending_count this is not being displayed.
Here is what i'm trying:
https://graph.facebook.com/v2.2/217733628398158/events?fields=id,cover,name,venue,description,ticket_uri,start_time,attending_count&access_token=....
Any ideas?
There's no attending_count field on the Event object, but there is an attending edge:
https://developers.facebook.com/docs/graph-api/reference/v2.2/event#readfields
https://developers.facebook.com/docs/graph-api/reference/v2.2/event/attending#read
Why are you trying to get the attending_count on the Venue's Page ID? That doesn't make much sense.
You can use the summary to get the absolute count of attendees like this
/217733628398158/events?fields=id,cover,name,venue,description,ticket_uri,start_time,attending.summary(true).limit(1)
To reduce the amount of data returned by the Graph API I added the .limit(1) which is only returning one attendee per event, but producing the summary count anyway. If you need the whole list, remove the limit parameter.
So, it returns
{
"data": [
{
"id": "326299167565639",
"cover": {
"cover_id": "839491946113340",
"source": "https://scontent-a.xx.fbcdn.net/hphotos-xpa1/t31.0-8/s720x720/10547740_839491946113340_540194689313513221_o.jpg",
"offset_y": 28,
"offset_x": 0
},
"name": "Feeling This • Histórias de verão",
"venue": {
"name": "Lima e Silva, 1037, Porto Alegre"
},
"description": "ARE YOU FEELING THIS?\nÉ a nostalgia da estação quente que tá batendo!\n\nFaça aqui a sua história de verão.\nThese are the best days of our lives!\n\n-\n\nIngressos:\nR$15 com Feeling Cup!*\nR$20 na lista* <http://goo.gl/1I9vV8>\nR$25 na hora\n\n* Lista encerra às 20h do dia da festa. \n* Válido para entrada até 00h\n\n\nQuer comemorar seu aniversário na FEELING THIS? Entre em contato através do e-mail cucko#cucko.com.br para saber das vantagens.",
"ticket_uri": "https://www.facebook.com/ajax/events/ticket.php?event_id=326299167565639&source=12&ext=1421830108&hash=ATX8w7CCSBVhKO6G",
"start_time": "2015-02-04T22:00:00-0200",
"attending": {
"data": [
{
"name": "Foo Pretenders",
"rsvp_status": "attending",
"id": "1395261354109384"
}
],
"paging": {
"cursors": {
"after": "TVRBd01EQTROelkxT1RnMk9UYzBPakUwTWpNd09UUTBNREE2TVRZMU1EZzBPRGsyT0RRNE5UZ3g=",
"before": "TVRBd01EQTROelkxT1RnMk9UYzBPakUwTWpNd09UUTBNREE2TVRZMU1EZzBPRGsyT0RRNE5UZ3g="
},
"next": "https://graph.facebook.com/v2.2/326299167565639/attending?summary=true&limit=1&after=TVRBd01EQTROelkxT1RnMk9UYzBPakUwTWpNd09UUTBNREE2TVRZMU1EZzBPRGsyT0RRNE5UZ3g="
},
"summary": {
"count": 634
}
}
},
....
]
There are 634 people attending the specific event.