001 /** 002 * Copyright 2012 The Kuali Foundation Licensed under the 003 * Educational Community License, Version 2.0 (the "License"); you may 004 * not use this file except in compliance with the License. You may 005 * obtain a copy of the License at 006 * 007 * http://www.osedu.org/licenses/ECL-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, 010 * software distributed under the License is distributed on an "AS IS" 011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 012 * or implied. See the License for the specific language governing 013 * permissions and limitations under the License. 014 * 015 * Created by David Yin on 8/7/12 016 */ 017 package org.kuali.student.enrollment.class2.courseoffering.service.impl; 018 019 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 020 import org.kuali.student.enrollment.class2.courseoffering.service.SeatPoolUtilityService; 021 import org.kuali.student.enrollment.courseoffering.dto.SeatPoolDefinitionInfo; 022 import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService; 023 import org.kuali.student.r2.common.dto.ContextInfo; 024 import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants; 025 import org.kuali.student.r2.common.util.constants.LuiServiceConstants; 026 027 import javax.xml.namespace.QName; 028 import java.util.ArrayList; 029 import java.util.Collections; 030 import java.util.Comparator; 031 import java.util.List; 032 033 /** 034 * This class //TODO ... 035 * 036 * @author Kuali Student Team 037 */ 038 public class SeatPoolUtilityServiceImpl implements SeatPoolUtilityService { 039 040 private transient CourseOfferingService courseOfferingService = null; 041 042 @Override 043 public void updateSeatPoolDefinitionList(List<SeatPoolDefinitionInfo> updatedSeatPoolList, String activityOfferingId, ContextInfo context) { 044 List<SeatPoolDefinitionInfo> updatedSeatPoolFinalList = new ArrayList<SeatPoolDefinitionInfo>(); 045 List <String> currentSeatPoolIds = getExistingSeatPoolIds(activityOfferingId, context); 046 047 try { 048 if (updatedSeatPoolList != null) { 049 Collections.sort(updatedSeatPoolList, new Comparator<SeatPoolDefinitionInfo>() { 050 @Override 051 public int compare(SeatPoolDefinitionInfo sp1, SeatPoolDefinitionInfo sp2) { 052 return sp1.getProcessingPriority().compareTo(sp2.getProcessingPriority()); 053 } 054 }); 055 056 int seatPoolPriority = 1; 057 for (SeatPoolDefinitionInfo seatPool : updatedSeatPoolList) { 058 seatPool.setProcessingPriority(seatPoolPriority); 059 if(seatPool.getId()!=null && !seatPool.getId().isEmpty() && currentSeatPoolIds.contains(seatPool.getId())) { 060 //update SP 061 SeatPoolDefinitionInfo seatPoolUpdated = getCourseOfferingService().updateSeatPoolDefinition(seatPool.getId(), seatPool, context); 062 updatedSeatPoolFinalList.add(seatPoolUpdated); 063 currentSeatPoolIds.remove(seatPool.getId()); 064 } else { 065 //create new SP 066 seatPool.setTypeKey(LuiServiceConstants.SEATPOOL_LUI_CAPACITY_TYPE_KEY); 067 seatPool.setStateKey(LuiServiceConstants.LUI_CAPACITY_ACTIVE_STATE_KEY); 068 SeatPoolDefinitionInfo seatPoolCreated = getCourseOfferingService().createSeatPoolDefinition(seatPool,context); 069 getCourseOfferingService().addSeatPoolDefinitionToActivityOffering(seatPoolCreated.getId(),activityOfferingId, context); 070 updatedSeatPoolFinalList.add(seatPoolCreated); 071 072 } 073 seatPoolPriority++; 074 } 075 076 //delete SPs that have been removed by the user 077 if (currentSeatPoolIds != null && currentSeatPoolIds.size() > 0){ 078 for(String seatPoolId: currentSeatPoolIds){ 079 getCourseOfferingService().deleteSeatPoolDefinition(seatPoolId, context); 080 } 081 } 082 083 } 084 } catch (Exception e) { 085 throw new RuntimeException(e); 086 } 087 } 088 089 private List<String> getExistingSeatPoolIds(String activityOfferingId, ContextInfo context) { 090 try { 091 List<SeatPoolDefinitionInfo> seatPoolList = getCourseOfferingService().getSeatPoolDefinitionsForActivityOffering(activityOfferingId, context); 092 List<String> seatPoolIds = new ArrayList<String>(); 093 094 if(seatPoolList != null && !seatPoolList.isEmpty()){ 095 for(SeatPoolDefinitionInfo seatPool : seatPoolList){ 096 seatPoolIds.add(seatPool.getId()); 097 } 098 } 099 100 return seatPoolIds; 101 } catch (Exception e) { 102 throw new RuntimeException(e); 103 } 104 } 105 106 private CourseOfferingService getCourseOfferingService() { 107 if (courseOfferingService == null) { 108 courseOfferingService = (CourseOfferingService) GlobalResourceLoader.getService(new QName(CourseOfferingServiceConstants.NAMESPACE, 109 CourseOfferingServiceConstants.SERVICE_NAME_LOCAL_PART)); 110 } 111 return courseOfferingService; 112 } 113 114 }