001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.impl.repository;
017
018import org.apache.commons.lang.StringUtils;
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.rice.krad.web.form.InquiryForm;
024import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
025import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
026import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
027import org.kuali.rice.krms.impl.ui.AgendaEditor;
028
029import java.util.ArrayList;
030import java.util.Collection;
031import java.util.List;
032
033/**
034 * This class returns all rule types of rules.
035 */
036public class RuleTypeValuesFinder extends UifKeyValuesFinderBase {
037
038    @Override
039        public List<KeyValue> getKeyValues(ViewModel model) {
040        List<KeyValue> keyValues = new ArrayList<KeyValue>();
041
042        AgendaEditor agendaEditor;
043        if (model instanceof InquiryForm) {
044            InquiryForm inquiryForm = (InquiryForm) model;
045            agendaEditor = ((AgendaEditor) inquiryForm.getDataObject());
046
047        } else {
048            MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) model;
049            agendaEditor = ((AgendaEditor) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject());
050        }
051
052        // if we have an agenda w/ a selected context
053        if (agendaEditor.getAgenda() != null && StringUtils.isNotBlank(agendaEditor.getAgenda().getContextId())) {
054            Collection<KrmsTypeDefinition> ruleTypes = getKrmsTypeRepositoryService().findAllRuleTypesByContextId(
055                    agendaEditor.getAgenda().getContextId());
056            for (KrmsTypeDefinition ruleType : ruleTypes) {
057                keyValues.add(new ConcreteKeyValue(ruleType.getId(), ruleType.getName()));
058            }
059        }
060
061        return keyValues;
062    }
063
064    public KrmsTypeRepositoryService getKrmsTypeRepositoryService() {
065        return KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
066    }
067
068}