KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.methodhead.shim;
22
23 import java.io.Serializable JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionErrors;
28 import org.apache.struts.action.ActionError;
29 import org.apache.struts.validator.DynaValidatorForm;
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import org.apache.struts.util.LabelValueBean;
34 import com.methodhead.sitecontext.SiteContext;
35 import com.methodhead.auth.AuthUtil;
36 import com.methodhead.tree.Tree;
37 import org.apache.commons.lang.StringUtils;
38 import java.io.File JavaDoc;
39 import org.apache.commons.validator.GenericValidator;
40
41 public final class ModuleForm
42 extends
43   DynaValidatorForm
44 implements
45   Serializable JavaDoc {
46
47   public void reset(
48     ActionMapping mapping,
49     HttpServletRequest JavaDoc request ) {
50
51     set( "fragments", ShimUtils.getHtmlFragmentOptions( request ) );
52   }
53
54   protected void validateConfigureNavModule(
55     ActionMapping mapping,
56     HttpServletRequest JavaDoc request,
57     ActionErrors errors ) {
58
59     if ( errors.size() == 0 ) {
60
61       //
62
// jsp
63
//
64
if ( "jsp".equals( get( "type" ) ) ) {
65         String JavaDoc jsp = ( String JavaDoc )get( "jsp" );
66
67         if ( StringUtils.isBlank( jsp ) ) {
68           errors.add(
69             "jsp", new ActionError( "shim.moduleForm.missingJsp" ) );
70         }
71         else {
72
73           //
74
// make sure it ends in jsp
75
//
76
if ( !jsp.endsWith( ".jsp" ) )
77             errors.add(
78               "jsp", new ActionError( "shim.moduleForm.invalidJsp" ) );
79
80           //
81
// verify jsp exists
82
//
83
File JavaDoc file =
84             new File JavaDoc( request.getSession().getServletContext().getRealPath(
85               "/WEB-INF/resources/" +
86               SiteContext.getContext( request ).getInt( "id" ) + "/" +
87               get( "jsp" ) ) );
88
89           if ( !file.exists() || !file.isFile() )
90             errors.add(
91               "jsp", new ActionError( "shim.moduleForm.jspDoesNotExist" ) );
92         }
93       }
94
95       //
96
// folding
97
//
98
else if ( "folding".equals( get( "type" ) ) ) {
99
100         //
101
// integer settings
102
//
103
if ( !GenericValidator.isInt( ( String JavaDoc )get( "startlev" ) ) )
104           errors.add(
105             "startlev", new ActionError( "shim.moduleForm.invalidStartLev" ) );
106
107         if ( !GenericValidator.isInt( ( String JavaDoc )get( "contextlev" ) ) )
108           errors.add(
109             "contextlev",
110             new ActionError( "shim.moduleForm.invalidStartLev" ) );
111
112         if ( !GenericValidator.isInt( ( String JavaDoc )get( "depthlev" ) ) )
113           errors.add(
114             "depthlev", new ActionError( "shim.moduleForm.invalidStartLev" ) );
115
116         if ( !GenericValidator.isInt( ( String JavaDoc )get( "toplev" ) ) )
117           errors.add(
118             "toplev", new ActionError( "shim.moduleForm.invalidTopLev" ) );
119
120         //
121
// html fragments
122
//
123
String JavaDoc s = null;
124
125         s = ( String JavaDoc )get( "header" );
126         if ( s.length() > 512 )
127           errors.add(
128             "header", new ActionError( "shim.moduleForm.headerTooLong" ) );
129
130         s = ( String JavaDoc )get( "link" );
131         if ( s.length() > 512 )
132           errors.add(
133             "link", new ActionError( "shim.moduleForm.linkTooLong" ) );
134
135         s = ( String JavaDoc )get( "curlink" );
136         if ( s.length() > 512 )
137           errors.add(
138             "curlink", new ActionError( "shim.moduleForm.curlinkTooLong" ) );
139
140         s = ( String JavaDoc )get( "footer" );
141         if ( s.length() > 512 )
142           errors.add(
143             "footer", new ActionError( "shim.moduleForm.footerTooLong" ) );
144       }
145     }
146   }
147
148   protected void validateConfigureIncludeModule(
149     ActionMapping mapping,
150     HttpServletRequest JavaDoc request,
151     ActionErrors errors ) {
152
153     if ( errors.size() == 0 ) {
154
155       String JavaDoc jsp = ( String JavaDoc )get( "jsp" );
156
157       if ( StringUtils.isBlank( jsp ) ) {
158         errors.add(
159           "jsp", new ActionError( "shim.moduleForm.missingJsp" ) );
160         return;
161       }
162
163       //
164
// make sure it ends in jsp
165
//
166
if ( !jsp.endsWith( ".jsp" ) )
167         errors.add(
168           "jsp", new ActionError( "shim.moduleForm.invalidJsp" ) );
169
170       //
171
// verify jsp exists
172
//
173
else {
174         File JavaDoc file =
175           new File JavaDoc( request.getSession().getServletContext().getRealPath(
176             "/WEB-INF/resources/" +
177             SiteContext.getContext( request ).getInt( "id" ) + "/" +
178             get( "jsp" ) ) );
179
180         if ( !file.exists() || !file.isFile() )
181           errors.add(
182             "jsp", new ActionError( "shim.moduleForm.jspDoesNotExist" ) );
183       }
184     }
185   }
186
187   public ActionErrors validate(
188     ActionMapping mapping,
189     HttpServletRequest JavaDoc request ) {
190
191     //
192
// don't do anything if we're cancelling
193
//
194
if ( !StringUtils.isBlank( ( String JavaDoc )get( "cancel" ) ) )
195       return new ActionErrors();
196
197     ActionErrors errors = super.validate( mapping, request );
198
199     if ( "/configureNavModule".equals( mapping.getPath() ) )
200       validateConfigureNavModule( mapping, request, errors );
201     else if ( "/configureIncludeModule".equals( mapping.getPath() ) )
202       validateConfigureIncludeModule( mapping, request, errors );
203     else if ( "/configureTextModule".equals( mapping.getPath() ) ) {
204       // nothing here
205
}
206     else
207       throw new ShimException(
208         "Unexpected mapping \"" + mapping.getPath() + "\"." );
209
210     return errors;
211   }
212 }
213
Popular Tags