Gridx Server-side filtering - reference? example? - java

I have successfully created a:
Gridx using JSONStore
Server-side paging using 'Range : items=0-99' header
Server-side sorting using 'sort(+name)'
... but after much effort and searching I haven't been able to setup the Filterbar module to perform Server-side filtering.
I have used 'filterSetupQuery' to pull information out of JSON (on the client) and append to the get URL for very basic filters, but with complicated filters it seems to make more sense to process the JSON in the Java based controller on the server.
What would this Java class and FlexJson deserializer even look like?
Does someone have a reference implementation of Server-side filtering, or even an example to show how to deserialize this object in Java?
Here is a simple JSON object that is sent back to the controller:
{ "op":"and",
"data":[{ "op":"or",
"data":[{"op":"contain","data":[{"op":"string","data":"1","isCol":true},
{"op":"string","data":"john"}]},
{"op":"contain","data":[{"op":"string","data":"2","isCol":true},
{"op":"string","data":"john"}]},
{"op":"contain","data":[{"op":"string","data":"3","isCol":true},
{"op":"string","data":"john"}]},
{"op":"contain","data":[{"op":"string","data":"4","isCol":true},
{"op":"string","data":"john"}]}
]}]}
Any help is greatly appreciated!
Thank you
Chris

You can check gridx source code.. Under \tests\test_grid_filter_serverside.html
filterSetupFilterQuery: function(expr){
var toExpr = function(expr){
if(!expr){ return ""; }
if(typeof expr.data != "object"){
if(expr.isCol){
return "column(\"" + expr.data + "\")";
}
return "\"" + expr.data + "\"";
}else{
var exprs = [];
for(var i in expr.data){
exprs.push(toExpr(expr.data[i]));
}
var op = expr.op;
if(expr.op == "and"){
op = "logicand";
}
if(expr.op == "or"){
op = "logicor";
}
return op + "(" + exprs.join(",") + ")";
}
};
console.log("expr is: ", expr);
var newExpr = toExpr(expr);
if(newExpr){ newExpr += ";"}
console.log("expr is: ", newExpr);
return {query: newExpr};
},
, the function above outputs something like ..
logicor(logicor(contain(column("1"),"drawal"),contain(column("2"),"drawal"),contain(column("3"),"drawal"),contain(column("4"),"drawal"),contain(column("5"),"drawal"),contain(column("6"),"drawal"),contain(column("7"),"drawal")));
I was able to change the function to output sql as below
var toExpr = function(expr) {
if (!expr) {
return "";
}
if ( typeof expr.data != "object") {
if (expr.isCol) {
return cols.item(expr.data);
// return "column(\"" + expr.data + "\")";
}
return "\"" + expr.data + "\"";
} else {
var exprs = [];
for (var i in expr.data) {
exprs.push(toExpr(expr.data[i]));
}
var op = expr.op;
if (op == 'not') {
return "(" + exprs[0].replace(/=/, '<>').replace(/like/, ' not like ') + ")";
}
if (op == 'contain') {
return "(" + exprs[0] + ' like ' + exprs[1].replace(/^"/, '"%').replace(/"$/, '%"') + ")";
}
if (op == 'startWith') {
return "(" + exprs[0] + ' like ' + exprs[1].replace(/^"/, '"%') + ")";
}
if (op == 'endWith') {
return "(" + exprs[0] + ' like ' + exprs[1].replace(/"$/, '"%') + ")";
}
return "(" + exprs.join(" " + op.replace(/equal/, '=') + " ") + ")";
}
};
var newExpr = toExpr(expr);
if (newExpr) {
newExpr += ";";
}
console.log("expr is: ", newExpr);

Related

Performance tuning for JavaRDD function

I want to convert dataframe to Array of Json using Java and Spark version 1.6, for which am converting the data from
Dataframe -> Json -> RDD -> Array
where the data looks like this.
[
{
"prtdy_pgm_x":"P818_C",
"prtdy_pgm_x":"P818",
"prtdy_attr_c":"Cost",
"prtdy_integer_r":0,
"prtdy_cds_d":"prxm",
"prtdy_created_s":"2018-05-12 04:12:19.0",
"prtdy_created_by_c":"brq",
"prtdy_create_proc_x":"w_pprtdy_security_t",
"snapshot_d":"2018-05-12-000018"
},
{
"prtdy_pgm_x":"P818_I",
"prtdy_pgm_x":"P818",
"prtdy_attr_c":"Tooling",
"prtdy_integer_r":0,
"prtdy_cds_d":"prxm",
"prtdy_created_s":"2018-05-12 04:12:20.0",
"prtdy_created_by_c":"brq",
"prtdy_create_proc_x":"w_pprtdy_security_t",
"snapshot_d":"2018-05-12-000018"
},
{
"prtdy_pgm_x":"P818_W",
"prtdy_pgm_x":"P818",
"prtdy_attr_c":"Weight",
"prtdy_integer_r":0,
"prtdy_cds_d":"prxm",
"prtdy_created_s":"2018-05-12 04:12:20.0",
"prtdy_created_by_c":"brq",
"prtdy_create_proc_x":"w_pprtdy_security_t",
"snapshot_d":"2018-05-12-000018"
},
......
]
so I wrote my code something like this.
if(cmnTableNames != null && cmnTableNames.length > 0)
{
for(int i=0; i < cmnTableNames.length; i++)
{
String cmnTableName = cmnTableNames[i];
DataFrame cmnTableContent = null;
if(cmnTableName.contains("PTR_security_t"))
{
cmnTableContent = hiveContext.sql("SELECT * FROM " + cmnTableName + " where fbrn04_snapshot_d = '" + snapshotId + "'");
}
else
{
cmnTableContent = hiveContext.sql("SELECT * FROM " + cmnTableName);
}
String cmnTable = cmnTableName.substring(cmnTableName.lastIndexOf(".") + 1);
if (cmnTableContent.count() > 0)
{
String cmnStgTblDir = hdfsPath + "/staging/" + rptName + "/common/" + cmnTable;
JavaRDD<String> cmnTblCntJson = cmnTableContent.toJSON().toJavaRDD();
String result = cmnTblCntJson.reduce((ob1, ob2) -> (String)ob1+","+(String)ob2); //This Part, takes more time than usual contains large set of data.
String output = "["+result+"]";
ArrayList<String> outputList = new ArrayList<String>();
outputList.add(output);
JavaRDD<String> finalOutputRDD = sc.parallelize(outputList);
String cmnStgMrgdDir = cmnStgTblDir + "/mergedfile";
if(dfs.exists(new Path(cmnStgTblDir + "/mergedfile"))) dfs.delete(new Path(cmnStgTblDir + "/mergedfile"), true);
finalOutputRDD.coalesce(1).saveAsTextFile(cmnStgMrgdDir, GzipCodec.class);
fileStatus = dfs.getFileStatus(new Path(cmnStgMrgdDir + "/part-00000.gz"));
dfs.setPermission(fileStatus.getPath(),FsPermission.createImmutable((short) 0770));
dfs.rename(new Path(cmnStgMrgdDir + "/part-00000.gz"), new Path(CommonPath + "/" + cmnTable + ".json.gz"));
}
else
{
System.out.println("There are no records in " + cmnTableName);
}
}
}
else
{
System.out.println("The common table lists are null.");
}
sc.stop();
but while reduce function is applied it's taking more time
JavaRDD<String> cmnTblCntJson = cmnTableContent.toJSON().toJavaRDD();
String result = cmnTblCntJson.reduce((ob1, ob2) -> (String)ob1+","+(String)ob2); //This Part, takes more time than usual contains large set of data.
the table with the partition "PTR_security_t" is huge and takes a lot of time compared to other tables which don't have partitions (40-50 mins odd for 588kb)
I Tried Applying Lambda but i ended up with Task not serializable error. Check the code below.
if(cmnTableNames != null && cmnTableNames.length > 0)
{
List<String> commonTableList = Arrays.asList(cmnTableNames);
DataFrame commonTableDF = sqc.createDataset(commonTableList,Encoders.STRING()).toDF();
commonTableDF.toJavaRDD().foreach(cmnTableNameRDD -> {
DataFrame cmnTableContent = null;
String cmnTableName = cmnTableNameRDD.mkString();
if(cmnTableName.contains("PTR_security_t"))
{
cmnTableContent = hiveContext.sql("SELECT * FROM " + cmnTableName + " where fbrn04_snapshot_d = '" + snapshotId + "'");
}
else
{
cmnTableContent = hiveContext.sql("SELECT * FROM " + cmnTableName);
}
String cmnTable = cmnTableName.substring(cmnTableName.lastIndexOf(".") + 1);
if (cmnTableContent.count() > 0)
{
String cmnStgTblDir = hdfsPath + "/staging/" + rptName + "/common/" + cmnTable;
JavaRDD<String> cmnTblCntJson = cmnTableContent.toJSON().toJavaRDD();
String result = cmnTblCntJson.reduce((ob1, ob2) -> (String)ob1+","+(String)ob2);
String output = "["+result+"]";
ArrayList<String> outputList = new ArrayList<String>();
outputList.add(output);
JavaRDD<String> finalOutputRDD = sc.parallelize(outputList);
String cmnStgMrgdDir = cmnStgTblDir + "/mergedfile";
if(dfs.exists(new Path(cmnStgTblDir + "/mergedfile"))) dfs.delete(new Path(cmnStgTblDir + "/mergedfile"), true);
finalOutputRDD.coalesce(1).saveAsTextFile(cmnStgMrgdDir, GzipCodec.class);
fileStatus = dfs.getFileStatus(new Path(cmnStgMrgdDir + "/part-00000.gz"));
dfs.setPermission(fileStatus.getPath(),FsPermission.createImmutable((short) 0770));
dfs.rename(new Path(cmnStgMrgdDir + "/part-00000.gz"), new Path(CommonPath + "/" + cmnTable + ".json.gz"));
}
else
{
System.out.println("There are no records in " + cmnTableName);
}
});
}
else
{
System.out.println("The common table lists are null.");
}
sc.stop();
is there any efficient way where i can enhance my Performance ?

Is it neccessary that left-hand side of an assignment must be a variable?

public String toString() {
return " Recipient:"+ this.toString(this.getRecipient()) +
" CC:"+ this.toString
(
if(this.getCC()==null)
{
getCC() = "";
}
) +
" Subject:"+this.getSubject() +
" Body:"+ this.getBody() +
" files:"+ this.getFiles();
}
I am having this error in:
getCC() = ""; <--------- Here.
Do you know why is this happening?
Thanks in advance
You can't assign a value to the return of a method. You also can't place an expression inside a method which takes no arguments. What you appear to have intended was to use a variable.
String cc = getCC();
if (cc == null) cc = "";
return ... " CC:" + cc + ...

Restrictions OR in hibernate (Java)

I want rewrite sql query to hibernate criteria and I have this problem.
It is original:
if (null != filterGroupIds && !filterGroupIds.isEmpty()) {
hqueryText += (requereAnd ? " and " : "") + "(";
requereAnd = true;
if (filterGroupIds.contains("null")) {
hqueryText += " w.crmUser.groupId = null ";
filterGroupIds.remove("null");
hqueryText += filterGroupIds.isEmpty() ? "" : "or";
}
if (!filterGroupIds.isEmpty())
hqueryText += " w.crmUser.groupId in (" + StringUtils.join(filterGroupIds.iterator(), ",") + ")";
hqueryText += ")";
}
It is my criteria:
if (null != filterGroupIds && !filterGroupIds.isEmpty()) {
if (filterGroupIds.contains(Integer.valueOf(-1))) {
criteria.add(Restrictions.eq("crmUser.groupId", null));
filterGroupIds.remove(Integer.valueOf(-1));
if (!filterGroupIds.isEmpty()) {
criteria.add(Restrictions.or(Restrictions.in("crmUser.groupId", filterGroupIds)));
}
}else {
criteria.add(Restrictions.in("crmUser.groupId", filterGroupIds));
}
}
but my OR Restrictions not work.
You should be getting compile time error for
criteria.add(Restrictions.or(Restrictions.in("crmUser.groupId", filterGroupIds)));
as Restrictions.or() takes two criterion values.Recheck your code and pass two criterion object to be compared.

Delay xmlHTTPresponse in ajax until result generate from backend

I have this java script code for ajax and I'm having trouble of get response.
xmlhttp.status always become 0. I have repeatCallHr Servlet and getting parameters from jsp page as follows.
function loadResults() {
var tt = document.getElementById("StartDate");
alert(tt);
if (xmlhttp) {
var query = "generateResults?name="
+ document.getElementById("name").value
+"&startDate="+document.getElementById("StartDate").value
+"&startTime="+document.getElementById("StartTime").value;
xmlhttp.open("GET", query, true);
xmlhttp.onreadystatechange = loadStallsResponse;
xmlhttp.send(null);
} else {
alert("Browser not supported!");
}}
function loadStallsResponse() {
alert("come here");
alert(xmlhttp.readyState);
if (xmlhttp.readyState == 4) {
alert("status is :"+xmlhttp.status);
if (xmlhttp.status == 200) {
resultList = [];
if (xmlhttp.responseText.indexOf("Error") == -1) {
alert("come to inside of status::")
console.log(xmlhttp.responseText);
stallList = $.parseJSON(xmlhttp.responseText);
populateStallData();
} else {
alert(xmlhttp.responseText);
}
} else {
alert("Error in loading salesman data");
}
}}
function populateStallData() {
var table = "<tr><th>Date</th><th>Time</th><th>Skill</th><th>Total Call</th><th>Unique Calls</th><th>Repeat Calls</th></tr>";
for (var i = 0; i < resultList.length; i++) {
table += "</td><td width='140'>"
+ resultList[i].date
+ "</td><td width='140'>"
+ resultList[i].time
+ "</td><td width='140'>"
+ resultList[i].skill
+ "</td><td width='140'><a style='color: -webkit-link; text-decoration: underline; cursor: auto;' href='http://"
+ resultList[i].totalCall
+ "</td><td width='140'>"
+ resultList[i].uniqueCall
+ "</td><td width='140'>"
+ resultList[i].repeatCall
+ "</td></tr>";
}
document.getElementById("ResultTable").innerHTML = table;}
In back end it takes more than 5 minutes to query the sql statement so that xmlhttp.status return as 0 (I guess, but i'm not sure). How can I delay the xmlhttp response time until I generate the results in back end?

Using 2 QueryParams to return an object requiring 2 different variables

I'm writing a special permission forms program using MySQL, Javascript, and HTML code, which both respond to. I'm doing all of this using singleton pattern access and facade code(s) in java, and a services code, which responds to the singleton pattern codes.
I'm trying To retrieve a form (AKA 1 Result) by the variables, courseDept & courseNm.
This is a snippet of code I'm using to do all this:
FormDataFacade.java code snippet:
#Path("/specialPermissions/sp")
#GET
#Produces("text/plain")
public Response getSpecialPermissionFormByDeptAndRoomNm(#QueryParam("courseDept") String theDept, #QueryParam("courseNm") String theNm)
throws NamingException, SQLException, ClassNotFoundException
{
//Referenciation to FormDataFacade class.
FormDataFacade iFacade = FormDataFacade.getInstance();
int intNm = 0;
try {
intNm = Integer.parseInt(theNm);
}catch (NumberFormatException FAIL) {
intNm = 1;
}
//Aiming for forms with matching departments & room numbers by calling FormDataFacade
//method, getSpecialPermissionFormByDeptAndRoomNm.
SpecialPermissionForms[] orgaForm = iFacade.getSpecialPermissionFormByDeptAndRoomNm(theDept, intNm);
//Json String Representation...
if (orgaForm != null)
{
Gson neoArcadia = new Gson();
String result = neoArcadia.toJson(orgaForm);
//Json String added to response message body...
ResponseBuilder rb = Response.ok(result, MediaType.TEXT_PLAIN);
rb.status(200); //HTTP Status code has been set!
return rb.build(); //Creating & Returning Response.
}
else
{ //In case of finding no form data for the procedure...
return Response.status(700).build();
}
}
FormDataServices.java code snippet:
public SpecialPermissionForms[] getSpecialPermissionFormByDeptAndRoomNm(String theDept, int theNm) throws SQLException, ClassNotFoundException
{
Connection con = zeon.getConnection();
PreparedStatement pstmt = con.prepareStatement("SELECT formID, studentName, courseDept, courseNm, semester, year, reasonCode FROM spforms WHERE courseDept = ? & courseNm = ?");
pstmt.setString(1, theDept);
pstmt.setInt(2, theNm);
ResultSet rs = pstmt.executeQuery();
SpecialPermissionForms[] neoForm = new SpecialPermissionForms[50];
int current = 0;
while (rs.next())
{
int formID3 = rs.getInt("formID");
String studentName3 = rs.getString("studentName");
String courseDept3 = rs.getString("courseDept");
String courseNm3 = rs.getString("courseNm");
String semester3 = rs.getString("semester");
int year3 = rs.getInt("year");
String reasonCode3 = rs.getString("reasonCode");
SpecialPermissionForms neo = new SpecialPermissionForms(formID3, studentName3, courseDept3, courseNm3, semester3, year3, reasonCode3);
neoForm[current] = neo;
current++;
}
if (current > 0)
{
neoForm = Arrays.copyOf(neoForm, current);
return neoForm;
}
else
{
return null;
}
}
Forms.html code snippet which both pieces of java code respond to:
$("#btnOneName").click(function() {
alert("clicked");
var inputId1=document.getElementById("t_specialFormCourseDept").value;
var inputId2=document.getElementById("t_specialFormCourseNm").value;
var theData = "courseDept=" + inputId1 + "&" + "courseNm=" + inputId2;
alert("Sending: " + theData);
var theUrl = "http://localhost:8080/onlineforms/services/enrollment/specialPermissions/sp?courseDept=&courseNm="+ theData;
$.ajax( {
url: theUrl,
type: "GET",
dataType: "text",
success: function(result) {
alert("success");
var neoForm = JSON.parse(result);
alert(neoForm);
var output="<h3>Current Form Lists:</h3>";
output += "<ul>";
for (var current = 0; current < neoForm.length; current++)
{
output += "<li>" + neoForm[current].formID + ": " + neoForm[current].studentName + " (" + neoForm[current].courseDept + " - " + neoForm[current].courseNm + ") " +
" (" + neoForm[current].semester + " - " + neoForm[current].year + ") " + " - " + neoForm[current].reasonCode + "</li>";
}
output += "</ul>";
alert(output);
$("#p_retrieveOneName").html(output);
},
error:function(xhr) {
alert("error");
$("#p_retrieveOneName").html("Error:"+xhr.status+" "+xhr.statusText);}
} );
});
});
Now, when I go test this code in my webservice after successfully compiling it, it does work, however it retrieves everything, including the specific results I was searching for - I only want to return the results I have specifically searched for and nothing else. What exactly am I doing wrong here in these snippets of code?
Any suggestions or steps in the right direction are highly welcome.

Categories

Resources