Fullcalendar one feed with multiple JSON objects - java

Hello Fullcalendar followers, i am trying to make use of threads in my server side to load events to the calendar so i can get a better performance on loading events, meaning:
1 - I only have ONE evenSource feed:
eventSources: [othersources.funcmap] //Employee calendar MAP
2 - In the Servlet ( I'm working on java ):
I am gathering all JSON objets from different sources and joining them together in one large big object (with threads) that i want to send back to Fullcalendar.
2.1 - If i send them individual ( multiple ajax call meaning [othersources.vacations,othersources.faults etc...]) has Strings ( JSON FORMAT ) they work fine and all the feeds are loaded.
This is the JSON object in the "individual" String -> `[{"title":"Vacation day","start":"02-09-2013", etc etc etc}]`
PROBLEM
The problem is when i join them together i make the object like this:
[
[{"title":"Vacation day","start":"02-09-2013", etc etc etc},{"title":"Vacation day","start":"02-09-2013", etc etc etc}],
[{"title":"Fault day","start":"02-09-2013", etc etc etc},{"title":"Faul day","start":"02-09-2013", etc etc etc}],
[{"title":"Birthday fault","start":"02-09-2013", etc etc etc},{"title":"Birthday fault","start":"02-09-2013", etc etc etc}]
]
This is actually a valid JSON object ( without the "etc etc etc" ofcourse :P ) but it doesnt work. fullcalendar wont render the events...
How can i join them together in one big object that Fullcalendar understand?
Or Fullcalendar only knows how to read simple JSON object?
Thank you in advance.

The easiest solution would probably be to flatten the events array before handing it to FullCalendar. But if you insist, then it is also possible by providing a function as a event source in FullCalendar.
The documentation for the function is here: http://arshaw.com/fullcalendar/docs/event_data/events_function/
And this is the basic way to do it:
eventSources: [function (start, end, callback) {
var eventArrays = [
[{
"title": "Vacation day",
"start": new Date()
}, {
"title": "Vacation day",
"start": new Date()
}],
[{
"title": "Fault day",
"start": new Date()
}, {
"title": "Faul day",
"start": new Date()
}],
[{
"title": "Birthday fault",
"start": new Date()
}, {
"title": "Birthday fault",
"start": new Date()
}]
];
// Using underscore.js to flatten the array
var events = _.flatten(eventArrays);
callback(events);
}]
You can check out a working example here: http://jsfiddle.net/kvakulo/q5HET/1/

In the end this makes no sense at all. Multiple AJAX calls are faster, than trying to make one that returns all even with threads in the servlet.
But it worked with the Flat JSON i just joined them all together [{},{}...] with no subarrays.

Related

Can We Work with Multiple Json Format from Single Topic using Single Stream App in Apache Kafka?

I have 2 json format in same kafka topic
1st JSON
{
"id": 123,
"name": "name",
"values": [
"one": 1,
"two": 2,
"three": 3,
"four": 4
]
}
2nd JSON
{
"id": 8,
"title": "Microsoft Surface Laptop 4",
"description": "Style and speed. Stand out on ...",
"price": 1499,
"discountPercentage": 10.23,
"rating": 4.43,
"stock": 68,
"brand": "Microsoft Surface",
"category": "laptops",
"thumbnail": "https://dummyjson.com/image/i/products/8/thumbnail.jpg",
"images": [
"https://dummyjson.com/image/i/products/8/1.jpg",
"https://dummyjson.com/image/i/products/8/2.jpg",
"https://dummyjson.com/image/i/products/8/3.jpg",
"https://dummyjson.com/image/i/products/8/4.jpg",
"https://dummyjson.com/image/i/products/8/thumbnail.jpg"
]
}
I want to perform some transformation on these two JSON's with single Stream application and produce output in different topics.
Is it possible to work with single stream app ? if yes, then how?
Iam currently using JAVA for stream application
Sure, you can consume the data as generic JsonObject/JsonNode classes using builtin JSON deserializer, then check for specific fields in a filter/branch operator before parsing to a specific POJO.
Or you can use String Serde, pass strings to JSON parsers, and do the same.
Alternatively, use two different topics and join events based on the ID field, for example

Sort the search result in ascending order of a multivalued field in Solr

I'm using Solr of version 6.6.0. I have a schema of title (text_general), description(text_general), id(integer). When I search for a keyword to list the results in ascending order of the title my code returns an error can not sort on multivalued field: title.
I have tried to set the sort using the following 3 methods
SolrQuery query = new SolrQuery();
1. query.setSort("title", SolrQuery.ORDER order);
2. query.addSort("title", SolrQuery.ORDER order);
3. SortClause ab = new SolrQuery.SortClause("title", SolrQuery.ORDER.asc);
query.addSort(ab);
but all of these returns the same error
I found a solution by referring to this answer
It says to use min/max functions.
query.setSort(field("pageTitle",min), ORDER.asc);
this what I'm trying to set as the query, I didn't understand what are the arguments used here.
This is the maven dependency that I'm using
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>6.5.1</version>
</dependency>
Unless title actually is multiValued - can your post have multiple titles - you should define it as multiValued="false" in your schema. However, there's a second issue - a field of the default type text_general isn't suited for sorting, as it'll generate multiple tokens, one for each word in the title. This is useful for searching, but will give weird and non-intuitive results when sorting.
So instead, define a title_sort field and use a field type with a KeywordTokenizer and LowerCaseFilter attached (if you want case insensitive sort), or if you want case sensitive sort, use the already defined string field type for the title_sort field.
The first thing to check is do you really need that title field to be multivalued, or do your documents really have multiple titles ? If not, you just need to fix the field definition by setting multivalued="false".
That said, sorting on a multivalued field doesn't make sense unless determining which one of these multiple values should be used to sort on, or how to combine them into one.
Let' say we need to sort a given resultset by title (alphabetically), first using a single-valued title field :
# Unsorted
"docs": [
{ "id": "1", "title": "One" },
{ "id": "2", "title": "Two" },
{ "id": "3", "title": "Three" },
]
# Sorted
"docs": [
{ "id": "1", "title": "One" },
{ "id": "3", "title": "Three" },
{ "id": "2", "title": "Two" },
]
# -> ok no problem here
Now applying the same logic with a multi-valued field is not possible as is, you would necessarily need to determine which title to use in each document to properly sort them :
# Unorted
"docs": [
{ "id": "1", "title": ["One", "z-One", "a-One"] },
{ "id": "2", "title": ["Two", "z-Two", "a-Two"] },
{ "id": "3", "title": ["Three", "z-Three", "a-Three"] }
]
Hopefully, Solr allows to sort results by the output of a function, meaning you can use any from Solr's function queries to "get" a single value per title field. The answer you referred to is a good example even though it may not work for you (because title would need docValues enabled - depends on field definition - and knowing that max/min functions should be used only with numeric values), just to get the idea :
# here the 2nd argument is a callback to max(), used precisely to get a single value from title
sort=field(title,max) asc

Checkboxes checked into JSON Format in SpringMVC

I am working on a spring MVC application. I have a sitaution where i need to check some checkboxes from UI and save the checked values in the form of JSON in the backend and i need to convert that into a string.
The picture shows more.
So i want to save like:
[{
Coast : 'East',
States : [ 'NY', 'MI' ]
},{
Coast : 'Central',
States : [ 'TX', 'OK' ]
}].
Please suggest me how can i implement this.
Your question is quite vague so I'm going to assume because you've used the json tag that you're asking for help on how to model this information in JSON and handle it within your Spring app.
You probably want to restructure your JSON schema to support extra fields being set per state. Instead of States being a list of strings, you could change it to a list of objects which has a name and selected field.
I'd also recommend you change the keys in your JSON to be lower case, this enables more fluent mapping between your JSON and model classes.
For example, MI is selected in the below JSON, whereas NY isn't:
[{
"coast": "East",
"states": [{
"name": "NY",
"selected": true
}, {
"name": "MI",
"selected": false
}]
}, {
...same again for West and Central
}]
You could then have some classes along the lines of and use Jackson to map between them:
public class Region {
String coast;
List<State> states;
}
public class State {
String name;
boolean selected;
}

Best practice for "connecting" json objects

My json:
{
"activities": [
{
"type": "post",
"id": "160",
"name": "John Wayne",
"content": "this is the actual post",
"timestamp" : "date + time",
},
{
"type": "post"
"id": "161",
"name": "Angelina Jolie",
"content": "this is the actual post",
"timestamp" : "date + time",
{
"type": "comment"
"id": "162",
"name": "Ravi Tamada",
"content": "content of the comment",
"timestamp" : "time + date",
"secondary_id": "160"
}
]
}
I have 2 types of objects which i have to consider: comment and post.
Every comment is linked to a post with its secondary id. That means if secondary_id is "160" this comment belongs to the the post with the id 160. In my json there could be 50 posts then 1 comment to the 2nd post follwed by 10 more psots and then 6 various comments. Means its not orderd.
So now im trying to figure out how to safe them in my java code to get them linked. Means i need every comment for a certain post. I could do 2 arrays 1 for posts 1 for comments and then search the comments array for every comment with the id 160...161..etc... this would be pretty messy i guess.
Isnt there some pattern or smth like the key is 160 and now give me all the secondary id´s mapped as value for that key or smth?
I hope you know what i mean, kinda of hard to expain.
Basically what i would need is smth like go through the json check if its post if yes safe it. If its a comment look up the secondary id and match it to the map(or smth) of the right primary id.
First of all when serializing JSON in java there are libraries you can use, Jackson for example.
Once you familiar yourself with Jackson, I would create a base class for comment and post that has all the fields that are shared by them.
I would create a custom deserializer that after creating a comment object looking up the post object and adding the comment to the post.

Flatten and De-flatten for Solr input/output

I have lots of Java objects which have parent child relationships.
These need to be put into Solr.
To do that, we convert the Java object into json as follows:
{
"employee": {
"name" : "John",
"address": {
"apt": 100,
"city": "New York",
"country": "USA"
},
"vehicles": [
{
"name" : "Hyundai",
"color" : "red"
},
{
"name" : "Toyota",
"color" : "black"
}
]
}
}
Now since Solr does not handle this, I am flattening it out as follows:
"employee.name": "John",
"employee.address.apt": 100,
"employee.address.city": "New York",
"employee.address.country": "USA",
"employee.vehicles_0.name": "Hyundai", // Note how arrays are being flattened
"employee.vehicles_0.color": "red",
"employee.vehicles_1.name": "Toyota",
"employee.vehicles_1.color": "black",
It is easy to flatten, but clients of my library do not want the flattened schema when they query.
So I need to de-flatten the above on return from Solr and convert them back to the original Java object.
Does anyone know how this can be done?
I am thinking of somewhat crude way of taking the flattened output from Solr (as shown above) and write a parser to put the fields back to Java objects. But this seems like lot of work. An easy way out or an existing tool would be much appreciated.
I am using Solr 4.5.1
Solr is designed for search, not storing deep object graphs. You might be better off optimizing the Solr records for search and then getting the original objects from the master store by recordID or some such.
Think about what will you be trying to find. For example, will you be searching for individual vehicles? If yes, your current document level should be a vehicle not an employee.
You can index your documents in a Parent->Child structure at first place.
Take a look at this blog post: http://blog.griddynamics.com/2013/09/solr-block-join-support.html

Categories

Resources