1 20 21 package com.methodhead.aikp; 22 23 import java.text.DateFormat ; 24 import java.text.ParseException ; 25 26 import java.util.Date ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import com.methodhead.auth.AuthAction; 35 import com.methodhead.util.StrutsUtil; 36 37 import com.methodhead.persistable.PersistableException; 38 import com.methodhead.MhfException; 39 40 import org.apache.log4j.Logger; 41 42 import org.apache.struts.action.ActionForm; 43 import org.apache.struts.action.ActionForward; 44 import org.apache.struts.action.ActionMapping; 45 import org.apache.struts.action.ActionMessage; 46 import org.apache.struts.action.ActionMessages; 47 48 import org.apache.struts.validator.DynaValidatorForm; 49 import org.apache.struts.action.DynaActionForm; 50 import org.apache.commons.beanutils.DynaProperty; 51 import com.methodhead.util.StrutsUtil; 52 import com.methodhead.util.OperationContext; 53 import com.methodhead.auth.AuthUser; 54 import com.methodhead.auth.AuthUtil; 55 import org.apache.commons.lang.StringUtils; 56 57 158 public abstract class AikpAction extends AuthAction { 159 160 162 164 166 168 171 protected abstract AutoIntKeyPersistable createPersistable( 172 OperationContext op ); 173 174 178 protected ActionForward getForwardForSave( 179 OperationContext op, 180 Object policy ) { 181 182 return new ActionForward( op.mapping.getInput() ); 183 } 184 185 189 protected ActionForward getForwardForDelete( 190 OperationContext op, 191 Object policy ) { 192 193 return op.mapping.findForward( "status" ); 194 } 195 196 202 protected static void populateFormProperty( 203 String name, 204 DynaActionForm form, 205 AutoIntKeyPersistable persistable ) { 206 207 if ( persistable.getDynaClass().getDynaProperty( name ).getType() == 208 Date .class ) { 209 DateFormat dateFormat = DateFormat.getDateInstance( DateFormat.SHORT ); 210 form.set( name, dateFormat.format( persistable.getDate( name ) ) ); 211 } 212 else { 213 form.set( name, persistable.get( name ).toString() ); 214 } 215 } 216 217 223 protected static void populatePersistableField( 224 String name, 225 AutoIntKeyPersistable persistable, 226 DynaActionForm form ) { 227 228 persistable.setAsObject( name, form.get( name ) ); 229 } 230 231 235 protected void populateForm( 236 DynaActionForm form, 237 AutoIntKeyPersistable persistable ) { 238 239 DynaProperty[] dynaProperties = 240 persistable.getDynaClass().getDynaProperties(); 241 242 for ( int i = 0; i < dynaProperties.length; i++ ) { 243 populateFormProperty( dynaProperties[ i ].getName(), form, persistable ); 244 } 245 } 246 247 253 protected void populatePersistable( 254 AutoIntKeyPersistable persistable, 255 DynaActionForm form ) { 256 257 DynaProperty[] dynaProperties = 258 persistable.getDynaClass().getDynaProperties(); 259 260 for ( int i = 0; i < dynaProperties.length; i++ ) { 261 populatePersistableField( 262 dynaProperties[ i ].getName(), persistable, form ); 263 } 264 } 265 266 270 protected ActionForward doList( 271 OperationContext op, 272 Object policy ) { 273 274 AutoIntKeyPersistable persistable = createPersistable( op ); 275 op.form.set( 276 "list", persistable.loadAll( null, null ) ); 277 return StrutsUtil.findForward( op.mapping, "list" ); 278 } 279 280 284 protected ActionForward doNew( 285 OperationContext op, 286 Object policy ) { 287 288 populateForm( op.form, createPersistable( op ) ); 289 op.form.set( "action", "saveNew" ); 290 291 return new ActionForward( op.mapping.getInput() ); 292 } 293 294 298 protected ActionForward doEdit( 299 OperationContext op, 300 Object policy ) { 301 302 AutoIntKeyPersistable persistable = createPersistable( op ); 303 persistable.load( new IntKey( op.form.get( "id" ) ) ); 304 populateForm( op.form, persistable ); 305 op.form.set( "action", "save" ); 306 307 return new ActionForward( op.mapping.getInput() ); 308 } 309 310 320 protected ActionForward doConfirm( 321 OperationContext op, 322 Object policy ) { 323 324 AutoIntKeyPersistable persistable = createPersistable( op ); 325 persistable.load( new IntKey( op.form.get( "id" ) ) ); 326 327 StrutsUtil.addMessage( 328 op.request, "aikpaction.confirm", persistable, null, null ); 329 330 op.form.set( "action", "delete" ); 331 332 return StrutsUtil.findForward( op.mapping, "confirm" ); 333 } 334 335 341 protected ActionForward doSaveNew( 342 OperationContext op, 343 Object policy ) { 344 345 AutoIntKeyPersistable persistable = createPersistable( op ); 346 populatePersistable( persistable, op.form ); 347 persistable.saveNew(); 348 populateForm( op.form, persistable ); 349 StrutsUtil.addMessage( 350 op.request, "aikpaction.saved", persistable, null, null ); 351 op.form.set( "action", "save" ); 352 353 return getForwardForSave( op, policy ); 354 } 355 356 363 protected ActionForward doSave( 364 OperationContext op, 365 Object policy ) { 366 367 AutoIntKeyPersistable persistable = createPersistable( op ); 368 persistable.load( new IntKey( op.form.get( "id" ) ) ); 369 populatePersistable( persistable, op.form ); 370 persistable.save(); 371 populateForm( op.form, persistable ); 372 StrutsUtil.addMessage( 373 op.request, "aikpaction.saved", persistable, null, null ); 374 op.form.set( "action", "save" ); 375 376 return getForwardForSave( op, policy ); 377 } 378 379 386 protected ActionForward doCancel( 387 OperationContext op, 388 Object policy ) { 389 390 AutoIntKeyPersistable persistable = createPersistable( op ); 391 persistable.load( new IntKey( op.form.get( "id" ) ) ); 392 populateForm( op.form, persistable ); 393 op.form.set( "action", "save" ); 394 StrutsUtil.addMessage( 395 op.request, "aikpaction.cancelled", persistable, null, null ); 396 397 return new ActionForward( op.mapping.getInput() ); 398 } 399 400 406 protected ActionForward doCancelDelete( 407 OperationContext op, 408 Object policy ) { 409 410 AutoIntKeyPersistable persistable = createPersistable( op ); 411 persistable.load( new IntKey( op.form.get( "id" ) ) ); 412 populateForm( op.form, persistable ); 413 op.form.set( "action", "save" ); 414 StrutsUtil.addMessage( 415 op.request, "aikpaction.cancelled", persistable, null, null ); 416 417 return new ActionForward( op.mapping.getInput() ); 418 } 419 420 426 protected ActionForward doCancelSave( 427 OperationContext op, 428 Object policy ) { 429 430 AutoIntKeyPersistable persistable = createPersistable( op ); 431 persistable.load( new IntKey( op.form.get( "id" ) ) ); 432 populateForm( op.form, persistable ); 433 op.form.set( "action", "save" ); 434 StrutsUtil.addMessage( 435 op.request, "aikpaction.cancelled", persistable, null, null ); 436 437 return new ActionForward( op.mapping.getInput() ); 438 } 439 440 443 protected ActionForward doCancelSaveNew( 444 OperationContext op, 445 Object policy ) { 446 447 return doList( op, policy ); 448 } 449 450 463 protected ActionForward doDelete( 464 OperationContext op, 465 Object policy ) { 466 467 AutoIntKeyPersistable persistable = createPersistable( op ); 468 persistable.load( new IntKey( op.form.get( "id" ) ) ); 469 persistable.delete(); 470 471 StrutsUtil.addMessage( 472 op.request, "aikpaction.deleted", persistable, null, null ); 473 474 return getForwardForDelete( op, policy ); 475 } 476 477 481 public ActionForward doExecute( 482 ActionMapping mapping, 483 ActionForm form, 484 HttpServletRequest request, 485 HttpServletResponse response ) { 486 487 DynaValidatorForm dynaForm = ( DynaValidatorForm )form; 488 Object policy = StrutsUtil.getPolicy( mapping ); 489 AuthUser user = AuthUtil.getUser( request ); 490 491 OperationContext op = 492 new OperationContext( mapping, dynaForm, request, response, user ); 493 494 String aikpaction = dynaForm.get( "action" ).toString(); 495 496 if ( "list".equals( aikpaction ) ) { 497 return doList( op, policy ); 498 } 499 500 if ( "new".equals( aikpaction ) ) { 501 return doNew( op, policy ); 502 } 503 504 else if ( "edit".equals( aikpaction ) ) { 505 return doEdit( op, policy ); 506 } 507 508 else if ( "saveNew".equals( aikpaction ) ) { 509 if ( !StringUtils.isBlank( ( String )dynaForm.get( "cancel" ) ) ) 510 return doCancelSaveNew( op, policy ); 511 else 512 return doSaveNew( op, policy ); 513 } 514 515 else if ( "save".equals( aikpaction ) ) { 516 517 if ( !StringUtils.isBlank( ( String )dynaForm.get( "cancel" ) ) ) 518 return doCancelSave( op, policy ); 519 else if ( !StringUtils.isBlank( ( String )dynaForm.get( "delete" ) ) ) 520 return doConfirm( op, policy ); 521 else 522 return doSave( op, policy ); 523 } 524 525 else if ( "delete".equals( aikpaction ) ) { 526 527 if ( !StringUtils.isBlank( ( String )dynaForm.get( "cancel" ) ) ) 528 return doCancelDelete( op, policy ); 529 else 530 return doDelete( op, policy ); 531 } 532 533 else { 534 throw new MhfException( "Unexpected aikpaction \"" + aikpaction + "\"" ); 535 } 536 } 537 538 540 542 Logger logger_ = Logger.getLogger( AikpAction.class ); 543 } 544 | Popular Tags |