|
@@ -1,142 +1,142 @@ |
1 |
|
- |
package org.litesoft.initfrom.server; |
2 |
|
- |
|
3 |
|
- |
import org.litesoft.bo.views.*; |
4 |
|
- |
import org.litesoft.changemanagement.*; |
5 |
|
- |
import org.litesoft.commonfoundation.typeutils.*; |
6 |
|
- |
import org.litesoft.core.*; |
7 |
|
- |
import org.litesoft.initfrom.client.boviews.*; |
8 |
|
- |
import org.litesoft.initfrom.server.boviews.podataproviders.*; |
9 |
|
- |
import org.litesoft.initfrom.server.pos.*; |
10 |
|
- |
|
11 |
|
- |
public class CurrentUserAccessor implements ServerStateChangedListener { |
12 |
|
- |
public static synchronized CurrentUserAccessor get() { |
13 |
|
- |
ServerStore zStore = ServerContext.get().getServerStore(); |
14 |
|
- |
CurrentUserAccessor zAccessor = zStore.get( CurrentUserAccessor.class ); |
15 |
|
- |
if ( zAccessor == null ) { |
16 |
|
- |
zStore.set( CurrentUserAccessor.class, zAccessor = new CurrentUserAccessor() ); |
17 |
|
- |
} |
18 |
|
- |
return zAccessor; |
19 |
|
- |
} |
20 |
|
- |
|
21 |
|
- |
private boolean mViewStale; |
22 |
|
- |
private ChangeListenerManager mRegisteredWithCLM; |
23 |
|
- |
private User mPO; |
24 |
|
- |
|
25 |
|
- |
public User getRequiredUser() { |
26 |
|
- |
User zUser = getOptionalUser( null ); |
27 |
|
- |
if ( zUser == null ) { |
28 |
|
- |
throw new SessionExpiredException( "No Current User" ); |
29 |
|
- |
} |
30 |
|
- |
return zUser; |
31 |
|
- |
} |
32 |
|
- |
|
33 |
|
- |
public User getOptionalUser( String pDefaultUserLoginEmail ) { |
34 |
|
- |
Pair zPair = getPair( pDefaultUserLoginEmail ); |
35 |
|
- |
if ( zPair.getVO() == null ) { |
36 |
|
- |
return null; |
37 |
|
- |
} |
38 |
|
- |
if ( (zPair.getPO() == null) || !zPair.inSync() ) { |
39 |
|
- |
zPair = refresh( zPair ); |
40 |
|
- |
} |
41 |
|
- |
return zPair.getPO(); |
42 |
|
- |
} |
43 |
|
- |
|
44 |
|
- |
public UserView getUserView( String pDefaultUserLoginEmail ) { |
45 |
|
- |
Pair zPair = getPair( pDefaultUserLoginEmail ); |
46 |
|
- |
if ( zPair.isStale() ) { |
47 |
|
- |
zPair = refresh( zPair ); |
48 |
|
- |
} |
49 |
|
- |
return zPair.getVO(); |
50 |
|
- |
} |
51 |
|
- |
|
52 |
|
- |
public void logout() { |
53 |
|
- |
update( null, null ); |
54 |
|
- |
} |
55 |
|
- |
|
56 |
|
- |
public UserView updateUser( User pPO ) { |
57 |
|
- |
UserView zView = getDataProvider().createView( pPO, false ); |
58 |
|
- |
update( zView, pPO ); |
59 |
|
- |
return zView; |
60 |
|
- |
} |
61 |
|
- |
|
62 |
|
- |
private Pair refresh( Pair pPair ) { |
63 |
|
- |
UserViewPoServerVoDataProvider zDP = getDataProvider(); |
64 |
|
- |
User zPO = (User) zDP.getPO( pPair.getVO().getID() ); |
65 |
|
- |
return update( zDP.createView( zPO, false ), zPO ); |
66 |
|
- |
} |
67 |
|
- |
|
68 |
|
- |
private Pair update( UserView pView, User pPO ) { |
69 |
|
- |
Pair rv; |
70 |
|
- |
User zOld, zNew; |
71 |
|
- |
synchronized ( this ) { |
72 |
|
- |
rv = new Pair( mViewStale = false, pView, pPO ); |
73 |
|
- |
ServerContext.get().getServerSession().setAttribute( UserView.class, pView ); |
74 |
|
- |
zOld = mPO; |
75 |
|
- |
zNew = mPO = pPO; |
76 |
|
- |
} |
77 |
|
- |
if ( !Objects.areNonArraysEqual( zOld, zNew ) ) { |
78 |
|
- |
if ( zOld != null ) { |
79 |
|
- |
mRegisteredWithCLM.removeListener( this ); |
80 |
|
- |
mRegisteredWithCLM = null; |
81 |
|
- |
} |
82 |
|
- |
if ( zNew != null ) { |
83 |
|
- |
(mRegisteredWithCLM = ChangeListenerManager.get()).addListener( this, User.class, zNew.getPersistentObjectUniqueKey() ); |
84 |
|
- |
} |
85 |
|
- |
} |
86 |
|
- |
return rv; |
87 |
|
- |
} |
88 |
|
- |
|
89 |
|
- |
private Pair getPair( String pDefaultUserLoginEmail ) { |
90 |
|
- |
synchronized ( this ) { |
91 |
|
- |
UserView zView = ServerContext.get().getServerSession().getAttribute( UserView.class ); |
92 |
|
- |
if ( (zView != null) || (pDefaultUserLoginEmail == null) ) { |
93 |
|
- |
return new Pair( mViewStale, zView, mPO ); |
94 |
|
- |
} |
95 |
|
- |
} |
96 |
|
- |
getDataProvider().loginDefaultUser( pDefaultUserLoginEmail ); |
97 |
|
- |
synchronized ( this ) { |
98 |
|
- |
UserView zView = ServerContext.get().getServerSession().getAttribute( UserView.class ); |
99 |
|
- |
return new Pair( mViewStale, zView, mPO ); |
100 |
|
- |
} |
101 |
|
- |
} |
102 |
|
- |
|
103 |
|
- |
@Override |
104 |
|
- |
public synchronized void serverStateChanged( ServerStateChangeSet pServerStateChangeSet ) { |
105 |
|
- |
mViewStale = true; |
106 |
|
- |
} |
107 |
|
- |
|
108 |
|
- |
private UserViewPoServerVoDataProvider getDataProvider() { |
109 |
|
- |
return (UserViewPoServerVoDataProvider) ServerContext.getMasterServerStore().get( ServerVoDataProviderMap.class ).get( UserView.class ); |
110 |
|
- |
} |
111 |
|
- |
|
112 |
|
- |
private static class Pair { |
113 |
|
- |
private boolean mStale; |
114 |
|
- |
private UserView mVO; |
115 |
|
- |
private User mPO; |
116 |
|
- |
|
117 |
|
- |
private Pair( boolean pStale, UserView pVO, User pPO ) { |
118 |
|
- |
mStale = (pVO != null) && pStale; |
119 |
|
- |
mVO = pVO; |
120 |
|
- |
mPO = pPO; |
121 |
|
- |
} |
122 |
|
- |
|
123 |
|
- |
public boolean isStale() { |
124 |
|
- |
return mStale; |
125 |
|
- |
} |
126 |
|
- |
|
127 |
|
- |
public UserView getVO() { |
128 |
|
- |
return mVO; |
129 |
|
- |
} |
130 |
|
- |
|
131 |
|
- |
public User getPO() { |
132 |
|
- |
return mPO; |
133 |
|
- |
} |
134 |
|
- |
|
135 |
|
- |
public boolean inSync() // Only called when Both are !null |
136 |
|
- |
{ |
137 |
|
- |
return !mStale && // |
138 |
|
- |
Objects.areNonArraysEqual( mPO.getID(), mVO.getID() ) && // |
139 |
|
- |
Objects.areNonArraysEqual( mPO.getRecordVersion(), mVO.getRecordVersion() ); |
140 |
|
- |
} |
141 |
|
- |
} |
142 |
|
- |
} |
|
1 |
+ |
package org.litesoft.initfrom.server; |
|
2 |
+ |
|
|
3 |
+ |
import org.litesoft.bo.views.*; |
|
4 |
+ |
import org.litesoft.changemanagement.*; |
|
5 |
+ |
import org.litesoft.commonfoundation.base.*; |
|
6 |
+ |
import org.litesoft.core.*; |
|
7 |
+ |
import org.litesoft.initfrom.client.boviews.*; |
|
8 |
+ |
import org.litesoft.initfrom.server.boviews.podataproviders.*; |
|
9 |
+ |
import org.litesoft.initfrom.server.pos.*; |
|
10 |
+ |
|
|
11 |
+ |
public class CurrentUserAccessor implements ServerStateChangedListener { |
|
12 |
+ |
public static synchronized CurrentUserAccessor get() { |
|
13 |
+ |
ServerStore zStore = ServerContext.get().getServerStore(); |
|
14 |
+ |
CurrentUserAccessor zAccessor = zStore.get( CurrentUserAccessor.class ); |
|
15 |
+ |
if ( zAccessor == null ) { |
|
16 |
+ |
zStore.set( CurrentUserAccessor.class, zAccessor = new CurrentUserAccessor() ); |
|
17 |
+ |
} |
|
18 |
+ |
return zAccessor; |
|
19 |
+ |
} |
|
20 |
+ |
|
|
21 |
+ |
private boolean mViewStale; |
|
22 |
+ |
private ChangeListenerManager mRegisteredWithCLM; |
|
23 |
+ |
private User mPO; |
|
24 |
+ |
|
|
25 |
+ |
public User getRequiredUser() { |
|
26 |
+ |
User zUser = getOptionalUser( null ); |
|
27 |
+ |
if ( zUser == null ) { |
|
28 |
+ |
throw new SessionExpiredException( "No Current User" ); |
|
29 |
+ |
} |
|
30 |
+ |
return zUser; |
|
31 |
+ |
} |
|
32 |
+ |
|
|
33 |
+ |
public User getOptionalUser( String pDefaultUserLoginEmail ) { |
|
34 |
+ |
Pair zPair = getPair( pDefaultUserLoginEmail ); |
|
35 |
+ |
if ( zPair.getVO() == null ) { |
|
36 |
+ |
return null; |
|
37 |
+ |
} |
|
38 |
+ |
if ( (zPair.getPO() == null) || !zPair.inSync() ) { |
|
39 |
+ |
zPair = refresh( zPair ); |
|
40 |
+ |
} |
|
41 |
+ |
return zPair.getPO(); |
|
42 |
+ |
} |
|
43 |
+ |
|
|
44 |
+ |
public UserView getUserView( String pDefaultUserLoginEmail ) { |
|
45 |
+ |
Pair zPair = getPair( pDefaultUserLoginEmail ); |
|
46 |
+ |
if ( zPair.isStale() ) { |
|
47 |
+ |
zPair = refresh( zPair ); |
|
48 |
+ |
} |
|
49 |
+ |
return zPair.getVO(); |
|
50 |
+ |
} |
|
51 |
+ |
|
|
52 |
+ |
public void logout() { |
|
53 |
+ |
update( null, null ); |
|
54 |
+ |
} |
|
55 |
+ |
|
|
56 |
+ |
public UserView updateUser( User pPO ) { |
|
57 |
+ |
UserView zView = getDataProvider().createView( pPO, false ); |
|
58 |
+ |
update( zView, pPO ); |
|
59 |
+ |
return zView; |
|
60 |
+ |
} |
|
61 |
+ |
|
|
62 |
+ |
private Pair refresh( Pair pPair ) { |
|
63 |
+ |
UserViewPoServerVoDataProvider zDP = getDataProvider(); |
|
64 |
+ |
User zPO = (User) zDP.getPO( pPair.getVO().getID() ); |
|
65 |
+ |
return update( zDP.createView( zPO, false ), zPO ); |
|
66 |
+ |
} |
|
67 |
+ |
|
|
68 |
+ |
private Pair update( UserView pView, User pPO ) { |
|
69 |
+ |
Pair rv; |
|
70 |
+ |
User zOld, zNew; |
|
71 |
+ |
synchronized ( this ) { |
|
72 |
+ |
rv = new Pair( mViewStale = false, pView, pPO ); |
|
73 |
+ |
ServerContext.get().getServerSession().setAttribute( UserView.class, pView ); |
|
74 |
+ |
zOld = mPO; |
|
75 |
+ |
zNew = mPO = pPO; |
|
76 |
+ |
} |
|
77 |
+ |
if ( !Currently.areEqual( zOld, zNew ) ) { |
|
78 |
+ |
if ( zOld != null ) { |
|
79 |
+ |
mRegisteredWithCLM.removeListener( this ); |
|
80 |
+ |
mRegisteredWithCLM = null; |
|
81 |
+ |
} |
|
82 |
+ |
if ( zNew != null ) { |
|
83 |
+ |
(mRegisteredWithCLM = ChangeListenerManager.get()).addListener( this, User.class, zNew.getPersistentObjectUniqueKey() ); |
|
84 |
+ |
} |
|
85 |
+ |
} |
|
86 |
+ |
return rv; |
|
87 |
+ |
} |
|
88 |
+ |
|
|
89 |
+ |
private Pair getPair( String pDefaultUserLoginEmail ) { |
|
90 |
+ |
synchronized ( this ) { |
|
91 |
+ |
UserView zView = ServerContext.get().getServerSession().getAttribute( UserView.class ); |
|
92 |
+ |
if ( (zView != null) || (pDefaultUserLoginEmail == null) ) { |
|
93 |
+ |
return new Pair( mViewStale, zView, mPO ); |
|
94 |
+ |
} |
|
95 |
+ |
} |
|
96 |
+ |
getDataProvider().loginDefaultUser( pDefaultUserLoginEmail ); |
|
97 |
+ |
synchronized ( this ) { |
|
98 |
+ |
UserView zView = ServerContext.get().getServerSession().getAttribute( UserView.class ); |
|
99 |
+ |
return new Pair( mViewStale, zView, mPO ); |
|
100 |
+ |
} |
|
101 |
+ |
} |
|
102 |
+ |
|
|
103 |
+ |
@Override |
|
104 |
+ |
public synchronized void serverStateChanged( ServerStateChangeSet pServerStateChangeSet ) { |
|
105 |
+ |
mViewStale = true; |
|
106 |
+ |
} |
|
107 |
+ |
|
|
108 |
+ |
private UserViewPoServerVoDataProvider getDataProvider() { |
|
109 |
+ |
return (UserViewPoServerVoDataProvider) ServerContext.getMasterServerStore().get( ServerVoDataProviderMap.class ).get( UserView.class ); |
|
110 |
+ |
} |
|
111 |
+ |
|
|
112 |
+ |
private static class Pair { |
|
113 |
+ |
private boolean mStale; |
|
114 |
+ |
private UserView mVO; |
|
115 |
+ |
private User mPO; |
|
116 |
+ |
|
|
117 |
+ |
private Pair( boolean pStale, UserView pVO, User pPO ) { |
|
118 |
+ |
mStale = (pVO != null) && pStale; |
|
119 |
+ |
mVO = pVO; |
|
120 |
+ |
mPO = pPO; |
|
121 |
+ |
} |
|
122 |
+ |
|
|
123 |
+ |
public boolean isStale() { |
|
124 |
+ |
return mStale; |
|
125 |
+ |
} |
|
126 |
+ |
|
|
127 |
+ |
public UserView getVO() { |
|
128 |
+ |
return mVO; |
|
129 |
+ |
} |
|
130 |
+ |
|
|
131 |
+ |
public User getPO() { |
|
132 |
+ |
return mPO; |
|
133 |
+ |
} |
|
134 |
+ |
|
|
135 |
+ |
public boolean inSync() // Only called when Both are !null |
|
136 |
+ |
{ |
|
137 |
+ |
return !mStale && // |
|
138 |
+ |
Currently.areEqual( mPO.getID(), mVO.getID() ) && // |
|
139 |
+ |
Currently.areEqual( mPO.getRecordVersion(), mVO.getRecordVersion() ); |
|
140 |
+ |
} |
|
141 |
+ |
} |
|
142 |
+ |
} |