001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import org.apache.struts.action.Action;
024: import org.apache.struts.action.ActionMapping;
025: import org.apache.struts.action.ActionForm;
026: import org.apache.struts.action.ActionForward;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import com.methodhead.util.OperationContext;
031: import com.methodhead.util.ServletUtils;
032: import org.apache.commons.lang.StringUtils;
033: import java.io.File;
034: import com.methodhead.sitecontext.SiteContext;
035: import com.methodhead.aikp.IntKey;
036: import java.util.List;
037: import java.util.Iterator;
038: import java.io.IOException;
039: import org.apache.commons.io.FileUtils;
040: import org.apache.commons.lang.exception.ExceptionUtils;
041:
042: public class SiteContextAction extends
043: com.methodhead.sitecontext.SiteContextAction {
044:
045: // constructors /////////////////////////////////////////////////////////////
046:
047: // constants ////////////////////////////////////////////////////////////////
048:
049: // classes //////////////////////////////////////////////////////////////////
050:
051: // methods //////////////////////////////////////////////////////////////////
052:
053: /**
054: * Returns a forward to list.
055: */
056: protected ActionForward getForwardForSave(OperationContext op,
057: Object policy) {
058:
059: return new ActionForward("/siteContext.do?action=list");
060: }
061:
062: /**
063: * Returns a forward to list.
064: */
065: protected ActionForward getForwardForDelete(OperationContext op,
066: Object policy) {
067:
068: return new ActionForward("/siteContext.do?action=list");
069: }
070:
071: /**
072: * Creates a public and resource directory for the id of the new site
073: * context.
074: */
075: public ActionForward doSaveNew(OperationContext op, Object policy) {
076:
077: ActionForward forward = super .doSaveNew(op, policy);
078:
079: //
080: // create directories
081: //
082: String id = (String) op.form.get("id");
083: if (!StringUtils.isBlank(id)) {
084: //
085: // create public directory
086: //
087: File file = ServletUtils.getRealFile(op.request, "/" + id);
088: if (!file.mkdir())
089: throw new ShimException("Couldn't create directory \""
090: + file + "\"");
091:
092: //
093: // create private directory
094: //
095: file = ServletUtils.getRealFile(op.request,
096: "/WEB-INF/resources/" + id);
097: if (!file.mkdir())
098: throw new ShimException("Couldn't create directory \""
099: + file + "\"");
100:
101: //
102: // create templates directory
103: //
104: file = new File(file, "templates");
105: if (!file.mkdir())
106: throw new ShimException("Couldn't create directory \""
107: + file + "\"");
108: }
109:
110: return forward;
111: }
112:
113: /**
114: * Deletes any pages associated with the site and public and private
115: * directories for the site context.
116: */
117: public ActionForward doDelete(OperationContext op, Object policy) {
118:
119: ShimPolicy shimPolicy = (ShimPolicy) policy;
120:
121: //
122: // delete all pages
123: //
124: SiteContext siteContext = new SiteContext();
125: siteContext.load(new IntKey(op.form.get("id")));
126:
127: Page page = shimPolicy.newPage();
128: page.setSiteContext(siteContext);
129:
130: List list = page.loadAll();
131: for (Iterator iter = list.iterator(); iter.hasNext();) {
132: Page p = (Page) iter.next();
133: p.loadFull(p.getKey());
134: p.delete();
135: }
136:
137: //
138: // delete resource directories
139: //
140: try {
141: File file = ServletUtils.getRealFile(op.request, "/"
142: + op.form.get("id"));
143: FileUtils.deleteDirectory(file);
144:
145: file = ServletUtils.getRealFile(op.request,
146: "/WEB-INF/resources/" + op.form.get("id"));
147: FileUtils.deleteDirectory(file);
148: } catch (IOException e) {
149: throw new ShimException("Unexpected IOException: "
150: + ExceptionUtils.getStackTrace(e));
151: }
152:
153: //
154: // delete html fragments
155: //
156: HtmlFragment fragment = new HtmlFragment();
157: fragment.setSiteContext(siteContext);
158: fragment.deleteAll();
159:
160: //
161: // call superclass
162: //
163: super .doDelete(op, policy);
164:
165: return new ActionForward("/siteContext.do?action=list");
166: }
167:
168: // properties ///////////////////////////////////////////////////////////////
169:
170: // attributes ///////////////////////////////////////////////////////////////
171: }
|