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