1 | /** |
---|
2 | * SynchronizationService Service |
---|
3 | * |
---|
4 | * Description of my service |
---|
5 | * |
---|
6 | * @author your email (+name?) |
---|
7 | * @since 2010mmdd |
---|
8 | * @package ??? |
---|
9 | * |
---|
10 | * Revision information: |
---|
11 | * $Rev: 1939 $ |
---|
12 | * $Author: robert@isdat.nl $ |
---|
13 | * $Date: 2011-06-24 10:56:22 +0000 (vr, 24 jun 2011) $ |
---|
14 | */ |
---|
15 | package dbnp.modules |
---|
16 | |
---|
17 | import dbnp.studycapturing.* |
---|
18 | import org.dbnp.gdt.* |
---|
19 | import org.springframework.web.context.request.RequestContextHolder |
---|
20 | |
---|
21 | class ModuleNotificationService implements Serializable { |
---|
22 | def remoteAuthenticationService |
---|
23 | |
---|
24 | static transactional = false |
---|
25 | |
---|
26 | /** |
---|
27 | * Sends a notification to assay modules that some part of a study has changed. |
---|
28 | * |
---|
29 | * Only modules that have the notify flag set to true will be notified. They will be notified on the URL |
---|
30 | * |
---|
31 | * [moduleUrl]/rest/notifyStudyChange?studyToken=abc |
---|
32 | * |
---|
33 | * Errors that occur when calling this URL are ignored. The module itself is responsible of |
---|
34 | * maintaining a synchronized state. |
---|
35 | * |
---|
36 | * @param study |
---|
37 | * @return |
---|
38 | */ |
---|
39 | def invalidateStudy( Study study ) { |
---|
40 | if( !study ) |
---|
41 | return |
---|
42 | |
---|
43 | log.info( "Invalidate " + study.code ) |
---|
44 | |
---|
45 | def modules = AssayModule.findByNotify(true); |
---|
46 | |
---|
47 | // If no modules are set to notify, return |
---|
48 | if( !modules ) |
---|
49 | return |
---|
50 | |
---|
51 | |
---|
52 | // Try to see which user is logged in. If no user is logged in (or no http session exists yet) |
---|
53 | // we send no authentication parameters |
---|
54 | def user |
---|
55 | try { |
---|
56 | user = RequestContextHolder.currentRequestAttributes().getSession().gscfUser |
---|
57 | |
---|
58 | if( !user ) |
---|
59 | throw new Exception( "No user is logged in" ); |
---|
60 | |
---|
61 | } catch( Exception e ) { |
---|
62 | log.warn "Sending study change notification without authentication, because an exception occurred: " + e.getMessage(); |
---|
63 | } |
---|
64 | |
---|
65 | def urls = [] |
---|
66 | modules.each { module -> |
---|
67 | // create a random session token that will be used to allow to module to |
---|
68 | // sync with gscf prior to presenting the measurement data |
---|
69 | def sessionToken = UUID.randomUUID().toString() |
---|
70 | def consumer = module.url |
---|
71 | |
---|
72 | // Create a URL to call |
---|
73 | def authenticationParameters = ""; |
---|
74 | if( user ) { |
---|
75 | // put the session token to work (for 15 minutes) |
---|
76 | remoteAuthenticationService.logInRemotely( consumer, sessionToken, user, 15 * 60 ) |
---|
77 | |
---|
78 | authenticationParameters = "consumer=" + consumer + "&sessionToken=" + sessionToken; |
---|
79 | } |
---|
80 | |
---|
81 | urls << module.url + '/rest/notifyStudyChange?studyToken=' + study.giveUUID() + ( authenticationParameters ? "&" + authenticationParameters : "" ) |
---|
82 | } |
---|
83 | |
---|
84 | // Notify the module in a separate thread, so the user doesn't have to wait for it |
---|
85 | Thread.start { |
---|
86 | urls.each { url -> |
---|
87 | try { |
---|
88 | def connection = url.toURL().openConnection() |
---|
89 | if( connection.responseCode == 200 ) { |
---|
90 | log.info( "GSCF NOTIFY-CALL SUCCEEDED: ${url}" ) |
---|
91 | } else { |
---|
92 | log.info( "GSCF NOTIFY-CALL FAILED: ${url}: " + connection.responseCode ) |
---|
93 | } |
---|
94 | } catch( Exception ignore) { |
---|
95 | log.info( "GSCF NOTIFY-CALL ERROR: ${url} - " + ignore.getMessage() ) |
---|
96 | } |
---|
97 | } |
---|
98 | }; |
---|
99 | } |
---|
100 | } |
---|