How to check which of the Object parameters got changed - java

I have a situation where i am changing few parameters values of an Object.
UserDetails has around 14 parameters.I am changing the values of few parameters and submitting them from a Form .These values should get updated on the database back-end.
Are there any inbuilt functions to check if any of the values got changed?

Are there any inbuilt functions to say which of the values got changed?
No.
Are there any inbuilt functions to check if any of the values got changed?
No.
However, you can implement your own methods to test these things. An equals method is easy to implement, and indeed many IDEs have "wizards" to generate them. A "what has changed" method is more complicated. The complexity comes in how the method tells the caller what fields have changed, and how the caller can make use of this information.
Alternatively, Apache Commons provides a class called EqualsBuilder that uses reflection, etcetera to compare objects based on their fields.
I also agree with JB Nizet. If you are doing this in an attempt to optimize database updates, you are probably wasting your time. You are probably better off just saving the all of the fields.
Consider this. Unless your front-end caches the old values of the fields read from the database while the user is updating the form (or not), your front end is going to have to re-query the database to find the old value. You would be better off just issuing the UPDATE to update all of the fields than doing a SELECT followed by a conditional UPDATE is something has changed.

Probably you can check this link.. I am not sure this can be done in java. But, you can try with javascript. Please check this link. You can do with EXT.js
handler: function(btn, evt) {
var f = btn.up('form').getForm();
f.submit({
url: '/some-path-on-my-server/save/,
getParams: function(useModelValues) {
var falseVal = false;
var fieldParams = this.form.getValues(falseVal, true, this.submitEmptyText !== falseVal, useModelValues, true);
return Ext.apply({}, fieldParams);
}
});
}
https://www.sencha.com/forum/showthread.php?173867-I-want-to-submit-only-dirty-field-values.

Related

Pact JVM check for a response that is either an array or null

I am trying to create a contract between with a consumer when making a request for an item information. The item has a very complex data structure with nested properties and contains fields that ranges from strings, lists, arrays of strings, arrays of enums etc. There is also a complexity added where some of the fields may be returned as a null or populated with a value respective to its type.
What I would like to do is to create a pact response matcher that can be re-used for any item. At the moment, there are 120 items (each item is a separate API call with the item name in the query param of the request) that I could request information for and writing a pact matcher for each item is not feasible / maintainable.
E.g. Sample response cut down ( the actual response is over 60 lines):
return new PactDslJsonBody()
.object("metadata")
.stringValue("name", "item-1")
.integerType("index", 1)
.eachLike("contents")
.stringType("param", "content-param")
.stringType("value", "content-value")
.closeArray
.closeObject
The problem I have is that the field contents could also come back as null from the provider. What I am trying to achieve is whether I can within the same PactDslJsonBody have an OR condition or something similar which can check for the eachLike but also can accept that the field is null.
Contents field populated:
{
"metadata":{
"name":"item-1",
"index":1,
"contents":[
{
"param":"content-param",
"value":"content-value"
}
]
}
}
Contents field null:
{
"metadata":{
"name":"item-1",
"index":1,
"contents":null
}
}
I see there is PactDslJsonBody.or() available but the usage of this is not documented clearly, hence I am unsure if this is the intended usage for the above use case.
Writing separate tests is not feasible as this is a specific use case to repeat the same PactDslJsonBody for n number of consumer tests, one for each item. We need to test each item as we have a direct mapping of some field values as enums between the consumer and provider hence we want to make sure that each item has the expected values for such fields. We also dont know in advance which items will this field as null and which ones have it populated, hence why we wanted to have the option to use something like orNull etc.
I have looked quite a lot in the web and couldn't find a definitive answer to the above. Any help on this would be appreciated on how to move forward with this. We really want to use Pact since it is the go to tool for us and the above is a crucial use case for us to test.
I am using Pact JVM version 4.3.15 and using V3 specification.
Thanks.
See https://docs.pact.io/faq#why-is-there-no-support-for-specifying-optional-attributes. You need to write two separate test cases that cover both scenarios.

Improve Performance

In my Java project there are so many classes and every class requires a common value to display the data like (NT_Login, Country, Location and Role) so for every class I am using the JDBC to get the value from AdminTable(SQL) like NT_Login, Country etc and then it displaying me the result. Am I doing correct ? Or at the starting of the project should I Call JDBC to get the values and then should I pass all the values as a parameter to the other class? I don't know the best possible way please suggest me even if there are other thing which I can try but It should be the standard method as per the Software Development.
//This is what I am doing for all the class
ClassFindAdmin admin = new ClassFindAdmin();
AdminBean bean = admin.getUserDetails(userName);
String country = bean.getCountry();`enter code here`
String location = bean.getLocation();
String role = bean.getRole();
String name = bean.getUser_Name();
If these values are same across a session, then it is better to get the values once and set in session. That way you can avoid to go to the DB all the time.
In my opinion, may be calling all the references from the database, at the initial point of execution is a great idea, which is pointed out by you. In this way, we do not need to look upon the jdbc, frequently. However, this approach may delay the execution of the project. Not sure, what should be the best approach.

Passing arguments to java function in bulk

What would you use if you wanted to pass a list of options into a function?
For example, if you have an interface to a server:
public interface Server {
public void authUser(String username, String password, <xyz> options);
}
What structure would use use for to pass a set of options? Something like a HashMap?
The reason I'm saying that it comes from tunnel vision is because I feel that this goes against Java standards. Java has method overloading. So if I get flames for raising the question I understand. But overall, maybe in different cases, would you ever pass bulk data in some collection and, if yes, which one?
Option1 : If you are choosing any collections like List or Set these are specific to an object . I mean,
Lets Assume, Set sets = new HashSet();
If I want 5 Object of different different class having no relationship to be send, then It would be very difficult to recognize that which Object is belong to which class while Iteration. So, I wont recommend Collections.
Option2 : If you are choosing Map, the same above problem may occurs while getting the Object Dynamically. So, This Options is also not recommended.
Option3 :
Why cann't you create your own DTO and in that DTO place your reqyired datastructure and pass it over.
If you want 5 different Object to be pass then, you can pass. If all are of same type then you may use Collection or array or Variable Arguement based on your scenerio.
I think anything Serializable is exactly the thing. If you can serialize the object, then you can pass (store, transmit...) it, passing it's properties in bulk. What format of serialized data to choose, is another question.
It depends on the data you want to pass.
You can use a map(hashmap) if you are passing key-value pairs.
If it is just a list of diffrent object, you can use List(ArrayList)
Other option is to create DTO(data transfer object) with getter and setter methods.
You may want to take a look at VARARGS feature that was introduced in JAVA5.
I'd suggest a Map [HashMap] as you can then access the argument values via their Keys.

How to make CachedRowSetImpl readonly

I am going to execute a query and return result using com.sun.rowset.CachedRowSetImpl to the users (which are not in my control). I want to make sure that they will not perform any update operation on this object which can make changes in database.
One way could be:
Extend CachedRowSetImpl and override setReadOnly() method so that no user can set it to false.
Is this sufficient? Or there are any other ways using which users can still update database? Should I also override clone() method?
Thanks
I don't see a reason to override clone(), but the simplest way to make sure no updates can happen seems to be to use ResultSet.CONCUR_READ_ONLY.
As you see from the Javadoc, all the mutators throw an SQLException in that case.
So to create a read-only cached row set, the following should suffice:
CachedRowSet rowSetImpl = new CachedRowSetImpl();
rowSetImpl.setConcurrency(ResultSet.CONCUR_READ_ONLY);
rowSetImpl.setCommand("Select * from foo");
rowSetImpl.execute();
classes under com.sun are for internal use they provide reference implementation of APIs and you are not supposed to need access them directly.
It's better to avoid coupling your code to classes present in com.sun, and rather code against javax. or java packages
Moreover, classes in com.sunpackages can be changed or dropped in any version of Java.

Infinispan MVCC with "put"

I wanted to use Infinispan today in a sort of probably unfamiliar matter.
I want to save a Variable, let's call it x a couple of times in the cache - while being able to adress it as X.
Plain, old MVCC. However, it seems that infinispan uses MVCC on the backend - but I wasn't able to use it in my little test-application.
This is the corresponding code:
acTest.put("test", "blubber", 0, TimeUnit.MILLISECONDS );
acTest.put("test", "nothing", 0, TimeUnit.MILLISECONDS );
if( acTest.containsKey("test") )
{
Object foo = acTest.get("test"); // don't know how to get the "blubber" out of that
String name = (String) test2.get("name");
System.out.println(name);
}
Sure enough, acTest contains the key - but I was not able to adress the value "blubber" of that key - when I higher the numerical value of "nothing" foo holds "nothing" ... but I want to get the first version of "foo" - hence "blubber"
I want to be able to adress the different versions of test. I think that I can create different versions of "test" with the different parameters in the put operation - however eclipse has absolutely no documentation for that matter ...
Could somebody help me?
Infinispan uses MVCC in it's container for internal purposes, this is currently not a feature exposed via user API, besides via writeSkewCheck.
In version 5.1 the API will expose Optimistic locking, which might be useful for some use cases needing to take advantage of the MVCC capabilities, but you still won't be able to extract a previous value.
You could use AtomicMap to store multiple values, or use custom key objects containing the version, building what you need on top of Infinispan's API.
DeltaAware is another option, but it's a low-level interface meant for experts.

Categories