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 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.DynaActionForm;
027: import org.apache.struts.action.ActionForward;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import com.methodhead.aikp.AikpAction;
032: import com.methodhead.aikp.AutoIntKeyPersistable;
033: import com.methodhead.aikp.IntKey;
034: import com.methodhead.util.OperationContext;
035: import com.methodhead.sitecontext.SiteContext;
036: import com.methodhead.util.StrutsUtil;
037: import com.methodhead.event.Event;
038: import com.methodhead.auth.AuthUser;
039: import com.methodhead.auth.AuthUtil;
040: import com.methodhead.auth.AuthAction;
041: import java.util.Iterator;
042: import java.util.List;
043: import org.apache.commons.lang.StringUtils;
044:
045: public class ProfileAction extends AuthAction {
046:
047: // constructors /////////////////////////////////////////////////////////////
048:
049: // constants ////////////////////////////////////////////////////////////////
050:
051: // classes //////////////////////////////////////////////////////////////////
052:
053: // methods //////////////////////////////////////////////////////////////////
054:
055: protected void populateForm(DynaActionForm form,
056: AutoIntKeyPersistable persistable) {
057:
058: User user = (User) persistable;
059:
060: form.set("id", "" + user.getInt("id"));
061: form.set("firstname", user.getContact().getString("firstname"));
062: form.set("middlename", user.getContact()
063: .getString("middlename"));
064: form.set("lastname", user.getContact().getString("lastname"));
065: form.set("company", user.getContact().getString("company"));
066: form.set("address1", user.getContact().getString("address1"));
067: form.set("address2", user.getContact().getString("address2"));
068: form.set("city", user.getContact().getString("city"));
069: form.set("state", user.getContact().getString("state"));
070: form.set("zip", user.getContact().getString("zip"));
071: form.set("country", user.getContact().getString("country"));
072: form.set("phone", user.getContact().getString("phone"));
073: form.set("fax", user.getContact().getString("fax"));
074: form.set("email", user.getContact().getString("email"));
075: form.set("url", user.getContact().getString("url"));
076: form.set("password", "");
077: form.set("verifypassword", "");
078: }
079:
080: protected void populatePersistable(
081: AutoIntKeyPersistable persistable, DynaActionForm form) {
082:
083: User user = (User) persistable;
084:
085: user.getContact().setString("firstname",
086: (String) form.get("firstname"));
087: user.getContact().setString("middlename",
088: (String) form.get("middlename"));
089: user.getContact().setString("lastname",
090: (String) form.get("lastname"));
091: user.getContact().setString("company",
092: (String) form.get("company"));
093: user.getContact().setString("address1",
094: (String) form.get("address1"));
095: user.getContact().setString("address2",
096: (String) form.get("address2"));
097: user.getContact().setString("city", (String) form.get("city"));
098: user.getContact()
099: .setString("state", (String) form.get("state"));
100: user.getContact().setString("zip", (String) form.get("zip"));
101: user.getContact().setString("country",
102: (String) form.get("country"));
103: user.getContact()
104: .setString("phone", (String) form.get("phone"));
105: user.getContact().setString("fax", (String) form.get("fax"));
106: user.getContact()
107: .setString("email", (String) form.get("email"));
108: user.getContact().setString("url", (String) form.get("url"));
109: }
110:
111: protected ActionForward doProfileForm(OperationContext op,
112: Object policy) {
113:
114: populateForm(op.form, (User) op.user);
115:
116: return op.mapping.findForward("form");
117: }
118:
119: protected ActionForward doProfile(OperationContext op, Object policy) {
120:
121: if (!StringUtils.isBlank((String) op.form.get("cancel")))
122: return op.mapping.findForward("cancelled");
123:
124: User user = (User) op.user;
125:
126: populatePersistable(user, op.form);
127:
128: user.save();
129:
130: StrutsUtil.addMessage(op.request, "reg.profile.profileupdated",
131: null, null, null);
132:
133: Event.log(SiteContext.getDefaultContext(), user.getLogin(),
134: "reg", "Updated profile.");
135:
136: return new ActionForward(op.mapping.getInput());
137: }
138:
139: protected ActionForward doPasswordForm(OperationContext op,
140: Object policy) {
141:
142: op.form.set("oldpassword", "");
143: op.form.set("password", "");
144: op.form.set("verifypassword", "");
145:
146: return op.mapping.findForward("form");
147: }
148:
149: protected ActionForward doPassword(OperationContext op,
150: Object policy) {
151:
152: if (!StringUtils.isBlank((String) op.form.get("cancel")))
153: return op.mapping.findForward("cancelled");
154:
155: User user = (User) op.user;
156:
157: user.setString("password", (String) op.form.get("password"));
158: user.save();
159:
160: Event.log(SiteContext.getDefaultContext(), user.getLogin(),
161: "reg", "Updated password.");
162:
163: StrutsUtil.addMessage(op.request,
164: "reg.profile.passwordupdated", null, null, null);
165:
166: return op.mapping.findForward("success");
167: }
168:
169: public ActionForward doExecute(ActionMapping mapping,
170: ActionForm form, HttpServletRequest request,
171: HttpServletResponse response) {
172:
173: //
174: // get some things we'll need
175: //
176: DynaActionForm dynaForm = (DynaActionForm) form;
177: Object policy = StrutsUtil.getPolicy(mapping);
178: AuthUser user = AuthUtil.getUser(request);
179:
180: OperationContext op = new OperationContext(mapping, dynaForm,
181: request, response, user);
182:
183: //
184: // execute the appopriate method
185: //
186: if (mapping.getPath().equals("/profileForm")) {
187: return doProfileForm(op, policy);
188: }
189: if (mapping.getPath().equals("/profile")) {
190: return doProfile(op, policy);
191: }
192: if (mapping.getPath().equals("/passwordForm")) {
193: return doPasswordForm(op, policy);
194: }
195: if (mapping.getPath().equals("/password")) {
196: return doPassword(op, policy);
197: }
198:
199: throw new RuntimeException("Unexpected mapping path \""
200: + mapping.getPath() + "\".");
201: }
202:
203: // properties ///////////////////////////////////////////////////////////////
204:
205: // attributes ///////////////////////////////////////////////////////////////
206: }
|