Adding a new row to another table using java in Maximo - java

I have two tables that I'm working with: KINCIDENT and ASSISTANT. The main one is KINCIDENT and the two are linked using an ID. I'm able to add a new row from the application but I want to do this using Java. I tried the following code but did not work:
MboSetRemote assistSet = MXServer.getMXServer().getMboSet("ASSISTANT",userInfo);
MboRemote newAssist = assistSet.add();
newAssist.setValue("LOCATION",x);
newAssist.setValue("INCNUM",y);
assistSet.save();
I checked to see if the row was added but it wasn't and I also didn't find any new entries in the database either. Am I missing something?

As long as your code is running, you should have seen that new record in the assistant table, but you definitely would not have seen that on the screen. To make the record appear on the screen, you have to know Maximo's "cache" system to get and edit the exact instance of the set that backs the screen, instead of just any instance (or a whole new instance like you created there).
I don't know where your Java code is (an app bean, an MBOSet, an MBO, or a field class) and I don't know what event/trigger you are hooking into (adding a new record, saving an existing record or something else), both of which are important to know. I will assume you are in an MBO class of the KINCIDENT object running in the "add()" method; meaning when a new KINCIDENT is created, you want to add a new ASSISTANT record. Running as part of that trigger should mean that you are already hooked into the screen instance of the KINCIDENT object when a user adds a new record. To make your ASSISTANT record appear in the set instance backing the screen, you need to the follow the screen's relationships from KINCIDENT to ASSISTANT. I'm assuming on the screen the ASSISTANT table is set up as a child of the KINCIDENT table using a relationship. In that case you just need to get the ASSISTANT set using that relationship. Assuming your relationship is named the same as the set ("ASSISTANT"), it would look something like this:
MboSetRemote assistSet = getMboSet("ASSISTANT");
MboRemote newAssist = assistSet.add();
newAssist.setValue("LOCATION",x);
newAssist.setValue("INCNUM",y);
This will not save your record yet (persist it to the database), but you want to keep your saves to a minimum. Let the user specify if a record should be saved or not by them hitting the "save"/disk icon in the toolbar.

Related

Observing #Relation changes in a DataBase

If I something like the following:
#Relation(parentColumn = "id", entityColumn = "fk_id", entity = User.class)
private List<User> users;
This list is a model which gets updated via LiveData.
But I want to be able to update this when I detect changes to the User table. Right now, it only picks up what exists in the DB, but whenever an update happens to the user table, this list does not get updated. Any suggestions?
You have not been very specific in what you are doing or want (which database you use, who is updating the database...).
But I want to be able to update this when I detect changes to the User
table. Right now, this only picks up what exists in the DB but
whenever an update happens to the user table, this list does not get
updated. Any suggestions?
Option 1
If you are doing the update/insert/delete in the same app as the List users, then you can easily update your List in your code.
Option 2
If another app/service is updating the database, I suggest you use a trigger to update your List.
See http://www.coderconsole.com/2015/02/android-sqlite-trigger-demo.html for an example.
Detecting the change in Java is tricky, see Calling a Java method from a sqlite trigger (android) .
Here is an example where you use a ContentObserver to monitor your Sqlite database (the flat file itself) https://gist.github.com/JBirdVegas/3874450
Option 3
If another app/service is updating the database, you can use a BroadcastReceiver to pick up the changes.

Possible overwriting to database table

Let's say you have a database table name table1 with columns name and surname. Two different clients open the same view from the java application and get the data for same person at the same time.
1) First client changed the name and pressed save button to update database record.
2) Client2 still sees the old record on the screen and then pressed to save button to change the surname.
It actually overwrite the record by old name. I think to check and get the latest data before updating the database when I click button but I do not like this solution because of making a second query before update.
So how can we solve this problem by using Hibernate and without using Hibarnete. Any idea?
Of course if you do not want that something will be overridden, then you have to check the data before an update. But it will be not always a real query with a good caching strategy. You could also use a timestamp with last update to compare it easier. Another strategy would be to lock the entities when the first user will read them. But that is normally not a good design for web applications or you have to integrate a messaging service, which will all user inform for an update who actually have open that entity. But I think that is not so easy to implement and a more advanced feature.
In short, compare the timestamp of an entity and if already updated, then compare the changes and show them for the user who wanted update that entity.

Read and update data using Hibernate

I'm newbie using Hibernate, I've read Hibernate can handle concurrency, but I don't know how to apply it in the following scenario:
In a web project, there are two enities called Invoice and Control.
Invoice entity has a numeric field named documentno.
Control has a field named lastdocnumber
When a new Invoice is created, the lastdocnumber field is read, increased and the new value is updated in database.
When two users create a new record at the same time, the lastdocnumber is duplicated, because the same value is read by the two requests, I tried to create an Utility static class with a synchronized method, but after a search in google I realised that it can be handled by hibernate.
Could anybody give some hint to achieve that?
Make lastdocnumber as unique column so it wont duplicate even if two or more users submit at same time -- JR

add new column in default contacts content provider of android

I have requirement to add new column in default contacts table provided by android so can I do that .I just want to add a new value in that column if it is added from my application.So please tell me if it is possible .
Edit:My aim is to add data in defaults contacts table programatically so that it will also be visible when user opens default phonebook.
No. This is not possible.
Though it technically is, the end result will not be desirable:
It would need root obviously. You can't modify the data of other apps without root.
You would need to find where the actual SQLite database file is located, open that, modify it.
Would the contacts app even work after this modification? Doubtful.
Would this modification persist? Doubtful.
Why not make the association some other way? Store whatever value you have with an identifuer from the contacts database.

How to create an undo function in spring MVC based web app.?

I have an employee and a corresponding employee history table.
Both the tables have same structure. History table is used to track the historical changes made to the employee over a period of time.
Now, I need to add an undo function to the changes made to the employee.
e.g. Employees title is changed on 1st August. Now, This will update the employees title in Employee table and insert an corresponding history record in employee_history table.
Now, I need to undo this change. Employee edit page will have a list of changes made to employee datewise with an undo button beside it.
Clicking on undo should revert changes in Employee table to previous value. Also I think the record in history table which says title is changed, should also be removed.
Also when I revert tghe changes to employee table i.e. revert title to previous title, this will fire an insert to history table, which I dont want.
I am not sure what is the best possible way to do this.
Any suggestions will be helpful.
In case you want to implement a "persistent" undo - one that would survive an application restart/session timeout, you should consider extending your DB schema width timestamp fields and either delete the last entry or replace it with an appropriate earlier entry.
A "light" version would be using a stack to store last interactions, including the original and the new value. You could persist the stack on session invalidation of course to combine both approaches. This seems to be what you are actually doing.
You could extend this solution by creating and storing or exporting SQL migration scripts for each change, recording the change and, if possible, the opposite action. So you could even transfer the scripts between application instances and environments and would have a complete "replayability" of your DB states.
tl;dr - it looks like you have already implemented a good solution
I would suggest using a flag telling the trigger/history logic to keep off while you have your undo running and not writing history data.
Normally this would be done by serializer-class feeding from your history table and restoring employee data and later cleaning up history-entries/unlocking history again.
You could maybe use the rollback feature of the transaction.

Categories