1 20 21 package com.methodhead.shim; 22 23 import java.io.Serializable ; 24 import javax.servlet.http.HttpServletRequest ; 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 ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 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 { 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 ) && 72 ( SiteContext.getContext( request ) != null ) ) { 73 ShimPolicy policy = getPolicy( mapping ); 74 75 Template template = policy.newTemplate(); 79 template.setSiteContext( SiteContext.getContext( request ) ); 80 List templates = template.getTemplateList( request ); 81 82 List templateOptions = new ArrayList (); 83 templateOptions.add( new LabelValueBean( "Select...", "" ) ); 84 for ( Iterator iter = templates.iterator(); iter.hasNext(); ) { 85 String s = ( String )iter.next(); 86 templateOptions.add( new LabelValueBean( ShimUtils.fileNameToLabel( s ), s ) ); 87 } 88 89 set( "templates", templateOptions ); 90 91 Page page = policy.newPage(); 95 page.setSiteContext( SiteContext.getContext( request ) ); 96 List pages = page.loadAll(); 97 98 List pageOptions = new ArrayList (); 99 pageOptions.add( new LabelValueBean( "Select...", "" ) ); 100 for ( Iterator 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 request ) { 112 113 if ( StringUtils.isBlank( ( String )get( "cancel" ) ) ) { 114 ActionErrors errors = super.validate( mapping, request ); 115 116 if ( errors.isEmpty() ) { 117 ShimPolicy policy = ( ShimPolicy )StrutsUtil.getPolicy( mapping ); 121 Page page = policy.newPage(); 122 page.setSiteContext( SiteContext.getContext( request ) ); 123 124 String alias = ( String )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 )get( "alias" ) ); 137 138 String id = ( String )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 } 158 } 159 } 160 161 if ( 165 StringUtils.isBlank( ( String )get( "template" ) ) && 166 StringUtils.isBlank( ( String )get( "copyfrom" ) ) ) 167 168 errors.add( 169 "template", new ActionError( "shim.pageForm.missingTemplateOrCopy" ) ); 170 171 if ( ( ( ( String )get( "metadescription" ) ).indexOf( "\"" ) != -1 ) || 175 ( ( ( String )get( "metadescription" ) ).indexOf( "'" ) != -1 ) ) 176 errors.add( 177 "metadescription", 178 new ActionError( "shim.pageForm.quotesInMetaDescription" ) ); 179 180 if ( ( ( ( String )get( "metakeywords" ) ).indexOf( "\"" ) != -1 ) || 181 ( ( ( String )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 |