Using Lookups.TableChanged event

Discussions related to custom development with Select.
kqian
Posts: 68
Joined: Wed Feb 23, 2011 4:01 pm

Using Lookups.TableChanged event

Post by kqian »

I am trying to subscribe to Lookups.TableChanged event as below. I added a new contact to "Order Contact - Person" table through SoftPro Client, but my handler was not called.
"lookupService.TableChanged += myhandler;"

I'd like to know how to use this event, when the event will be raised.

Thanks.
Mark McKenna

Re: Using Lookups.TableChanged event

Post by Mark McKenna »

The event should fire when the ApplyChanges method of the Lookups service finishes successfully. For example...

Code: Select all

Lookups lookupService = server.GetService<Lookups>();
LookupQuerySpec spec = new LookupQuerySpec();
spec.Table = "MyTableName";
spec.SchemaOnly = false;
ILookupTable table = lookupService.QueryTable(spec);

// make some changes to table...

// if this succeeds, the event will fire
lookupService.ApplyChanges(table); 
Graham Campbell
Posts: 61
Joined: Fri Jul 01, 2011 10:06 am
Location: Raleigh, North Carolina
Contact:

Re: Using Lookups.TableChanged event

Post by Graham Campbell »

The Lookups.TableChanged event should be raised during a successful ApplyChanges call against the Lookups API.

If applying changes to a new lookup table which previously has not existed then this event will not be raised. The Lookups.TableAdded will be raised instead.

You can apply changes against multiple lookup tables at once. When this is the case, one event should be raised for each existing lookup table which has been modified.




You mention that you are adding a new record to a lookup table through the client. Adding a new row into the client grid will not fire this event, but the event should be raised when saving changes to the lookup table so that they are persisted.

If you are modifying the lookup table through the management console then you will need to click the save button in the upper left corner of the application to cause the tab to apply changes. This should cause the tab header to go from having a * to no longer having a *, indicating a save occurred successfully.

If you are modifying the lookup table through ProForm by launching the lookups dialog then you will need to click the same button on the top menu of the dialog.

In both cases the save button should diable, indicating that the save has occurred successfully.



Has this helped at all? If not, we may be further details about your exact workflow and version of the application.
Graham Campbell
SoftPro Software Engineer
kqian
Posts: 68
Joined: Wed Feb 23, 2011 4:01 pm

Re: Using Lookups.TableChanged event

Post by kqian »

Thanks for the help! I verified adding new entry from SoftPro client in both ways (management console & Proform dialog). The new entry did get saved. But I still don't get the event.
My setup is that I created a listener app on a client machine which connects to the Select Server on a remote server machine.

var lookupService = Myhelper.GetLookupService();
lookupService.TableChanged += lookupService_TableChanged;

The connection was set up fine, and can get data from the table with no problem, but my event hander was just not called when a new entry was added from SoftPro Client.

Is this the correct way to subscribe to the event from another machine? Did I miss anything?
Graham Campbell
Posts: 61
Joined: Fri Jul 01, 2011 10:06 am
Location: Raleigh, North Carolina
Contact:

Re: Using Lookups.TableChanged event

Post by Graham Campbell »

These are simple .NET events in this area. When a change is made on the server it does not currently signal out to all connected sessions that a change has been made.


I believe that the problem you are encountering is that you have two sessions with the server. It sounds like your listener application is running on a separate machine than the SoftPro client application. Even if that is not the case, if the listener application creates it's own session with our APIs and services instead of getting the SelectServer reference that the SoftPro client application is using then it won't get the events being raised by the client application.



If you need to listen to the TableChanged event that is within a different session then you will probably have to build your own construct for that.

Here is an example design:
You could build a package which is installed on all of the Softpro clients that you want to listen to. That package could listen to the TableChanged event and when the event is fired it could call out to a webservice that you have defined. That webservice would know whenever a lookup table has changed.


Does this sound like I am on the right track?
Graham Campbell
SoftPro Software Engineer
kqian
Posts: 68
Joined: Wed Feb 23, 2011 4:01 pm

Re: Using Lookups.TableChanged event

Post by kqian »

Thanks for the help. Following your suggestion, I tried subscribing the event in my package as following:

partial class MyPackage : Package
{
protected override void OnInitialize()
{
var lookups = GetService<Lookups>();

lookups.TableChanged += new EventHandler<LookupTableChangedEventArgs>(lookups_TableChanged);

//TODO: Provide package initialization logic here.
base.RegisterHandler(MyPackage.RibbonButton1, this.RibbonButton1_Invoked);
}
...

Launched "Selecte.exe /rootsuffix:Exp", verified my package loaded and works (my package was set to autoload). Added new entries in this Select client instance in both ProForm dialog window and Console management window. But did not see my event handler get called.

Is it the right location to register my event handler? Why the event not fired?

Thanks.
kqian
Posts: 68
Joined: Wed Feb 23, 2011 4:01 pm

Re: Using Lookups.TableChanged event

Post by kqian »

More info. I tried again, and found that actually the "lookups" from following line is null.
var lookups = GetService<Lookups>();
It looks like the "Lookups" service is not available at that point.
So how should I register the hander for TableChanged Event? Or is the "Lookups" service available at all in a custom package?
Thanks.
Graham Campbell
Posts: 61
Joined: Fri Jul 01, 2011 10:06 am
Location: Raleigh, North Carolina
Contact:

Re: Using Lookups.TableChanged event

Post by Graham Campbell »

Try getting the Lookups service from the SelectServer service.

Code: Select all

SelectServer selectServer= GetService<SelectServer>();
Lookups lookups = selectServer.GetService<Lookups>();
Graham Campbell
SoftPro Software Engineer
kqian
Posts: 68
Joined: Wed Feb 23, 2011 4:01 pm

Re: Using Lookups.TableChanged event

Post by kqian »

I tried it inside Oninitialized() and could not get the server either, the selectServer is null.
Graham Campbell
Posts: 61
Joined: Fri Jul 01, 2011 10:06 am
Location: Raleigh, North Carolina
Contact:

Re: Using Lookups.TableChanged event

Post by Graham Campbell »

I have confirmed your experience. I really would have thought that the SelectServer would be available at that point, but it is unfortunately not. I have found a place where we do something like what you are trying to do in our code.

In the OnInitialized() the IShell should be available. Try hooking up an event to there which listens to the shell loading. Then once it is loaded the SelectServer should be available. At that point your events can be registered and you can unregister that event.


Give this a try:

Code: Select all

partial class MyPackage : Package
{
    protected override void OnInitialize()
    {
        IShell shell = GetService<IShell>();
        shell.Load += new EventHandler(Shell_Load);
    }

    private void Shell_Load(object sender, EventArgs e)
    {
        SelectServer selectServer= GetService<SelectServer>();
        Lookups lookups= selectServer.GetService<Lookups>();

        lookups.TableChanged += new EventHandler<LookupTableChangedEventArgs>(lookups_TableChanged);

        //remove the event handler so it only executes one time
        IShell shell = GetService<IShell>();
        shell.Load -= this.Shell_Load;
    }
}
Graham Campbell
SoftPro Software Engineer
Post Reply