Skip to content

Commit

Permalink
COURSESPLT-16
Browse files Browse the repository at this point in the history
Integrate the Degree Progress code provided by Oakland.
  • Loading branch information
waymirec committed Jan 22, 2013
1 parent 039e169 commit a57b1f7
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 16 deletions.
4 changes: 4 additions & 0 deletions courses-portlet-api/pom.xml
Expand Up @@ -16,6 +16,10 @@
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
@@ -0,0 +1,93 @@
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jasig.portlet.degreeprogress.dao;

import javax.portlet.PortletRequest;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portlet.degreeprogress.model.xml.DegreeProgressReport;

/**
* This {@link org.jasig.portlet.degreeprogress.dao.IDegreeProgressDao} implementation decorates one or more other DAOs and
* adds caching features to them. Whatever data the underlying DAOs provide
* will be stored in cache according to parameters specified in ehcache.xml.
* All caching is per-user.
*
* @author Chris Waymire, chris@waymire.net
*/
public class CachingDegreeProgressDao implements IDegreeProgressDao {
private final Log log = LogFactory.getLog(getClass());

/*
* Spring-wired dependencies
*/

private IDegreeProgressDao enclosedDegreeProgressDao;

public void setEnclosedDegreeProgressDao(IDegreeProgressDao enclosedDegreeProgressDao) {
this.enclosedDegreeProgressDao = enclosedDegreeProgressDao;
}

private Cache progressReportCache;

public void setProgressReportCache(Cache progressReportCache) {
this.progressReportCache = progressReportCache;
}

@Override
public DegreeProgressReport getProgressReport(PortletRequest request) {
DegreeProgressReport rslt = null;
String cacheKey = getDegreeProgressCacheKey(request);
Element m = progressReportCache.get(cacheKey);

if (m != null) {
rslt = (DegreeProgressReport) m.getValue();
} else {
if (log.isDebugEnabled()) {
log.debug("Fetching new DegreeProgressReport from enclosedDegreeProgressDao for user '" + request.getRemoteUser() + "'");
}
rslt = enclosedDegreeProgressDao.getProgressReport(request);
m = new Element(cacheKey, rslt);
progressReportCache.put(m);
}
return rslt;
}

@Override
public Boolean getWebEnabled(PortletRequest request) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public WhatIfRequest createWhatIfRequest(PortletRequest request) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

@Override
public DegreeProgressReport getWhatIfReport(WhatIfRequest whatIfRequest) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

private String getDegreeProgressCacheKey(PortletRequest request) {
return CachingDegreeProgressDao.class.getName() + "." + request.getRemoteUser();

}
}
Expand Up @@ -8,7 +8,6 @@
import org.jasig.portlet.degreeprogress.dao.ProgramInformation;
import org.springframework.stereotype.Service;

@Service
public class MockDegreeProgramDaoImpl implements IDegreeProgramDao {

public DegreeProgramSummary getProgramSummary(PortletRequest request) {
Expand Down
Expand Up @@ -23,7 +23,6 @@
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

@Service
public class MockDegreeProgressDaoImpl implements IDegreeProgressDao, InitializingBean {

protected final Log log = LogFactory.getLog(getClass());
Expand Down
@@ -0,0 +1,167 @@
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jasig.portlet.degreeprogress.dao.xml;

import java.util.HashMap;
import java.util.Map;
import javax.portlet.PortletRequest;
import javax.xml.bind.JAXBElement;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portlet.courses.util.IParameterEvaluator;
import org.jasig.portlet.degreeprogress.dao.IDegreeProgressDao;
import org.jasig.portlet.degreeprogress.dao.WhatIfRequest;
import org.jasig.portlet.degreeprogress.model.StudentCourseRegistration;
import org.jasig.portlet.degreeprogress.model.xml.Course;
import org.jasig.portlet.degreeprogress.model.xml.CourseRequirement;
import org.jasig.portlet.degreeprogress.model.xml.DegreeProgressReport;
import org.jasig.portlet.degreeprogress.model.xml.DegreeRequirementSection;
import org.jasig.portlet.degreeprogress.model.xml.GeneralRequirementType;
import org.jasig.portlet.degreeprogress.model.xml.GpaRequirement;
import org.jasig.portlet.degreeprogress.model.xml.Grade;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;

/**
* HttpDegreeProgressDaoImpl retrieves degree progress from a Basic Authentication
* protected XML feed.
*
* @author Chris Waymire, chris@waymire.net
*/
public class HttpDegreeProgressDaoImpl implements IDegreeProgressDao {
private final Log log = LogFactory.getLog(getClass());

private IParameterEvaluator usernameEvaluator;

public void setUsernameEvaluator(IParameterEvaluator usernameEvaluator) {
this.usernameEvaluator = usernameEvaluator;
}

private IParameterEvaluator passwordEvaluator;

private Map<String,IParameterEvaluator> urlParams = new HashMap<String,IParameterEvaluator>();

public void setUrlParams(Map<String,IParameterEvaluator> params) {
this.urlParams = params;
}

public void setPasswordEvaluator(IParameterEvaluator passwordEvaluator) {
this.passwordEvaluator = passwordEvaluator;
}

private RestTemplate restTemplate;

public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

private String degreeProgressUrlFormat = null;

public void setDegreeProgressUrlFormat(String urlFormat) {
this.degreeProgressUrlFormat = urlFormat;
}

@Override
public DegreeProgressReport getProgressReport(PortletRequest request) {
Map<String,String> params = createParameters(request, urlParams);
if (log.isDebugEnabled()) {
log.debug("Invoking uri '" + degreeProgressUrlFormat + "' with the following parameters: " + params.toString());
}

HttpEntity<?> requestEntity = getRequestEntity(request);
HttpEntity<DegreeProgressReport> response = restTemplate.exchange(
degreeProgressUrlFormat, HttpMethod.GET, requestEntity,
DegreeProgressReport.class, params);

DegreeProgressReport report = response.getBody();
for (DegreeRequirementSection section : report.getDegreeRequirementSections()) {
for (JAXBElement<? extends GeneralRequirementType> requirement : section.getGeneralRequirements()) {
GeneralRequirementType req = requirement.getValue();
if (req instanceof GpaRequirement) {
section.setRequiredGpa(((GpaRequirement) req).getRequiredGpa());
}
}
for (CourseRequirement req : section.getCourseRequirements()) {
for (Course course : req.getCourses()) {
StudentCourseRegistration registration = new StudentCourseRegistration();
registration.setCredits(course.getCredits());
registration.setSource(course.getSource());
registration.setSemester(course.getSemester());
registration.setCourse(course);
Grade grade = new Grade();
grade.setCode(course.getGrade().getCode());
registration.setGrade(grade);
req.getRegistrations().add(registration);
}
}
report.addSection(section);
}
return report;
}

@Override
public Boolean getWebEnabled(PortletRequest request) {
return Boolean.TRUE;
}

@Override
public WhatIfRequest createWhatIfRequest(PortletRequest request) {
return null;
}

@Override
public DegreeProgressReport getWhatIfReport(WhatIfRequest whatIfRequest) {
return null;
}

/**
* Get a request entity prepared for basic authentication.
*/
protected HttpEntity<?> getRequestEntity(PortletRequest request) {

String username = usernameEvaluator.evaluate(request);
String password = passwordEvaluator.evaluate(request);

if (log.isDebugEnabled()) {
boolean hasPassword = password != null;
log.debug("Preparing HttpEntity for user '" + username + "' (password provided = " + hasPassword + ")");
}

HttpHeaders requestHeaders = new HttpHeaders();
String authString = username.concat(":").concat(password);
String encodedAuthString = new Base64().encodeToString(authString.getBytes());
requestHeaders.set("Authorization", "Basic ".concat(encodedAuthString));

HttpEntity<?> rslt = new HttpEntity<Object>(requestHeaders);
return rslt;

}

protected Map<String,String> createParameters(PortletRequest request,Map<String,IParameterEvaluator> params) {
Map<String,String> result = new HashMap<String,String>();

for(String key : params.keySet()) {
result.put(key,params.get(key).evaluate(request));
}
return result;
}
}
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import org.apache.commons.lang.StringUtils;
import org.jasig.portlet.courses.dao.ICoursesDao;
Expand All @@ -27,7 +28,9 @@
@Controller
@RequestMapping("VIEW")
public class DegreeProgressController {

public static final String PREFERENCE_INFORMATION_LINK = "DegreeProgress.INFORMATION_LINK";
public static final String PREFERENCE_INFORMATION_LINK_DEFAULT = "#;";

private IDegreeProgressDao degreeProgressDao;

@Autowired(required = true)
Expand All @@ -52,7 +55,11 @@ public void setCoursesDao(ICoursesDao coursesDao) {
@RequestMapping
public ModelAndView getMainView(PortletRequest request) {
Map<String, Object> model = new HashMap<String, Object>();
PortletPreferences preferences = request.getPreferences();
model.put("terms", degreeProgramDao.getEntryTerms());
model.put("infoLink",preferences.getValue(PREFERENCE_INFORMATION_LINK,PREFERENCE_INFORMATION_LINK_DEFAULT));
return new ModelAndView("selectReport",model);
/*
Boolean webEnabled = degreeProgressDao.getWebEnabled(request);
if (webEnabled == null) {
return new ModelAndView("error", model);
Expand All @@ -62,7 +69,7 @@ public ModelAndView getMainView(PortletRequest request) {
} else {
return new ModelAndView("selectWhatIf", model);
}

*/
}

@RequestMapping(params = "action=getScenario")
Expand Down
4 changes: 4 additions & 0 deletions courses-portlet-webapp/src/main/resources/ehcache.xml
Expand Up @@ -37,4 +37,8 @@
overflowToDisk="false" diskPersistent="false" timeToLiveSeconds="900"
memoryStoreEvictionPolicy="LRU" statistics="true" />

<cache name="progressReportCache" eternal="false" maxElementsInMemory="500"
overflowToDisk="false" diskPersistent="false" timeToLiveSeconds="900"
memoryStoreEvictionPolicy="LRU" statistics="true" />

</ehcache>
Expand Up @@ -91,6 +91,31 @@
-->
</util:list>

<bean class="org.jasig.portlet.degreeprogress.dao.CachingDegreeProgressDao">
<property name="enclosedDegreeProgressDao">
<bean class="org.jasig.portlet.degreeprogress.dao.mock.MockDegreeProgressDaoImpl"/>
<!--
<bean class="org.jasig.portlet.degreeprogress.dao.xml.HttpDegreeProgressDaoImpl" p:restTemplate-ref="restTemplate">
<property name="degreeProgressUrlFormat" value="http://localhost/degreeprogress/report.xml"/>
<property name="urlParams">
<util:map key-type="java.lang.String" value-type="org.jasig.portlet.courses.util.IParameterEvaluator">
</util:map>
</property>
<property name="usernameEvaluator" ref="userLoginIdParameterEvaluator"/>
<property name="passwordEvaluator">
<bean class="org.jasig.portlet.courses.util.StringLiteralParameterEvaluator" p:value="password"/>
</property>
</bean>
-->
</property>
<property name="progressReportCache">
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean"
p:cacheManager-ref="cacheManager" p:cacheName="progressReportCache"/>
</property>
</bean>

<bean class="org.jasig.portlet.degreeprogress.dao.mock.MockDegreeProgramDaoImpl"/>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:/ehcache.xml"/>

Expand Down
Expand Up @@ -25,9 +25,10 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="org.jasig.portlet.degreeprogress"/>
<context:annotation-config />

<bean class="org.jasig.portlet.degreeprogress.mvc.portlet.DegreeProgressController"/>

<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors"><bean class="org.jasig.portlet.courses.mvc.MinimizedStateHandlerInterceptor"/></property>
</bean>
Expand Down

0 comments on commit a57b1f7

Please sign in to comment.