KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.StrutsUtil;
37 import com.methodhead.persistable.PersistableException;
38 import org.apache.commons.lang.StringUtils;
39
40 public final class PageForm
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 or if there is no site context;
69
// action will forward to login
70
//
71
if ( ( AuthUtil.getUser( request ) != null ) &&
72          ( SiteContext.getContext( request ) != null ) ) {
73       ShimPolicy policy = getPolicy( mapping );
74
75       //
76
// get template options
77
//
78
Template template = policy.newTemplate();
79       template.setSiteContext( SiteContext.getContext( request ) );
80       List JavaDoc templates = template.getTemplateList( request );
81
82       List JavaDoc templateOptions = new ArrayList JavaDoc();
83       templateOptions.add( new LabelValueBean( "Select...", "" ) );
84       for ( Iterator JavaDoc iter = templates.iterator(); iter.hasNext(); ) {
85         String JavaDoc s = ( String JavaDoc )iter.next();
86         templateOptions.add( new LabelValueBean( ShimUtils.fileNameToLabel( s ), s ) );
87       }
88
89       set( "templates", templateOptions );
90
91       //
92
// get page options
93
//
94
Page page = policy.newPage();
95       page.setSiteContext( SiteContext.getContext( request ) );
96       List JavaDoc pages = page.loadAll();
97
98       List JavaDoc pageOptions = new ArrayList JavaDoc();
99       pageOptions.add( new LabelValueBean( "Select...", "" ) );
100       for ( Iterator JavaDoc iter = pages.iterator(); iter.hasNext(); ) {
101         Page p = ( Page )iter.next();
102         pageOptions.add( new LabelValueBean( p.getString( "title" ), "" + p.getInt( "id" ) ) );
103       }
104
105       set( "pages", pageOptions );
106     }
107   }
108
109   public ActionErrors validate(
110     ActionMapping mapping,
111     HttpServletRequest JavaDoc request ) {
112
113     if ( StringUtils.isBlank( ( String JavaDoc )get( "cancel" ) ) ) {
114       ActionErrors errors = super.validate( mapping, request );
115
116       if ( errors.isEmpty() ) {
117         //
118
// has the page alias been used?
119
//
120
ShimPolicy policy = ( ShimPolicy )StrutsUtil.getPolicy( mapping );
121         Page page = policy.newPage();
122         page.setSiteContext( SiteContext.getContext( request ) );
123
124         //
125
// has an alias been specified
126
//
127
String JavaDoc alias = ( String JavaDoc )get( "alias" );
128
129         if ( !StringUtils.isBlank( alias ) ) {
130           if ( !StringUtils.isAlphanumeric( alias ) )
131             errors.add(
132               "alias", new ActionError( "shim.pageForm.invalidAlias" ) );
133
134           else {
135             try {
136               page.loadForAlias( ( String JavaDoc )get( "alias" ) );
137
138               String JavaDoc id = ( String JavaDoc )get( "id" );
139
140               if ( StringUtils.isBlank( id ) )
141                 errors.add(
142                   "alias",
143                   new ActionError(
144                     "shim.pageForm.aliasUsed", page.getString( "title" ) ) );
145               else {
146                 if ( !id.equals( "" + page.getInt( "id" ) ) )
147                   errors.add(
148                     "alias",
149                     new ActionError(
150                       "shim.pageForm.aliasUsed", page.getString( "title" ) ) );
151               }
152             }
153             catch ( PersistableException e ) {
154               //
155
// alias is not in use
156
//
157
}
158           }
159         }
160
161         //
162
// has a template or a page to copy been selected?
163
//
164
if (
165           StringUtils.isBlank( ( String JavaDoc )get( "template" ) ) &&
166           StringUtils.isBlank( ( String JavaDoc )get( "copyfrom" ) ) )
167
168           errors.add(
169             "template", new ActionError( "shim.pageForm.missingTemplateOrCopy" ) );
170
171         //
172
// are there quotes in the meta tags?
173
//
174
if ( ( ( ( String JavaDoc )get( "metadescription" ) ).indexOf( "\"" ) != -1 ) ||
175              ( ( ( String JavaDoc )get( "metadescription" ) ).indexOf( "'" ) != -1 ) )
176           errors.add(
177             "metadescription",
178             new ActionError( "shim.pageForm.quotesInMetaDescription" ) );
179
180         if ( ( ( ( String JavaDoc )get( "metakeywords" ) ).indexOf( "\"" ) != -1 ) ||
181              ( ( ( String JavaDoc )get( "metakeywords" ) ).indexOf( "'" ) != -1 ) )
182           errors.add(
183             "metakeywords",
184             new ActionError( "shim.pageForm.quotesInMetaKeywords" ) );
185       }
186
187       return errors;
188     }
189
190     return new ActionErrors();
191   }
192 }
193
Popular Tags