{"id":1400,"date":"2018-04-16T21:29:46","date_gmt":"2018-04-16T21:29:46","guid":{"rendered":"http:\/\/capture.ccio.us\/?p=1400"},"modified":"2018-04-16T21:29:46","modified_gmt":"2018-04-16T21:29:46","slug":"java-javascript-living-together","status":"publish","type":"post","link":"https:\/\/capture.club\/portal\/2018\/04\/16\/java-javascript-living-together\/","title":{"rendered":"Java and JavaScript, Living Together!"},"content":{"rendered":"<body><p><\/p><iframe loading=\"lazy\" title=\"Mass Hysteria\" width=\"696\" height=\"392\" src=\"https:\/\/www.youtube.com\/embed\/JmzuRXLzqKk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><br>\n<!--<a href=\"https:\/\/www.youtube.com\/watch?v=JmzuRXLzqKk\" target=\"blank\"> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1401\" src=\"http:\/\/capture.ccio.us\/wp-content\/uploads\/2018\/04\/Screenshot-2018-04-16-10.57.20.png\" alt=\"screenshot-2018-04-16-10-57-20\" width=\"851\" height=\"353\" \/><\/a>\n-->\n<h1>\u201cDogs and Cats, living together\u2026 mass hysteria!\u201d<\/h1>\n<p>One of the very interesting features of a JavaScript stage in Fusion (and of Java 8\/Nashorn, in general) is the ability to intermingle the two languages. By way of example, I offer the following JavaScript stage. In this case, we\u2019re pulling from an geolocation process, and creating a query from the resulting attributes. You\u2019ll notice at the top that we declare several Java classes, and then when we go to JSON, presto!, we switch over to JavaScript and run through our list, feeding the values into a java.util.ArrayList, and then we switch back to Java and iterate over the list and build our query. We could have used a regular old JavaScript Array, but in this case it makes a textbook illustration how the two languages can interact as one in the context of the Java ScriptEngine.<\/p>\n<table border=\"0\" cellspacing=\"4\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td>\n<pre>      function (request, response) {\n    \/\/ java imports\n    var e = java.lang.Exception;\n    var URL = java.net.URL;\n    var BufferedReader = java.io.BufferedReader;\n    var InputStreamReader = java.io.InputStreamReader;\n    var String = java.lang.String;\n   \/\/ local declarations\n    var result = new String(\"\");\n    var ArrayList = java.util.ArrayList;\n    try {\n        var url = \"https:\/\/mapservices.gis.saccounty.net\/arcgistest\/rest\/services\/ADDRESS_wSubAddress\/GeocodeServer\/findAddressCandidates?outFields=*&amp;outSR=4326&amp;f=pjson&amp;Single+Line+Input=799+G+St\";\n        var targetUrl = new URL(url);\n        var ins = new BufferedReader(new InputStreamReader(targetUrl.openStream()));\n        var inputLine = \"\";\n        var list = new ArrayList();\n        while ((inputLine = ins.readLine()) !== null) {\n            \/\/logger.info(inputLine); \/\/ if you want to see what is on each line\n            result += inputLine;\n        }\n        ins.close();\n        logger.info(\"Return RESULT: \" + result); \/\/ full result\n        \/\/ convert to JSON using JavaScript\n        var json = JSON.parse(result);\n        logger.info(\" ** JSON CREATED OKAY ** \" + JSON.stringify(json));\n        \/\/ get candidates\n        var candidates = json.candidates;\n        logger.info(\"Candidates: \" + candidates.length);\n        for (var i = 0; i  0) {\n            var count = 0;\n            for each (var ref in list) {\n                if (count &gt; 0) {\n                    query += \" OR \";\n                }\n                \/\/ @TODO:  CHANGE THE FIELD TO QUERY TO AN EXISTING FIELD IN YOUR SCHEMA\n                query += \"FIELD_TO_QUERY:\" + ref;\n                count++;\n            }\n           \/\/ print out the query in the logs\n            logger.info(\"**** QUERY: *** \" + query);\n            \/\/ add to response\n            response.initialEntity.appendStringList(\"solr_query\", Java.to(query, Java.type('java.util.List')));\n        }\n    } catch (e) {\n        logger.error(e);\n    }\n}\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Why is this so odd? Well, here\u2019s the thing: Java and JavaScript, despite sharing the similarity of the word \u201cjava\u201d are really two entirely different beasts.<br>\nJava is a strictly-typed, procedural language (albeit with a few functional traits). JavaScript, on the other hand, is a loosely-typed functional language, with more in common with languages like LISP and Scala. So to see these two playing nice in the same sandbox is certainly a treat, and a powerful tool, indeed.<\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>\u201cDogs and Cats, living together\u2026 mass hysteria!\u201d One of the very interesting features of a JavaScript stage in Fusion (and of Java 8\/Nashorn, in general) is the ability to intermingle the two languages. By way of example, I offer the following JavaScript stage. In this case, we\u2019re pulling from an geolocation process, and creating a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[],"tags":[],"class_list":["post-1400","post","type-post","status-publish","format-standard","hentry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts\/1400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/comments?post=1400"}],"version-history":[{"count":0,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts\/1400\/revisions"}],"wp:attachment":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/media?parent=1400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/categories?post=1400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/tags?post=1400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}