001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/integration/helper/integrated/AgentHelperImpl.java $
003: * $Id: AgentHelperImpl.java 9497 2006-05-15 23:46:05Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 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.sakaiproject.tool.assessment.integration.helper.integrated;
021:
022: import java.util.Collection;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: //import javax.servlet.http.HttpServletRequest;
027: //import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import org.sakaiproject.tool.cover.ToolManager;
033: import org.sakaiproject.authz.api.AuthzGroup;
034: import org.sakaiproject.authz.api.GroupNotDefinedException;
035: import org.sakaiproject.authz.cover.AuthzGroupService;
036: import org.sakaiproject.site.cover.SiteService;
037: import org.sakaiproject.user.cover.UserDirectoryService;
038: import org.sakaiproject.user.api.User;
039: import org.sakaiproject.authz.api.Role;
040:
041: import org.sakaiproject.tool.assessment.integration.helper.ifc.AgentHelper;
042: import org.sakaiproject.tool.assessment.osid.shared.impl.AgentImpl;
043: import org.sakaiproject.tool.assessment.osid.shared.impl.IdImpl; //cwen
044: import org.sakaiproject.tool.api.Placement;
045: import org.sakaiproject.tool.assessment.facade.AgentFacade;
046: import org.sakaiproject.component.cover.ServerConfigurationService;
047:
048: /**
049: *
050: * <p>Description:
051: * This is an integrated context implementation helper delegate class for
052: * the AgentFacade class. "Integrated" means that Samigo (Tests and Quizzes)
053: * is running within the context of the Sakai portal and authentication
054: * mechanisms, and therefore makes calls on Sakai for things it needs.</p>
055: * <p>Note: To customize behavior you can add your own helper class to the
056: * Spring injection via the integrationContext.xml for your context.
057: * The particular integrationContext.xml to be used is selected by the
058: * build process.
059: * </p>
060: * <p>Sakai Project Copyright (c) 2005</p>
061: * <p> </p>
062: * @author Ed Smiley <esmiley@stanford.edu>
063: * based on code originally in AgentFacade
064: *
065: *
066: */
067: public class AgentHelperImpl implements AgentHelper {
068: private static Log log = LogFactory.getLog(AgentHelperImpl.class);
069: AgentImpl agent;
070:
071: /**
072: * Get an osid Agent implementation class instance.
073: *
074: * @return an AgentImpl: osid Agent implementation class.
075: */
076: public AgentImpl getAgent() {
077: AgentImpl agent = new AgentImpl("Administrator", null,
078: new IdImpl("admin"));
079: return agent;
080: }
081:
082: /**
083: * Get the agent string.
084: * @return the agent string.
085: */
086: public String getAgentString(String agentString) {
087: String agentS = "";
088: // this is anonymous user sign 'cos sakai doesn't know about them-daisyf
089: try {
090: User user = UserDirectoryService.getCurrentUser();
091:
092: if (user == null || user.getId() == null
093: || ("").equals(user.getId())) {
094: agentS = getAnonymousId(agentString);
095: } else {
096: agentS = user.getId();
097: }
098: } catch (Exception ex) {
099: log.warn("getAgentString(): " + ex.getMessage());
100: }
101: return agentS;
102: }
103:
104: public String getEid(String agentString) {
105: String eid = "";
106: // this is anonymous user sign 'cos sakai doesn't know about them-daisyf
107: // this returns the currently logged in user's eid.
108: try {
109: User user = UserDirectoryService.getCurrentUser();
110:
111: if (user == null || user.getId() == null
112: || ("").equals(user.getId())) {
113: eid = getAnonymousId(agentString);
114: } else {
115: //log.debug("**** userEid = " + user.getEid());
116: eid = user.getEid();
117: }
118: } catch (Exception ex) {
119: log.warn("getEid: " + ex.getMessage());
120: }
121: return eid;
122: }
123:
124: /**
125: * Get the Agent Eid given an Id String.
126: * @param agentS the Agent Id string.
127: * @return the Agent Eid.
128: */
129: public String getEidById(String agentString) {
130: log.debug("getEidById agentString = " + agentString);
131: String s = "";
132: try {
133: if (!agentString.startsWith("anonymous_"))
134: s = UserDirectoryService.getUser(agentString).getEid();
135: log.debug("getEidById agentString s = " + s);
136: } catch (Exception e) {
137: log.warn("getEidById: " + e.getMessage());
138: }
139: return s;
140: }
141:
142: /**
143: * Get the Agent display name.
144: * @param agentS the Agent string.
145: * @return the Agent display name.
146: */
147: public String getDisplayName(String agentString) {
148: String s = "";
149: try {
150: if (!agentString.startsWith("anonymous_"))
151: s = UserDirectoryService.getUser(agentString)
152: .getDisplayName();
153: } catch (Exception e) {
154: log.warn("getDisplayName: " + e.getMessage());
155: }
156: return s;
157: }
158:
159: /**
160: * Get the Agent first name.
161: * @param agentString teh agent string
162: * @return the Agent first name.
163: */
164: public String getFirstName(String agentString) {
165: String s = "";
166: try {
167: if (!agentString.startsWith("anonymous_"))
168: s = UserDirectoryService.getUser(agentString)
169: .getFirstName();
170: } catch (Exception e) {
171: log.warn("getFirstName:" + e.getMessage());
172: }
173: return s;
174: }
175:
176: /**
177: * Get the Agent last name.
178: * @param agentString teh agent string
179: * @return the Agent last name.
180: */
181: public String getLastName(String agentString) {
182: String s = "";
183: try {
184: if (!agentString.startsWith("anonymous_"))
185: s = UserDirectoryService.getUser(agentString)
186: .getLastName();
187: } catch (Exception e) {
188: // if agentString is anonymous, s=""
189: log.warn("getLastName: " + e.getMessage());
190: }
191: return s;
192: }
193:
194: /**
195: * Get the Agent email.
196: * @param agentString teh agent string
197: * @return the Agent email.
198: */
199: public String getEmail(String agentString) {
200: String s = "";
201: try {
202: if (!agentString.startsWith("anonymous_"))
203: s = UserDirectoryService.getUser(agentString)
204: .getEmail();
205: } catch (Exception e) {
206: log.warn(e.getMessage());
207: }
208: return s;
209: }
210:
211: /**
212: * Can be called statically from AgentFacade from an instance
213: * @param agentString the agent string for an agent
214: * @return role string
215: */
216: public String getRole(String agentString) {
217: String role = "anonymous_access";
218: String this SiteId = null;
219: try {
220: this SiteId = ToolManager.getCurrentPlacement().getContext();
221: } catch (Exception ex) {
222: log.warn("Failure to get site id from ToolManager. \n"
223: + "Need to fix if not running in unit test.");
224: log.warn("getRole : " + ex.getMessage());
225: }
226: //cwen
227: if ((this SiteId == null) || (this SiteId.equals("")))
228: return role;
229:
230: String realmName = "/site/" + this SiteId;
231: Role userRole = null;
232:
233: try {
234: AuthzGroup siteAuthzGroup = AuthzGroupService
235: .getAuthzGroup(realmName);
236: if (siteAuthzGroup != null)
237: userRole = siteAuthzGroup.getUserRole(agentString);
238: if (userRole != null)
239: role = userRole.getId();
240: //log.debug(realmName + ":" + role);
241: } catch (Exception e) {
242: e.printStackTrace();
243: }
244: return role;
245: }
246:
247: /**
248: * Called by AgentFacade from an instance. In integrated just wrap the above.
249: * @param agentString the agent string for current AgentFacade instance
250: * @return role string
251: */
252: public String getRoleForCurrentAgent(String agentString) {
253: return this .getRole(agentString);
254: }
255:
256: /**
257: * Get the current site id.
258: * @return the site id.
259: */
260: public String getCurrentSiteId(boolean accessViaUrl) {
261: // access via url => users does not login via any sites
262: String currentSiteId = null;
263: if (!accessViaUrl) {
264: // cwen
265: Placement this Placement = ToolManager.getCurrentPlacement();
266: if (this Placement != null)
267: currentSiteId = this Placement.getContext();
268: }
269: return currentSiteId;
270: }
271:
272: // this method should live somewhere else-daisyf
273: /**
274: * Create anonymous user and return the anonymous user id.
275: * @return the anonymous user id.
276: */
277:
278: public String createAnonymous(AgentFacade agent) {
279: String anonymousId = "anonymous_";
280: try {
281: anonymousId += (new java.util.Date()).getTime();
282: agent.setAgentInstanceString(anonymousId);
283: } catch (Exception ex) {
284: log.warn("createAnonymous : " + ex.getMessage());
285: // leave... ...mostly for unit testing
286: }
287: return anonymousId;
288: }
289:
290: /**
291: * Get the current site name.
292: * @return the site name.
293: */
294: public String getCurrentSiteName(boolean accessViaUrl) {
295: // access via url => users does not login via any sites-daisyf
296: String currentSiteName = null;
297: if (!accessViaUrl) {
298: try {
299: currentSiteName = SiteService.getSite(
300: getCurrentSiteId(accessViaUrl)).getTitle();
301: } catch (Exception e) {
302: log.warn("getCurrentSiteName : " + e.getMessage());
303: }
304: }
305: return currentSiteName;
306: }
307:
308: /**
309: * Get the site name.
310: * @param siteId site id
311: * @return the site name.
312: */
313: public String getSiteName(String siteId) {
314: String siteName = null;
315: try {
316: siteName = SiteService.getSite(siteId).getTitle();
317: //log.debug("**** siteName="+siteName);
318: } catch (Exception ex) {
319: log.warn("getSiteName : " + ex.getMessage());
320: log
321: .warn("SiteService not available. "
322: + "This needs to be fixed if you are not running a unit test.");
323: }
324: return siteName;
325: }
326:
327: // should phrase out this one -daisyf
328: /**
329: * Get the display name for a specific agent id string.
330: * @param agentId the agent id string.
331: * @return the display name.
332: */
333: public String getDisplayNameByAgentId(String agentId) {
334: String name = null;
335: try {
336: name = UserDirectoryService.getUser(agentId)
337: .getDisplayName();
338: } catch (Exception e) {
339: e.printStackTrace();
340: }
341: return name;
342: }
343:
344: /**
345: * Is this a standlaone environment?
346: * @return false, in this implementation
347: */
348: public boolean isStandaloneEnvironment() {
349: return false;
350: }
351:
352: /**
353: * Is this an integrated environment?
354: * @return true, in this implementation
355: */
356: public boolean isIntegratedEnvironment() {
357: return true;
358: }
359:
360: /**
361: * Set the agent id string.
362: * @param idString the isd string.
363: */
364:
365: /**
366: * Get the anonymous user id.
367: * @return the anonymous user id.
368: */
369: public String getAnonymousId(String agentString) {
370: String agentS = "";
371: if (!UNASSIGNED_AGENT_STRING.equals(agentString)) {
372: agentS = agentString;
373: }
374: return agentS;
375: }
376:
377: /**
378: * This gets the current site id and transforms it into the realm.
379: * From there it asks the AuthzGroupService for the roles of the given users
380: *
381: * @param inUsers the Collection of users who have their roles looked up.
382: * This is a Collection of userId Strings
383: * @return Returns the map of users as keys and their roles as values.
384: * If the user is not in the realm then they will have a null role.
385: */
386: public Map getUserRolesFromContextRealm(Collection inUsers) {
387: //Get the SiteId
388: String this SiteId = null;
389: try {
390: this SiteId = ToolManager.getCurrentPlacement().getContext();
391: } catch (Exception ex) {
392: log.warn("Failure to get site id from ToolManager. \n"
393: + "Need to fix if not running in unit test.");
394: log.warn("getUserRolesFromContextRealm : "
395: + ex.getMessage());
396: }
397: //If none the returna blank map
398: if (this SiteId == null)
399: return new HashMap();
400:
401: //create the realm from the site
402: String realmName = "/site/" + this SiteId;
403:
404: //get the roles from the realm and set of users
405: return AuthzGroupService.getUsersRole(inUsers, realmName);
406: }
407:
408: //cwen
409: public String getRoleForAgentAndSite(String agentString,
410: String siteId) {
411: String role = "anonymous_access";
412:
413: if (siteId == null)
414: return role;
415:
416: String realmName = "/site/" + siteId;
417: Role userRole = null;
418:
419: try {
420: AuthzGroup siteAuthzGroup = AuthzGroupService
421: .getAuthzGroup(realmName);
422: if (siteAuthzGroup != null)
423: userRole = siteAuthzGroup.getUserRole(agentString);
424: if (userRole != null)
425: role = userRole.getId();
426: log.debug(realmName + ":" + role);
427: } catch (GroupNotDefinedException e) {
428: log.error("error in:" + this + "-getRoleForAgnetAndSite");
429: }
430: return role;
431: }
432:
433: /**
434: * This is a kludge to work around a JSF scriptlet dependency introduced by cwen
435: * on org.sakaiproject.component.cover.ServerConfigurationService.
436: * @todo for 2.2 remove method when done with refactor.
437: * @deprecated
438: *
439: * @return true unless it is turned off
440: */
441: public boolean isFileUploadAvailable() {
442: String commentOutFileUpload = ServerConfigurationService
443: .getString("sam_file_upload_comment_out");
444:
445: if (commentOutFileUpload == null)
446: return true;
447:
448: return !commentOutFileUpload.equalsIgnoreCase("true");
449: }
450: }
|