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 vgadiyak on 5/23/12
016 */
017package org.kuali.student.enrollment.class2.courseoffering.keyvalue;
018
019import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
020import org.kuali.rice.core.api.util.ConcreteKeyValue;
021import org.kuali.rice.core.api.util.KeyValue;
022import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
023import org.kuali.rice.krad.uif.view.ViewModel;
024import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
025import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
026import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
027import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
028import org.kuali.student.mock.utilities.TestHelper;
029import org.kuali.student.r2.common.dto.ContextInfo;
030import org.kuali.student.r2.common.exceptions.DoesNotExistException;
031import org.kuali.student.r2.lum.course.dto.CourseInfo;
032import org.kuali.student.r2.lum.course.dto.FormatInfo;
033import org.kuali.student.r2.lum.course.service.CourseService;
034import org.kuali.student.r2.lum.util.constants.CourseServiceConstants;
035
036import javax.xml.namespace.QName;
037import java.io.Serializable;
038import java.util.ArrayList;
039import java.util.List;
040
041public class CourseOfferingIdFormatKeyValues extends UifKeyValuesFinderBase implements Serializable {
042
043    private static final long serialVersionUID = 1L;
044
045    private transient CourseOfferingService courseOfferingService;
046    private transient CourseService courseService;
047
048    @Override
049    public List<KeyValue> getKeyValues(ViewModel model) {
050
051        List<KeyValue> keyValues = new ArrayList<KeyValue>();
052
053        MaintenanceDocumentForm form1 = (MaintenanceDocumentForm)model;
054        FormatOfferingInfo form = (FormatOfferingInfo)form1.getDocument().getDocumentDataObject();
055
056        ContextInfo context = TestHelper.getContext1();
057        String courseOfferingId = form.getCourseOfferingId();
058
059        if (courseOfferingId != null) {
060            List<FormatInfo> formats;
061            try {
062                CourseOfferingInfo courseOfferingInfo = getCourseOfferingService().getCourseOffering(courseOfferingId, context);
063                CourseInfo courseInfo = getCourseService().getCourse(courseOfferingInfo.getCourseId(), context);
064                formats = courseInfo.getFormats();
065            } catch (DoesNotExistException e) {
066                throw new RuntimeException("No subject areas found! There should be some in the database", e);
067            } catch (Exception e) {
068                throw new RuntimeException("Error looking up subject areas", e);
069            }
070
071            for(FormatInfo format : formats) {
072                keyValues.add(new ConcreteKeyValue(format.getId(), format.getTypeKey()));
073            }
074        }
075
076        return keyValues;
077    }
078
079    protected CourseOfferingService getCourseOfferingService() {
080        if(courseOfferingService == null) {
081            courseOfferingService = (CourseOfferingService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/courseOffering", "CourseOfferingService"));
082        }
083        return this.courseOfferingService;
084    }
085
086    protected CourseService getCourseService() {
087        if(courseService == null) {
088            courseService = (CourseService) GlobalResourceLoader.getService(new QName(CourseServiceConstants.COURSE_NAMESPACE, "CourseService"));
089        }
090        return this.courseService;
091    }
092}