KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > MovePageForm


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21
22 package com.methodhead.shim;
23
24 import java.io.Serializable JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
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 JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
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 JavaDoc {
45
46   /**
47    * Instantiates the policy for this action.
48    */

49   private ShimPolicy getPolicy(
50     ActionMapping mapping ) {
51
52     try {
53       return
54         ( ShimPolicy )Class.forName( mapping.getParameter() ).newInstance();
55     }
56     catch ( Exception JavaDoc 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 JavaDoc request ) {
66
67     //
68
// don't do anything if no one is logged in; action will forward to login
69
//
70
if ( AuthUtil.getUser( request ) == null )
71       return;
72
73     ShimPolicy policy = getPolicy( mapping );
74
75     //
76
// get page options
77
//
78
Page page = policy.newPage();
79     page.setSiteContext( SiteContext.getContext( request ) );
80     List JavaDoc pages = page.loadAll();
81
82     List JavaDoc pageOptions = new ArrayList JavaDoc();
83     pageOptions.add( new LabelValueBean( "Select...", "" ) );
84     for ( Iterator JavaDoc 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 JavaDoc request ) {
95
96     //
97
// don't do anything if we're cancelling
98
//
99
if ( !StringUtils.isBlank( ( String JavaDoc )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 JavaDoc )get( "id" ) ) );
110       Link dest = siteMap.find( Integer.parseInt( ( String JavaDoc )get( "destid" ) ) );
111
112       String JavaDoc 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 JavaDoc 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