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.util.StrutsUtil;
037: import com.methodhead.persistable.PersistableException;
038: import org.apache.commons.lang.StringUtils;
039:
040: public final class PageForm 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: public void reset(ActionMapping mapping, HttpServletRequest request) {
060:
061: //
062: // don't do anything if no one is logged in or if there is no site context;
063: // action will forward to login
064: //
065: if ((AuthUtil.getUser(request) != null)
066: && (SiteContext.getContext(request) != null)) {
067: ShimPolicy policy = getPolicy(mapping);
068:
069: //
070: // get template options
071: //
072: Template template = policy.newTemplate();
073: template.setSiteContext(SiteContext.getContext(request));
074: List templates = template.getTemplateList(request);
075:
076: List templateOptions = new ArrayList();
077: templateOptions.add(new LabelValueBean("Select...", ""));
078: for (Iterator iter = templates.iterator(); iter.hasNext();) {
079: String s = (String) iter.next();
080: templateOptions.add(new LabelValueBean(ShimUtils
081: .fileNameToLabel(s), s));
082: }
083:
084: set("templates", templateOptions);
085:
086: //
087: // get page options
088: //
089: Page page = policy.newPage();
090: page.setSiteContext(SiteContext.getContext(request));
091: List pages = page.loadAll();
092:
093: List pageOptions = new ArrayList();
094: pageOptions.add(new LabelValueBean("Select...", ""));
095: for (Iterator iter = pages.iterator(); iter.hasNext();) {
096: Page p = (Page) iter.next();
097: pageOptions.add(new LabelValueBean(
098: p.getString("title"), "" + p.getInt("id")));
099: }
100:
101: set("pages", pageOptions);
102: }
103: }
104:
105: public ActionErrors validate(ActionMapping mapping,
106: HttpServletRequest request) {
107:
108: if (StringUtils.isBlank((String) get("cancel"))) {
109: ActionErrors errors = super .validate(mapping, request);
110:
111: if (errors.isEmpty()) {
112: //
113: // has the page alias been used?
114: //
115: ShimPolicy policy = (ShimPolicy) StrutsUtil
116: .getPolicy(mapping);
117: Page page = policy.newPage();
118: page.setSiteContext(SiteContext.getContext(request));
119:
120: //
121: // has an alias been specified
122: //
123: String alias = (String) get("alias");
124:
125: if (!StringUtils.isBlank(alias)) {
126: if (!StringUtils.isAlphanumeric(alias))
127: errors.add("alias", new ActionError(
128: "shim.pageForm.invalidAlias"));
129:
130: else {
131: try {
132: page.loadForAlias((String) get("alias"));
133:
134: String id = (String) get("id");
135:
136: if (StringUtils.isBlank(id))
137: errors.add("alias", new ActionError(
138: "shim.pageForm.aliasUsed", page
139: .getString("title")));
140: else {
141: if (!id.equals("" + page.getInt("id")))
142: errors
143: .add(
144: "alias",
145: new ActionError(
146: "shim.pageForm.aliasUsed",
147: page
148: .getString("title")));
149: }
150: } catch (PersistableException e) {
151: //
152: // alias is not in use
153: //
154: }
155: }
156: }
157:
158: //
159: // has a template or a page to copy been selected?
160: //
161: if (StringUtils.isBlank((String) get("template"))
162: && StringUtils
163: .isBlank((String) get("copyfrom")))
164:
165: errors.add("template", new ActionError(
166: "shim.pageForm.missingTemplateOrCopy"));
167:
168: //
169: // are there quotes in the meta tags?
170: //
171: if ((((String) get("metadescription")).indexOf("\"") != -1)
172: || (((String) get("metadescription"))
173: .indexOf("'") != -1))
174: errors.add("metadescription", new ActionError(
175: "shim.pageForm.quotesInMetaDescription"));
176:
177: if ((((String) get("metakeywords")).indexOf("\"") != -1)
178: || (((String) get("metakeywords")).indexOf("'") != -1))
179: errors.add("metakeywords", new ActionError(
180: "shim.pageForm.quotesInMetaKeywords"));
181: }
182:
183: return errors;
184: }
185:
186: return new ActionErrors();
187: }
188: }
|