Meteor dynamic templates from http call and form - java

I'm currently recovering JSON formated from HTTP CALL.
This function is launched with parameters provided by a form template and parameters are pushed on variables with submit event.
My function getusershttp is able to return some results that I'm able to see with console.log(results.content);:
"results" : [
{
"name" : {
"first" : "Billy",
"last" : "McKornic"
},
"id" : "a1c3fd06c71ccc50998baa02074976b4d639e4cf",
"situation" : "free",
},
{
"name" : {
"first" : "Dough",
"last" : "Wallas"
},
"id" : "5694c02beaf20d2d4b5747668b82264af8547e33",
"situation" : "occuped",
}
],
"status" : "OK"
}
I would like to put each result in my articles template with:
<template name="articles">
{{#each results}}
<header>
<p>{{name.first}} {{name.last}}</p>
<p>{{id}}</p>
<p>{{situation}}</p>
</header>
{{/each}}
</template>
What template event and function I must create in order to provide each results data?
Currently I have:
Template.articles.helpers({
results : function() {
return Meteor.call("getusershttp",FormParamX,FormParamY);
}
});
But I have a java error on page loading since my form is not submited and FormParamX and FormParamY are not populated.
How do I force my Template (event & function) to wait my form submited in order to start providing results?
Thanks!

Set the values for FormParamX and Y to reactive variables, then you can simply do:
results() {
if( Template.instance().FormParamX.get() && Template.instance().FormParamY.get() ) {
return Meteor.call("getusershttp",FormParamX,FormParamY);
}
}
Then any time the form parameters change, you'll recall your Meteor method and get the appropriate data.
Additional documentation on reactive variables are here, and there is also a nice walkthrough on TheMeteorChef.

After Stephen tips I took a look with session variables and it works!
In order to help, I provide the solution according my exemple.
First, my form template event function:
Template.search.events({
'submit form':function (event){
event.preventDefault();
var FormParamX = event.target.FormParamX.value;
var FormParamY = event.target.FormParamY.value;
Meteor.call("getusershttp",FormParamX,FormParamY,function(error, results) {
Session.set("MyUsers",results.content);
});
}
});
Here, my session variable MyUsers store my HTTP CALL JSON output.
Then, my articles template helpers:
Template.articles.helpers({
results : function() {
if (Session.get("MyUsers")) {
console.log(JSON.parse(Session.get("MyUsers")).results);
return JSON.parse(Session.get("MyUsers")).results;
};
}
});
Helpers only provide session variable MySearch value if it have some values.
I tested changing my form and on each submit, helpers provide different values as expected.
My template articles is not able now to provide each results but this is another issue.
Update: In order to provide each results value, I need to parse my JSON output. I updated the code and now my template is working.
Another time, thanks Stephen!

Related

Elastic ExistsRequst for field value and field type

I'm working on a project creating integration tests and the data I have includes all the schema updates people have done in the past. For example My "documents" field has a property of user, which has been an array in the past, but is now an Object.
I'm trying to write an integration test that will ONLY use a document if the user type is Object, not array.
Is there a way to do this with an ExistsQuery? So far I have not been able to find anything. Here's my current query which brings back inconsistent data:
GET _search
{
"query" : {
"bool" : {
"must" : {
"exists" : {
"document.user"
}
}
}
}
}
Ideally I'd like to check for document.user[] and filter those values out.
You can use script query like below
{
"query": {
"script": {
"script": "if (doc['document.keyword'].size()==1) return true;"
}
}
}

HashMap cannot be cast to model when trying to iterate set

I am using spring data jpa for creating micro services. In repository I am using JPQL query. Using following code I am able to get set of data. But I want to iterate set of data for further logic.
For iteration I used for each but when I am using for each loop I am getting error "java.util.HashMap cannot be cast to com.spacestudy.model.RoomDepartmentMapping",
public Set<RoomDepartmentMapping> loadStatusOfRooms() {
Set<RoomDepartmentMapping> roomDeptMapping = roomDeptMappingRepo.findStaus();
return roomDeptMapping;
}
}
Repository
#Repository
public interface RoomDepartmentMappingRepository extends JpaRepository<RoomDepartmentMapping, Integer> {
#Query("select new map(roomDeptMap.sStatus as sStatus) from RoomDepartmentMapping as roomDeptMap")
Set<RoomDepartmentMapping> findStaus();
}
Result
[
{
"sStatus": "A"
},
{
"sStatus": "I"
},
{
"sStatus": "R"
}
]
Expecting Result
[
{
"sStatus": "Accepted"
},
{
"sStatus": "In Progress"
},
{
"sStatus": "Remaining"
}
]
For getting above expected result I am trying to iterate Set of data using for each and planning to use switch case. But I am not able to iterate using following loop.
For each loop
for(RoomDepartmentMapping roomDeptMappingObj:roomDeptMapping) {
System.out.println(roomDeptMappingObj);
}
Can any one tell me why I am not able to iterate set using for each loop?
Or please suggest any another way to do that.
Based on the name of your method I will assume that you need all available status values.
The query will be:
#Query("select distinct rdm.sStatus from RoomDepartmentMapping rdm")
Set<String> findStatus();
distinct => you need set
Set => RoomDepartmentMapping sStatus is a String

How to reduce the length of a multivalued field in Solr

We have a multivalued field in Solr that we want to reduce its length.
A sample result response is as follows:
response": {
"numFound": 1,
"start": 0,
"docs": [
{
"created_date": "2016-11-23T13:47:46.55Z",
"solr_index_date": "2016-12-01T08:21:59.78Z",
"modified_date": "2016-12-13T08:45:44.507Z",
"id": "FEAE38C2-ABFF-4F0C-8AFD-9B8F51036D8A",
"Field1": [
"false",
"true",
"true",
..... <= 1200 items
]
}
]
}
We have big data, a couple of TB and we are looking for an optimized way to alter all documents within Solr and to modify Field1 to contain only the first 100 items.
Can something like this be done without the need to write a script to manually fetch the document, make adjustments and push it back to solr? Has anyone had a similar experience? Thanks
We have faced this problem. But we use Two collections to solve this problem. Use SoleEntityProcessor to move the document from one collection to another.
[SolrEntityProcessor]
<dataConfig>
<document>
<entity name="sep" processor="SolrEntityProcessor" url="http://localhost:8983/solr/db" query="*:*"/>
</document>
</dataConfig>
While moving pass that document through updateRequestProcessorChain where we can write StatelessScriptUpdateProcessorFactory to edit our documents or to truncate the multivalued field.
In StatelessScriptUpdateProcessorFactory you can get the field and apply your operations and then reset that field.
[StatelessScriptUpdateProcessorFactory]
function processAdd(cmd) {
doc = cmd.solrDoc;
multiDate = doc.getFieldValue("multiValueField");
//Apply your operation to above field
//doc.setField("multiValueField",value);
}
function processDelete(cmd) {
// no-op
}
function processMergeIndexes(cmd) {
// no-op
}
function processCommit(cmd) {
// no-op
}
function processRollback(cmd) {
// no-op
}
function finish() {
// no-op
}
For More information on StatelessScriptUpdateProcessorFactory, you can refer to this question
On solr how can i copy selected values only from multi valued field to another multi valued field?
in which they edit the multivalued field using the script.

Lucen/Elasticsearch: a query for a filed with multiple values

Here is what I would like to do. I have an index with several documents in Elasticsearch. In every document I have two field: deviceField (name of the device) and pressionField (the value of the pression periodically). I want to query in my index the average pression per device. Do you know a way to do it in a single query? Indeed, I do not want to do a kind of loop 'for' in order to the query per deviceName. It will take too much time due to the fact that I have millions of devices.
Thank you for your attention and your help.
S
You have to use metric aggregations. If you want to list all at same query you use subaggregation
{
"aggs" : {
"devices" : {
"terms" : { "field" : "deviceField" },
"aggs" : {
"avg_pression" : { "avg" : { "field" : "pressionField" } }
}
}
}
}
Here is the link of documentation:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html

Problems passing data to jquery's getJSON() - Will not accept map

I am trying to serialize my form (JSP/Struts 1.1) and put it into an object or map or whatever jQuery's .getJSON() method needs. Here is my js code:
// This function makes an AJAX call, passing the entire form to the Action class
function ajaxCallWithForm(inputURL, formName, onReturnFunction)
{
var formAsMap = serializeForm(formName);
$.getJSON(inputURL, formAsMap, onReturnFunction);
}
function serializeForm(formName)
{
var obj = {};
var a = $('#'+formName).serializeArray();
$.each(a, function() {
if (obj[this.name] !== undefined) {
if (!obj[this.name].push) {
obj[this.name] = [obj[this.name]];
}
obj[this.name].push(this.value || '');
} else {
obj[this.name] = this.value || '';
}
});
return obj;
}
This results in a java.lang.IllegalArgumentException on the back end (something to do with the BeanUtils.populate servlet method).
If I set the 2nd of 3 parameters of my .getJSON() call to something like this, it works fine and the data shows up in the form object in my Java back end:
// This function makes an AJAX call, passing the entire form to the Action class
function ajaxCallWithForm(inputURL, formName, onReturnFunction)
{
$.getJSON(inputURL, {"vehicleKeyNum":12345,
"vehicleID":"12345",
"rand":Math.random()},
onReturnFunction);
}
I have also tried creating a string with the proper syntax that includes the data from the form and that results in the same thing. I may have my syntax wrong for that. At any rate, my main problem is that:
1) The .getJSON() method accepts, "A map or string that is sent to the server with the request." as its 2nd parameter (see http://api.jquery.com/jQuery.getJSON/)
2) I am passing what I think is a "map"
3) I am getting a java.lang.IllegalArgumentException and don't know where to go from here
If you want to submit a form to server, you can simply use jQuery's serialize() OR serializeArray() method.
$.getJSON(inputURL, $(formName).serialize(), onReturnFunction);
You should have the data returned by the serialize/serializeArray method populated in your form bean if the element names are matched right.
here is a working example of serialize method (copied from jQuery website)
java.lang.IllegalArgumentException from the BeanUtils.populate servlet method is due to data type mismatch between the data submitted and the data on the form bean.

Categories