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 java.io.Serializable;
024: import javax.servlet.http.HttpServletRequest;
025:
026: import org.apache.struts.action.ActionMapping;
027: import org.apache.struts.action.ActionErrors;
028: import org.apache.struts.action.ActionError;
029: import org.apache.struts.validator.DynaValidatorForm;
030: import java.util.List;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import org.apache.struts.util.LabelValueBean;
034: import com.methodhead.sitecontext.SiteContext;
035: import com.methodhead.auth.AuthUtil;
036: import com.methodhead.tree.Tree;
037: import org.apache.commons.lang.StringUtils;
038: import java.io.File;
039:
040: public final class NavModuleForm extends DynaValidatorForm implements
041: Serializable {
042:
043: /**
044: * Instantiates the policy for this action.
045: */
046: private ShimPolicy getPolicy(ActionMapping mapping) {
047:
048: try {
049: return (ShimPolicy) Class.forName(mapping.getParameter())
050: .newInstance();
051: } catch (Exception e) {
052: throw new ShimException(
053: "Unexpected exception while instantiating \""
054: + mapping.getParameter() + "\":"
055: + e.getMessage());
056: }
057: }
058:
059: /*
060: public void reset(
061: ActionMapping mapping,
062: HttpServletRequest request ) {
063:
064: //
065: // don't do anything if no one is logged in; action will forward to login
066: //
067: if ( AuthUtil.getUser( request ) == null )
068: return;
069:
070: ShimPolicy policy = getPolicy( mapping );
071:
072: //
073: // get page options
074: //
075: Page page = policy.newPage();
076: page.setSiteContext( SiteContext.getContext( request ) );
077: List pages = page.loadAll();
078:
079: List pageOptions = new ArrayList();
080: pageOptions.add( new LabelValueBean( "Select...", "" ) );
081: for ( Iterator iter = pages.iterator(); iter.hasNext(); ) {
082: Page p = ( Page )iter.next();
083: pageOptions.add( new LabelValueBean( p.getString( "title" ), "" + p.getInt( "id" ) ) );
084: }
085:
086: set( "pages", pageOptions );
087: }
088: */
089:
090: public ActionErrors validate(ActionMapping mapping,
091: HttpServletRequest request) {
092:
093: //
094: // don't do anything if we're cancelling
095: //
096: if (!StringUtils.isBlank((String) get("cancel")))
097: return new ActionErrors();
098:
099: ActionErrors errors = super .validate(mapping, request);
100:
101: if (errors.size() == 0) {
102:
103: if ("jsp".equals(get("type"))) {
104: if (StringUtils.isBlank((String) get("jsp"))) {
105: errors.add("jsp", new ActionError(
106: "shim.navModuleForm.missingJsp"));
107: } else {
108: //
109: // verify jsp exists
110: //
111: File file = new File(request.getSession()
112: .getServletContext().getRealPath(
113: "/WEB-INF/resources/"
114: + SiteContext.getContext(
115: request).getInt(
116: "id") + "/"
117: + get("jsp")));
118:
119: if (!file.exists() || !file.isFile())
120: errors.add("jsp", new ActionError(
121: "shim.navModuleForm.invalidJsp"));
122: }
123: }
124: }
125: return errors;
126: }
127: }
|