1 20 21 package com.methodhead.shim; 22 23 import org.apache.struts.action.Action; 24 import org.apache.struts.action.ActionMapping; 25 import org.apache.struts.action.ActionForm; 26 import org.apache.struts.action.ActionForward; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import com.methodhead.util.OperationContext; 31 import com.methodhead.util.ServletUtils; 32 import org.apache.commons.lang.StringUtils; 33 import java.io.File ; 34 import com.methodhead.sitecontext.SiteContext; 35 import com.methodhead.aikp.IntKey; 36 import java.util.List ; 37 import java.util.Iterator ; 38 import java.io.IOException ; 39 import org.apache.commons.io.FileUtils; 40 import org.apache.commons.lang.exception.ExceptionUtils; 41 42 public class SiteContextAction 43 extends 44 com.methodhead.sitecontext.SiteContextAction { 45 46 48 50 52 54 57 protected ActionForward getForwardForSave( 58 OperationContext op, 59 Object policy ) { 60 61 return new ActionForward( "/siteContext.do?action=list" ); 62 } 63 64 67 protected ActionForward getForwardForDelete( 68 OperationContext op, 69 Object policy ) { 70 71 return new ActionForward( "/siteContext.do?action=list" ); 72 } 73 74 78 public ActionForward doSaveNew( 79 OperationContext op, 80 Object policy ) { 81 82 ActionForward forward = super.doSaveNew( op, policy ); 83 84 String id = ( String )op.form.get( "id" ); 88 if ( !StringUtils.isBlank( id ) ) { 89 File file = ServletUtils.getRealFile( op.request, "/" + id ); 93 if ( !file.mkdir() ) 94 throw new ShimException( "Couldn't create directory \"" + file + "\"" ); 95 96 file = ServletUtils.getRealFile( op.request, "/WEB-INF/resources/" + id ); 100 if ( !file.mkdir() ) 101 throw new ShimException( "Couldn't create directory \"" + file + "\"" ); 102 103 file = new File ( file, "templates" ); 107 if ( !file.mkdir() ) 108 throw new ShimException( "Couldn't create directory \"" + file + "\"" ); 109 } 110 111 return forward; 112 } 113 114 118 public ActionForward doDelete( 119 OperationContext op, 120 Object policy ) { 121 122 ShimPolicy shimPolicy = ( ShimPolicy )policy; 123 124 SiteContext siteContext = new SiteContext(); 128 siteContext.load( new IntKey( op.form.get( "id" ) ) ); 129 130 Page page = shimPolicy.newPage(); 131 page.setSiteContext( siteContext ); 132 133 List list = page.loadAll(); 134 for ( Iterator iter = list.iterator(); iter.hasNext(); ) { 135 Page p = ( Page )iter.next(); 136 p.loadFull( p.getKey() ); 137 p.delete(); 138 } 139 140 try { 144 File file = 145 ServletUtils.getRealFile( op.request, "/" + op.form.get( "id" ) ); 146 FileUtils.deleteDirectory( file ); 147 148 file = 149 ServletUtils.getRealFile( 150 op.request, "/WEB-INF/resources/" + op.form.get( "id" ) ); 151 FileUtils.deleteDirectory( file ); 152 } 153 catch ( IOException e ) { 154 throw new ShimException( 155 "Unexpected IOException: " + ExceptionUtils.getStackTrace( e ) ); 156 } 157 158 HtmlFragment fragment = new HtmlFragment(); 162 fragment.setSiteContext( siteContext ); 163 fragment.deleteAll(); 164 165 super.doDelete( op, policy ); 169 170 return new ActionForward( "/siteContext.do?action=list" ); 171 } 172 173 175 } 177 | Popular Tags |