Dynamic search input as variable for cypher - javascript

I'm trying to create an result page with structr 2.0.1.
enter image description here
Within these page I want to show the results of the user input. The string typed into my input field should be transferred via cypher query to my Neo4j-DB.
Input = "admin" -> Cypher(Match (n) Where n.name = 'admin' Return n)
The return value will be used to instantiate a graph obj via an integer-id (that works totally fine and is no issue).
After hours of investigating unfortunately I'm not able to do it witch the built-in functionality. I tried a lot with the "Query & Data Binding", & "HTML-Properties"-Page and Java Script as well but I couldn't transfer the value from my html element to my cypher query function within my fronted.
[input field ("String")--> button fuction()--> cypher (Query) --> function input {graph.addNode(ID)}]
There must be a solution within structr to solve this problem without a direct ajax-call.
Maybe someone of you discovered the same problem or has a solution for this.
I would very appreciate some help in this case.
Thanks!
Maze

the value of your request parameter is available in StructrScript, see https://support.structr.com/article/119 for more details on that.
In the scripting context, there is an object named request that you can access directly, which will contain any request parameter you send to Structr. So in your case, you can access the value of your input field (provided that you set the name to name) like this:
${request.name}
The following steps are needed to make that work:
create <form method="POST" action="/${page.name}">
insert <input type="text" name="name" value="${request.name}">
insert <input type="submit" value="submit">
When you submit the form, the request parameter "name" will be available in the Structr context as described above.
To insert that value into a Cypher query, you have to construct the query around the request value, for example like that:
${cypher(concat('MATCH (n) WHERE n.name = "', request.name, '" RETURN n'))}
But be very careful with a setup like that, because the above code is vulnerable to a query injection attack (aka SQL injection, or rather CQL injection in this case).

Related

concatenate a list of form fields and mixed text content inside another form field

I am using the following web app, ESRI GeoForm, and attempting to customize it for concatenation purposes. It is a private, internal to our organization only, form. So I can't share the form I am using other than through video or images.
So basically, when the users enter 'Address Number' and 'Address Prefix' and other pieces to the 'Address' I would like to concatenate those entries in the fields "Full Address" and "Full Name."
All of these fields are needed, but I would REALLY rather not require users to do a double input so many times. SO...
This is a Dojo app, built on ESRI's Javascript API. I am fairly comfortable with editing small Javascript scripts, and I've built fairly robust ones years ago, but I'm somewhat lost with such a complex and lengthy apps as this one. Luckily, I just had some developers in the GIT Hub group make a very similar change to add the ability to 'lock' form fields. I have been instructed that I could mimic their addition for the locked functionality for fields to add a concatenate functionality for the fields. So... onward I go.
My first step was to go to the JSON for the fields that are added. This JSON is in the default.js file. I decided to utilize what I believe is an Array object as an attribute option. So, for example, when building the form in the JSON and selecting what fields are needed from the database and how to lay them out on the form you create these field attributes. To create a default value for one of these fields you would create an attribute like so:
...
"defaultValue" : "true"
...
What I've done for the concatenate option is to go with an entry like so:
...
"concantenated": [ "FIELD_ONE", "space", "FIELD_TWO", "space",... ],
...
My thoughts are that each HTML Form Field is identified with an ID that is equal to the actual field name. Like so...
<input type="text" class="form-control" data-input-type="String" maxlength="20" id="ADDRNUM" placeholder="175" data-display-type="text">
That field populates the database field ADDRNUM and it uses that field name as the ID. So in my concatenator attribute when I feed a list of field names I can point to those HTML Form Fields via ID.
This way... can be told to concatenate somehow, maybe like so?
('ADDRNUM').keyup(function () { ('FULLADDR').val(this.value); }
The problem I am having is I am not sure where, in the main.js file I need to add the concatenation process/method/function?
If I follow the system used to integrate a 'Locked' option that was just added a day ago... then I would need to duplicate the "disabled: !!currentField.locked" with something like concantenated: !!currentField.concatenator but I am lost as to where / how I integrate a concatenation method.
UPDATE (10APR18):
So I've begun adapting code under [form folder]/js/main.js to test and work with this. I'm starting on line 772.
if (!!currentField.concantenated) {
domClass.add(formContent, "concantenated");
array.forEach(currentField.concantenated, function(entry, i){
console.log(JSON.stringify(entry, null, 4));
console.log( currentField );
if(entry !== "_space_" || "_comma_")
{
console.log( "It is not _space_ nor _comma_ but rather " + entry );
concatField = dom.byId(entry.stringify);
console.log( concatField );
testField = dom.byId('ADDRNUM');
console.log( testField );
test = on(concatField, 'click', 'foo');
console.log( entry + " was just updated with foo");
}
});
}
This is giving me some good feedback through the console.log and what I am finding out is that the fields I need to reference aren't created yet. Now I could just move the fields to the bottom of the JSON list in [form folder]/config/default.js

Issue with picking multiple JSON

I have recorded a script and "search" an id from it.
I have performed the following things
Have parametrize the "searcID" so that it can be picked from the "CSV_Data Config"
Have extracted the "key" from the URL through "Regular Expression Extractor" to provide it to the desired URL requiring "key", so that it can be dynamic
Now the issue is, since the script is recorded for one search id, in "/build-4.4.10.0/SECChecker/Search/Html?_dc=0.5557150364018139&Grid-Ajax", the last line of my script has a body of the one recorded "searchId".
The script runs and return for each thread this same result of JSON (that is present in the last line i mentioned), i want this too to be dynamic, how can i do that? Please guide
If you want to parametrize the last request, you should use the following notation: ${"var name"} and use a CSV manager. link2
for instance, if you want to parametrize the fist param of the body you should have something like this:
{"SortField":"${var_name}",....
One thing, the dc param, part of the path, looks like a random used to avoid cache, so I use this to simulate the requests during the test:
.../Html?_dc=${__RandomString(15,0123456789)}&Grid-Ajax
This function returns a string which its length is 15 (1st param) and has a set of numbers (2nd param)
Hope It helps you.

Converting input string into rest link javascript

I'm developing a search mechanism with REST requests.
There is an input form, that accepts product titles:
Macbook Pro
iPhone-6
Jack's iPad
I need to send those titles to server in rest format (as a part of a link), such as:
Macbook+Pro
iPhone???6
Jack???s-iPad
Is there a library or a way to do this conversion using javascript? (*I can't use json)
Thank you for your time.
If I am understanding you correctly, you wish to communicate search terms to a REST service?
Assuming your example of Macbook+Pro did not indicate a search operator, you just need to encode your parameters so that the values properly embed into a URL.
For example https://www.google.nl/search?q=These are search arguments will become https://www.google.nl/search?q=These%20are%20search%20arguments.
That, or you do a POST which contains a body (an example is a <form> submit with <input> controls).
Encoding example using plain javascript (jsFiddle):
var searchTerms = 'Macbook Pro';
var searchTermsEncoded = encodeURIComponent(searchTerms);
var url = 'https://www.google.nl/search?q=' + searchTermsEncoded;
console.debug(url); // https://www.google.nl/search?q=Macbook%20Pro
Similarly, Jack's iPad becomes Jack%27s+iPad.
Depending on the type of back-end you are using, your server will decode for you or you have to do it manually. Either way you end up with the original input strings as typed by the user.

Best Practice for using Form Data in a http request

We have a previous implementation whereby we send one key value pair in the form data to the server
ie. message=something_blah
Is there any point in actually having this in form data? Should form data only be used if we are building an object or array of object?
If we only have one key value pair would it not be better to insert it as part of the query request parameter?
Form parameters and query parameters are accessed, more or less, from the same place from within a servlet.
For example, in doGet() (doPost() would've gone the same way, so would all the other http methods)
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String message = request.getParameter("message"); // would return something_blah or null if no parameter
...
}
To answer your question in the OP comments, parameters coming from request.getParameter() are Strings. You can then convert them to other types (parse to numerical value) or use them to initialize Class instances. Their use is up to you.
This parameter could have come from
<form method="get">
<input name="message" type="text" value="something_blah"/>
</form>
or from the url
http://www.yourdns.com/myapp?message=something_blah
To answer Should form data only be used if we are building an object or array of object?, some frameworks like Spring use parameters coming from html forms (and query string) to automatically instantiate objects for you using reflection and some naming conventions. This is not necessary, it's just useful. So review your requirements and see if you need it.

Related Parameters in HTML

I have a table of rows and columns on an HTML-based entry form that allows the user to edit multiple records. Each row corresponds to a database record and each column to a database field.
When the user submits the form, the server needs to figure out which request parameter belongs to which row. The method I've been using for years is to prefix or suffix each HTML input element's name to indicate the row it belongs to. For example, all input elements would have the suffix "row1" so that the server would know that request parameters whose names end with "row1" are field values for the first row.
While this works, one caveat of the suffix/prefix approach is that you're adding a constraint that you can't name any other elements with a particular suffix/prefix. So I wonder if there's a better, more elegant approach. I'm using JSP for the presentation layer, by the way.
Thanks.
I don't know JSP very well, but in PHP you would define your input fields' names with an array syntax.
<input name='person[]'>
<input name='person[]'>
<input name='person[]'>
When PHP receives a form like that, it gives you an array (within the standard $_POST array), thus:
$_POST['person']=array('alice','bob','charlie');
Which makes it very easy to deal with having as many sets of fields as you want.
You can also explicitly name the array elements:
<input name='person[teamleader]'>
<input name='person[developer1]'>
would give you an array with those keys. If your current prefixes are meaningful beyond simply numbering the records, this would solve that problem.
I don't know whether the identical syntax would work for JSP, but I imagine it would allow something very similar.
Hope that helps.
Current user agents send back the values in the order of the fields as presented to the user.
This means that you could (theoretically) drop the prefix/suffix altogether and sort it out based on the ordering of the values. You'd get something like
/?name=Tom&gender=M&name=Jane&gender=F&name=Roger&gender=M
I don't know how your framework returns that, but many return it as lists of each value
name = [Tom, Jane, Roger]
gender = [M, F, M]
If you pop an element off of each list, you should get a related set that you can work with.
The downside to this is that it relies on a standard behavior which is not actually required by the specification. Still... it's a convenient solution with a behavior that won't be problematic in practice.
When browsers POST that information back to the server, it is just a list of parameters:
?name_row1=Jeff&browser_row1=Chrome&name_row2=Mark&browser_row2=IE8
So really, I think you can answer a simpler question: how do you relate keys in a key-value list?
Alternatively, you can go to a more structured delivery method (JSON or XML), which will automatically give you a structured data format. Of course, this means you'll need to build this value on the browser first, then send it via AJAX (or via the value of a hidden input field) and then unpack/deserialize it in the server code.
XML:
<rows>
<row><id>1</id><name>Jeff</name><browser>Chrome</browser></row>
<row>...</row>
</rows>
or JSON:
[{ "name":"Jeff", "browser":"Chrome"}, { "name":"Mark", "browser":"IE8" }]
There are many resources/tutorials on how to do this... Google it. Or go with the ostensible StackOverflow consensus and try jQuery.

Categories

Resources