/** * Copyright © 2001 The JA-SIG Collaborative. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by the JA-SIG Collaborative * (http://www.jasig.org/)." * * THIS SOFTWARE IS PROVIDED BY THE JA-SIG COLLABORATIVE "AS IS" AND ANY * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JA-SIG COLLABORATIVE OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * */ package org.jasig.portal; import java.util.Iterator; import org.apache.xpath.XPathAPI; import org.jasig.portal.groups.IEntity; import org.jasig.portal.Applicant; import org.jasig.portal.groups.IEntityGroup; import org.jasig.portal.groups.IGroupMember; import org.jasig.portal.groups.IEntityGroupStore; import org.jasig.portal.groups.EntityGroupStoreFactory; import org.jasig.portal.properties.PropertiesManager; import org.jasig.portal.security.provider.AccountManager; import org.jasig.portal.services.GroupService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jasig.portal.utils.DocumentFactory; import org.jasig.portal.utils.SmartCache; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * Provides a list of and otherwise manages current applicants for graduate admission. * @author rtwigg@uccs.edu */ public class ApplicantManager { private static final Log log = LogFactory.getLog(ApplicantManager.class); protected static final IApplicantStore as = ApplicantStoreFactory.getApplicantStoreImpl(); protected static final IEntityGroupStore egs = EntityGroupStoreFactory.getEntityGroupStoreImpl(); // Cache timeout property protected static final int applicantCacheTimeout = PropertiesManager.getPropertyAsInt("org.jasig.portal.ApplicantManager.applicant_cache_timeout"); // I18n property protected static final boolean localeAware = PropertiesManager.getPropertyAsBoolean("org.jasig.portal.i18n.LocaleManager.locale_aware"); // Caches protected static final SmartCache applicantCache = new SmartCache(applicantCacheTimeout); // Cache keys private static final String APPLICANT_CACHE_KEY = "ApplicantCacheKey"; // Permission constants private static final String FRAMEWORK_OWNER = "UP_FRAMEWORK"; private static final String SUBSCRIBER_ACTIVITY = "SUBSCRIBE"; private static final String GRANT_PERMISSION_TYPE = "GRANT"; /** * Returns a copy of the applicant listing as a Document. * @return a copy of the applicant listing as a Document */ public static Document getApplicantListing() throws PortalException { Document applicantListing = (Document)applicantCache.get(APPLICANT_CACHE_KEY); if (applicantListing == null) { // Applicant listing has expired, so get it and cache it try { applicantListing = getApplicantListingXML(); } catch (Exception e) { throw new PortalException(e); } if (applicantListing != null) { applicantCache.put(APPLICANT_CACHE_KEY, applicantListing); log.info( "Caching applicant listing."); } } return applicantListing; } /** * Returns an XML document which describes the applicant listing. * @return doc the applicant listing document * @throws java.lang.Exception */ public static Document getApplicantListingXML() throws Exception { Document doc = DocumentFactory.getNewDocument(); Element appListing = doc.createElement("appListing"); doc.appendChild(appListing); // Get current applicants Applicant [] applicants = as.getApplicants(); for (int i = 0; i < applicants.length; i++) { Applicant applicant = applicants[i]; Element applicantE = applicant.getDocument(doc); applicantE = (Element)doc.importNode(applicantE, true); appListing.appendChild(applicantE); } return doc; } /** * Looks in applicant listing for an applicant matching the * given userName. * @param userName the user name * @return the applicant element matching the specified user name * @throws PortalException */ public static Element getApplicantDetails (String userName) throws PortalException { Document applicantListing = getApplicantListing(); Element applicantE = null; try { // This is unfortunately dependent on Xalan 2. Is there a way to use a standard interface? applicantE = (Element)XPathAPI.selectSingleNode(applicantListing, "(//applicant[@userName = '" + userName + "'])[1]"); } catch (javax.xml.transform.TransformerException te) { throw new GeneralRenderingException("Not able to find applicant " + userName + " within applicant listing: " + te.getMessageAndLocation()); } return applicantE; } /** * Lists an applicant. * @param applicant the applicant XML fragment * @throws java.lang.Exception */ public static void listApplicant (Element applicantE) throws Exception { // Reset the applicant cache if (applicantCache != null) { applicantCache.remove(APPLICANT_CACHE_KEY); } Applicant applicant = null; // Use current user name if modifying a previously listed applicant, otherwise get a new user name String firstName = applicantE.getAttribute("firstName"); String userName = applicantE.getAttribute("userName"); firstName = applicantE.getAttribute("firstName"); String lastName = applicantE.getAttribute("lastName"); String passwd = null; if (userName.length() == 0) { applicant=as.newApplicant(firstName, lastName); applicantE.setAttribute("userName", applicant.getUserName()); log.debug("Attempting to list new applicant " + userName + "..."); } else { applicant = as.getApplicant(userName); log.debug("Attempting to modify applicant " + userName + "..."); } // Add applicant applicant.setFromXML(applicantE); as.putApplicant(applicant); } /** * Notifies graduate committee members a new applicant is ready for committee evaluation. * @param applicant the applicant XML fragment * @throws java.lang.Exception */ public static void notifyCommittee (Element applicantE) throws Exception { String msg = "There is a new applicant ready for graduate committee evaluation."; // Find all graduate committee members & give notification that a new applicant is ready for evaluation try { IEntityGroup gcomm = GroupService.getDistinguishedGroup(GroupService.GRAD_COMMITTEE); Iterator gcmembers = egs.findEntitiesForGroup(gcomm); while (gcmembers.hasNext()) { IEntity gcmember = (IEntity)gcmembers.next(); String userName = gcmember.getKey(); AccountManager am = new AccountManager(); am.updateUserMessage(userName, msg); } } catch (Exception e) { log.error(e, e); } } }