001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/tool/src/java/org/theospi/portfolio/presentation/control/AddViewerController.java $
003: * $Id:AddViewerController.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.theospi.portfolio.presentation.control;
021:
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.regex.Pattern;
027:
028: import javax.mail.internet.AddressException;
029: import javax.mail.internet.InternetAddress;
030:
031: import org.sakaiproject.metaobj.shared.model.Agent;
032: import org.sakaiproject.metaobj.shared.model.impl.AgentImpl;
033: import org.sakaiproject.metaobj.utils.Config;
034: import org.springframework.validation.BindException;
035: import org.springframework.validation.Errors;
036: import org.springframework.web.servlet.ModelAndView;
037: import org.theospi.portfolio.presentation.model.Presentation;
038:
039: public class AddViewerController extends AbstractPresentationController {
040:
041: private static final Pattern emailPattern = Pattern
042: .compile(".*@.*");
043:
044: public ModelAndView handleRequest(Object requestModel, Map request,
045: Map session, Map application, Errors errors) {
046: AgentImpl viewer = (AgentImpl) requestModel;
047: List viewers = null;
048: boolean isGuest = request.get("isGuest") != null
049: && request.get("isGuest").equals("true");
050:
051: if (Config.getInstance().getProperties().getProperty(
052: "allowGuests").equals("true")
053: && isGuest) {
054: viewers = findByEmailOrDisplayName(viewer.getDisplayName());
055: if (viewers.size() == 0) {
056: if (validateEmail(viewer, errors)) {
057: viewer.setRole(Agent.ROLE_GUEST);
058: viewer.setId(getIdManager().getId(
059: viewer.getDisplayName()));
060: viewers.add(viewer);
061: }
062: }
063: } else {
064: viewers = getAgentManager().findByProperty("displayName",
065: viewer.getDisplayName());
066: }
067:
068: if (viewers != null && viewers.size() == 1) {
069: Presentation presentation = (Presentation) session
070: .get("presentation");
071: presentation.getViewers().add(viewers.get(0));
072: } else {
073: errors.rejectValue("displayName", "unknown user",
074: "unknown user: " + viewer.getDisplayName());
075: }
076:
077: request.put("presentation", session.get("presentation"));
078: request.put(BindException.ERROR_KEY_PREFIX + "viewer", errors);
079: BindException newErrors = new BindException(session
080: .get("presentation"), "presentation");
081: request.put(BindException.ERROR_KEY_PREFIX + "presentation",
082: newErrors);
083:
084: return new ModelAndView("success", referenceData(request,
085: session.get("presentation"), newErrors));
086: }
087:
088: protected boolean validateEmail(AgentImpl viewer, Errors errors) {
089: String email = viewer.getDisplayName();
090: if (!emailPattern.matcher(email).matches()) {
091: errors.rejectValue("displayName", "Invalid email address",
092: new Object[0], "Invalid email address");
093: return false;
094: }
095:
096: try {
097: InternetAddress.parse(email, true);
098: } catch (AddressException e) {
099: errors.rejectValue("displayName", "Invalid email address",
100: new Object[0], "Invalid email address");
101: return false;
102: }
103:
104: return true;
105: }
106:
107: public Map referenceData(Map request, Object command, Errors errors) {
108: Map model = new HashMap();
109: model.put("viewer", new AgentImpl());
110: //if (!errors.hasFieldErrors("displayName")){
111: // model.put(BindException.ERROR_KEY_PREFIX + "viewer", new BindException(viewer,"viewer"));
112: //}
113: String siteId = getWorksiteManager().getCurrentWorksiteId()
114: .getValue();
115:
116: String filter = (String) request.get("filter");
117: if (filter != null) {
118: List members = getAgentManager().getWorksiteAgents(siteId);
119: model.put("members", members);
120: model.put("filter", filter);
121: }
122:
123: model.put("roles", getAgentManager().getWorksiteRoles(siteId));
124: return model;
125: }
126:
127: /**
128: *
129: * @param displayName - for a guest user, this is the email address
130: * @return
131: */
132: protected List findByEmailOrDisplayName(String displayName) {
133: List retVal = new ArrayList();
134:
135: List guestUsers = getAgentManager().findByProperty("email",
136: displayName);
137:
138: if (guestUsers == null || guestUsers.size() == 0) {
139: guestUsers = getAgentManager().findByProperty(
140: "displayName", displayName);
141: }
142:
143: if (guestUsers != null) {
144: retVal.addAll(guestUsers);
145: }
146:
147: return retVal;
148: }
149: }
|