JavaFX refetch observablelist from database - javafx

I need to show a custom made list of nodes from a database table that is frequently updated from external sources.
When changes are made to the database, the list needs to be updated on the screen accordingly.
For this I have implemented an ObservableList with an onChanged method that should check for changes and rebuild the custom list.
I my case the onChanged method will never fire when new data is added in the database, only when changes are made on the fly to the already defined ObservableList.
How can i let the ObservableList know when new data exists in the database ?
Thanks in advance!

Given your answer to jkaufmann it seems you need to change your logic.
Instead of rebuilding ObservableList each time you need to calculate the difference with new data and update ObservableList with new/removed data.

Related

QSqlQueryModel for dynamically changed information

Trying to build an application with a UI in QML and accessing data from a Sqlite database.
The QML view has a TableView to display information in a data grid with a model in C++.
The model is a class inheriting from QSqlQueryModel with a query to the database: setQuery("SELECT * FROM Samples", GetDataBase())
I am adding a new row in the database table every second in another thread.
When I navigate to the view where the TableView is placed, the grid is populated with the information in the table. All good. But then, every time I add a new row I call setQuery again but the table is not updated. If I go back and forth to the view again it is populated with all the new rows.
I thought the view was "automatically" notified whenever the model changed but I suppose I am missing any kind of notification to let the view know that there is new rows or something??
Also...this is just a testing application but I foresee that in my real app the information to be displayed in the grid will change very fast and the table can have several rows of information (order hundred thousand rows) so I wonder if QSqlQueryModel is the right model for this kind data.

Displaying data in a datagrid in an Alfresco task form

I have a json array variable defined in my activity workflow definition. Now, I need to present the data of the json array in a datagrid inside a form task. One of those columns should be a check entry filed so that the user may select some rows. After that, I need to get back that data in a subsequent form task (so I need the data to be updated in the json variable so I can read it back, mainly the user selected checks).
Any example to accomplish this? I didnt find almos nothing relating with using datagrids in Alfresco task forms.
thank you!
Miguel

LINQ to SQL - updating records

Using asp.net 4 though C#.
In my data access layer I have methods for saving and updating records. Saving is easy enough but the updating is tedious.
I previously used SubSonic which was great as it had active record and knew that if I loaded a record, changed a few entries and then saved it again, it recognised it as an update and didn't try to save a new entry in the DB.
I don't know how to do the same thing in LINQ. As a result my workflow is like this:
Web page grabs 'Record A' from the DB
Some values in it are changed by the user.
'Record A' is passed back to the data access layer
I now need to load Record A again, calling it 'SavedRecord A', update all values in this object with the values from the passed 'Record A' and then update/ save 'SavedRecord A'!
If I just save 'Record A' I end up with a new entry in the DB.
Obviously it would be nicer to just pass Record A and do something like:
RecordA.Update();
I'm presuming there's something I'm missing here but I can't find a straightforward answer on-line.
You can accomplish what you want using the Attach method on the Table instance, and committing via the SubmitChanges() method on the DataContext.
This process may not be as straight-forward as we would like, but you can read David DeWinter's LINQ to SQL: Updating Entities for a more in depth explanation/tutorial.
let's say you have a product class OR DB, then you will have to do this.
DbContext _db = new DbContext();
var _product = ( from p in _db.Products
where p.Id == 1 // suppose you getting the first product
select p).First(); // this will fetch the first record.
_product.ProductName = "New Product";
_db.SaveChanges();
// this is for EF LINQ to Objects
_db.Entry(_product).State = EntityState.Modified;
_db.SaveChanges();
-------------------------------------------------------------------------------------
this is another example using Attach
-------------------------------------------------------------------------------------
public static void Update(IEnumerable<Sample> samples , DataClassesDataContext db)
{
db.Samples.AttachAll(samples);
db.Refresh(RefreshMode.KeepCurrentValues, samples)
db.SubmitChanges();
}
If you attach your entities to the context and then Refresh (with KeepCurrentValues selected), Linq to SQL will get those entities from the server, compare them, and mark updated those that are different
When LINQ-to-SQL updates a record in the database, it needs to know exactly what fields were changed in order to only update those. You basically have three options:
When the updated data is posted back to the web server, load the existing data from the database, assign all properties to the loaded object and call SubmitChanges(). Any properties that are assigned the existing value will not be updated.
Keep track of the unmodified state of the object and use Attach with both the unmodified and modified values.
Initialize a new object with all state required by the optimistic concurrency check (if enabled, which it is by default). Then attach the object and finally update any changed properties after the attach to make the DataContext change tracker be aware of those updated.
I usually use the first option as it is easiest. There is a performance penalty with two DB calls but unless you're doing lots of updates it won't matter.

How to clear the existing list data values

I would like to know how to delete(clear) the existing list data values before binding new values.
I'm using the list for binding data dynamically using http services. When I make a new call to the service, I want the exiting items to be flushed and bind the newly retrieved data to the same list. How can I achieve this?
You can explicitly set the dataProvider to null, or you can use removeAll() on the dataProvider.
You need not clear the data if you are binding httpService.lastResult to list.dataProvider. The data binding will create a new ListCollectionView object (ArrayCollection or XMLListCollection as the case may be) and assign it to list.dataProvider. Old data will be cleared automatically.

Linq update problem

I have an application with user and admin sections. If an admin updates data with the help of sql datasource then it's updated the database. However, when we retrieve data with linq query then it's showing its old value rather than the updated value.
After some time, the linq query automatically shows the correct value.
I think its caching the value, but I find myself helpless. Please help me with this.
When you say
when we retrieve data with linq query
Do you mean you call your select methods again or are you using the current in memory objects?
In either case, you can always refresh an entity with :
Context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, entity)
Make sure that you're using your DataContext efficiently (ideally one per unit of work).
After each update, make sure you call DataContext.SubmitChanges(); to commit your changes back to the database.
Also be aware that any context you instanciate between your changes being added to another context and calling SubmitChanges() will not reflect those changes.

Resources