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.reg;
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.DynaActionForm;
028: import org.apache.struts.action.ActionErrors;
029: import org.apache.struts.action.ActionError;
030: import org.apache.struts.validator.DynaValidatorForm;
031: import com.methodhead.aikp.AikpForm;
032: import com.methodhead.auth.AuthUtil;
033: import java.util.List;
034: import java.util.ArrayList;
035: import java.util.Iterator;
036: import org.apache.struts.Globals;
037: import org.apache.struts.util.LabelValueBean;
038: import org.apache.struts.util.MessageResources;
039: import com.methodhead.sitecontext.SiteContext;
040: import com.methodhead.util.StrutsUtil;
041: import com.methodhead.util.OperationContext;
042: import org.apache.commons.lang.StringUtils;
043:
044: public class RolesForm extends DynaValidatorForm implements
045: Serializable {
046:
047: /**
048: * Populates <code>form.roleOptions</code> with roles returned by {@link
049: * RegPolicy#getRoleOptions RegPolicy.getRoleOptions()}, fetching message
050: * resources for labels where possible. Populates
051: * <code>form.siteOptions</code> with options for each site in the database.
052: */
053: public void reset(ActionMapping mapping, HttpServletRequest request) {
054:
055: RegPolicy policy = (RegPolicy) StrutsUtil.getPolicy(mapping);
056:
057: //
058: // create a op
059: //
060: OperationContext op = new OperationContext(mapping, this ,
061: request, null, AuthUtil.getUser(request));
062:
063: //
064: // get role options
065: //
066: List options = policy.getRoleOptions(op);
067:
068: //
069: // look up message resources
070: //
071: MessageResources resources = (MessageResources) request
072: .getAttribute(Globals.MESSAGES_KEY);
073:
074: for (Iterator iter = options.iterator(); iter.hasNext();) {
075: LabelValueBean lv = (LabelValueBean) iter.next();
076:
077: String label = resources.getMessage(lv.getLabel());
078:
079: if (label != null)
080: lv.setLabel(label);
081: }
082:
083: set("roleOptions", options);
084:
085: //
086: // get site context options
087: //
088: options = new ArrayList();
089: options.add(new LabelValueBean("Select...", ""));
090:
091: List siteContexts = SiteContext.loadAll();
092:
093: for (Iterator iter = siteContexts.iterator(); iter.hasNext();) {
094: SiteContext sc = (SiteContext) iter.next();
095:
096: String label = (String) sc.getDomains().get(0);
097: if (!StringUtils.isBlank(sc.getString("path")))
098: label += "/" + sc.getString("path");
099:
100: options
101: .add(new LabelValueBean(label, "" + sc.getInt("id")));
102: }
103:
104: set("siteOptions", options);
105: }
106:
107: /**
108: * Overrides default to bypass validation if cancel was clicked.
109: */
110: public ActionErrors validate(ActionMapping mapping,
111: HttpServletRequest request) {
112:
113: //
114: // no errors if we're cancelling
115: //
116: if (!StringUtils.isBlank((String) get("cancel"))) {
117: return new ActionErrors();
118: }
119:
120: return super.validate(mapping, request);
121: }
122: }
|