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 */
016package org.kuali.student.enrollment.class2.acal.keyvalue;
017
018import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
019import org.kuali.rice.core.api.util.ConcreteKeyValue;
020import org.kuali.rice.core.api.util.KeyValue;
021import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
022import org.kuali.rice.krad.uif.view.ViewModel;
023import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
024import org.kuali.student.enrollment.class2.acal.form.CalendarSearchForm;
025import org.kuali.student.enrollment.class2.acal.util.CalendarConstants;
026import org.kuali.student.r2.common.dto.ContextInfo;
027import org.kuali.student.r2.common.exceptions.InvalidParameterException;
028import org.kuali.student.r2.common.exceptions.MissingParameterException;
029import org.kuali.student.r2.common.exceptions.OperationFailedException;
030import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
031import org.kuali.student.common.util.security.ContextUtils;
032import org.kuali.student.r2.core.constants.AcademicCalendarServiceConstants;
033import org.kuali.student.r2.core.class1.state.dto.StateInfo;
034
035import javax.xml.namespace.QName;
036import java.io.Serializable;
037import java.util.ArrayList;
038import java.util.Collections;
039import java.util.List;
040
041/**
042 * This is used at calendar search view to display the acal,hcal,term state names on the ui instead of
043 * state key. (CalendarSearchView.xml)
044 *
045 * @author Kuali Student Team
046 */
047public class AtpStateKeyValues extends UifKeyValuesFinderBase implements Serializable {
048
049    private static final long serialVersionUID = 1L;
050
051    private transient AcademicCalendarService acalService;
052
053    private static List<StateInfo> holidayStates;
054
055    private static List<StateInfo> acalStates;
056
057    private static List<StateInfo> termStates;
058
059    @Override
060    public List<KeyValue> getKeyValues(ViewModel model) {
061        CalendarSearchForm form = (CalendarSearchForm)model;
062        String atpType = form.getCalendarType();
063
064        List<KeyValue> keyValues = new ArrayList<KeyValue>();
065
066
067        try {
068            List<StateInfo> states = null;
069
070            if(atpType.equals(CalendarConstants.HOLIDAYCALENDER)){
071                states = getHolidayStates();
072            }else if(atpType.equals(CalendarConstants.ACADEMICCALENDER)) {
073                states = getAcalStates();
074            }else if(atpType.equals(CalendarConstants.TERM)){
075                states = getTermStates();
076            }
077
078            //FindBugs: No null check < sates will be always one of the above
079            for (StateInfo state : states) {
080                ConcreteKeyValue keyValue = new ConcreteKeyValue();
081                keyValue.setKey(state.getKey());
082                keyValue.setValue(state.getName());
083                keyValues.add(keyValue);
084            }
085        } catch (Exception e) {
086            throw new RuntimeException(e);
087        }
088
089        return keyValues;
090    }
091
092    public List<StateInfo> getHolidayStates() throws InvalidParameterException, MissingParameterException, PermissionDeniedException, OperationFailedException {
093        if(holidayStates == null) {
094            ContextInfo context = ContextUtils.createDefaultContextInfo();
095
096            holidayStates = Collections.unmodifiableList(getAcalService().getHolidayCalendarStates(context));
097        }
098        return holidayStates;
099    }
100
101    public List<StateInfo> getAcalStates() throws InvalidParameterException, MissingParameterException, PermissionDeniedException, OperationFailedException {
102        if(acalStates == null) {
103            ContextInfo context = ContextUtils.createDefaultContextInfo();
104
105            acalStates = Collections.unmodifiableList(getAcalService().getAcademicCalendarStates(context));
106        }
107
108        return acalStates;
109    }
110
111    public List<StateInfo> getTermStates() throws InvalidParameterException, MissingParameterException, PermissionDeniedException, OperationFailedException {
112        if(termStates == null) {
113            ContextInfo context = ContextUtils.createDefaultContextInfo();
114
115            termStates = Collections.unmodifiableList(getAcalService().getTermStates(context));
116        }
117        return termStates;
118    }
119
120    public AcademicCalendarService getAcalService() {
121        if(acalService == null) {
122            acalService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName(AcademicCalendarServiceConstants.NAMESPACE, AcademicCalendarServiceConstants.SERVICE_NAME_LOCAL_PART));
123        }
124        return this.acalService;
125    }
126}