Play! Framework save list of rows from form - java

I display a list of db table rows which are editable. I would like to allow users to edit the data in the displayed table and save all updates at the same time. How should I get back the list of updates to the controller?

As Play can bind to POJO's and also bind to Lists, it should be possible to simply create a list of POJOs.
See here for more details http://www.playframework.org/documentation/1.2.2/controllers#array.
It does not give this as an explicit example, but it should work.
If it does not, then the other options you have is to create your own custom binder (also described on the page linked to above).

Related

How hide alfresco data list?

Suppose a data list is created for holding infomation for one workflow. Is is needed to hide this data list - e.g. prevent it from addinig, creating data to it, and more over creating new data list type from UI. Is there a way to do this?
The easiest way to prevent users from adding, removing and editing data would be to set custom permissions on the datalist items using a java behaviour.

How to get the value from Jtable

Currently im doing on a project on ordering system. Let me explain the flow of the system. In ordering class, i have 2 jtable. One of the jtables shows the list of food retrieve from the database. I just have to click on one of the food and press"add", the selected food will transfer to another jtable . After i press"proceed" i will be led to a jtable, to the next jpanel, which consists of the food i have selected. However" i allow one cell to be editable,"quantity" is there any way on how to retrieve the data input by the user and store to database?
Depending on the implementation of the TableModel, you could get the data for an individual row. This would, generally, be the preferred solution, as the data for the row would be encapsulated within a POJO which maintains all the data that you need in a single place that easily manageable.
If you've just dumped a bunch of unassociated properties into the table, you will need to use something like JTable#getValueAt or TableModel#getValueAt
See How to Use Tables for more details

Avoiding DB hits

In my java application, I have a dropdown box which needs to be populated with values from a table games.
There is a separate functionality to add a game. (i:e. an insert in the table games)
The values of the dropdown wont be changing unless someone adds something in the database.
To develop the functionality to populate the dropdown box,
One way is to -
Hit the table everytime a page load happens and fetch the data to populate the dropdown.
Is this an effective way? Can someone suggest me a better alternative design?
You could use a cache... everytime a game is added you simply update your cache.
Implementing a cache is simple.
If inserting data and retrieving it for the dropdown are performed in the same java application, then we might implement a kind of cache for the list of values.
So, we need a structure to store last list of games fetched from database, for example List<Game>, and a flag that is essentially an indicator for the event "someone added row to database".
Each time the page with dropdown is requested, we first check the indicator - if it is true, then we need to reload the list from database and set it back to false, if it is false - we can return the cached list.
Each time user adds entry to database, we set this indicator to true.
Also please be advised of possible concurrency problems when implementing that kind of cache.

How would i go about to attache a MongoDb Collection to a Java View

How would i go about to attache a collection of MongoDb logging Document to a JTable or JList or even a JTextPane. The state of all Document in the Collection must be shown in real time sorted by insertion Date. When a new Documents is inserted the View must reflect that.
This can of course be done by writing the code. What i look for is some existing technics for this
The java display should be easy. Just implement a Table model for your JTable, that displays the field from the DBObject returned by MongoDB. Then just trigger a redraw, when new data is available.
Why would you want to use a Text-Pane?

How to retrieve "${selectedElement}" from a JTable where the elements are bound to a List

In my application I have a JTable and a List.
The List:
The list is populated using a JPA query. The user can re-execute the query by changing attributes in the GUI. Let's assume the query has a named parameter "year" and the user can change this. Then the following happens (simplified by leaving out exception handling):
myList.clear()
mylist.addAll( myQuery.setParameter("year", 2010) )
As the list changes, the binding fires the required handlers and the table now reflects the new dataset.
The JTable:
The contents of the JTable come from a BeansBinding (more precisely a JTableBinding). The binding source is the aforementioned list.
The query is only executed for intensive tasks. Like applying a rough filter on a huge dataset. The earlier example with the year is a good example. This will always return a manageable chunk of data to the client. Now, to have a more responsive user experience, more fine-grained filters happen in the JTable itself. This avoids unnecessary round-trips to the database.
Next, assume the following scenario: A user selects a row in the table and hit's the delete button. If the table has not been filtered the required code is straightforward (again, no error-checking, concurrency locking, and exception handlers for code simplicity):
MyObject = myList.get( myTable.getSelectedRow() );
myEntityManager.getTransaction().begin()
myEntityManager.remove( myObject )
myEntityManager.getTransaction().commit()
However: If the table is filtered on the client side, the table won't reflect the data inside the List. So getSelectedRow() will not return an index which will map to the same entry in the List (I have not tested this, but I beleive I am correct with this assumption?)
So... My question:
How best to solve this?
A possible solution?
I've solved the problem at hand with the following:
I created a new Bean called selectedTableElement which contains a member holding the element which is currently selected in the table.
Next, I created a new binding (source: my table, target: my "selectedElement" bean) using
binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, myTable, ELProperty.create("${selectedElement}"), selectedTableElement, BeanProperty.create("selectedElement"), "selectedElementBinding");
This solution effectively solves the problem by keeping track of the selected element of the table using Beans Binding.
But is this really necessary? It feels clunky to me. A whole new class only the encapsulate the selected element? Is there no other, more direct way of retrieving the "${selectedElement}" property of the JTable?
I'll try to answer to both your questions.
Regarding the first question (the filtered selected index vs the real list index):
I assume that you bound the table using BeansBinding, via createJTableBinding. So, the client-side filtering might be applied through the use of swing TableRowSorter and RowFilter. Am I right ? if so, you could use the method
int row = myTable.convertRowIndexToModel(myTable.getSelectedRow());
to convert the selected row in the filtered view to the actual selected row in the model.
For the second question (bean to keep the selected item of table)
You could also create a binding using this as the source/target object, and create a property selectedElement in the class containing the table. Thus, you won't need another class. The code will be : createAutoBinding(UpdateStrategy.READ_WRITE, myTable,
ELProperty.create("${selectedElement}"), this, BeanProperty.create("selectedElement"), "selectedElementBinding");
(note also that READ_WRITE binding is not really used, as beans binding does not support changing the selected element from the bound property)

Categories

Resources