I am using spring 3.0 and I am using the JqGrid plugin. I am working on the search feature which sends a json string with all the search criteria. Here is what the string can look like.
{"groupOp":"AND","rules":[{"field":"firstName","op":"bw","data":"John"},{"field":"lastName","op":"cn","data":"Doe"},{"field":"gender","op":"eq","data":"Male"}]}
If you look at the "op" property inside the rules array, you will see the operation which must be executed. The Jq-grid has the following operations
['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
which corresponds with
['equal','not equal', 'less', 'less or equal','greater','greater or equal', 'begins with','does not begin with','is in','is not in','ends with','does not end with','contains','does not contain']
I plan to use hibernate criteria searching to enable the search feature. For this I am using Jackson's ObjectMapper to convert the incoming JSON into Java. This is all well and good. Here is my code that converts the json.
public class JsonJqgridSearchModel {
public String groupOp;
public ArrayList<JqgridSearchCriteria> rules;
}
public class JqgridSearchCriteria {
public String field;
public String op;
public String data;
public SimpleExpression getRestriction(){
if(op.equals("cn")){
return Restrictions.like(field, data);
}else if(op.equals("eq")){
return Restrictions.eq(field, data);
}else if(op.equals("ne")){
return Restrictions.ne(field, data);
}else if(op.equals("lt")){
return Restrictions.lt(field, data);
}else if(op.equals("le")){
return Restrictions.le(field, data);
}else if(op.equals("gt")){
return Restrictions.gt(field, data);
}else if(op.equals("ge")){
return Restrictions.ge(field, data);
}else{
return null;
}
}
}
#RequestMapping(value = "studentjsondata", method = RequestMethod.GET)
public #ResponseBody String studentjsondata(#RequestParam("_search") Boolean search ,HttpServletRequest httpServletRequest) {
StringBuilder sb = new StringBuilder();
Format formatter = new SimpleDateFormat("MMMM dd, yyyy");
if(search){
ObjectMapper mapper = new ObjectMapper();
try {
JsonJqgridSearchModel searchModel= mapper.readValue(httpServletRequest.getParameter("filters"), JsonJqgridSearchModel.class);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(Person.class);
Iterator<JqgridSearchCriteria> iterator = searchModel.rules.iterator();
while(iterator.hasNext()){
System.out.println("before");
criteria.add(iterator.next().getRestriction());
System.out.println("after");
}
} catch (JsonParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{//do other stuff here}
This is where the problem comes in. How do I transalate the jqGrid operation into the equivelent hibernate command ? For example
"cn" should correspond with
criteria.add(Restrictions.like("firstName", myJsonJqgridSearchModel.data));
Interestingly, I've just written almost identical code to what you have above (mine doesn't use JqGrid however). I'm wondering if your problem is specifically related to the "cn" - LIKE condition? I had problems with this - I had to specify the MatchMode to get the "contains" like I wanted:
return Restrictions.ilike(
searchCriterion.getPropertyName(),
searchCriterion.getValue().toString(),
MatchMode.ANYWHERE);
I found that without specifying the MatchMode, it was generating SQL as:
WHERE property LIKE 'value'
By specifying the MatchMode.ANYWHERE, it generated SQL as:
WHERE property LIKE '%value%'
which is the "contains" operation that I was expecting. Perhaps this is your issue as well?
Related
I have the following database class which uses ADO.NET to connect to a SQL database without any models. I use the GetData method to return a serialized json string which I then convert in the ActionResult get request in my controller a shown below. The issue is I am first returning a DataTable object from the ADO.NET query which is then serialized to a string using a third party nuget package Newtonsoft which is converted to a json/application type in my controller. Is there no way to achieve these results with less steps or third party plugins?
When using a model approach with the DB such as Entity Framework I was able to use Ok() method to convert to JSON format for my EF query. This was so simple but I hated the idea of keeping models that I will never use so I switched to ADO.NET. I am still learning both ADO.NET and ASP.NET Core so any help is appreciated.
Database class
public String GetData(String connectionString, string str, params IDataParameter[] sqlParams)
{
int rows = -1;
DataTable objResult = new DataTable();
try
{
SqlDataReader myReader;
using (SqlConnection myCon = new SqlConnection(connectionString))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand(str, myCon))
{
if (sqlParams != null)
{
foreach (IDataParameter para in sqlParams)
{
myCommand.Parameters.Add(para);
}
}
myReader = myCommand.ExecuteReader();
objResult.Load(myReader);
myReader.Close();
myCon.Close();
}
}
}
catch (Exception ex)
{
}
return JsonConvert.SerializeObject(objResult);
}
Controller:
public ActionResult DatatableLoad()
{
//Since ADO.NET returns a datatable we serialize it first in GetData()
//We have parameters here just in case we want to use them
string query = "select * from dbo.UserAccountsTbl WHERE USERNAME=#UserName;";
var parameters = new IDataParameter[]
{
new SqlParameter("#UserName", "SB")
};
String jsonResult = db.GetData(connectionString, query, parameters);
return Content(jsonResult, "application/json");
}
To return the output of a query in JSON format you need to do this:
return Json(jsonResult, JsonRequestBehavior.AllowGet);
jsonResult is the output list of the Executed Query.
In my code, I am returning a path in String form(/successPassword or /systemError). Basically this tells Spring to go to the path /successPassword or /systemError, /successPassword goes to successPassword.html and /systemError goes to systemError.html. These html files are located in src/main/resources/templates.
#RequestMapping(value = "/user/new_password", method = RequestMethod.POST)
public String createNewPassword(#RequestParam(value = "newpassword", required = false) String password,
#RequestParam(value = "hash") String hash, Model model) {
LOG.info("set new password using hash " + hash);
LOG.info("new password " + password);
boolean hashValid = passwordService.isHashValid(hash);
if (hashValid) {
ValidateMessage message = passwordService.validateNewPassword(password);
if (!message.isError()) {
Account account = passwordService.getAccount(hash);
passwordResetService.saveNewPassword(password, account);
model.addAttribute("account", account);
return "/successPassword";
} else {
LOG.info("Password not complex enough.");
return "/systemError";
}
} else {
LOG.info("Invalid hash.");
return "/systemError";
}
}
Now it looks like I need to return a json object instead. I know that I have to annotate the method with #ResponseBody instead, but my question is how would i package now the object that I will return so that whoever receives it will know to which path it needs to go to? My html codes are using Thymeleaf (no javascript whatsoever).
I am using deeplinking in my app and Im looking to preset some parameters when navigating to the viewmodel using a IMvxNavigationFacade. The deep link url is like this:
myappname://deeplink/toviewwithdata/?navigatetoview=viewtype1&id=78910
So the deep linking is working and im getting to the navigation facade using the assembly attribute
[assembly: MvxNavigation(typeof(RoutingFacade), #"myappname://deeplink/toviewwithdata/\?navigatetoview=(?<viewtype>viewtype1)&id=(?<id>\d{5})")]
I tried to add other parameters to the MvxViewModelRequest using a MvxBundle but dont think im doing it right. here is my navigation facade:
public class RoutingFacade : IMvxNavigationFacade
{
public Task<MvxViewModelRequest> BuildViewModelRequest(string url, IDictionary<string, string> currentParameters)
{
var viewModelType = typeof(FirstViewModel);
var parameters = new MvxBundle();
try
{
// TODO: Update this to handle different view types and add error handling
if (currentParameters != null)
{
Debug.WriteLine($"RoutingFacade - {currentParameters["viewtype"]}, {currentParameters["id"]}");
switch (currentParameters["viewtype"])
{
case "viewtype1":
viewModelType = typeof(FirstViewModel);
parameters.Data.Add("test", "somevalue");
break;
default:
case "viewtype2":
viewModelType = typeof(FirstViewModel);
break;
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"RoutingFacade - Exception: {ex.Message}");
//TODO viewModelType = typeof(ErrorViewModel);
}
return Task.FromResult(new MvxViewModelRequest(viewModelType, parameters, null));
}
then my viewmodel Init method
public void Init(string id, string viewtype, string test)
{
// Do stuff with parameters
}
but the test parameter is null? How do you pass parameters into a MvxViewModelRequest?
Update:
Don’t know if its possible from looking at the source here https://github.com/MvvmCross/MvvmCross/blob/f4b2a7241054ac288a391c4c7b7a7342852e1e19/MvvmCross/Core/Core/Navigation/MvxNavigationService.cs#L122 as the request parameters get set from the regex of the deeplink url and the return from BuildViewModelRequest, facadeRequest.parameterValues get ignored.
Added this functionality in this pull request
I would like to be able to modify the filter inside the controller and then return the data based on the altered filter.
So for I have an ODataQueryOptions parameter on the server side that I can use to look at the FilterQueryOption.
Let's assume the filter is something like this "$filter=ID eq -1" but on the server side if I see "-1" for an ID this tells me that the user wants to select all records.
I tried to change the "$filter=ID eq -1" to "$filter=ID ne -1" which would give me all by setting the Filter.RawValue but this is read only.
I tried to create a new FilterQueryOption but this requires a ODataQueryContext and a ODataQueryOptionParser which I can't figure out how to create.
I then tried to set the Filter = Null and then us the ApplyTo which seems to work when I set a break point in the controller and check this on the immediate window but once it leaves the GET method on the controller then it "reverts" back to what was passed in the URL.
This article talks about doing something very similar "The best way to modify a WebAPI OData QueryOptions.Filter" but once it leaves the controller GET method then it reverts back to the URL query filter.
UPDATE WITH SAMPLE CODE
[EnableQuery]
[HttpGet]
public IQueryable<Product> GetProducts(ODataQueryOptions<Product> queryOptions)
{
if (queryOptions.Filter != null)
{
var url = queryOptions.Request.RequestUri.AbsoluteUri;
string filter = queryOptions.Filter.RawValue;
url = url.Replace("$filter=ID%20eq%201", "$filter=ID%20eq%202");
var req = new HttpRequestMessage(HttpMethod.Get, url);
queryOptions = new ODataQueryOptions<Product>(queryOptions.Context, req);
}
IQueryable query = queryOptions.ApplyTo(db.Products.AsQueryable());
return query as IQueryable<Product>;
}
Running this code will not return any product this is because the original query in the URL wanted product 1 and I swapped the ID filter of product 1 with product 2.
Now if I run SQL Profiler, I can see that it added something like "Select * from Product WHERE ID = 1 AND ID = 2".
BUT if I try the same thing by replacing the $top then it works fine.
[EnableQuery]
[HttpGet]
public IQueryable<Product> GetProducts(ODataQueryOptions<Product> queryOptions)
{
if (queryOptions.Top != null)
{
var url = queryOptions.Request.RequestUri.AbsoluteUri;
string filter = queryOptions.Top.RawValue;
url = url.Replace("$top=2", "$top=1");
var req = new HttpRequestMessage(HttpMethod.Get, url);
queryOptions = new ODataQueryOptions<Product>(queryOptions.Context, req);
}
IQueryable query = queryOptions.ApplyTo(db.Products.AsQueryable());
return query as IQueryable<Product>;
}
END RESULT
With Microsoft's help. Here is the final output that supports filter, count, and paging.
using System.Net.Http;
using System.Web.OData;
using System.Web.OData.Extensions;
using System.Web.OData.Query;
/// <summary>
/// Used to create custom filters, selects, groupings, ordering, etc...
/// </summary>
public class CustomEnableQueryAttribute : EnableQueryAttribute
{
public override IQueryable ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
{
IQueryable result = default(IQueryable);
// get the original request before the alterations
HttpRequestMessage originalRequest = queryOptions.Request;
// get the original URL before the alterations
string url = originalRequest.RequestUri.AbsoluteUri;
// rebuild the URL if it contains a specific filter for "ID = 0" to select all records
if (queryOptions.Filter != null && url.Contains("$filter=ID%20eq%200"))
{
// apply the new filter
url = url.Replace("$filter=ID%20eq%200", "$filter=ID%20ne%200");
// build a new request for the filter
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, url);
// reset the query options with the new request
queryOptions = new ODataQueryOptions(queryOptions.Context, req);
}
// set a top filter if one was not supplied
if (queryOptions.Top == null)
{
// apply the query options with the new top filter
result = queryOptions.ApplyTo(queryable, new ODataQuerySettings { PageSize = 100 });
}
else
{
// apply any pending information that was not previously applied
result = queryOptions.ApplyTo(queryable);
}
// add the NextLink if one exists
if (queryOptions.Request.ODataProperties().NextLink != null)
{
originalRequest.ODataProperties().NextLink = queryOptions.Request.ODataProperties().NextLink;
}
// add the TotalCount if one exists
if (queryOptions.Request.ODataProperties().TotalCount != null)
{
originalRequest.ODataProperties().TotalCount = queryOptions.Request.ODataProperties().TotalCount;
}
// return all results
return result;
}
}
Remove [EnableQuery] attribute, your scenario should work, because after using this attribute, OData/WebApi will apply your original query option after you return data in controller, if you already apply in your controller method, then you shouldn't use that attribute.
But if your query option contains $select, those code are not working because the result's type is not Product, we use a wrapper to represent the result of $select, so I suggest you use try this:
Make a customized EnableQueryAttribute
public class MyEnableQueryAttribute : EnableQueryAttribute
{
public override IQueryable ApplyQuery(IQueryable queryable, ODataQueryOptions queryOptions)
{
if (queryOptions.Filter != null)
{
queryOptions.ApplyTo(queryable);
var url = queryOptions.Request.RequestUri.AbsoluteUri;
url = url.Replace("$filter=Id%20eq%201", "$filter=Id%20eq%202");
var req = new HttpRequestMessage(HttpMethod.Get, url);
queryOptions = new ODataQueryOptions(queryOptions.Context, req);
}
return queryOptions.ApplyTo(queryable);
}
}
Use this attribute in your controller method
[MyEnableQueryAttribute]
public IHttpActionResult Get()
{
return Ok(_products);
}
Hope this can solve your problem, thanks!
Fan.
In response of #Chris Schaller I post my own solution as below:
public class CustomEnableQueryAttribute : EnableQueryAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var url = actionContext.Request.RequestUri.OriginalString;
//change something in original url,
//for example change all A charaters to B charaters,
//consider decoding url using WebUtility.UrlDecode() if necessary
var newUrl = ModifyUrl(url);
actionContext.Request.RequestUri = new Uri(newUrl);
base.OnActionExecuting(actionContext);
}
}
I have written a Hive UDF that does decryption using an in-house API as follows:
public Text evaluate(String customer) {
String result = new String();
if (customer == null) { return null; }
try {
result = com.voltage.data.access.Data.decrypt(customer.toString(), "name");
} catch (Exception e) {
return new Text(e.getMessage());
}
return new Text(result);
}
and Data.decrypt does:
public static String decrypt(String data, String type) throws Exception {
configure();
String FORMAT = new String();
if (type.equals("ccn")) {
FORMAT = "CC";
} else if (type.equals("ssn")) {
FORMAT = "SSN";
} else if (type.equals("name")) {
FORMAT = "AlphaNumeric";
}
return library.FPEAccess(identity, LibraryContext.getFPE_FORMAT_CUSTOM(),String.format("formatName=%s", FORMAT),authMethod, authInfo, data);
}
where configure() creates a pretty expensive context object.
My question is: Does Hive execute this UDF once for every row returned by the query? i.e. If I'm selecting 10,000 rows, does the evaluate method get run 10,000 times?
My gut instinct tells me yes. And if so, then here's a second question:
Is there any way I can do one of the following:
a) run configure() once when the query first starts, then share the context object
b) instead of the UDF returning a decrypted string, it aggregates the encrypted string into some Set, then I do a bulk decrypt on the set?
Thanks in advance
Is configure() something that needs to be called once per JVM, or once per instance of the UDF class?
If once per JVM, just put it in a static block in the class, like so:
static {
configure();
}
If once per instance, put it in the constructor:
public [class name]() {
super();
configure();
}