1 20 21 22 package com.methodhead.shim; 23 24 import java.io.Serializable ; 25 import javax.servlet.http.HttpServletRequest ; 26 27 import org.apache.struts.action.ActionMapping; 28 import org.apache.struts.action.ActionErrors; 29 import org.apache.struts.action.ActionError; 30 import org.apache.struts.validator.DynaValidatorForm; 31 import java.util.List ; 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import org.apache.struts.util.LabelValueBean; 35 import com.methodhead.sitecontext.SiteContext; 36 import com.methodhead.auth.AuthUtil; 37 import com.methodhead.tree.Tree; 38 import org.apache.commons.lang.StringUtils; 39 40 public final class MovePageForm 41 extends 42 DynaValidatorForm 43 implements 44 Serializable { 45 46 49 private ShimPolicy getPolicy( 50 ActionMapping mapping ) { 51 52 try { 53 return 54 ( ShimPolicy )Class.forName( mapping.getParameter() ).newInstance(); 55 } 56 catch ( Exception e ) { 57 throw new ShimException( 58 "Unexpected exception while instantiating \"" + 59 mapping.getParameter() +"\":" + e.getMessage() ); 60 } 61 } 62 63 public void reset( 64 ActionMapping mapping, 65 HttpServletRequest request ) { 66 67 if ( AuthUtil.getUser( request ) == null ) 71 return; 72 73 ShimPolicy policy = getPolicy( mapping ); 74 75 Page page = policy.newPage(); 79 page.setSiteContext( SiteContext.getContext( request ) ); 80 List pages = page.loadAll(); 81 82 List pageOptions = new ArrayList (); 83 pageOptions.add( new LabelValueBean( "Select...", "" ) ); 84 for ( Iterator iter = pages.iterator(); iter.hasNext(); ) { 85 Page p = ( Page )iter.next(); 86 pageOptions.add( new LabelValueBean( p.getString( "title" ), "" + p.getInt( "id" ) ) ); 87 } 88 89 set( "pages", pageOptions ); 90 } 91 92 public ActionErrors validate( 93 ActionMapping mapping, 94 HttpServletRequest request ) { 95 96 if ( !StringUtils.isBlank( ( String )get( "cancel" ) ) ) 100 return new ActionErrors(); 101 102 ActionErrors errors = super.validate( mapping, request ); 103 104 if ( errors.size() == 0 ) { 105 ShimPolicy policy = getPolicy( mapping ); 106 107 SiteMap siteMap = ShimUtils.getSiteMap( request ); 108 109 Link link = siteMap.find( Integer.parseInt( ( String )get( "id" ) ) ); 110 Link dest = siteMap.find( Integer.parseInt( ( String )get( "destid" ) ) ); 111 112 String position = Tree.POSITION_UNDER; 113 if ( "under".equals( get( "position" ) ) ) 114 position = Tree.POSITION_UNDER; 115 else if ( "before".equals( get( "position" ) ) ) 116 position = Tree.POSITION_BEFORE; 117 else if ( "after".equals( get( "position" ) ) ) 118 position = Tree.POSITION_AFTER; 119 else 120 throw new ShimException( 121 "Unexpected position \"" + get( "position" ) + "\"." ); 122 123 String result = siteMap.validateMove( link, dest, position ); 124 125 if ( Tree.INVALIDMOVE_CANNOTMOVEROOT.equals( result ) ) 126 errors.add( 127 "dest", new ActionError( "shim.movePageForm.cannotMoveRoot" ) ); 128 else if ( Tree.INVALIDMOVE_CANNOTMOVEBEFOREROOT.equals( result ) ) 129 errors.add( 130 "dest", new ActionError( "shim.movePageForm.cannotMoveBeforeRoot" ) ); 131 else if ( Tree.INVALIDMOVE_CANNOTMOVEAFTERROOT.equals( result ) ) 132 errors.add( 133 "dest", new ActionError( "shim.movePageForm.cannotMoveAfterRoot" ) ); 134 else if ( Tree.INVALIDMOVE_CANNOTMOVENEARSELF.equals( result ) ) 135 errors.add( 136 "dest", new ActionError( "shim.movePageForm.cannotMoveNearSelf" ) ); 137 else if ( Tree.INVALIDMOVE_CANNOTMOVEUNDERSELF.equals( result ) ) 138 errors.add( 139 "dest", new ActionError( "shim.movePageForm.cannotMoveUnderSelf" ) ); 140 } 141 142 return errors; 143 } 144 } 145 | Popular Tags |