Best Practice Updating DB Records - java

Say you retrieve 100 records, and display them on a page. The user only updates 2 of the records on the page. Now you want to update only the two records, and not the other 98.
Is it best to have one submit on the page, then somehow know which 2 are updated, then send only those two to the db for an update?
What does the "somehow" look like?
Or, would you have an update-submit button for each row, and have it only update the record its tied to?

Of course there are different ways you could do this. In general, you can save yourself some trouble and server-side processing by using Javascript to assemble your POST data for only the records that have changed. Two thoughts on how this might work:
1) Go the ajax route and do live-editing. So records are presented in a table and appear to be non-editable. When a user clicks a particular row, that row becomes editable by using Javascript to create the appropriate html form on the fly. Then have either a submit button or some other handler (say, moving focus to another table row) which will trigger the POST which updates the DB (asynchronously via your preferred ajax method). Happily the mainstream Javascript frameworks can help a lot in this area.
2) Checkboxes - whenever a row is edited, its checkbox becomes checked. When the submit button is clicked, use javascript to post the POST data by grabbing everything in row whose checkbox is checked. A user can un-check a box to cancel changes to that row before submitting.

Ajax it using jQuery or some other JavaScript library and put and update button on each row.

There are many answers to this question and to some extent they depend upon your development tools and the "feel" of the site.
If you were implementing Ajax calls to do the updates on a line by line basis then this would logically seem right to have a button per line and then update it with an Ajax call when a line was changed.
This is also just the scenario that disconnected data sets were designed to solve and ADO.net handles these very well.
So as ever, the answer is "It Depends!"

You can use JavaScript to mark each field as changed when a user changes an input field. Create a hidden fields that has the id of the row you are updating, and dirty flag. (like is_dirty_$id) In JavaScript, create an onChange handler that sets the hidden field as dirty. when any input is changed.
Alternatively, you can create hidden fields for each real field you display. the hidden field would contain the initial values. check each field on the server side to determine what has changed.
You probably want to store a last_modified date as a hidden field for each record. This way if another user updates the same record, you can display an error message saying "this record has been updated by another user" or similar.

One submit button. I could foresee case I might use more then one, but in the general case just one. (Note, this looks like a web page question to me, so I'm answering with that assumption.)
There are 3 ways that come to mind which you could handle the tracking changes:
JavaScript: Put a onChange() function on the controls that update a hidden field. If that hidden has a value, then update the associated record. Requires JS on the browser, and doesn't tell you which fields to update, just which records.
Lots of form fields: Put a hidden field out with each control and compare them all when they come back. This would be ugly, but it would allow you to know which fields to update (not just the record). It would also allow you to know if someone undid a change that started.
Sessions: You could place the original values in session variables, then do the comparison when the values come back. This would be a little more elegant then lots of hidden fields, and less open to people playing with the posted back data (since you should never trust anything that comes back, even in hidden fields). Requires cookies on the browser and sessions on the server technology.

Related

Defining and editing variables inside template

In my play app I'm trying to offer the user the oppotunity to edit a specific item he selects from a table by clicking on a button in the same table row as the item.
To be more precise the user will edit the students that have enrolled for an exam. After clicking on the exam he wants to edit a modal will open. Inside the modal a table will show all available students with a checked checkbox for students which are already enrolled.
My only problem is: How do I pass the modal the id of the exam the user wants to edit? As far as I've researched I cant't define and edit variables inside of the template. In Angular I would just modify a variable in the scope.
What is the correct way to do this?
The general principle of moving initialization data from the back-end to the front-end is to inject it into the markup someplace, using the templating engine. In your case, in whatever loop you're using in your template to render the table rows, you simply drop the ID in it's own cell in the table, in a class or id attribute, or maybe most appropriately, in a data- attribute on the table row. I would say each of these options is pretty much equivalent, mostly differing aesthetically.
Then, in your click handler, you just use a relative selector on the event target to pull the ID into your script, wherever you stashed it.
The other approach is to render your table on the front-end, pulling this information from the back-end via API, in the style of a single-page app. Then, that association between ID and data lives in the model before its even rendered. This may be overkill for this one example, but if you've got rich interactivity with a dozen of these sorts of screens, I think this is a much more scalable approach.

JQuery datatable refreshing after Serverside processing

I have one table having number of columns in which some columns are dependent on the other.
How can I refresh the jQuery datatable after editing one column in the table so that values at other columns are also updated after server side processing.
I haven't tried this approach, but I've seen that everything in datatables is done through custom functions. For example, in the buttons section, you can add a custom button with
,{
"sExtends": "text",
"sButtonText": "Refressh Serverside",
"fnClick": function ( nButton, oConfig, oFlash ) {
MyRefreshFn();
}
}
where MyRefreshFn(); can be anything you want. You can define it outside the datatables declaration and use, for example, jQuery.ajax to detect when the server answers, then trigger a common Datatables method. In your case, you might want to trigger the whole row rendering queue, to make the changed value affect every other dependant cell.
You can do one thing, after getting response refresh page. If you are using some plugin find his documentation you will get method related to refresh.
OR send request to same page and display it. It gives you fresh result.
$.post("page_url",{parameter},function(data, textStatus,req){
$("div_id").html(req.responseText);
});

Pagination view in web pages

I have a query. I am using JSP with tomcat for web development. I have a set of values which have been retrieved from database as a list. Now, I want to show the values one by one on a page with a next button. How this can be done? Do we make multiple pages or dynamically refresh the page with new value on pressing next button? Could some one please guide me on this?
if I'm not wrong, JSP does not provide any build in pagination control. you can use pagination through javascript, or create you own control for pagination. here is a link which might will you in pagination with javascript.
Tell me one thing, you are going to pick the records from database. So this is all dynamic data. So approach 1 is a big NO NO.
You should be dynamically refreshing, the page with Next button pointing to the next record to display. But don't you think having a single record to display in a page, can make a UI experience bad.
UI should be simple yet cover most information, so rather go for the standard solution with list of records in a table like format with action link to view details for each record which will point me to the desired record detailed page. In actual this detailed page will just be one, but displaying dynamic data.
You can have a look at Datatable in JQuery for pagination ofcourse.

How to tackle double submit form?

How to handle a situation where user might accidentally click submit button more than one time? If it happens then two rows will be generated in table. How to avoid it.
I am working in JSP.
Also want to avoid back button and refresh page which cause to create a problem of double entry in database.
I'd suggest never to rely on anything client-side too hard. Using very accessible tools you can pretty much change any behavior that is client-side enforced. If your application crashes/has undefined behavior after something client-side fails, you should change your application.
One thing you could do is to make a class that takes all the fields resulting from this POST, override it's equals() method to honor all the fields, hold the request at the user level and compare all other incoming requests to the list of requests you already have for that user.
You can then remove the request objects as soon as they have been successfully processed.
If you, on the other hand, somehow get the same request again by the user (by looking through the list of 'open' requests whenever the user POSTs a request), you can just ignore it.
Several way:
You can use javascript to forbid two cliks in the submit button, but it does not work if the user use the "back" button of the navigator between the two clicks.
On the server side, you can send the form with an unique id kept in the user session (could also be used for CSRF counter-measure). Then when submitting the form you can compare then remove the id in the user session. Be carreful for concurrent access to the session (each request has its own thread).
Detect in the database, but it depends of the data and the constraints on it. One extension of the point 2 would be to keep the generated ID of the form in the database and removing it during the first access. This method has the advantage to user the transaction support in case of concurrent access.
If the form update an specific entry in the database (row id is sent with the form), the problem could be considered as solved by itself, as it update twice the same row with the same data.
First, on the client side, disable the button as soon as the user once clicks on it.
Second, on the server side, Some information in that form for will be a primary key in your database table. So when the same information is sent again to the server while inserting the data, you will get an exception since there cannot be duplicate values of primary key.
First solution . Disable sumbit button .. second check some unique key if possible ...
If you want to make 100% certain that the submit button cannot be clicked twice, you could disable it in the javascript within the onclick= method of the button.
For instance, in the javascript (jquery) below, if the button id was 'send'...
$('#send').click(function(e) {
this.disabled = true;
businessLogicCall();
});
Have a wait time for which you don't accept any more clicks on the button, for example disabling the submit button.
Disable the submit button until user changes the input information, thus showing he wants to submit another portion of information.

Dynamic pagination of rich:dataTable/rich:datascroller with large datasets

I currently have a Richfaces dataTable bound to a backing bean that contains all the data. This table is bound to a Richfaces data scroller.
Users can then enter scroll between the data.
There is also a text box that dynamically updates the data table results displayed to them, based on text they enter into it. It reRenders the dataTable and datascroller on the keyUp event.
The backing bean it’s bound to first pulls all the data from a number of database tables. This data is pulled when the user submits a normal post request to the server, and it’s the results of this request that are used for all subsequent Ajax related queries (Results saved as list of objects, no more database calls made).
I have a problem in that the dataset can be huge at times, 100,000s of records.
This causes the initial request to the server to take a very long time.
A possible solution I'm looking at is pulling back only a small amount of the data in one thread for the initial user port request. This data can then be displayed in the data table while the main thread works in the background pulling the bulk of data back.
Is this feasible? Would it be possible to update my datatable/datascoller as the main thread pulls back new data? Would it be difficult?
Database and query optimization has been looked at, so no more improvements of any significance can be made there.
Thanks in advance (I know this probably is not an easy question to answer)
Implement SerializableDataModel to hold your data for sorting and paginating.
Seems like you need exactly what Seam Application Framework's Query object is providing.
If you don't want to use Seam, you can either view the source, and copy how they are doing it and just steal the idea.
Basically what you need to do is fetch a given result set for each time the user press next, previous, first, last etc

Categories