litesoft
@ 804
litesoft / trunk / GWT_Sandbox / InitFrom / src / org / litesoft / initfrom / server / boviews / podataproviders / UserViewPoServerVoDataProvider.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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
package org.litesoft.initfrom.server.boviews.podataproviders; import java.util.*; import org.litesoft.bo.views.*; import org.litesoft.bo.views.communication.*; import org.litesoft.bo.views.server.*; import org.litesoft.core.*; import org.litesoft.core.typeutils.*; import org.litesoft.exceptions.*; import org.litesoft.initfrom.client.boviews.*; import org.litesoft.initfrom.server.*; import org.litesoft.initfrom.server.boviews.*; import org.litesoft.initfrom.server.pos.*; import org.litesoft.orsup.*; import org.litesoft.orsup.base.*; import org.litesoft.orsup.selection.*; import org.litesoft.orsup.transact.*; import org.litesoft.sql.*; public class UserViewPoServerVoDataProvider extends PoServerVoDataProvider<UserView> implements UserViewNames { private String mDefaultUserLoginEmail = Strings.noEmpty( System.getProperty( "User" ) ); private String getDefaultUserLoginEmail() { String zInitialUserEmail = mDefaultUserLoginEmail; mDefaultUserLoginEmail = null; return ((zInitialUserEmail != null) && (null == cursoryEmailCheck( zInitialUserEmail, aLogonEmail ))) ? zInitialUserEmail : null; } public UserViewPoServerVoDataProvider( MetaDataStore pMetaDataStore ) { super( UserViewMetaData.getInstance(), pMetaDataStore ); } @Override protected void LLinitialize() { super.LLinitialize(); register( new UserViewServerFunctionLogin() ); register( new UserViewServerFunctionLogout() ); register( new UserViewServerFunctionSwitchRestrictedResources() ); register( new UserViewServerFunctionChangePassword() ); register( new UserViewServerFunctionValidatePassword() ); register( new UserViewServerFunctionValidateLogonEmailAvailable() ); register( new UserViewServerFunctionNextDemoLoginUser() ); register( new UserViewServerFunctionFetchCurrentLoginUser() ); } @Override public void updatePO( PersistentObject pPO, UserView pMember ) { super.updatePO( pPO, pMember ); //To change body of overridden methods use File | Settings | File Templates. } public SCresult changePassword( UserView pUserView ) { String zLogonEmail = pUserView.getLogonEmail(); String zCurrentPassword = pUserView.getCurrentPassword(); String zNewPassword = Strings.noEmpty( pUserView.getNewPassword() ); String zConfirmPassword = Strings.noEmpty( pUserView.getConfirmPassword() ); String zError = cursoryPasswordCheck( zCurrentPassword, aCurrentPassword ); if ( null == zError ) { if ( null == (zError = cursoryPasswordCheck( zNewPassword, aNewPassword )) ) { if ( null == (zError = cursoryPasswordCheck( zConfirmPassword, aConfirmPassword )) ) { if ( !zNewPassword.equals( zConfirmPassword ) ) { zError = "New & Confirm Passwords do not match"; } else { while ( zError == null ) { User zUser = getUserByEmailFromCurrentDS( zLogonEmail ); if ( (zUser == null) || !zUser.validatePassword( zCurrentPassword ) ) { zError = "Invalid Credentials"; } else if ( null == (zError = zUser.acceptablePassword( zNewPassword )) ) { Transaction zTransaction = DataStoreLocator.get().getUnfilteredFinder().createTransaction(); zUser = zUser.copyInto( zTransaction ); zUser.setPassword( zNewPassword ); try { zTransaction.commit(); return VOSC.result( createView( zUser, false ) ); } catch ( ConcurrentPOModificationException e ) { // Loop... } catch ( RuntimeException e ) { LOGGER.warn.log( e ); zError = e.getMessage(); } } } } } } } return VOSC.result( zError ); } public String validatePassword( UserView pUserView ) { String zCurrentPassword = pUserView.getCurrentPassword(); String zError = cursoryPasswordCheck( zCurrentPassword, aCurrentPassword ); if ( null == zError ) { zError = "Invalid"; User zUser = getUserByEmailFromCurrentDS( pUserView.getLogonEmail() ); if ( (zUser != null) && zUser.validatePassword( zCurrentPassword ) ) { return null; } } return zError; } public UserView validateLogonEmailAvailable( String pLogonEmail ) { String zError = cursoryEmailCheck( pLogonEmail, aLogonEmail ); if ( zError != null ) { throw new DisplayableRuntimeException( zError ); } User zUser = getUserByEmailFromCurrentDS( pLogonEmail ); return createView( zUser, false ); } public UserView loginUser( String pLogonEmail, String pPassword ) { String zError = cursoryEmailCheck( pLogonEmail, aLogonEmail ); if ( null == zError ) { if ( null == (zError = cursoryPasswordCheck( pPassword, aCurrentPassword )) ) { zError = "Invalid Credentials"; User zUser = getUserByEmailFromMasterDS( pLogonEmail ); if ( zUser != null ) { if ( zUser.validatePassword( pPassword ) ) { return updateLoggedInUserFromMasterDS( zUser ); } } } } throw new DisplayableRuntimeException( zError ); } public UserView loginDefaultUser( String pLogonEmail ) { try { User zUser = getUserByEmailFromMasterDS( pLogonEmail ); if ( zUser != null ) { return updateLoggedInUserFromMasterDS( zUser ); } } catch ( RuntimeException e ) { LOGGER.error.log( e ); } return null; } public UserView switchRestrictedResources( RestrictedResourceView pRestrictedResourceView ) { User zUser = CurrentUserAccessor.get().getRequiredUser(); RestrictedResource zRestrictedResource = (RestrictedResource) getNotUsDataProvider( RestrictedResourceView.class ).getExistingPO( DataStoreLocator.get().getUnfilteredFinder(), pRestrictedResourceView ); if ( zRestrictedResource == null ) { throw new DisplayableRuntimeException( "RestrictedResourceNotFound", pRestrictedResourceView.toString() ); } UserRestrictedResourcePair zPair = zUser.loginToRestrictedResourceIfAcceptable( zRestrictedResource ); return finiUserLogin( zPair.getUser(), zPair.getRestrictedResource() ); } private UserView updateLoggedInUserFromMasterDS( User pUser ) { if ( !pUser.isDemo() ) { initializeRealDB(); } else { initializeDemoDB(); Objects.assertNotNull( "Demo User Refreshed", pUser = getUserByEmailFromCurrentDS( pUser.getLogonEmail() ) ); } return finiUserLogin( pUser ); } public UserView nextDemoUserView() { initializeDemoDB(); int zNextIndex = findCurrentDemoIndex( currentLoggedInUserView() ) + 1; for ( int i = 2 + DEMO_USERS_EMAIL_LIST.length; --i > 0; zNextIndex++ ) { if ( DEMO_USERS_EMAIL_LIST.length <= zNextIndex ) { zNextIndex = 0; } User zUser = getUserByEmailFromCurrentDS( DEMO_USERS_EMAIL_LIST[zNextIndex] ); if ( zUser != null ) { zUser.setDemo(); return finiUserLogin( zUser ); } } throw new IllegalStateException( "No Demo Users!" ); } private void initializeRealDB() { ServerContext.get().getServerStore().set( DataStore.class, ServerContext.getMasterServerStore().get( DataStore.class ) ); // Master DS } private void initializeDemoDB() { ServerContext zContext = ServerContext.get(); DemoDataStoreManager.Struct zDemoDS = DemoDataStoreManager.getDemoDS( zContext.getContextID() ); // Demo DS zContext.getServerStore().set( DataStore.class, zDemoDS.getDataStore() ); if ( zDemoDS.isEmptyDemo() ) { populateDemoDS(); } } public UserView currentLoggedInUserView() { return CurrentUserAccessor.get().getUserView( getDefaultUserLoginEmail() ); } public void logoutUser() { CurrentUserAccessor.get().logout(); CurrentRestrictedResourceAccessor.get().logout(); } private UserView finiUserLogin( User pUser ) { return finiUserLogin( pUser, CurrentRestrictedResourceAccessor.get().getOptionalRestrictedResource() ); } private UserView finiUserLogin( User pUser, RestrictedResource pRestrictedResource ) { UserRestrictedResourcePair zUserRestrictedResourcePair = pUser.getLoginRestrictedResource( pRestrictedResource ); User zUser = zUserRestrictedResourcePair.getUser(); RestrictedResource zRestrictedResource = zUserRestrictedResourcePair.getRestrictedResource(); CurrentRestrictedResourceAccessor zRestrictedResourceAccessor = CurrentRestrictedResourceAccessor.get(); zRestrictedResourceAccessor.updateRestrictedResource( zRestrictedResource ); if ( !zRestrictedResource.equals( zUser.getLastRestrictedResource() ) ) { Transaction zTransaction = zUser.getFinder().createTransaction(); zUser = zUser.copyInto( zTransaction ); zUser.setLastRestrictedResource( zRestrictedResource ); zTransaction.commit(); } zRestrictedResourceAccessor.updateRestrictedResource( zRestrictedResource ); UserView zUserView = CurrentUserAccessor.get().updateUser( zUser ); zUserView.setCurrentRestrictedResource( zRestrictedResourceAccessor.getRestrictedResourceView() ); if ( !zUser.canAccessAllRestrictedResources() ) { zUserView.setAlternateRestrictedResourceOptions( convertToRestrictedResourceOptions( zUser.getAlternateRestrictedResourceOptions( zRestrictedResource ) ) ); } return zUserView; } private User getUserByEmailFromMasterDS( String pLogonEmail ) { return getUserByEmail( ServerContext.getMasterServerStore().get( DataStore.class ).getUnfilteredFinder(), pLogonEmail ); } private User getUserByEmailFromCurrentDS( String pLogonEmail ) { return getUserByEmail( DataStoreLocator.get().getUnfilteredFinder(), pLogonEmail ); } private User getUserByEmail( Finder pFinder, String pLogonEmail ) { User zUser = pFinder.findOne( User.class, WCF.isEqual( User.CD_LogonEmail, pLogonEmail ) ); if ( isUserDemo( zUser ) ) { zUser.setDemo(); } return zUser; } private AlternateRestrictedResourceOption[] convertToRestrictedResourceOptions( RestrictedResource[] pRestrictedResources ) { AlternateRestrictedResourceOption[] rv = new AlternateRestrictedResourceOption[pRestrictedResources.length]; PoServerVoDataProvider<RestrictedResourceView> zRestrictedResourceViewDP = getNotUsDataProvider( RestrictedResourceView.class ); for ( int i = 0; i < pRestrictedResources.length; i++ ) { rv[i] = zRestrictedResourceViewDP.createView( pRestrictedResources[i], false ); } Arrays.sort( rv ); return rv; } @Override protected WhereClause augment_getMatchingVOs( WhereClause pWhereClause ) { User zUser = CurrentUserAccessor.get().getRequiredUser(); if ( !zUser.canAccessAllRestrictedResources() ) // If no user it will blow up with a null pointer exception which is appropriate. { pWhereClause = WCF.and( pWhereClause, User.getLimitToCurrentRestrictedResourceAndVisible() ); // Add filter to where clause to only select based on site. } return super.augment_getMatchingVOs( pWhereClause ); } private boolean isUserDemo( User pUser ) { return ((pUser != null) && DEMO_USERS_EMAIL_SET.contains( Strings.deNull( pUser.getLogonEmail() ).toLowerCase() )); } private int findCurrentDemoIndex( UserView pUser ) { if ( pUser == null ) { return 0; } int i = DEMO_USERS_EMAIL_LIST.length; while ( --i >= 0 ) { if ( DEMO_USERS_EMAIL_LIST[i].equalsIgnoreCase( pUser.getLogonEmail() ) ) { return i; } } return i; } private static final String[] DEMO_USERS_EMAIL_LIST = new String[] // { // "akern@testsite.com", // ....... Anthony Kern "saly@testsite.com", // ........ Sally Dispop "hdeskman@asmeds.com", // ...... Helpy Deskman "dsteele@testsite.com", // ..... Donald Steele "slick@asmeds.com", // ......... Slick Salesman "centralinvmgr@savrx.com", // .. Jack Barta "peds@sns.to", // .............. George Smith "dm@testsite.com", // .......... Dee Man }; private static final Set<String> DEMO_USERS_EMAIL_SET = new HashSet<String>( Arrays.asList( DEMO_USERS_EMAIL_LIST ) ); private void populateDemoDS() { DataLoader.INSTANCE.loadDemoData(); } } |