KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import com.methodhead.util.OperationContext;
31 import com.methodhead.util.ServletUtils;
32 import org.apache.commons.lang.StringUtils;
33 import java.io.File JavaDoc;
34 import com.methodhead.sitecontext.SiteContext;
35 import com.methodhead.aikp.IntKey;
36 import java.util.List JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.io.IOException JavaDoc;
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   // constructors /////////////////////////////////////////////////////////////
47

48   // constants ////////////////////////////////////////////////////////////////
49

50   // classes //////////////////////////////////////////////////////////////////
51

52   // methods //////////////////////////////////////////////////////////////////
53

54   /**
55    * Returns a forward to list.
56    */

57   protected ActionForward getForwardForSave(
58     OperationContext op,
59     Object JavaDoc policy ) {
60
61     return new ActionForward( "/siteContext.do?action=list" );
62   }
63
64   /**
65    * Returns a forward to list.
66    */

67   protected ActionForward getForwardForDelete(
68     OperationContext op,
69     Object JavaDoc policy ) {
70
71     return new ActionForward( "/siteContext.do?action=list" );
72   }
73
74   /**
75    * Creates a public and resource directory for the id of the new site
76    * context.
77    */

78   public ActionForward doSaveNew(
79     OperationContext op,
80     Object JavaDoc policy ) {
81
82     ActionForward forward = super.doSaveNew( op, policy );
83
84     //
85
// create directories
86
//
87
String JavaDoc id = ( String JavaDoc )op.form.get( "id" );
88     if ( !StringUtils.isBlank( id ) ) {
89       //
90
// create public directory
91
//
92
File JavaDoc file = ServletUtils.getRealFile( op.request, "/" + id );
93       if ( !file.mkdir() )
94         throw new ShimException( "Couldn't create directory \"" + file + "\"" );
95
96       //
97
// create private directory
98
//
99
file = ServletUtils.getRealFile( op.request, "/WEB-INF/resources/" + id );
100       if ( !file.mkdir() )
101         throw new ShimException( "Couldn't create directory \"" + file + "\"" );
102
103       //
104
// create templates directory
105
//
106
file = new File JavaDoc( file, "templates" );
107       if ( !file.mkdir() )
108         throw new ShimException( "Couldn't create directory \"" + file + "\"" );
109     }
110
111     return forward;
112   }
113
114   /**
115    * Deletes any pages associated with the site and public and private
116    * directories for the site context.
117    */

118   public ActionForward doDelete(
119     OperationContext op,
120     Object JavaDoc policy ) {
121
122     ShimPolicy shimPolicy = ( ShimPolicy )policy;
123
124     //
125
// delete all pages
126
//
127
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 JavaDoc list = page.loadAll();
134     for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
135       Page p = ( Page )iter.next();
136       p.loadFull( p.getKey() );
137       p.delete();
138     }
139
140     //
141
// delete resource directories
142
//
143
try {
144       File JavaDoc 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 JavaDoc e ) {
154       throw new ShimException(
155         "Unexpected IOException: " + ExceptionUtils.getStackTrace( e ) );
156     }
157
158     //
159
// delete html fragments
160
//
161
HtmlFragment fragment = new HtmlFragment();
162     fragment.setSiteContext( siteContext );
163     fragment.deleteAll();
164
165     //
166
// call superclass
167
//
168
super.doDelete( op, policy );
169
170     return new ActionForward( "/siteContext.do?action=list" );
171   }
172
173   // properties ///////////////////////////////////////////////////////////////
174

175   // attributes ///////////////////////////////////////////////////////////////
176
}
177
Popular Tags