001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.user.web;
018:
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021:
022: import org.apache.commons.lang.StringUtils;
023: import org.apache.struts.action.ActionForm;
024: import org.apache.struts.action.ActionForward;
025: import org.apache.struts.action.ActionMapping;
026: import org.apache.struts.action.ActionMessage;
027: import org.apache.struts.action.ActionMessages;
028:
029: import edu.iu.uis.eden.KEWServiceLocator;
030: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
031: import edu.iu.uis.eden.user.UserService;
032: import edu.iu.uis.eden.user.WorkflowUser;
033: import edu.iu.uis.eden.user.WorkflowUserId;
034: import edu.iu.uis.eden.web.WorkflowAction;
035:
036: /**
037: * A Struts Action which provides reporting, editing, and creation of users.
038: *
039: * @see UserService
040: * @see WorkflowUser
041: *
042: * @author ewestfal
043: */
044: public class WorkflowUserAction extends WorkflowAction {
045:
046: public ActionForward start(ActionMapping mapping, ActionForm form,
047: HttpServletRequest request, HttpServletResponse response)
048: throws Exception {
049: return createNew(mapping, form, request, response);
050: }
051:
052: /**
053: * Creates a new WorkflowUser and populates it in the form.
054: */
055: public ActionForward createNew(ActionMapping mapping,
056: ActionForm form, HttpServletRequest request,
057: HttpServletResponse response) throws Exception {
058: WorkflowUserForm userForm = (WorkflowUserForm) form;
059: userForm.setUser(new WebWorkflowUser(KEWServiceLocator
060: .getUserService().getBlankUser()));
061: return mapping.findForward("basic");
062: }
063:
064: /**
065: * Initiates the editing of an existing WorkflowUser.
066: */
067: public ActionForward edit(ActionMapping mapping, ActionForm form,
068: HttpServletRequest request, HttpServletResponse response)
069: throws Exception {
070: WorkflowUserForm userForm = (WorkflowUserForm) form;
071: userForm
072: .setUser(new WebWorkflowUser(userForm.getExistingUser()));
073: userForm.setShowEdit("yes");
074: return mapping.findForward("basic");
075: }
076:
077: public ActionForward save(ActionMapping mapping, ActionForm form,
078: HttpServletRequest request, HttpServletResponse response)
079: throws Exception {
080: WorkflowUserForm userForm = (WorkflowUserForm) form;
081: // apply changes to the user and then save it
082: WorkflowUser modifiedUser = applyChanges(userForm);
083: getUserService().save(modifiedUser);
084: userForm.setShowEdit("no");
085: saveDocumentActionMessage("user.IUUserService.saved", userForm
086: .getUser().getDisplayName(), request);
087: return mapping.findForward("summary");
088: }
089:
090: public ActionForward report(ActionMapping mapping, ActionForm form,
091: HttpServletRequest request, HttpServletResponse response)
092: throws Exception {
093: WorkflowUserForm userForm = (WorkflowUserForm) form;
094: // the existing user gets set up by establish required state
095: userForm.setUser(userForm.getExistingUser());
096: // now clear out the existing user so it doesn't show up on the report form
097: userForm.setExistingUser(null);
098: return mapping.findForward("report");
099: }
100:
101: /**
102: * Applies the changes made to the given WebWorkflowUser on the form to the underlying user object
103: * and returns the modified user object. This could be a newly created user object if
104: * this is a new user creation.
105: */
106: private WorkflowUser applyChanges(WorkflowUserForm form)
107: throws Exception {
108: WorkflowUser user = form.getUser();
109: WorkflowUser modifiedUser = null;
110: if (user.getWorkflowId() != null
111: && user.getWorkflowUserId() != null
112: && !user.getWorkflowUserId().isEmpty()) {
113: try {
114: WorkflowUser existingUser = getUserService()
115: .getWorkflowUser(user.getWorkflowUserId());
116: if (existingUser != null) {
117: modifiedUser = getUserService().copy(existingUser,
118: true);
119: }
120: } catch (EdenUserNotFoundException e) {
121: // we didn't find an existing user for the given id
122: }
123: }
124: if (modifiedUser == null) {
125: modifiedUser = KEWServiceLocator.getUserService()
126: .getBlankUser();
127: }
128: // copy the data from the form onto the existing user
129: modifiedUser.setAuthenticationUserId(user
130: .getAuthenticationUserId());
131: modifiedUser.setDisplayName(user.getDisplayName());
132: modifiedUser.setEmailAddress(user.getEmailAddress());
133: modifiedUser.setEmplId(user.getEmplId());
134: modifiedUser.setGivenName(user.getGivenName());
135: modifiedUser.setLastName(user.getLastName());
136: modifiedUser.setUuId(user.getUuId());
137: return modifiedUser;
138: }
139:
140: /**
141: * Do we really want to be deleting users?
142: *
143: * public ActionForward delete(ActionMapping mapping, ActionForm form,
144: * HttpServletRequest request, HttpServletResponse response) throws
145: * Exception { WorkflowUserForm userForm = (WorkflowUserForm) form;
146: * getUserService().delete(userForm.getUser()); userForm.setShowEdit("no");
147: * saveDocumentActionMessage("user.IUUserService.deleted",
148: * userForm.getUser().getDisplayName(), request); return
149: * mapping.findForward("summary"); }
150: */
151:
152: public ActionForward clear(ActionMapping mapping, ActionForm form,
153: HttpServletRequest request, HttpServletResponse response)
154: throws Exception {
155: WorkflowUserForm userForm = (WorkflowUserForm) form;
156: if (userForm.getExistingUser() != null) {
157: userForm.setUser(new WebWorkflowUser(userForm
158: .getExistingUser()));
159: } else {
160: userForm.setUser(new WebWorkflowUser(KEWServiceLocator
161: .getUserService().getBlankUser()));
162: }
163: return mapping.findForward("basic");
164: }
165:
166: private void saveDocumentActionMessage(String messageKey,
167: String insertValue, HttpServletRequest request) {
168: ActionMessages messages = new ActionMessages();
169: messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
170: messageKey, insertValue));
171: saveMessages(request, messages);
172: }
173:
174: /**
175: * Sets up the existing user on the form. The existing user is always the user which exists in the database
176: * with the given workflow id.
177: */
178: public ActionMessages establishRequiredState(
179: HttpServletRequest request, ActionForm form)
180: throws Exception {
181: request.getSession().getServletContext().setAttribute(
182: "UserCaps",
183: KEWServiceLocator.getUserService().getCapabilities());
184: WorkflowUserForm userForm = (WorkflowUserForm) form;
185: WorkflowUser workflowUser = null;
186: if (!StringUtils.isEmpty(userForm.getWorkflowId())) {
187: workflowUser = getUserService().getWorkflowUser(
188: new WorkflowUserId(userForm.getWorkflowId()));
189: if (workflowUser == null) {
190: throw new Exception(
191: "Could not locate user for the given workflow id: "
192: + userForm.getWorkflowId());
193: }
194: userForm.setExistingUser(new WebWorkflowUser(workflowUser));
195: }
196: return null;
197: }
198:
199: public UserService getUserService() {
200: return KEWServiceLocator.getUserService();
201: }
202:
203: }
|